KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > servlet > ExceptionHandler


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.directwebremoting.extend.Handler;
25 import org.directwebremoting.util.Continuation;
26 import org.directwebremoting.util.Logger;
27 import org.directwebremoting.util.MimeConstants;
28
29 /**
30  * Handles an exception occuring during the request disptaching.
31  * @author Joe Walker [joe at getahead dot ltd dot uk]
32  */

33 public class ExceptionHandler implements Handler
34 {
35     /* (non-Javadoc)
36      * @see org.directwebremoting.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
37      */

38     public void handle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc
39     {
40         // Allow Jetty RequestRetry exception to propogate to container
41
Continuation.rethrowIfContinuation(cause);
42
43         log.warn("Error: " + cause);
44         if (cause instanceof SecurityException JavaDoc && log.isDebugEnabled())
45         {
46             log.debug("- User Agent: " + request.getHeader(HttpConstants.HEADER_USER_AGENT));
47             log.debug("- Remote IP: " + request.getRemoteAddr());
48             log.debug("- Request URL:" + request.getRequestURL());
49             log.debug("- Query: " + request.getQueryString());
50             log.debug("- Method: " + request.getMethod());
51         }
52
53         // We are going to act on this in engine.js so we are hoping that
54
// that SC_NOT_IMPLEMENTED (501) is not something that the servers
55
// use that much. I would have used something unassigned like 506+
56
// But that could cause future problems and might not get through
57
// proxies and the like
58
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
59         response.setContentType(MimeConstants.MIME_HTML);
60         PrintWriter JavaDoc out = response.getWriter();
61         out.println(cause.getMessage());
62
63         log.warn("Sent 501", cause);
64     }
65
66     /**
67      * @param cause The cause of the failure
68      */

69     public void setException(Exception JavaDoc cause)
70     {
71         this.cause = cause;
72     }
73
74     /**
75      * The cause of the failure
76      */

77     private Exception JavaDoc cause;
78
79     /**
80      * The log stream
81      */

82     private static final Logger log = Logger.getLogger(ExceptionHandler.class);
83 }
84
Popular Tags