1 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 ; 25 import javax.servlet.FilterChain ; 26 import javax.servlet.FilterConfig ; 27 import javax.servlet.ServletContext ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.ServletResponse ; 31 import java.io.IOException ; 32 import java.util.AbstractMap ; 33 import java.util.Collections ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 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 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 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 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 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 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 } 181 | Popular Tags |