Reputation: 12621
First, I will introduce you to my testcase before handling my question. There are a few components in my basic Maven Webapplication:
I will share the code of important components.
pretty-config.xml
<url-mapping id="page">
<pattern value="/page" />
<view-id value="/page.xhtml" />
</url-mapping>
FirstFilter.java
@WebFilter
public class FirstFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("FirstFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("FirstFilter, response");
}
// override init and destroy
}
ThirdFilter.java
@WebFilter
public class ThirdFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("ThirdFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("ThirdFilter, response");
}
// override init and destroy
}
web.xml
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>nl.mhoogeveen.nl.rootapplication.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>testingapplications.filterchaining.PrettyFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
<filter-name>ThirdFilter</filter-name>
<filter-class>testingapplications.filterchaining.ThirdFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ThirdFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Situation
Calling localhost:8080/page.xhtml (and thus not activating Pretty Faces redirection)
INFO: FirstFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, response
INFO: FirstFilter, response
Calling localhost:8080/page (and thus activating Pretty Faces redirection)
INFO: FirstFilter, request: http://localhost:8080/page
INFO: FirstFilter, response
Question
What is causing this situation in which my chain will be incomplete? It will not be cut off as I still get my response on the FirstFilter. It just seems like it does not ever reach ThirdFilter.
Is there anything wrong with my web.xml
, am I missing a dispatcher
?
Thanks in advance.
Upvotes: 3
Views: 1560
Reputation: 5668
You dispatcher settings are not correct. Let me explain what happens:
The request for /page
comes in and is first processed by FirstFilter
. After that PrettyFaces intercepts the request and forwards it to /page.xhtml
. This forwarded request is handled as a new request and therefore travels thought the filter chain again. But your filters don't have any dispatcher settings which is the same as setting <dispatcher>REQUEST</dispatcher>
. In this config the filters are only applied to regular request but NOT to forwarded ones.
If you want to apply a filter also to forwarded requests, you have to add <dispatcher>FORWARD</dispatcher>
to the filter configuration.
That's also the reason why you typically have to adjust the dispatcher settings for 3rd party filters like MyFaces Tomahawk / PrimeFaces, etc. See question 2 in the FAQ:
http://ocpsoft.org/prettyfaces/#section-16
Upvotes: 7