webtier@glassfish.java.net

Redirect JSF resource request to custom HTTP status 404 error page

From: <forums_at_java.net>
Date: Tue, 14 Feb 2012 10:55:56 -0600 (CST)

Hello!

Is there a way to redirect a JSF resource request for a non-existent resource
to a custom 404 status code error page?

Specifically, when specifying directly in browser a url like
/http://host:port/appname/javax.faces.resource/non-existent-page.xhtml/, I
want the user to be redirected to a custom error page.

In the web.xml, I have specified the following:

<error-page> <error-code>404</error-code>
<location>/pages/error/pageNotFound.xhtml</location> </error-page>
The JSF servlet mapping is:   

<servlet> <servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>
<servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
I also made the resource requests to be unsecured:

<security-constraint> <web-resource-collection> <web-resource-name>Unsecured
resources</web-resource-name>
<url-pattern>/javax.faces.resource/*</url-pattern> </web-resource-collection>
</security-constraint>
The redirect to my custom error page works well for all requests except those
containing //javax.faces.resource/*/ (I guess where ResourceHandler is
implied). I studied the source code for */FacesServlet/*. In its
*/service()/* method, when a

request for non-existing resource is made, the method
*/ResourceHandler.handleResourceRequest()/* is beeing called, which in turn
calls */HttpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND)./*
The documentation on */HttpServletResponse/*

reads /"This method is used to set the return status code when there is no
error (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If
there is an error, and the caller wishes to invoke an error-page defined in
the web/

/application, the sendError method should be used instead."/ From this I
conclude that this is the reason why my custom page gets never called.

Does any workaround exist to this behavior? I need to redirect to my custom
page with the purpose to hide the version of Tomcat, which is displayed on
the default 404 HTTP status page.

I'm using the following environment:

  *     Apache Tomcat 7.0.19
  *     Mojarra 2.1.2 JSF implementation

Thank you in advance!


--
[Message sent by forum member 'dtb']
View Post: http://forums.java.net/node/883521