1 40 41 42 package org.jahia.services.applications; 43 44 import java.util.*; 45 46 import org.jahia.data.applications.ServletBean; 47 48 import javax.servlet.RequestDispatcher ; 49 import javax.servlet.ServletException ; 50 import javax.servlet.ServletRequest ; 51 import javax.servlet.ServletResponse ; 52 53 59 public class RequestDispatcherWrapper implements RequestDispatcher { 60 61 private static org.apache.log4j.Logger logger = 62 org.apache.log4j.Logger.getLogger (RequestDispatcherWrapper.class); 63 64 private StrutsRequestDispatcherProcessor strutsRDP; 66 67 private RequestDispatcher requestDispatcher; 68 69 private boolean dispatcherObtainedByGetNamedDispatcher = false; 70 71 public static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri" ; 72 public static final String INCLUDE_CONTEXT_PATH = "javax.servlet.include.context_path"; 73 public static final String INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path"; 74 public static final String INCLUDE_PATH_INFO = "javax.servlet.include.path_info"; 75 public static final String INCLUDE_QUERY_STRING = "javax.servlet.include.query_string"; 76 77 public static final String FORWARD_REQUEST_URI = "javax.servlet.forward.request_uri" ; 78 public static final String FORWARD_CONTEXT_PATH = "javax.servlet.forward.context_path"; 79 public static final String FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path"; 80 public static final String FORWARD_PATH_INFO = "javax.servlet.forward.path_info"; 81 public static final String FORWARD_QUERY_STRING = "javax.servlet.forward.query_string"; 82 83 public static String [] SERVLET_DISPACHER_REQUEST_ATTRIBUTES = 84 {INCLUDE_REQUEST_URI,INCLUDE_CONTEXT_PATH,INCLUDE_SERVLET_PATH, 85 INCLUDE_PATH_INFO, INCLUDE_QUERY_STRING, FORWARD_REQUEST_URI, 86 FORWARD_CONTEXT_PATH, FORWARD_SERVLET_PATH, FORWARD_PATH_INFO, 87 FORWARD_QUERY_STRING}; 88 89 public static final String EMPTY_ATTRIBUTE = "RequestDispatcherWrapper.EMPTY_ATTRIBUTE"; 90 91 95 public RequestDispatcherWrapper (RequestDispatcher requestDispatcher, 96 boolean dispatcherObtainedByGetNamedDispatcher) { 97 98 logger.debug ("Creating a new wrapper."); 99 100 this.requestDispatcher = requestDispatcher; 101 this.dispatcherObtainedByGetNamedDispatcher = 102 dispatcherObtainedByGetNamedDispatcher; 103 this.strutsRDP = new StrutsRequestDispatcherProcessor (); 104 } 105 106 public void forward (ServletRequest request, ServletResponse response) 108 throws ServletException , java.io.IOException { 109 110 this.strutsRDP.preForward ((ServletIncludeRequestWrapper) request, 111 (ServletIncludeResponseWrapper) response); 112 113 String requestURI = ((ServletIncludeRequestWrapper) request).getRequestURI (); 114 logger.debug("requestWrapper.getRequestURI() returned : " + requestURI); 115 String contextPath = ((ServletIncludeRequestWrapper) request).getContextPath(); 116 logger.debug("requestWrapper.getContextPath() returned : " + contextPath); 117 String servletPath = ((ServletIncludeRequestWrapper) request).getServletPath(); 118 logger.debug("requestWrapper.getServletPath() returned : " + servletPath); 119 String pathInfo = ((ServletIncludeRequestWrapper) request).getPathInfo(); 120 logger.debug("requestWrapper.getPathInfo() returned : " + pathInfo); 121 String queryString = ((ServletIncludeRequestWrapper) request).getQueryString(); 122 logger.debug("requestWrapper.getQueryString() returned : " + queryString); 123 124 PersistantServletRequest appRequest = ((ServletIncludeRequestWrapper) request).getPersistantServletRequest (); 126 if (appRequest != null) { 127 if (appRequest.getWebAppType () == ServletBean.JSP_TYPE 128 && requestURI != null 129 && (requestURI.toLowerCase ().indexOf (".jsp") == -1)) { 130 if ( !this.dispatcherObtainedByGetNamedDispatcher ){ 132 request.setAttribute(RequestDispatcherWrapper.INCLUDE_REQUEST_URI,requestURI); 133 request.setAttribute(RequestDispatcherWrapper.INCLUDE_CONTEXT_PATH,contextPath); 134 request.setAttribute(RequestDispatcherWrapper.INCLUDE_SERVLET_PATH,servletPath); 135 request.setAttribute(RequestDispatcherWrapper.INCLUDE_PATH_INFO,pathInfo); 136 request.setAttribute(RequestDispatcherWrapper.INCLUDE_QUERY_STRING,queryString); 137 } else { 138 144 } 145 146 this.include (request, response); 147 148 this.strutsRDP.postForward ((ServletIncludeRequestWrapper) request, 149 (ServletIncludeResponseWrapper) response); 150 return; 151 } 152 } 153 if ( !this.dispatcherObtainedByGetNamedDispatcher ){ 154 request.setAttribute(RequestDispatcherWrapper.FORWARD_REQUEST_URI,requestURI); 155 request.setAttribute(RequestDispatcherWrapper.FORWARD_CONTEXT_PATH,contextPath); 156 request.setAttribute(RequestDispatcherWrapper.FORWARD_SERVLET_PATH,servletPath); 157 request.setAttribute(RequestDispatcherWrapper.FORWARD_PATH_INFO,pathInfo); 158 request.setAttribute(RequestDispatcherWrapper.FORWARD_QUERY_STRING,queryString); 159 } else { 160 166 } 167 168 this.requestDispatcher.forward (request, response); 169 170 this.strutsRDP.postForward ((ServletIncludeRequestWrapper) request, 171 (ServletIncludeResponseWrapper) response); 172 173 195 196 } 197 198 public void include (ServletRequest request, ServletResponse response) 200 throws ServletException , java.io.IOException { 201 204 this.strutsRDP.preInclude ((ServletIncludeRequestWrapper) request, 205 (ServletIncludeResponseWrapper) response); 206 207 String requestURI = ((ServletIncludeRequestWrapper) request).getRequestURI (); 208 logger.debug("requestWrapper.getRequestURI() returned : " + requestURI); 209 String contextPath = ((ServletIncludeRequestWrapper) request).getContextPath(); 210 logger.debug("requestWrapper.getContextPath() returned : " + contextPath); 211 String servletPath = ((ServletIncludeRequestWrapper) request).getServletPath(); 212 logger.debug("requestWrapper.getServletPath() returned : " + servletPath); 213 String pathInfo = ((ServletIncludeRequestWrapper) request).getPathInfo(); 214 logger.debug("requestWrapper.getPathInfo() returned : " + pathInfo); 215 String queryString = ((ServletIncludeRequestWrapper) request).getQueryString(); 216 logger.debug("requestWrapper.getQueryString() returned : " + queryString); 217 218 if ( !this.dispatcherObtainedByGetNamedDispatcher ){ 219 request.setAttribute(RequestDispatcherWrapper.INCLUDE_REQUEST_URI,requestURI); 220 request.setAttribute(RequestDispatcherWrapper.INCLUDE_CONTEXT_PATH,contextPath); 221 request.setAttribute(RequestDispatcherWrapper.INCLUDE_SERVLET_PATH,servletPath); 222 request.setAttribute(RequestDispatcherWrapper.INCLUDE_PATH_INFO,pathInfo); 223 request.setAttribute(RequestDispatcherWrapper.INCLUDE_QUERY_STRING,queryString); 224 } else { 225 231 } 232 233 this.requestDispatcher.include (request, response); 234 235 this.strutsRDP.postInclude ((ServletIncludeRequestWrapper) request, 236 (ServletIncludeResponseWrapper) response); 237 238 } 239 240 241 public static HashMap backupServletDispatcherRequestAttributes(ServletRequest request){ 242 243 HashMap attributes = new HashMap(); 244 245 for ( int i=0; i<SERVLET_DISPACHER_REQUEST_ATTRIBUTES.length; i++ ){ 246 String val = (String )request.getAttribute(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i]); 247 if ( val == null ){ 248 val = EMPTY_ATTRIBUTE; 249 } 250 attributes.put(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i],val); 251 request.removeAttribute(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i]); 252 } 253 return attributes; 254 } 255 256 public static void restoreServletDispatcherRequestAttributes(ServletRequest request, 257 HashMap attributes){ 258 259 Iterator iterator = attributes.keySet().iterator(); 260 while ( iterator.hasNext() ){ 261 String key = (String )iterator.next(); 262 String val = (String )attributes.get(key); 263 if ( EMPTY_ATTRIBUTE.equals(val) ){ 264 request.removeAttribute(key); 265 } else { 266 request.setAttribute(key, val); 267 } 268 } 269 } 270 271 } 272 | Popular Tags |