using pretty faces with web filters
Asked Answered
T

1

10

Using Tomcat 7 --- Primefaces 3.4.1 --- javax faces 2.1.17 --- prettyfaces-jsf2 3.3.3

I configured pretty faces on my project correctly but my web filters are not working with new urls which are written by pretty faces.

Here is an example pretty-config.xml

<url-mapping id="home">
    <pattern value="/home"/>
    <view-id value="/secure/homepage.xhtml"/>
</url-mapping>

<url-mapping id="register">
    <pattern value="/register"/>
    <view-id value="/public/register.xhtml"/>
</url-mapping>

<url-mapping id="welcome">
    <pattern value="/"/>
    <view-id value="/public/welcome.xhtml"/>
</url-mapping>

<url-mapping id="profile">
    <pattern value="/profile/#{userId}"/>
    <view-id value="/profile.xhtml"/>
</url-mapping>

login(welcome) and register pages are in "public" folder, and their web filter is defined with annotation : @WebFilter("/public/*")

for my home page in "secure" folder (exactly there will be more pages in the folder), i defined a web filter also and its annotation : @WebFilter("/secure/*)

pretty urls are working fine, but these filters are only working when i write the original urls.

1) How can i repair my webfilters ?

2) I also want to block user for entering original url. I know that pretty faces is hiding original urls fully but is there a way to do it ?

-- SOLVED -- thanks for BalusC

if you defined your filters with annotations, you can configure dispatcher settings like

@WebFilter(urlPatterns = "/public/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD})

Throat answered 25/3, 2013 at 1:14 Comment(0)
B
13

PrettyFaces uses like many URL-rewriting solutions RequestDispatcher#forward() to forward the request to the desired target resource.

Servlet filters, when mapped without any <dispatcher>, listens by default on "initial" requests only, not on forwarded, included, nor error'ed requests.

So, when you map another servlet filter in web.xml after the PrettyFaces one, then it would by default not be triggered, unless you explicitly set a <dispatcher> on FORWARD next to the default of REQUEST (you should keep this one for the case PrettyFaces actually doesn't need to perform a forward).

<filter-mapping>
    ...
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Or, for the case you're using @WebFilter on your filters, use the dispatcherTypes attribute:

@WebFilter(..., dispatcherTypes = { REQUEST, FORWARD })

Alternatively, if the filter in question doesn't change the request/response target in any way, e.g. setting the charset, compressing using Gzip, listening on exceptions, etc, then you can also just put it before the PrettyFaces one.

Butyl answered 25/3, 2013 at 1:21 Comment(4)
I did not specified any filter in my web.xml, BalusC. Because i am using version="3.0" and pretty is configuring himself automatically as written in documents. Also my web filters are clearly defined with annotations. Have i to define it directly on web.xml for dispatcher settings ?Throat
thank you @BalusC, it works very well. @WebFilter(urlPatterns = "/public/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}) solved my problem. But i want to ask a minor question, updating or refreshing same page (for example using primefaces poll) always catched by filters and its totally normal actually. But Is there a way to escape it ?Throat
You could map the filter on a more specific URL pattern. Alternatively, if your concern is merely that the filter is dong a relatively expensive job, you could also precheck if it's a JSF ajax request by adding a if ("partial/ajax".equals(request.getHeader("Faces-Request"))) to do a fast-skip of the whole filter job (just call FilterChain#doFilter()).Butyl
I understand my friend, will check it out. Thank you again.Throat

© 2022 - 2024 — McMap. All rights reserved.