1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.io.IOException ; 23 24 import javax.servlet.RequestDispatcher ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.catalina.CometEvent; 30 import org.apache.catalina.Context; 31 import org.apache.catalina.Globals; 32 import org.apache.catalina.Wrapper; 33 import org.apache.catalina.connector.ClientAbortException; 34 import org.apache.catalina.connector.Request; 35 import org.apache.catalina.connector.Response; 36 import org.apache.catalina.deploy.ErrorPage; 37 import org.apache.catalina.util.RequestUtil; 38 import org.apache.catalina.util.StringManager; 39 import org.apache.catalina.valves.ValveBase; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 44 55 56 final class StandardHostValve 57 extends ValveBase { 58 59 60 private static Log log = LogFactory.getLog(StandardHostValve.class); 61 62 64 65 68 private static final String info = 69 "org.apache.catalina.core.StandardHostValve/1.0"; 70 71 72 75 private static final StringManager sm = 76 StringManager.getManager(Constants.Package); 77 78 79 81 82 85 public String getInfo() { 86 87 return (info); 88 89 } 90 91 92 94 95 107 public final void invoke(Request request, Response response) 108 throws IOException , ServletException { 109 110 Context context = request.getContext(); 112 if (context == null) { 113 response.sendError 114 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 115 sm.getString("standardHost.noContext")); 116 return; 117 } 118 119 if( context.getLoader() != null ) { 121 Thread.currentThread().setContextClassLoader 124 (context.getLoader().getClassLoader()); 125 } 126 127 context.getPipeline().getFirst().invoke(request, response); 129 130 if (Globals.STRICT_SERVLET_COMPLIANCE) { 133 request.getSession(false); 134 } 135 136 response.setSuspended(false); 138 139 Throwable t = (Throwable ) request.getAttribute(Globals.EXCEPTION_ATTR); 140 141 if (t != null) { 142 throwable(request, response, t); 143 } else { 144 status(request, response); 145 } 146 147 Thread.currentThread().setContextClassLoader 149 (StandardHostValve.class.getClassLoader()); 150 151 } 152 153 154 164 public final void event(Request request, Response response, CometEvent event) 165 throws IOException , ServletException { 166 167 Context context = request.getContext(); 169 170 if( context.getLoader() != null ) { 172 Thread.currentThread().setContextClassLoader 175 (context.getLoader().getClassLoader()); 176 } 177 178 context.getPipeline().getFirst().event(request, response, event); 180 181 if (Globals.STRICT_SERVLET_COMPLIANCE) { 184 request.getSession(false); 185 } 186 187 response.setSuspended(false); 189 190 Throwable t = (Throwable ) request.getAttribute(Globals.EXCEPTION_ATTR); 191 192 if (t != null) { 193 throwable(request, response, t); 194 } else { 195 status(request, response); 196 } 197 198 Thread.currentThread().setContextClassLoader 200 (StandardHostValve.class.getClassLoader()); 201 202 } 203 204 205 207 208 219 protected void throwable(Request request, Response response, 220 Throwable throwable) { 221 Context context = request.getContext(); 222 if (context == null) 223 return; 224 225 Throwable realError = throwable; 226 227 if (realError instanceof ServletException ) { 228 realError = ((ServletException ) realError).getRootCause(); 229 if (realError == null) { 230 realError = throwable; 231 } 232 } 233 234 if (realError instanceof ClientAbortException ) { 236 if (log.isDebugEnabled()) { 237 log.debug 238 (sm.getString("standardHost.clientAbort", 239 realError.getCause().getMessage())); 240 } 241 return; 242 } 243 244 ErrorPage errorPage = findErrorPage(context, throwable); 245 if ((errorPage == null) && (realError != throwable)) { 246 errorPage = findErrorPage(context, realError); 247 } 248 249 if (errorPage != null) { 250 response.setAppCommitted(false); 251 request.setAttribute 252 (ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, 253 errorPage.getLocation()); 254 request.setAttribute(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR, 255 new Integer (ApplicationFilterFactory.ERROR)); 256 request.setAttribute 257 (Globals.STATUS_CODE_ATTR, 258 new Integer (HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); 259 request.setAttribute(Globals.ERROR_MESSAGE_ATTR, 260 throwable.getMessage()); 261 request.setAttribute(Globals.EXCEPTION_ATTR, 262 realError); 263 Wrapper wrapper = request.getWrapper(); 264 if (wrapper != null) 265 request.setAttribute(Globals.SERVLET_NAME_ATTR, 266 wrapper.getName()); 267 request.setAttribute(Globals.EXCEPTION_PAGE_ATTR, 268 request.getRequestURI()); 269 request.setAttribute(Globals.EXCEPTION_TYPE_ATTR, 270 realError.getClass()); 271 if (custom(request, response, errorPage)) { 272 try { 273 response.flushBuffer(); 274 } catch (IOException e) { 275 container.getLogger().warn("Exception Processing " + errorPage, e); 276 } 277 } 278 } else { 279 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 284 response.setError(); 286 287 status(request, response); 288 } 289 290 291 } 292 293 294 303 protected void status(Request request, Response response) { 304 305 int statusCode = response.getStatus(); 306 307 Context context = request.getContext(); 309 if (context == null) 310 return; 311 312 317 if (!response.isError()) 318 return; 319 320 ErrorPage errorPage = context.findErrorPage(statusCode); 321 if (errorPage != null) { 322 response.setAppCommitted(false); 323 request.setAttribute(Globals.STATUS_CODE_ATTR, 324 new Integer (statusCode)); 325 326 String message = RequestUtil.filter(response.getMessage()); 327 if (message == null) 328 message = ""; 329 request.setAttribute(Globals.ERROR_MESSAGE_ATTR, message); 330 request.setAttribute 331 (ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, 332 errorPage.getLocation()); 333 request.setAttribute(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR, 334 new Integer (ApplicationFilterFactory.ERROR)); 335 336 337 Wrapper wrapper = request.getWrapper(); 338 if (wrapper != null) 339 request.setAttribute(Globals.SERVLET_NAME_ATTR, 340 wrapper.getName()); 341 request.setAttribute(Globals.EXCEPTION_PAGE_ATTR, 342 request.getRequestURI()); 343 if (custom(request, response, errorPage)) { 344 try { 345 response.flushBuffer(); 346 } catch (IOException e) { 347 container.getLogger().warn("Exception Processing " + errorPage, e); 348 } 349 } 350 } 351 352 } 353 354 355 364 protected static ErrorPage findErrorPage 365 (Context context, Throwable exception) { 366 367 if (exception == null) 368 return (null); 369 Class clazz = exception.getClass(); 370 String name = clazz.getName(); 371 while (!Object .class.equals(clazz)) { 372 ErrorPage errorPage = context.findErrorPage(name); 373 if (errorPage != null) 374 return (errorPage); 375 clazz = clazz.getSuperclass(); 376 if (clazz == null) 377 break; 378 name = clazz.getName(); 379 } 380 return (null); 381 382 } 383 384 385 397 protected boolean custom(Request request, Response response, 398 ErrorPage errorPage) { 399 400 if (container.getLogger().isDebugEnabled()) 401 container.getLogger().debug("Processing " + errorPage); 402 403 request.setPathInfo(errorPage.getLocation()); 404 405 try { 406 407 Integer statusCodeObj = 411 (Integer ) request.getAttribute(Globals.STATUS_CODE_ATTR); 412 int statusCode = statusCodeObj.intValue(); 413 String message = 414 (String ) request.getAttribute(Globals.ERROR_MESSAGE_ATTR); 415 response.reset(statusCode, message); 416 417 ServletContext servletContext = 419 request.getContext().getServletContext(); 420 RequestDispatcher rd = 421 servletContext.getRequestDispatcher(errorPage.getLocation()); 422 rd.forward(request.getRequest(), response.getResponse()); 423 424 response.setSuspended(false); 426 427 return (true); 429 430 } catch (Throwable t) { 431 432 container.getLogger().error("Exception Processing " + errorPage, t); 434 return (false); 435 436 } 437 438 } 439 440 441 } 442 | Popular Tags |