KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > filter > ApplicationContextFilter


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

18 package org.apache.activemq.web.filter;
19
20 import org.springframework.web.bind.ServletRequestDataBinder;
21 import org.springframework.web.context.WebApplicationContext;
22 import org.springframework.web.context.support.WebApplicationContextUtils;
23
24 import javax.servlet.Filter JavaDoc;
25 import javax.servlet.FilterChain JavaDoc;
26 import javax.servlet.FilterConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.AbstractMap JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * Exposes Spring ApplicationContexts to JSP EL and other view technologies.
39  * Currently a variable is placed in application scope (by default called
40  * 'applicationContext') so that POJOs can be pulled out of Spring in a JSP page
41  * to render things using EL expressions. <br/>
42  *
43  * e.g. ${applicationContext.cheese} would access the cheese POJO. Or
44  * ${applicationContext.cheese.name} would access the name property of the
45  * cheese POJO. <br/>
46  *
47  * You can then use JSTL to work with these POJOs such as &lt;c.set var="myfoo"
48  * value="${applicationContext.foo}"/&gt; <br/>
49  *
50  * In addition to applicationContext a 'requestContext' variable is created
51  * which will automatically bind any request parameters to the POJOs extracted
52  * from the applicationContext - which is ideal for POJOs which implement
53  * queries in view technologies.
54  *
55  * @version $Revision: 504235 $
56  */

57 public class ApplicationContextFilter implements Filter JavaDoc {
58
59     private ServletContext JavaDoc servletContext;
60     private String JavaDoc applicationContextName = "applicationContext";
61     private String JavaDoc requestContextName = "requestContext";
62     private String JavaDoc requestName = "request";
63
64     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
65         this.servletContext = config.getServletContext();
66         this.applicationContextName = getInitParameter(config, "applicationContextName", applicationContextName);
67         this.requestContextName = getInitParameter(config, "requestContextName", requestContextName);
68         this.requestName = getInitParameter(config, "requestName", requestName);
69
70         // register the application context in the applicationScope
71
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
72         Map JavaDoc wrapper = createApplicationContextWrapper(context);
73         servletContext.setAttribute(applicationContextName, wrapper);
74     }
75
76     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
77         // lets register a requestContext in the requestScope
78
Map JavaDoc requestContextWrapper = createRequestContextWrapper(request);
79         request.setAttribute(requestContextName, requestContextWrapper);
80         request.setAttribute(requestName, request);
81         chain.doFilter(request, response);
82     }
83
84     public void destroy() {
85     }
86
87     public ServletContext JavaDoc getServletContext() {
88         return servletContext;
89     }
90
91     public String JavaDoc getApplicationContextName() {
92         return applicationContextName;
93     }
94
95     public void setApplicationContextName(String JavaDoc variableName) {
96         this.applicationContextName = variableName;
97     }
98
99     public String JavaDoc getRequestContextName() {
100         return requestContextName;
101     }
102
103     public void setRequestContextName(String JavaDoc requestContextName) {
104         this.requestContextName = requestContextName;
105     }
106
107     protected String JavaDoc getInitParameter(FilterConfig JavaDoc config, String JavaDoc key, String JavaDoc defaultValue) {
108         String JavaDoc parameter = config.getInitParameter(key);
109         return (parameter != null) ? parameter : defaultValue;
110     }
111
112     /**
113      * Creates a wrapper around the web application context so that it can be
114      * accessed easily from inside JSP EL (or other expression languages in
115      * other view technologies).
116      */

117     protected Map JavaDoc createApplicationContextWrapper(final WebApplicationContext context) {
118         Map JavaDoc wrapper = new AbstractMap JavaDoc() {
119
120             public WebApplicationContext getContext() {
121                 return context;
122             }
123
124             public Object JavaDoc get(Object JavaDoc key) {
125                 if (key == null) {
126                     return null;
127                 }
128                 return context.getBean(key.toString());
129             }
130
131             public Set JavaDoc entrySet() {
132                 return Collections.EMPTY_SET;
133             }
134
135         };
136         return wrapper;
137     }
138
139     /**
140      * Creates a wrapper around the request context (e.g. to allow POJOs to be
141      * auto-injected from request parameter values etc) so that it can be
142      * accessed easily from inside JSP EL (or other expression languages in
143      * other view technologies).
144      */

145     protected Map JavaDoc createRequestContextWrapper(final ServletRequest JavaDoc request) {
146         final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
147         Map JavaDoc wrapper = new AbstractMap JavaDoc() {
148
149             public WebApplicationContext getContext() {
150                 return context;
151             }
152
153             public Object JavaDoc get(Object JavaDoc key) {
154                 if (key == null) {
155                     return null;
156                 }
157                 return bindRequestBean(context.getBean(key.toString()), request);
158             }
159
160             public Set JavaDoc entrySet() {
161                 return Collections.EMPTY_SET;
162             }
163
164         };
165         return wrapper;
166
167     }
168
169     /**
170      * Binds properties from the request parameters to the given POJO which is
171      * useful for POJOs which are configurable via request parameters such as
172      * for query/view POJOs
173      */

174     protected Object JavaDoc bindRequestBean(Object JavaDoc bean, ServletRequest JavaDoc request) {
175         ServletRequestDataBinder binder = new ServletRequestDataBinder(bean, null);
176         binder.bind(request);
177         return bean;
178     }
179
180 }
181
Popular Tags