public void doFilter(ServletRequest rq,
ServletResponse rs,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) rq;
HttpServletResponse response = (HttpServletResponse) rs;
ServletContext servletContext = filterConfig.getServletContext();
SiteMeshWebAppContext webAppContext = new SiteMeshWebAppContext(request, response, servletContext);
ContentProcessor contentProcessor = initContentProcessor(webAppContext);
DecoratorSelector decoratorSelector = initDecoratorSelector(webAppContext);
if (filterAlreadyAppliedForRequest(request)) {
// Prior to Servlet 2.4 spec, it was unspecified whether the filter should be called again upon an include().
chain.doFilter(request, response);
return;
}
if (!contentProcessor.handles(webAppContext)) {
// Optimization: If the content doesn't need to be processed, bypass SiteMesh.
chain.doFilter(request, response);
return;
}
if (containerTweaks.shouldAutoCreateSession()) {
// Some containers (such as Tomcat 4) will not allow sessions to be created in the decorator.
// (i.e after the response has been committed).
request.getSession(true);
}
try {
Content content = obtainContent(contentProcessor, webAppContext, request, response, chain);
if (content == null) {
return;
}
Decorator decorator = decoratorSelector.selectDecorator(content, webAppContext);
decorator.render(content, webAppContext);
} catch (IllegalStateException e) {
// Some containers (such as WebLogic) throw an IllegalStateException when an error page is served.
// It may be ok to ignore this. However, for safety it is propegated if possible.
if (!containerTweaks.shouldIgnoreIllegalStateExceptionOnErrorPage()) {
throw e;
}
} catch (RuntimeException e) {
if (containerTweaks.shouldLogUnhandledExceptions()) {
// Some containers (such as Tomcat 4) swallow RuntimeExceptions in filters.
servletContext.log("Unhandled exception occurred whilst decorating page", e);
}
throw e;
}
}
|