Search code examples
angularjsspringservlet-filtersservlet-3.0tuckey-urlrewrite-filter

Spring and Tuckey URLRewriter StackOverflow Exception


Server: Tomcat 7
Spring: Version 4
Spring Security version 3.25
Tuckey: 4.0.3

I am trying to use tuckey url rewriter with my spring application in order to better support angularjs html5mode.

What I would like is to forward any request to / if the request-uri is not /api or /socket. If the request is /api or /socket i would like them to go through spring and the dispather servlet and have spring web security applied.

Here is my webintializer class: public class ApplicationInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(PersistenceConfig.class, WebSecurityConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.register(ApplicationConfig.class);

    AnnotationConfigWebApplicationContext socketContext = new AnnotationConfigWebApplicationContext();
    webContext.register(WebSocketConfig.class);

    ServletRegistration.Dynamic restApi = servletContext.addServlet("rest-api", new DispatcherServlet(webContext));
    restApi.setLoadOnStartup(1);
    restApi.addMapping("/api");

    ServletRegistration.Dynamic webSocket = servletContext.addServlet("web-socket", new DispatcherServlet(socketContext));
    webSocket.setLoadOnStartup(1);
    webSocket.addMapping("/socket");

    Dynamic springSecurityFilter = servletContext.addFilter(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, DelegatingFilterProxy.class);
    springSecurityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/api/*");
    springSecurityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/socket/*");

    UrlRewriteFilter urlRewriteFilter = new UrlRewriteFilter();
    Map<String, String> urlRewriteFilterParams = new HashMap<String, String>();
    urlRewriteFilterParams.put("confReloadCheckInterval", "0");
    urlRewriteFilterParams.put("confPath", "/WEB-INF/classes/urlrewrite.xml");
    urlRewriteFilterParams.put("statusEnabled", "true");
    urlRewriteFilterParams.put("logLevel", "DEBUG");
    urlRewriteFilterParams.put("statusEnabledOnHosts", "localhost");

    Dynamic tuckeyRewriteFilter = servletContext.addFilter("UrlRewriteFilter", urlRewriteFilter);
    tuckeyRewriteFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
    tuckeyRewriteFilter.setInitParameters(urlRewriteFilterParams);

}
}

The rewriter is getting picked up because I can go to localhost:8080/rewrite-status and my config is there and the page says it should be doing what I want it to do.

My Tuckey UrlRewrite.XML file is below:

  <!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite default-match-type="wildcard">
    <rule>
        <condition type="request-uri" operator="notequal">/api/**</condition>
        <condition type="request-uri" operator="notequal">/socket/**</condition>
        <from>/**</from>
        <to type="forward">/$1</to>
    </rule>
</urlrewrite>

The error I am getting is as follows. It seems to be some recursiveness somewhere within tuckey. If i remove tuckey everything works fine Exception in thread "http-bio-8080-exec-1" java.lang.StackOverflowError at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281) at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:281)


Solution

  • the DispatcherType.FORWARD in the filter definition is causing the rule to be applied recursively to itself. When using a URL rewrite filter you'll want to map it only to the REQUEST.

    Hope that helps