私のプロジェクトでは、JSF 2.1、RichFaces 4.1、GlassFish 3.1.2 を使用しています。web.xml にフィルターを登録しました。
質問: プロジェクトに自動的に含まれる JSF と RichFaces (例: javax.faces.resource/richfaces-queue.js.xhtml) のリソースをフィルタリングする方法を教えてください。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- configuration of the session -->
<session-config>
<session-timeout>15</session-timeout>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>org.richfaces.skin</param-name>
<param-value>myskin</param-value>
</context-param>
<!-- The runtime must interpret each entry with Facelet tag library-->
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/tags/taglib.xml</param-value>
</context-param>
<!-- Alternate suffix for JSF pages -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<!-- XML comments in the Facelets source page are not delivered to the client -->
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<!-- Project Stage for JSF (Production | Development | UnitTest | SystemTes | Extension) -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>ru.axetta.utils.web.JSFAuthenticationFilter</filter-class>
<init-param>
<param-name>includes</param-name>
<param-value>/index.xhtml, /login.xhtml</param-value>
</init-param>
<init-param>
<!--
Relational to context root, must start from "/".
Example to be excludes: /javax.faces.resource/jquery.position.js.xhtml, /resource/images/logo.png, /rfRes/toolbar.ecss.xhtml
-->
<param-name>excludes</param-name>
<param-value>^(/rfRes/)(.+)(\u002E[a-zA-Z]+)(\u002Exhtml)$,
^(/javax.faces.resource/)(.+)(\u002E[a-zA-Z]+)(\u002Exhtml)$,
^(/resources/)([a-zA-Z]+/)(\w+)(\u002E)([a-zA-Z]+)$, /services, /cryptopro/applet
</param-value>
</init-param>
<init-param>
<param-name>loginPage</param-name>
<param-value>/login.xhtml</param-value>
</init-param>
<init-param>
<param-name>loginPageReplacement</param-name>
<param-value>/index.xhtml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- Page for errors -->
<error-page>
<error-code>404</error-code>
<location>/include/error/ErrorPage.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/include/error/ErrorPage.xhtml</location>
</error-page>
<error-page>
<error-code>503</error-code>
<location>/include/error/ErrorPage.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/include/error/ErrorPage.xhtml</location>
</error-page>
</web-app>
JSFAuthenticationFilter
public class JSFAuthenticationFilter implements Filter {
// ... variables
@Override //initialize
public void init(FilterConfig filterConfig) throws ServletException {
// ... do something
}
@Override //filtering URI
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession httpSession = httpRequest.getSession(true);
HttpServletResponse httpResponse = (HttpServletResponse) response;
String servletPath = httpRequest.getServletPath();
//excluded data for the filtration (/javax.faces.resource/richfaces-queue.js.xhtml and other)
if (isURLExcluded(servletPath)) {
chain.doFilter(request, response);
return;
}
//processed data to filter (/login.xhtml and /index.html)
if (isURLIncluded(servletPath)) {
// ... do something
} else { //for errors page
httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Error message: page:{0} not found", servletPath));//404
chain.doFilter(request, response);
return;//redirect -> errorPage.xhtml
}
}
@Override
public void destroy() {
// nothing to do here
}
}
問題 # 1:ブラウザーのアドレス バーにパス http:///myapp/javax.faces.resource/richfaces-queue.js.xhtmlを入力すると、 ウィンドウにインクルード リソースのすべてのテキストが出力されます。
問題 # 2: Excluded リソースを確認するには、regexp を使用します。有効なアドレスを入力すると、たとえばhttp:///myapp/javax.faces.resource/richfaces2-queue.js.xhtml ウィンドウに次のように出力されます。
XML 解析エラー: 要素が見つかりません アドレス: http:///myapp/javax.faces.resource/richfaces2-queue.js.xhtml 行 1、文字 1:
ページErrorPage.xhtmlにリダイレクトせず、GlassFish ログに情報がありません。リソースをフィルタリングしてエラーページに切り替えるように教えてください。
PS: 同様の問題 ( https://forums.oracle.com/forums/thread.jspa?threadID=1717040 )