1 5 package com.opensymphony.webwork.dispatcher; 6 7 import com.opensymphony.util.ClassLoaderUtil; 8 import com.opensymphony.webwork.WebWorkStatics; 9 import com.opensymphony.webwork.config.Configuration; 10 import com.opensymphony.webwork.dispatcher.mapper.ActionMapper; 11 import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory; 12 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping; 13 import com.opensymphony.xwork.ActionContext; 14 import com.opensymphony.xwork.interceptor.component.ComponentConfiguration; 15 import com.opensymphony.xwork.interceptor.component.ComponentManager; 16 import com.opensymphony.xwork.interceptor.component.DefaultComponentManager; 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 import javax.servlet.*; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.http.HttpSession ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.URL ; 28 import java.net.URLDecoder ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 33 102 public class FilterDispatcher implements Filter, WebWorkStatics { 103 private static final Log LOG = LogFactory.getLog(FilterDispatcher.class); 104 105 protected FilterConfig filterConfig; 106 protected String [] pathPrefixes; 107 108 public FilterConfig getFilterConfig() { 109 return filterConfig; 110 } 111 112 public void destroy() { 113 } 114 115 public void init(FilterConfig filterConfig) throws ServletException { 116 this.filterConfig = filterConfig; 117 String param = filterConfig.getInitParameter("packages"); 118 String packages = "com.opensymphony.webwork.static template"; 119 if (param != null) { 120 packages = param + " " + packages; 121 } 122 this.pathPrefixes = parse(packages); 123 DispatcherUtils.initialize(filterConfig.getServletContext()); 124 } 125 126 protected String [] parse(String packages) { 127 if (packages == null) { 128 return null; 129 } 130 List pathPrefixes = new ArrayList (); 131 132 StringTokenizer st = new StringTokenizer (packages, ", \n\t"); 133 while (st.hasMoreTokens()) { 134 String pathPrefix = st.nextToken().replace('.', '/'); 135 if (!pathPrefix.endsWith("/")) { 136 pathPrefix += "/"; 137 } 138 pathPrefixes.add(pathPrefix); 139 } 140 141 return (String []) pathPrefixes.toArray(new String [pathPrefixes.size()]); 142 } 143 144 145 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException , ServletException { 146 HttpServletRequest request = (HttpServletRequest ) req; 147 HttpServletResponse response = (HttpServletResponse ) res; 148 ActionMapper mapper = ActionMapperFactory.getMapper(); 149 ActionMapping mapping = mapper.getMapping(request); 150 151 try { 152 setupContainer(request); 153 154 if (mapping == null) { 155 if (request.getServletPath().startsWith("/webwork")) { 157 String name = request.getServletPath().substring("/webwork".length()); 158 findStaticResource(name, response); 159 } else { 160 chain.doFilter(request, response); 162 } 163 } else { 164 DispatcherUtils du = DispatcherUtils.getInstance(); 165 du.prepare(request, response); 166 167 try { 168 request = du.wrapRequest(request, filterConfig.getServletContext()); 169 } catch (IOException e) { 170 String message = "Could not wrap servlet request with MultipartRequestWrapper!"; 171 LOG.error(message, e); 172 du.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, new ServletException(message, e)); 173 return; 174 } 175 176 du.serviceAction(request, response, filterConfig.getServletContext(), mapping); 177 } 178 } finally { 179 ActionContexCleanUp.cleanUp(req); 180 } 181 } 182 183 protected void setupContainer(HttpServletRequest request) { 184 ComponentManager container = null; 185 HttpSession session = request.getSession(false); 186 ComponentManager fallback = null; 187 if (session != null) { 188 fallback = (ComponentManager) session.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY); 189 } 190 191 ServletContext servletContext = getServletContext(session); 192 if (fallback == null) { 193 fallback = (ComponentManager) servletContext.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY); 194 } 195 196 if (fallback != null) { 197 container = createComponentManager(); 198 container.setFallback(fallback); 199 } 200 201 ComponentConfiguration config = (ComponentConfiguration) servletContext.getAttribute("ComponentConfiguration"); 202 if (config != null) { 203 if (container == null) { 204 container = createComponentManager(); 205 } 206 207 config.configure(container, "request"); 208 request.setAttribute(ComponentManager.COMPONENT_MANAGER_KEY, container); 209 } 210 } 211 212 220 protected ServletContext getServletContext(HttpSession session) { 221 return filterConfig.getServletContext(); 222 } 223 224 protected void tearDownContainer() { 225 } 226 227 protected void findStaticResource(String name, HttpServletResponse response) throws IOException { 228 if (!name.endsWith(".class")) { 229 for (int i = 0; i < pathPrefixes.length; i++) { 230 InputStream is = findInputStream(name, pathPrefixes[i]); 231 if (is != null) { 232 try { 233 copy(is, response.getOutputStream()); 234 } finally { 235 is.close(); 236 } 237 return; 238 } 239 } 240 } 241 242 response.sendError(HttpServletResponse.SC_NOT_FOUND); 243 } 244 245 protected void copy(InputStream input, OutputStream output) throws IOException { 246 final byte[] buffer = new byte[4096]; 247 int n; 248 while (-1 != (n = input.read(buffer))) { 249 output.write(buffer, 0, n); 250 } 251 } 252 253 protected InputStream findInputStream(String name, String packagePrefix) throws IOException { 254 String resourcePath; 255 if (packagePrefix.endsWith("/") && name.startsWith("/")) { 256 resourcePath = packagePrefix + name.substring(1); 257 } else { 258 resourcePath = packagePrefix + name; 259 } 260 261 String enc = (String ) Configuration.get("webwork.i18n.encoding"); 262 resourcePath = URLDecoder.decode(resourcePath, enc); 263 264 return ClassLoaderUtil.getResourceAsStream(resourcePath, getClass()); 265 } 266 267 270 protected boolean checkUrl(URL url, String rawResourcePath) { 271 272 if (url.getPath().endsWith("/")) { 274 return false; 275 } 276 277 if (url.toExternalForm().indexOf(rawResourcePath) == -1) { 280 return false; 281 } 282 283 return true; 284 } 285 286 292 protected DefaultComponentManager createComponentManager() { 293 return new DefaultComponentManager(); 294 } 295 } 296 | Popular Tags |