01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.servlet;
17:
18: import java.io.IOException;
19: import java.io.PrintWriter;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpServletResponse;
23:
24: import org.apache.commons.logging.LogFactory;
25: import org.apache.commons.logging.Log;
26: import org.directwebremoting.extend.Handler;
27: import org.directwebremoting.util.Continuation;
28: import org.directwebremoting.util.MimeConstants;
29:
30: /**
31: * Handles an exception occurring during the request dispatching.
32: * @author Joe Walker [joe at getahead dot ltd dot uk]
33: */
34: public class ExceptionHandler implements Handler {
35: /* (non-Javadoc)
36: * @see org.directwebremoting.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
37: */
38: public void handle(HttpServletRequest request,
39: HttpServletResponse response) throws IOException {
40: // Allow Jetty RequestRetry exception to propagate to container
41: Continuation.rethrowIfContinuation(cause);
42:
43: log.warn("Error: " + cause);
44: log.debug("Debug for stack trace:", cause);
45:
46: if (cause instanceof SecurityException && log.isDebugEnabled()) {
47: log
48: .debug("- User Agent: "
49: + request
50: .getHeader(HttpConstants.HEADER_USER_AGENT));
51: log.debug("- Remote IP: " + request.getRemoteAddr());
52: log.debug("- Request URL:" + request.getRequestURL());
53: log.debug("- Query: " + request.getQueryString());
54: log.debug("- Method: " + request.getMethod());
55: }
56:
57: try {
58: // We are going to act on this in engine.js so we are hoping that
59: // that SC_NOT_IMPLEMENTED (501) is not something that the servers
60: // use that much. I would have used something unassigned like 506+
61: // But that could cause future problems and might not get through
62: // proxies and the like
63: response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
64: response.setContentType(MimeConstants.MIME_HTML);
65: PrintWriter out = response.getWriter();
66: out.println(cause.getMessage());
67: } catch (Exception ex) {
68: // If the browser has gone away we expect to fail, and may not be
69: // able to recover
70: // Technically an IOException should work here, but Jetty appears to
71: // throw an ArrayIndexOutOfBoundsException sometimes, if the browser
72: // has gone away.
73: log
74: .debug("Error in error handler, the browswer probably went away: "
75: + ex);
76: }
77: }
78:
79: /**
80: * @param cause The cause of the failure
81: */
82: public void setException(Exception cause) {
83: this .cause = cause;
84: }
85:
86: /**
87: * The cause of the failure
88: */
89: private Exception cause;
90:
91: /**
92: * The log stream
93: */
94: private static final Log log = LogFactory
95: .getLog(ExceptionHandler.class);
96: }
|