KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > faces > FacesExtensionFilter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.faces;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.faces.FactoryFinder;
21 import javax.faces.context.FacesContext;
22 import javax.faces.context.FacesContextFactory;
23 import javax.faces.lifecycle.Lifecycle;
24 import javax.faces.lifecycle.LifecycleFactory;
25 import javax.servlet.Filter JavaDoc;
26 import javax.servlet.FilterChain JavaDoc;
27 import javax.servlet.FilterConfig JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.ServletResponse JavaDoc;
32
33 import org.directwebremoting.util.Logger;
34
35 /**
36  * Filter integration for DWR framework. This filter was inspired by this
37  * article: http://www.thoughtsabout.net/blog/archives/000033.html
38  * @author Pierpaolo Follia (Latest revision: $Author: esa50833 $)
39  * @author Joe Walker [joe at getahead dot ltd dot uk]
40  * @noinspection AbstractClassNeverImplemented
41  */

42 public class FacesExtensionFilter implements Filter JavaDoc
43 {
44     /* (non-Javadoc)
45      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
46      */

47     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc
48     {
49         servletContext = config.getServletContext();
50     }
51
52     /* (non-Javadoc)
53      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
54      */

55     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
56     {
57         FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
58         LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
59         Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
60
61         // Either set a private member
62
// servletContext = filterConfig.getServletContext();
63
// in your filter init() method or set it here like this:
64
// ServletContext servletContext =
65
// ((HttpServletRequest) request).getSession().getServletContext();
66
// Note that the above line would fail if you are using any other
67
// protocol than http
68

69         // Doesn't set this instance as the current instance of
70
// FacesContext.getCurrentInstance
71
FacesContext facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);
72
73         // Set using our inner class
74
InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
75
76         try
77         {
78             // call the filter chain
79
chain.doFilter(request, response);
80         }
81         finally
82         {
83             // Clean up after ourselves as FacesContext is a ThreadLocal object
84
try
85             {
86                 facesContext.release();
87             }
88             catch (IllegalStateException JavaDoc ex)
89             {
90                 // Perhaps the FacesContext has already been released?
91
log.warn("Double release of faces context?", ex);
92             }
93         }
94     }
95
96     /* (non-Javadoc)
97      * @see javax.servlet.Filter#destroy()
98      */

99     public void destroy()
100     {
101     }
102
103     // You need an inner class to be able to call
104
// FacesContext.setCurrentInstance
105
// since it's a protected method
106
private abstract static class InnerFacesContext extends FacesContext
107     {
108         protected static void setFacesContextAsCurrentInstance(FacesContext facesContext)
109         {
110             FacesContext.setCurrentInstance(facesContext);
111         }
112     }
113
114     /**
115      * The servlet context
116      */

117     private ServletContext JavaDoc servletContext = null;
118
119     /**
120      * The log stream
121      */

122     private static final Logger log = Logger.getLogger(FacesExtensionFilter.class);
123 }
124
Popular Tags