KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > JSR154Filter


1 // ========================================================================
2
// $Id: JSR154Filter.java,v 1.5 2005/12/04 19:47:18 janb Exp $
3
// Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
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
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletRequestAttributeEvent JavaDoc;
27 import javax.servlet.ServletRequestAttributeListener JavaDoc;
28 import javax.servlet.ServletRequestEvent JavaDoc;
29 import javax.servlet.ServletRequestListener JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
35
36 import org.mortbay.util.LazyList;
37
38 /* ------------------------------------------------------------ */
39 /** JSR 154 Stupidness filter.
40  * This filter contains the elements of the JSR154 servlet 2.4 specification
41  * that are too objectionable to include in the core of Jetty. Thus they have
42  * been added as a filter that can be removed from the defaultweb.xml file if
43  * none of the following "features" are required:<ul>
44  * <li>RequestAttributeListeners. If you actually have a real use for these, please
45  * tell the jetty lists what they are and why you can't use a normal filter/wrapper for
46  * this?</li>
47  * <li>SRV.6.2.2 Dispatachers where the container cannot wrap the request or
48  * response. See http://jetty.mortbay.org/jetty/doc/servlet24.html#d0e711
49  * to find out why this is stupid.</li>
50  * </ul>
51  *
52  * The boolean init parameter "unwrappedDispatched"
53  *
54  */

55 public class JSR154Filter implements Filter JavaDoc
56 {
57     private static ThreadLocal JavaDoc __states=new ThreadLocal JavaDoc();
58     private ServletContext JavaDoc _servletContext;
59     private Object JavaDoc _requestListeners;
60     private Object JavaDoc _requestAttributeListeners;
61     private boolean _unwrappedDispatchSupported;
62     
63     /* ------------------------------------------------------------ */
64     public void init(FilterConfig JavaDoc filterConfig)
65         throws ServletException JavaDoc
66     {
67         _servletContext=filterConfig.getServletContext();
68         _unwrappedDispatchSupported=Boolean.valueOf(filterConfig.getInitParameter("unwrappedDispatch")).booleanValue();
69     }
70
71     /* ------------------------------------------------------------ */
72     protected void setRequestAttributeListeners(Object JavaDoc list)
73     {
74         _requestAttributeListeners=list;
75     }
76     
77     /* ------------------------------------------------------------ */
78     protected void setRequestListeners(Object JavaDoc list)
79     {
80         _requestListeners=list;
81     }
82
83     /* ------------------------------------------------------------ */
84     public boolean isUnwrappedDispatchSupported()
85     {
86         return _unwrappedDispatchSupported;
87     }
88
89     /* ------------------------------------------------------------ */
90     /**
91      * @param supportUnwrappedDispatch The supportUnwrappedDispatch to set.
92      */

93     public void setUnwrappedDispatchSupported(boolean supportUnwrappedDispatch)
94     {
95         _unwrappedDispatchSupported = supportUnwrappedDispatch;
96     }
97     
98     /* ------------------------------------------------------------ */
99     public void setDispatch(Dispatcher.DispatcherRequest request, Dispatcher.DispatcherResponse response)
100     {
101         ThreadState state=state();
102         state.dispatchRequest=request;
103         state.dispatchResponse=response;
104     }
105
106     /* ------------------------------------------------------------ */
107     public Dispatcher.DispatcherRequest getDispatchRequest()
108     {
109         ThreadState state=state();
110         return state.dispatchRequest;
111     }
112     
113     /* ------------------------------------------------------------ */
114     public void doFilter(ServletRequest JavaDoc request,
115                          ServletResponse JavaDoc response,
116                          FilterChain JavaDoc chain)
117     throws IOException JavaDoc, ServletException JavaDoc
118     {
119         HttpServletRequest JavaDoc srequest = (HttpServletRequest JavaDoc)request;
120         HttpServletResponse JavaDoc sresponse = (HttpServletResponse JavaDoc)response;
121         Request JavaDoc requestWrapper=null;
122         Response JavaDoc responseWrapper=null;
123         boolean root_filter=false;
124         
125
126         // Do we need a root wrapper?
127
ThreadState state = state();
128         if (_unwrappedDispatchSupported || LazyList.size(_requestAttributeListeners)>0 )
129         {
130             if (srequest instanceof ServletHttpRequest)
131             {
132                 request=state.rootRequest=requestWrapper=new Request JavaDoc(srequest);
133                 response=state.rootResponse=responseWrapper=new Response JavaDoc(sresponse);
134                 root_filter=true;
135             }
136             else
137             {
138                 requestWrapper=state.rootRequest;
139                 responseWrapper=state.rootResponse;
140             }
141         }
142         
143         // Is this the first time this request has been in this _context?
144
boolean first_in_context=root_filter ||
145             requestWrapper!=null &&
146             requestWrapper.getRequest()!=null && requestWrapper.getRequest() instanceof Dispatcher.DispatcherRequest &&
147             ((Dispatcher.DispatcherRequest)requestWrapper.getRequest()).crossContext();
148         
149         if (first_in_context)
150         {
151             requestInitialized(request);
152             
153             if (requestWrapper!=null && LazyList.size(_requestAttributeListeners)>0)
154                 requestWrapper.addContextFilter(this);
155         }
156         
157         // setup dispatch
158
boolean dispatch=false;
159         if (_unwrappedDispatchSupported && state.dispatchRequest!=null)
160         {
161             dispatch=true;
162             requestWrapper.pushWrapper(state.dispatchRequest);
163             responseWrapper.pushWrapper(state.dispatchResponse);
164             state.dispatchRequest=null;
165             state.dispatchResponse=null;
166         }
167         
168         try
169         {
170             chain.doFilter(request, response);
171         }
172         finally
173         {
174             if (first_in_context)
175             {
176                 requestDestroyed(request);
177                 if (requestWrapper!=null && LazyList.size(_requestAttributeListeners)>0)
178                     requestWrapper.delContextFilter(this);
179             }
180             
181             if (root_filter)
182                 state.clear();
183             
184             if (dispatch)
185             {
186                 requestWrapper.popWrapper();
187                 responseWrapper.popWrapper();
188             }
189         }
190     }
191
192     /* ------------------------------------------------------------ */
193     public void destroy()
194     {
195     }
196     
197     /* ------------------------------------------------------------ */
198     private void requestInitialized(ServletRequest JavaDoc request)
199     {
200         ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_servletContext,request);
201         for (int i=0;i<LazyList.size(_requestListeners);i++)
202             ((ServletRequestListener JavaDoc)LazyList.get(_requestListeners,i))
203                         .requestInitialized(event);
204     }
205     
206     /* ------------------------------------------------------------ */
207     private void requestDestroyed(ServletRequest JavaDoc request)
208     {
209         ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_servletContext,request);
210         for (int i=LazyList.size(_requestListeners);i-->0;)
211             ((ServletRequestListener JavaDoc)LazyList.get(_requestListeners,i))
212                         .requestDestroyed(event);
213     }
214     
215     /* ------------------------------------------------------------ */
216     private void attributeNotify(ServletRequest JavaDoc request,String JavaDoc name,Object JavaDoc oldValue,Object JavaDoc newValue)
217     {
218         ServletRequestAttributeEvent JavaDoc event =
219             new ServletRequestAttributeEvent JavaDoc(_servletContext,request,name,oldValue==null?newValue:oldValue);
220         for (int i=0;i<LazyList.size(_requestAttributeListeners);i++)
221         {
222             ServletRequestAttributeListener JavaDoc listener =
223                 ((ServletRequestAttributeListener JavaDoc)LazyList.get(_requestAttributeListeners,i));
224             if (oldValue==null)
225                 listener.attributeAdded(event);
226             else if (newValue==null)
227                 listener.attributeRemoved(event);
228             else
229                 listener.attributeReplaced(event);
230         }
231     }
232
233     /* ------------------------------------------------------------ */
234     private static ThreadState state()
235     {
236         ThreadState state=(ThreadState)__states.get();
237         if (state==null)
238         {
239             state=new ThreadState();
240             __states.set(state);
241         }
242         return state;
243     }
244
245     /* ------------------------------------------------------------ */
246     /* ------------------------------------------------------------ */
247     /* ------------------------------------------------------------ */
248     private static class ThreadState
249     {
250         Request JavaDoc rootRequest;
251         Response JavaDoc rootResponse;
252         Dispatcher.DispatcherRequest dispatchRequest;
253         Dispatcher.DispatcherResponse dispatchResponse;
254         
255         void clear()
256         {
257             rootRequest=null;
258             rootResponse=null;
259             dispatchRequest=null;
260             dispatchResponse=null;
261         }
262     }
263     
264     /* ------------------------------------------------------------ */
265     /* ------------------------------------------------------------ */
266     /* ------------------------------------------------------------ */
267     private static class Request extends HttpServletRequestWrapper JavaDoc
268     {
269         Object JavaDoc contextFilters;
270         
271         /* ------------------------------------------------------------ */
272         Request(HttpServletRequest JavaDoc httpServletRequest)
273         {
274             super(httpServletRequest);
275         }
276         
277
278         /* ------------------------------------------------------------ */
279         /**
280          * @param filter
281          */

282         public void delContextFilter(JSR154Filter filter)
283         {
284             contextFilters=LazyList.remove(contextFilters,filter);
285         }
286
287
288         /* ------------------------------------------------------------ */
289         /**
290          * @param filter
291          */

292         public void addContextFilter(JSR154Filter filter)
293         {
294             contextFilters=LazyList.add(contextFilters,filter);
295         }
296
297         /* ------------------------------------------------------------ */
298         public void setAttribute(String JavaDoc name, Object JavaDoc value)
299         {
300             Object JavaDoc old=getAttribute(name);
301             super.setAttribute(name,value);
302             for (int i=LazyList.size(contextFilters);i-->0;)
303                 ((JSR154Filter)LazyList.get(contextFilters, i)).attributeNotify(this,name,old,value);
304         }
305         
306         /* ------------------------------------------------------------ */
307         public void removeAttribute(String JavaDoc name)
308         {
309             Object JavaDoc old=getAttribute(name);
310             super.removeAttribute(name);
311             for (int i=LazyList.size(contextFilters);i-->0;)
312                 ((JSR154Filter)LazyList.get(contextFilters, i)).attributeNotify(this,name,old,null);
313         }
314
315         /* ------------------------------------------------------------ */
316         public void pushWrapper(HttpServletRequestWrapper JavaDoc wrapper)
317         {
318             wrapper.setRequest(getRequest());
319             setRequest(wrapper);
320         }
321         
322         /* ------------------------------------------------------------ */
323         public void popWrapper()
324         {
325             HttpServletRequestWrapper JavaDoc wrapper=(HttpServletRequestWrapper JavaDoc)getRequest();
326             HttpServletRequest JavaDoc request=(HttpServletRequest JavaDoc)wrapper.getRequest();
327             setRequest(request);
328         }
329     }
330     
331     /* ------------------------------------------------------------ */
332     /* ------------------------------------------------------------ */
333     /* ------------------------------------------------------------ */
334     private static class Response extends HttpServletResponseWrapper JavaDoc
335     {
336         /* ------------------------------------------------------------ */
337         Response(HttpServletResponse JavaDoc httpServletResponse)
338         {
339             super(httpServletResponse);
340         }
341         
342         /* ------------------------------------------------------------ */
343         public void pushWrapper(HttpServletResponseWrapper JavaDoc wrapper)
344         {
345             wrapper.setResponse(getResponse());
346             setResponse(wrapper);
347         }
348         
349         /* ------------------------------------------------------------ */
350         public void popWrapper()
351         {
352             HttpServletResponseWrapper JavaDoc wrapper=(HttpServletResponseWrapper JavaDoc)getResponse();
353             HttpServletResponse JavaDoc response=(HttpServletResponse JavaDoc)wrapper.getResponse();
354             setResponse(response);
355         }
356     }
357
358 }
359
360
Popular Tags