Spring boot cannot find urlrewrite.xml inside jar file

3.1k views Asked by At

I'm using Spring Boot with an Embedded Tomcat, and the class UrlRewriteFilter can't find the configuration file urlrewrite.xml, this class uses servletcontext.getResourceAsStream(this.confPath) and I read in other article that this method doesn't work when the package is a jar. Someone had this problem ?

4

There are 4 answers

0
goat On

I'm not proud of it, but this worked for me. The library itself uses ClassLoader.getSystemResourceAsStream() at one point to try to resolve the filename, so I think this will work unless the library updates/changes that one day.

String fileName = "urlrewrite.xml";
// The prefixed version seems to be needed when running within a jar. 
// Rumor has it that it may be a tomcat 8 specific.
String confPath = java.lang.ClassLoader.getSystemResourceAsStream(fileName) != null ? fileName : "/WEB-INF/classes/" + fileName;
registrationBean.addInitParameter("confPath", confPath);
0
dmunozfer On

Found in blog post

import org.springframework.core.io.Resource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

//Adding @Component Annotation to a Filter is enough to register the Filter, when you have no web.xml
@Component
public class MyUrlRewriteFilter extends UrlRewriteFilter {

    private static final String CONFIG_LOCATION = "classpath:/urlrewrite.xml";

    //Inject the Resource from the given location
    @Value(CONFIG_LOCATION)
    private Resource resource;

    //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 " + CONFIG_LOCATION, ex);
        }
    }
}
0
Andrew Kaluzniacki On

I am using the Tuckey UrlRewriteFilter with Spring Boot. I followed the initial question implementation in this stack overflow question (not the answers). I also added

registrationBean.addInitParameter("confPath", "urlrewrite.xml");

to set a specific path for the urlrewrite.xml file. In the project I put the file in src/main/webapp.

This works when using 'mvn spring-boot:run' which I think meets your need for it to run in a jar. In my project, I build a war file, where this also works.

0
gursahib.singh.sahni On

The following code worked for me.

Please use the following dependency:

         <dependency>
           <groupId>org.tuckey</groupId>
           <artifactId>urlrewritefilter</artifactId>
           <version>4.0.4</version>
         </dependency>

Create urlrewrite.xml in resource folder:

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite>
    <rule>
        <name>Domain Name Check</name>
        <condition name="host" operator="notequal">www.userdomain.com</condition>
        <from>^(.*)$</from>
        <to type="redirect">http://www.userdomain.com$1</to>
    </rule>
</urlrewrite>

Add the following in main ApplicationRunner.java:

@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new CustomURLRewriter());
    return registrationBean;
}

And create a CustomURLRewriter:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
import javax.servlet.*;
import java.io.InputStream;

public class CustomURLRewriter extends UrlRewriteFilter {
private UrlRewriter urlRewriter;

@Autowired
Environment env;

@Override
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
    try {
        ClassPathResource classPathResource = new ClassPathResource("urlrewrite.xml");
        InputStream inputStream = classPathResource.getInputStream();
        Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "urlrewrite.xml", "");
        urlRewriter = new UrlRewriter(conf1);
    } catch (Exception e) {
        throw new ServletException(e);
    }
}

@Override
public UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) {
    return urlRewriter;
}

@Override
public void destroyUrlRewriter() {
    if(urlRewriter != null)
        urlRewriter.destroy();
}
}