KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > servlet > ControlFilter


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

19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 /**
31  * The ControlFilter class provides an implementation of an HTTP servlet filter that supports
32  * running controls in the web tier. It works, in conjunction with the {@link ServletBeanContext}
33  * class to provide runtime containment for controls. It ensures that a valid BeanContext has
34  * been set up prior to forwarding the request to the actual target servlet, and does
35  * post-processing to ensure that resources have been properly released.
36  *
37  * This filter needs to be configured in web.xml for URL mappings that will be hosting servlets.
38  */

39 public class ControlFilter implements Filter JavaDoc
40 {
41     public static String JavaDoc BEAN_CONTEXT_ATTRIBUTE = ServletBeanContext.class.getName();
42
43     /*
44      * THIS FILTER RECOGNIZES THE FOLLOWING INITIALIZATION PARAMETERS:
45      */

46
47     /**
48      * The useSession init parameter is a boolean value that indicates whether the bean context
49      * should be stored in session. This makes the context (and any contained controls)
50      * accessible across multiple http requests.
51      */

52     public static String JavaDoc INIT_PARAM_USE_SESSION = "useSession";
53
54     /**
55      * The contextClass init parameter is a class name that defines the BeanContext class to use
56      * for containing Controls in the servlet container. This class <b>must be</b> a subclass of
57      * the org.apache.beehive.runtime.servlet.ServletBeanContext class.
58      */

59     public static String JavaDoc INIT_PARAM_CONTEXT_CLASS = "contextClass";
60
61     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
62     {
63         _filterConfig = filterConfig;
64         _useSessionContext = new Boolean JavaDoc(filterConfig.getInitParameter(INIT_PARAM_USE_SESSION)).booleanValue();
65         String JavaDoc contextClassName = filterConfig.getInitParameter(INIT_PARAM_CONTEXT_CLASS);
66         if(contextClassName != null)
67         {
68             try
69             {
70                 _contextClass = Class.forName(contextClassName);
71             }
72             catch(Exception JavaDoc e)
73             {
74                 throw new ServletException JavaDoc("Cannot load container context class '"+contextClassName+"'");
75             }
76             if(!ServletBeanContext.class.isAssignableFrom(_contextClass))
77             {
78                 throw new ServletException JavaDoc("'"+contextClassName+"' is not a ServletBeanContext sub-class");
79             }
80             
81         }
82     }
83
84     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
85                 throws java.io.IOException JavaDoc, ServletException JavaDoc
86     {
87         if (request instanceof HttpServletRequest JavaDoc)
88         {
89             ServletBeanContext beanContext = null;
90             HttpSession JavaDoc httpSession = null;
91
92             //
93
// If preserving control bean context within session state, then retrieve
94
// it.
95
//
96
if (_useSessionContext)
97             {
98                 httpSession = ((HttpServletRequest JavaDoc)request).getSession(true);
99                 beanContext = (ServletBeanContext)httpSession.getAttribute(BEAN_CONTEXT_ATTRIBUTE);
100             }
101
102             //
103
// If no context exists, then create a new one and (optionally) store it
104
// within session state.
105
// OPTIMIZE: Avoid per-request creation using TLS or some other mechanism
106
//
107
if (beanContext == null)
108             {
109                 try
110                 {
111                     beanContext = (ServletBeanContext)_contextClass.newInstance();
112                 }
113                 catch(Exception JavaDoc e)
114                 {
115                     throw new ServletException JavaDoc("Cannot construct BeanContext class instance: " +
116                                                _contextClass.getName());
117                 }
118                 if (httpSession != null)
119                     httpSession.setAttribute(BEAN_CONTEXT_ATTRIBUTE, beanContext);
120             }
121
122             //
123
// Start a new execution context
124
//
125
beanContext.beginContext(_filterConfig.getServletContext(), request, response);
126             try
127             {
128                 //
129
// Pass the requst on to the next filter or target servlet
130
//
131
chain.doFilter(request, response);
132             }
133             finally
134             {
135                 //
136
// End the execution context
137
//
138
beanContext.endContext();
139             }
140         }
141         else
142         {
143             //
144
// If the filter is (mis)configured on something other than an http servlet, just
145
// pass it on
146
//
147
chain.doFilter(request, response);
148         }
149     }
150
151     public void destroy() {}
152
153     /**
154      * The FilterConfig associated with this filter instance.
155      */

156     FilterConfig JavaDoc _filterConfig;
157
158     /**
159      * The resolved value of the 'useSession' filter attribute.
160      */

161     private boolean _useSessionContext = false;
162
163     /**
164      * The BeanContext class to use as the container for controls running in the ServletContainer
165      */

166     private Class JavaDoc _contextClass = ServletBeanContext.class;
167 }
168
Popular Tags