001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.faces;
017:
018: import java.io.IOException;
019:
020: import javax.faces.FactoryFinder;
021: import javax.faces.context.FacesContext;
022: import javax.faces.context.FacesContextFactory;
023: import javax.faces.lifecycle.Lifecycle;
024: import javax.faces.lifecycle.LifecycleFactory;
025: import javax.servlet.Filter;
026: import javax.servlet.FilterChain;
027: import javax.servlet.FilterConfig;
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.ServletRequest;
031: import javax.servlet.ServletResponse;
032:
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.commons.logging.Log;
035:
036: /**
037: * Filter integration for DWR framework. This filter was inspired by this
038: * article: http://www.thoughtsabout.net/blog/archives/000033.html
039: * @author Pierpaolo Follia (Latest revision: $Author: esa50833 $)
040: * @author Joe Walker [joe at getahead dot ltd dot uk]
041: * @noinspection AbstractClassNeverImplemented
042: */
043: public class FacesExtensionFilter implements Filter {
044: /* (non-Javadoc)
045: * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
046: */
047: public void init(FilterConfig config) throws ServletException {
048: servletContext = config.getServletContext();
049: }
050:
051: /* (non-Javadoc)
052: * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
053: */
054: public void doFilter(ServletRequest request,
055: ServletResponse response, FilterChain chain)
056: throws IOException, ServletException {
057: FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
058: .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
059: LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
060: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
061: Lifecycle lifecycle = lifecycleFactory
062: .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
063:
064: // Either set a private member
065: // servletContext = filterConfig.getServletContext();
066: // in your filter init() method or set it here like this:
067: // ServletContext servletContext =
068: // ((HttpServletRequest) request).getSession().getServletContext();
069: // Note that the above line would fail if you are using any other
070: // protocol than http
071:
072: // Doesn't set this instance as the current instance of
073: // FacesContext.getCurrentInstance
074: FacesContext facesContext = contextFactory.getFacesContext(
075: servletContext, request, response, lifecycle);
076:
077: // Set using our inner class
078: InnerFacesContext
079: .setFacesContextAsCurrentInstance(facesContext);
080:
081: try {
082: // call the filter chain
083: chain.doFilter(request, response);
084: } finally {
085: // Clean up after ourselves as FacesContext is a ThreadLocal object
086: try {
087: facesContext.release();
088: } catch (IllegalStateException ex) {
089: // Perhaps the FacesContext has already been released?
090: log.warn("Double release of faces context?", ex);
091: }
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see javax.servlet.Filter#destroy()
097: */
098: public void destroy() {
099: }
100:
101: // You need an inner class to be able to call
102: // FacesContext.setCurrentInstance
103: // since it's a protected method
104: private abstract static class InnerFacesContext extends
105: FacesContext {
106: protected static void setFacesContextAsCurrentInstance(
107: FacesContext facesContext) {
108: FacesContext.setCurrentInstance(facesContext);
109: }
110: }
111:
112: /**
113: * The servlet context
114: */
115: private ServletContext servletContext = null;
116:
117: /**
118: * The log stream
119: */
120: private static final Log log = LogFactory
121: .getLog(FacesExtensionFilter.class);
122: }
|