KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > servlet > DwrWebContextFilter


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.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.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.directwebremoting.Container;
33 import org.directwebremoting.WebContextFactory.WebContextBuilder;
34 import org.directwebremoting.util.Logger;
35 import org.directwebremoting.util.ServletLoggingOutput;
36
37 /**
38  * A Servlet Filter that can be used with other web frameworks to allow use of
39  * WebContextFactory. Any servlet threads that have their request URL mapped
40  * through a DwrWebContextFilter will have a WebContext available to them.
41  * @author Joe Walker [joe at getahead dot ltd dot uk]
42  */

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

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

56     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
57     {
58         ServletContext JavaDoc servletContext = filterConfig.getServletContext();
59
60         Container container = (Container) servletContext.getAttribute(Container.class.getName());
61         if (container == null)
62         {
63             log.error("DwrWebContextFilter can not find ServletContext attribute for the DWR Container. Is DwrServlet configured in this web-application?");
64         }
65
66         ServletConfig JavaDoc servletConfig = (ServletConfig JavaDoc) servletContext.getAttribute(ServletConfig JavaDoc.class.getName());
67         if (servletConfig == null)
68         {
69             log.error("DwrWebContextFilter can not find ServletContext attribute for the ServletConfig.");
70         }
71
72         WebContextBuilder webContextBuilder = (WebContextBuilder) servletContext.getAttribute(WebContextBuilder.class.getName());
73         if (webContextBuilder == null)
74         {
75             log.error("DwrWebContextFilter can not find ServletContext attribute for the WebContextBuilder. WebContext will not be available.");
76         }
77         else
78         {
79             try
80             {
81                 webContextBuilder.set((HttpServletRequest JavaDoc) request, (HttpServletResponse JavaDoc) response, servletConfig, servletContext, container);
82
83                 // It is totally legitimate for a servlet to be unavailable
84
// (e.g. Spring DwrController)
85
HttpServlet JavaDoc servlet = (HttpServlet JavaDoc) servletContext.getAttribute(HttpServlet JavaDoc.class.getName());
86                 if (servlet != null)
87                 {
88                     ServletLoggingOutput.setExecutionContext(servlet);
89                 }
90
91                 chain.doFilter(request, response);
92             }
93             finally
94             {
95                 webContextBuilder.unset();
96                 ServletLoggingOutput.unsetExecutionContext();
97             }
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see javax.servlet.Filter#destroy()
103      */

104     public void destroy()
105     {
106     }
107
108     /**
109      * The log stream
110      */

111     private static final Logger log = Logger.getLogger(DwrWebContextFilter.class);
112
113     /**
114      * The filter config, that we use to get at the servlet context
115      */

116     private FilterConfig JavaDoc filterConfig;
117 }
118
Popular Tags