org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: unable to find urlrewrite conf file at urlrewrite.xml

1.9k views Asked by At

I'm trying to use Tuckey as urlRewrite in my spring boot project but i get error below registering Tuckey filter:

org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: unable to find urlrewrite conf file at `urlrewrite.xml` even i have `urlrewrite.xml` file under resources. 

I register Tucky filter like below in my Configuration class:

@Bean
public FilterRegistrationBean urlRewriteFilterRegistration() throws IOException {
    log.info("UrlRewriteFilter registered!");     
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new UrlRewriteFilter());
    registration.setName("UrlRewriteFilter"); 
    List<String> urlPatterns = new ArrayList<>();
    urlPatterns.add("/urlmap");
    registration.setUrlPatterns(urlPatterns); 
    registration.getInitParameters().put("confPath","/urlrewrite.xml");
    return registration;
}

Even i used to register Filter like below:

FilterRegistration.Dynamic urlRewriteFilter = servletContext.addFilter("urlRewriteFilter",  new UrlRewriteFilter());
    urlRewriteFilter.setInitParameter("confPath", "urlrewrite.xml");
    urlRewriteFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");

it was working in none boot version! what is wrong with my configuration.

1

There are 1 answers

0
Fabien Kruba On BEST ANSWER

Had the same issue recently. Found this post with an answer: Spring boot cannot find urlrewrite.xml inside jar file

TL;DR: Created a class extending UrlRewriteFilter and overwrote the loadUrlRewriterMethod to allow loading a the configuration as a org.springframework.core.io.Resource

Please adapt to your needs:

public class BootCompliantUrlRewriteFilter extends UrlRewriteFilter {

    private Resource resource;

    public BootCompliantUrlRewriteFilter(Resource config){
        this.resource = config;
    }

    //Override the loadUrlRewriter method, and write your own implementation
    @Override
    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        try {
            //Create a UrlRewrite Conf object with the injected resource
            Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@yourOwnSystemId@@");
            checkConf(conf);
        } catch (IOException ex) {
            throw new ServletException("Unable to load URL rewrite configuration file from " + this.resource, ex);
        }
    }
}

and in the application class:

public static final String REWRITE_FILTER_NAME = "rewriteFilter";
public static final String REWRITE_FILTER_CONF_PATH = "urlrewrite.xml";

@Bean
public FilterRegistrationBean rewriteFilterConfig() {
    FilterRegistrationBean reg = new FilterRegistrationBean();
    reg.setName(REWRITE_FILTER_NAME);
    reg.setFilter(new BootCompliantUrlRewriteFilter(new ClassPathResource(REWRITE_FILTER_CONF_PATH)));

    //reg.addInitParameter("confPath", REWRITE_FILTER_CONF_PATH);
    reg.addInitParameter("confReloadCheckInterval", "-1");
    reg.addInitParameter("statusPath", "/redirect");
    reg.addInitParameter("statusEnabledOnHosts", "*");
    reg.addInitParameter("logLevel", "WARN");
    return reg;
}