1 3 package jodd.servlet; 4 5 import jodd.servlet.filter.CharArrayResponseWrapper; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 import javax.servlet.ServletRequest ; 10 import javax.servlet.ServletResponse ; 11 import javax.servlet.ServletException ; 12 import javax.servlet.RequestDispatcher ; 13 import javax.servlet.ServletContext ; 14 import java.io.IOException ; 15 16 19 public class DispatcherUtil { 20 21 23 26 public static boolean include(ServletRequest request, ServletResponse response, String page) throws IOException , ServletException { 27 RequestDispatcher dispatcher = request.getRequestDispatcher(page); 28 if (dispatcher != null) { 29 dispatcher.include(request, response); 30 return true; 31 } 32 return false; 33 } 34 35 38 public static boolean includeNamed(HttpServletRequest request, ServletResponse response, String resource) throws IOException , ServletException { 39 return includeNamed(request.getSession().getServletContext(), request, response, resource); 40 } 41 42 43 46 public static boolean includeNamed(ServletContext context, ServletRequest request, ServletResponse response, String page) throws IOException , ServletException { 47 RequestDispatcher dispatcher = context.getNamedDispatcher(page); 48 if (dispatcher != null) { 49 dispatcher.include(request, response); 50 return true; 51 } 52 return false; 53 } 54 55 56 57 60 public static boolean includeAbsolute(HttpServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 61 return includeAbsolute(request.getSession().getServletContext(), request, response, page); 62 } 63 64 65 68 public static boolean includeAbsolute(ServletContext context, ServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 69 RequestDispatcher dispatcher = context.getRequestDispatcher(page); 70 if (dispatcher != null) { 71 dispatcher.include(request, response); 72 return true; 73 } 74 return false; 75 } 76 77 78 80 public static CharArrayResponseWrapper wrap(ServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 81 CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response); 82 include(request, wrapper, page); 83 return wrapper; 84 } 85 86 public static CharArrayResponseWrapper wrapNamed(HttpServletRequest request, HttpServletResponse response, String resource) throws IOException , ServletException { 87 CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response); 88 includeNamed(request, wrapper, resource); 89 return wrapper; 90 } 91 92 public static CharArrayResponseWrapper wrapNamed(ServletContext context, HttpServletRequest request, HttpServletResponse response, String resource) throws IOException , ServletException { 93 CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response); 94 includeNamed(context, request, wrapper, resource); 95 return wrapper; 96 } 97 98 public static CharArrayResponseWrapper wrapAbsolute(HttpServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 99 CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response); 100 includeAbsolute(request, wrapper, page); 101 return wrapper; 102 } 103 104 105 public static CharArrayResponseWrapper wrapAbsolute(ServletContext context, ServletRequest request, HttpServletResponse response, String page) throws IOException , ServletException { 106 CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response); 107 includeAbsolute(context, request, wrapper, page); 108 return wrapper; 109 } 110 111 112 114 117 public static boolean forward(ServletRequest request, ServletResponse response, String page) throws IOException , ServletException { 118 RequestDispatcher dispatcher = request.getRequestDispatcher(page); 119 if (dispatcher != null) { 120 dispatcher.forward(request, response); 121 return true; 122 } 123 return false; 124 } 125 126 129 public static boolean forwardNamed(HttpServletRequest request, ServletResponse response, String resource) throws IOException , ServletException { 130 return forwardNamed(request.getSession().getServletContext(), request, response, resource); 131 } 132 133 136 public static boolean forwardNamed(ServletContext context, ServletRequest request, ServletResponse response, String resource) throws IOException , ServletException { 137 RequestDispatcher dispatcher = context.getNamedDispatcher(resource); 138 if (dispatcher != null) { 139 dispatcher.forward(request, response); 140 return true; 141 } 142 return false; 143 } 144 145 148 public static boolean forwardAbsolute(HttpServletRequest request, ServletResponse response, String page) throws IOException , ServletException { 149 return forwardAbsolute(request.getSession().getServletContext(), request, response, page); 150 } 151 152 155 public static boolean forwardAbsolute(ServletContext context, ServletRequest request, ServletResponse response, String resource) throws IOException , ServletException { 156 RequestDispatcher dispatcher = context.getRequestDispatcher(resource); 157 if (dispatcher != null) { 158 dispatcher.forward(request, response); 159 return true; 160 } 161 return false; 162 } 163 164 166 167 170 public static void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { 171 if (url.startsWith("/") == true) { 172 url = request.getContextPath() + url; 173 } 174 response.sendRedirect(response.encodeRedirectURL(url)); 175 } 176 177 178 180 183 public static String getActionPath(HttpServletRequest request) { 184 return makeActionPath(request.getRequestURI(), request.getQueryString()); 185 } 186 187 190 public static String makeActionPath(String uri, String query) { 191 if ((query != null) && (query.length() != 0)) { 192 uri += '?' + query; 193 } 194 return uri; 195 } 196 197 198 200 public static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri"; 201 public static final String INCLUDE_CONTEXT_PATH = "javax.servlet.include.context_path"; 202 public static final String INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path"; 203 public static final String INCLUDE_PATH_INFO = "javax.servlet.include.path_info"; 204 public static final String INCLUDE_QUERY_STRING = "javax.servlet.include.query_string"; 205 206 207 public static String getIncludeRequestUri(HttpServletRequest request) { 208 return (String ) request.getAttribute(INCLUDE_REQUEST_URI); 209 } 210 211 public static String getIncludeContextPath(HttpServletRequest request) { 212 return (String ) request.getAttribute(INCLUDE_CONTEXT_PATH); 213 } 214 215 public static String getIncludeServletPath(HttpServletRequest request) { 216 return (String ) request.getAttribute(INCLUDE_SERVLET_PATH); 217 } 218 219 public static String getIncludePathInfo(HttpServletRequest request) { 220 return (String ) request.getAttribute(INCLUDE_PATH_INFO); 221 } 222 223 public static String getIncludeQueryString(HttpServletRequest request) { 224 return (String ) request.getAttribute(INCLUDE_QUERY_STRING); 225 } 226 227 229 public static final String FORWARD_REQUEST_URI = "javax.servlet.forward.request_uri"; 230 public static final String FORWARD_CONTEXT_PATH = "javax.servlet.forward.context_path"; 231 public static final String FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path"; 232 public static final String FORWARD_PATH_INFO = "javax.servlet.forward.path_info"; 233 public static final String FORWARD_QUERY_STRING = "javax.servlet.forward.query_string"; 234 235 public static String getForwardRequestUri(HttpServletRequest request) { 236 return (String ) request.getAttribute(FORWARD_REQUEST_URI); 237 } 238 239 public static String getForwardContextPath(HttpServletRequest request) { 240 return (String ) request.getAttribute(FORWARD_CONTEXT_PATH); 241 } 242 243 public static String getForwardServletPath(HttpServletRequest request) { 244 return (String ) request.getAttribute(FORWARD_SERVLET_PATH); 245 } 246 247 public static String getForwardPathInfo(HttpServletRequest request) { 248 return (String ) request.getAttribute(FORWARD_PATH_INFO); 249 } 250 251 public static String getForwardQueryString(HttpServletRequest request) { 252 return (String ) request.getAttribute(FORWARD_QUERY_STRING); 253 } 254 255 257 260 public static boolean isPageIncluded(HttpServletRequest request, HttpServletResponse response) { 261 return (response.isCommitted() || (getIncludeServletPath(request) != null)); 262 } 263 264 268 public static boolean isTopLevelRequest(HttpServletRequest request) { 269 return (getForwardRequestUri(request) == null) && (getIncludeRequestUri(request) == null); 270 } 271 272 274 277 public static String getBaseRequestUri(HttpServletRequest request) { 278 String result = getForwardRequestUri(request); 279 if (result == null) { 280 result = request.getRequestURI(); 281 } 282 return result; 283 } 284 287 public static String getRequestUri(HttpServletRequest request) { 288 String result = getIncludeRequestUri(request); 289 if (result == null) { 290 result = request.getRequestURI(); 291 } 292 return result; 293 } 294 295 public static String getBaseContextPath(HttpServletRequest request) { 296 String result = getForwardContextPath(request); 297 if (result == null) { 298 result = request.getContextPath(); 299 } 300 return result; 301 } 302 public static String getContextPath(HttpServletRequest request) { 303 String result = getIncludeContextPath(request); 304 if (result == null) { 305 result = request.getContextPath(); 306 } 307 return result; 308 } 309 310 311 public static String getBaseServletPath(HttpServletRequest request) { 312 String result = getForwardServletPath(request); 313 if (result == null) { 314 result = request.getServletPath(); 315 } 316 return result; 317 } 318 public static String getServletPath(HttpServletRequest request) { 319 String result = getIncludeServletPath(request); 320 if (result == null) { 321 result = request.getServletPath(); 322 } 323 return result; 324 } 325 326 327 public static String getBasePathInfo(HttpServletRequest request) { 328 String result = getForwardPathInfo(request); 329 if (result == null) { 330 result = request.getPathInfo(); 331 } 332 return result; 333 } 334 public static String getPathInfo(HttpServletRequest request) { 335 String result = getIncludePathInfo(request); 336 if (result == null) { 337 result = request.getPathInfo(); 338 } 339 return result; 340 } 341 342 343 public static String getBaseQueryString(HttpServletRequest request) { 344 String result = getForwardQueryString(request); 345 if (result == null) { 346 result = request.getQueryString(); 347 } 348 return result; 349 } 350 public static String getQueryString(HttpServletRequest request) { 351 String result = getIncludeQueryString(request); 352 if (result == null) { 353 result = request.getQueryString(); 354 } 355 return result; 356 } 357 } 358 | Popular Tags |