Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot cannot find urlrewrite.xml inside jar file

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 ?

like image 925
renandiass Avatar asked Mar 15 '23 15:03

renandiass


2 Answers

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);
        }
    }
}
like image 52
dmunozfer Avatar answered Mar 18 '23 04:03

dmunozfer


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();
}
}
like image 32
gursahib.singh.sahni Avatar answered Mar 18 '23 05:03

gursahib.singh.sahni