1 25 26 27 package org.apache.catalina.core; 28 29 import java.io.*; 30 31 import javax.servlet.*; 32 import javax.servlet.http.*; 33 34 import org.apache.catalina.Context; 35 import org.apache.catalina.Globals; 36 import org.apache.catalina.Wrapper; 37 import org.apache.catalina.connector.ClientAbortException; 38 import org.apache.catalina.connector.ResponseFacade; 39 import org.apache.catalina.deploy.ErrorPage; 40 import org.apache.catalina.util.RequestUtil; 41 import org.apache.catalina.util.ResponseUtil; 42 import org.apache.catalina.util.StringManager; 43 import org.apache.catalina.valves.ErrorReportValve; 44 import org.apache.coyote.tomcat5.CoyoteResponseFacade; 45 46 import com.sun.org.apache.commons.logging.Log; 47 import com.sun.org.apache.commons.logging.LogFactory; 48 49 64 class ApplicationDispatcherForward { 65 66 private static Log log = LogFactory.getLog( 67 ApplicationDispatcherForward.class); 68 69 private static final StringManager sm = 70 StringManager.getManager(org.apache.catalina.valves.Constants.Package); 71 72 73 static void commit(HttpServletRequest request, 74 HttpServletResponse response, 75 Context context, 76 Wrapper wrapper) 77 throws IOException, ServletException { 78 79 int statusCode = getStatus(response); 80 if (statusCode >= 400 81 && request.getAttribute(Globals.EXCEPTION_ATTR) == null) { 82 boolean matchFound = status(request, response, context, wrapper, 83 statusCode); 84 if (!matchFound) { 85 serveDefaultErrorPage(request, response, statusCode); 86 } 87 } 88 89 try { 90 PrintWriter writer = response.getWriter(); 91 writer.close(); 92 } catch (IllegalStateException e) { 93 try { 94 ServletOutputStream stream = response.getOutputStream(); 95 stream.close(); 96 } catch (IllegalStateException f) { 97 ; 98 } catch (IOException f) { 99 ; 100 } 101 } catch (IOException e) { 102 ; 103 } 104 } 105 106 107 121 private static boolean status(HttpServletRequest request, 122 HttpServletResponse response, 123 Context context, 124 Wrapper wrapper, 125 int statusCode) { 126 127 boolean matchFound = false; 128 129 ErrorPage errorPage = context.findErrorPage(statusCode); 130 if (errorPage != null) { 131 132 matchFound = true; 133 134 String requestPath = (String ) request.getAttribute( 136 ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR); 137 if (requestPath == null 138 || !requestPath.equals(errorPage.getLocation())) { 139 String message = RequestUtil.filter(getMessage(response)); 140 if (message == null) { 141 message = ""; 142 } 143 prepareRequestForDispatch(request, 144 wrapper, 145 errorPage.getLocation(), 146 statusCode, 147 message); 148 custom(request, response, errorPage, context); 149 } 150 } else { 151 errorPage = ((StandardHost) context.getParent()).findErrorPage( 152 statusCode); 153 if (errorPage != null) { 154 matchFound = true; 155 try { 156 serveErrorPage(response, errorPage, statusCode); 157 } catch (Exception e) { 158 log.warn("Exception processing " + errorPage, e); 159 } 160 } 161 } 162 163 return matchFound; 164 } 165 166 167 171 private static void custom(HttpServletRequest request, 172 HttpServletResponse response, 173 ErrorPage errorPage, 174 Context context) { 175 try { 176 if (response.isCommitted()) { 178 185 resetResponse(response); 186 } 187 ServletContext servletContext = context.getServletContext(); 188 RequestDispatcher rd = 189 servletContext.getRequestDispatcher(errorPage.getLocation()); 190 rd.forward(request, response); 191 } catch (IllegalStateException ise) { 192 log.warn("Exception processing " + errorPage, ise); 193 } catch (Throwable t) { 194 log.warn("Exception processing " + errorPage, t); 195 } 196 } 197 198 199 202 private static void prepareRequestForDispatch(HttpServletRequest request, 203 Wrapper errorServlet, 204 String errorPageLocation, 205 int errorCode, 206 String errorMessage) { 207 request.setAttribute( 208 ApplicationFilterFactory.DISPATCHER_TYPE_ATTR, 209 new Integer (ApplicationFilterFactory.ERROR)); 210 211 request.setAttribute( 212 Globals.EXCEPTION_PAGE_ATTR, 213 request.getRequestURI()); 214 215 if (errorServlet != null) { 216 request.setAttribute(Globals.SERVLET_NAME_ATTR, 218 errorServlet.getName()); 219 } 220 221 request.setAttribute( 222 ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, 223 errorPageLocation); 224 225 request.setAttribute(Globals.STATUS_CODE_ATTR, new Integer (errorCode)); 226 227 request.setAttribute(Globals.ERROR_MESSAGE_ATTR, errorMessage); 228 } 229 230 233 private static void serveErrorPage(HttpServletResponse response, 234 ErrorPage errorPage, 235 int statusCode) 236 throws Exception { 237 238 ServletOutputStream ostream = null; 239 PrintWriter writer = null; 240 FileReader reader = null; 241 BufferedInputStream istream = null; 242 IOException ioe = null; 243 244 String message = errorPage.getReason(); 245 if (message != null && !response.isCommitted()) { 246 response.reset(); 247 response.setStatus(statusCode, message); 248 } 249 250 try { 251 ostream = response.getOutputStream(); 252 } catch (IllegalStateException e) { 253 writer = response.getWriter(); 256 } 257 258 if (writer != null) { 259 reader = new FileReader(errorPage.getLocation()); 260 ioe = ResponseUtil.copy(reader, writer); 261 try { 262 reader.close(); 263 } catch (Throwable t) { 264 ; 265 } 266 } else { 267 istream = new BufferedInputStream( 268 new FileInputStream(errorPage.getLocation())); 269 ioe = ResponseUtil.copy(istream, ostream); 270 try { 271 istream.close(); 272 } catch (Throwable t) { 273 ; 274 } 275 } 276 277 if (ioe != null) { 279 throw ioe; 280 } 281 } 282 283 284 287 private static void serveDefaultErrorPage(HttpServletRequest request, 288 HttpServletResponse response, 289 int statusCode) 290 throws IOException, ServletException { 291 292 if (response.isCommitted() || statusCode < 400) { 294 return; 295 } 296 297 String message = RequestUtil.filter(getMessage(response)); 298 if (message == null) { 299 message = ""; 300 } 301 302 String report = null; 304 try { 305 report = sm.getString("http." + statusCode, message); 306 } catch (Throwable t) { 307 ; 308 } 309 if (report == null) { 310 return; 311 } 312 313 String responseContents = 314 ErrorReportValve.makeErrorPage(statusCode, message, null, null, 315 report, response); 316 response.setLocale(sm.getResourceBundleLocale(response.getLocale())); 318 320 try { 321 response.setContentType("text/html"); 322 response.getWriter().write(responseContents); 323 } catch (Throwable t) { 324 log.warn("Exception sending default error page", t); 325 } 326 } 327 328 329 private static int getStatus(ServletResponse response) { 330 331 while (response instanceof ServletResponseWrapper) { 332 response = ((ServletResponseWrapper) response).getResponse(); 333 } 334 335 return ((CoyoteResponseFacade) response).getStatus(); 336 } 337 338 339 private static String getMessage(ServletResponse response) { 340 341 while (response instanceof ServletResponseWrapper) { 342 response = ((ServletResponseWrapper) response).getResponse(); 343 } 344 345 return ((CoyoteResponseFacade) response).getMessage(); 346 } 347 348 349 private static void resetResponse(ServletResponse response) { 350 351 while (response instanceof ServletResponseWrapper) { 352 response = ((ServletResponseWrapper) response).getResponse(); 353 } 354 355 ((CoyoteResponseFacade) response).setSuspended(false); 356 ((CoyoteResponseFacade) response).setAppCommitted(false); 357 } 358 359 } 360 | Popular Tags |