Stripes Framework on WebSphere 8.x application server fails with FileNotFoundException

stripes_framework

If you are struggling with running Stripes framework based application on IBM WebSphere application server 8.x I may help you. You probably end up with getting HTTP 403 error pages on every request. If you persuade your app server to give you some reasonable logging data (which is not easy in the world of Java application servers :/ ) you probably see FileNotFoundException logged during the request.

Here comes the explanation and a solution for you ...

 (bug is also documented in issue STS-905 and will be hopefully merged into the next version of Stripes framework)

Stripes use 2 servlet filters to handle all requests - StripesFilter and DynamicMappingFilter. First one prepares request for later processing and cleans up afterwards and is not important for us in this case. The problem lies in DynamicMappingFilter in following statement:


// Catch FileNotFoundException, which some containers (e.g. GlassFish) throw instead of setting SC_NOT_FOUND
boolean fileNotFoundExceptionThrown = false;
try {
   chain.doFilter(request, wrapper);
} catch (FileNotFoundException exc) {
   fileNotFoundExceptionThrown = true;
}

DynamicMappingFilter delegates request processing through filter chain and when application server says that there is no resource there it gets into the way and tries to find appropriate ActionBean for it to handle request. In version 1.5.1 of the Stripes framework it only wrapped HTTP response object to intercept setting status code SC_NOT_FOUND for detecting such situation but in version 1.5.7 there is also try ... catch clausule for FileNotFoundException that is thrown instead on some application servers.

And there is the catch - WebSphere 8.x wraps FileNotFoundException into the ServletException and thus it is not caught by this code. Until patch is included in the Stripes framework core you have to duplicate the filter class with application of this patch:


try {
    chain.doFilter(request, wrapper);
} catch (FileNotFoundException exc) {
    fileNotFoundExceptionThrown = true;
} catch (ServletException ex) {
    if (ex.getCause() instanceof FileNotFoundException) {
        fileNotFoundExceptionThrown = true;
    } else {
        throw ex;
    }
}

And there is another tweak you have to make - in the very same class you have to modify inner class ErrorTrappingResponseWrapper by overriding two more methods:


@Override
public void setStatus(int sc) {
   this.errorCode = sc;
}
@Override
public void setStatus(int sc, String sm) {
   this.errorCode = sc;
   this.errorMessage = sm;
}

Click here to download full class.

Also you have to alter your web.xml configuration to map patched filter:


<filter>
   <filter-name>DynamicMappingFilter</filter-name>
   <filter-class>net.sourceforge.stripes.controller.HackedDynamicMappingFilter</filter-class>
</filter>
....
<filter-mapping>
   <filter-name>DynamicMappingFilter</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>REQUEST</dispatcher>
   <dispatcher>FORWARD</dispatcher>
   <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

Happy coding!