1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 package org.apache.activemq.web.filter; 18 19 import java.io.IOException; 20 import java.util.AbstractMap; 21 import java.util.Collections; 22 import java.util.Map; 23 import java.util.Set; 24 25 import javax.servlet.Filter; 26 import javax.servlet.FilterChain; 27 import javax.servlet.FilterConfig; 28 import javax.servlet.ServletContext; 29 import javax.servlet.ServletException; 30 import javax.servlet.ServletRequest; 31 import javax.servlet.ServletResponse; 32 33 import org.springframework.web.bind.ServletRequestDataBinder; 34 import org.springframework.web.context.WebApplicationContext; 35 import org.springframework.web.context.support.WebApplicationContextUtils; 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 <c.set var="myfoo" 48 * value="${applicationContext.foo}"/> <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: 565003 $ 56 */ 57 public class ApplicationContextFilter implements Filter { 58 59 private ServletContext servletContext; 60 private String applicationContextName = "applicationContext"; 61 private String requestContextName = "requestContext"; 62 private String requestName = "request"; 63 64 public void init(FilterConfig config) throws ServletException { 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 wrapper = createApplicationContextWrapper(context); 73 servletContext.setAttribute(applicationContextName, wrapper); 74 } 75 76 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 77 // lets register a requestContext in the requestScope 78 Map 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 getServletContext() { 88 return servletContext; 89 } 90 91 public String getApplicationContextName() { 92 return applicationContextName; 93 } 94 95 public void setApplicationContextName(String variableName) { 96 this.applicationContextName = variableName; 97 } 98 99 public String getRequestContextName() { 100 return requestContextName; 101 } 102 103 public void setRequestContextName(String requestContextName) { 104 this.requestContextName = requestContextName; 105 } 106 107 protected String getInitParameter(FilterConfig config, String key, String defaultValue) { 108 String 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 createApplicationContextWrapper(final WebApplicationContext context) { 118 Map wrapper = new AbstractMap() { 119 120 public WebApplicationContext getContext() { 121 return context; 122 } 123 124 public Object get(Object key) { 125 if (key == null) { 126 return null; 127 } 128 return context.getBean(key.toString()); 129 } 130 131 public Set 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 createRequestContextWrapper(final ServletRequest request) { 146 final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); 147 Map wrapper = new AbstractMap() { 148 149 public WebApplicationContext getContext() { 150 return context; 151 } 152 153 public Object get(Object key) { 154 if (key == null) { 155 return null; 156 } 157 return bindRequestBean(context.getBean(key.toString()), request); 158 } 159 160 public Set 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 bindRequestBean(Object bean, ServletRequest request) { 175 ServletRequestDataBinder binder = new ServletRequestDataBinder(bean, null); 176 binder.bind(request); 177 return bean; 178 } 179 180 }