001: // ========================================================================
002: // Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.jetty.servlet;
016:
017: import java.io.IOException;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.servlet.ServletContext;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.mortbay.jetty.HttpStatus;
027: import org.mortbay.jetty.handler.ContextHandler;
028: import org.mortbay.jetty.handler.ErrorHandler;
029: import org.mortbay.jetty.webapp.WebAppContext;
030: import org.mortbay.log.Log;
031: import org.mortbay.util.TypeUtil;
032:
033: /** Error Page Error Handler
034: *
035: * An ErrorHandler that maps exceptions and status codes to URIs for dispatch using
036: * the internal ERROR style of dispatch.
037: * @author gregw
038: *
039: */
040: public class ErrorPageErrorHandler extends ErrorHandler {
041: protected ServletContext _servletContext;
042: protected Map _errorPages; // code or exception to URL
043:
044: /* ------------------------------------------------------------ */
045: /**
046: * @param context
047: */
048: public ErrorPageErrorHandler() {
049: }
050:
051: /* ------------------------------------------------------------ */
052: /*
053: * @see org.mortbay.jetty.handler.ErrorHandler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
054: */
055: public void handle(String target, HttpServletRequest request,
056: HttpServletResponse response, int dispatch)
057: throws IOException {
058: if (_errorPages != null) {
059: String error_page = null;
060: Class exClass = (Class) request
061: .getAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE);
062:
063: if (ServletException.class.equals(exClass)) {
064: error_page = (String) _errorPages
065: .get(exClass.getName());
066: if (error_page == null) {
067: Throwable th = (Throwable) request
068: .getAttribute(ServletHandler.__J_S_ERROR_EXCEPTION);
069: while (th instanceof ServletException)
070: th = ((ServletException) th).getRootCause();
071: if (th != null)
072: exClass = th.getClass();
073: }
074: }
075:
076: while (error_page == null && exClass != null) {
077: error_page = (String) _errorPages
078: .get(exClass.getName());
079: exClass = exClass.getSuperclass();
080: }
081:
082: if (error_page == null) {
083: Integer code = (Integer) request
084: .getAttribute(ServletHandler.__J_S_ERROR_STATUS_CODE);
085: if (code != null)
086: error_page = (String) _errorPages.get(TypeUtil
087: .toString(code.intValue()));
088: }
089:
090: if (error_page != null) {
091: String old_error_page = (String) request
092: .getAttribute(WebAppContext.ERROR_PAGE);
093: if (old_error_page == null
094: || !old_error_page.equals(error_page)) {
095: request.setAttribute(WebAppContext.ERROR_PAGE,
096: error_page);
097:
098: Dispatcher dispatcher = (Dispatcher) _servletContext
099: .getRequestDispatcher(error_page);
100: try {
101: if (dispatcher != null) {
102: dispatcher.error(request, response);
103: return;
104: } else {
105: Log.warn("No error page " + error_page);
106: }
107: } catch (ServletException e) {
108: Log.warn(Log.EXCEPTION, e);
109: return;
110: }
111: }
112: }
113: }
114:
115: super .handle(target, request, response, dispatch);
116: }
117:
118: /* ------------------------------------------------------------ */
119: /**
120: * @return Returns the errorPages.
121: */
122: public Map getErrorPages() {
123: return _errorPages;
124: }
125:
126: /* ------------------------------------------------------------ */
127: /**
128: * @param errorPages The errorPages to set. A map of Exception class name or error code as a string to URI string
129: */
130: public void setErrorPages(Map errorPages) {
131: _errorPages = errorPages;
132: }
133:
134: /* ------------------------------------------------------------ */
135: /**
136: */
137: public void addErrorPage(Class exception, String uri) {
138: if (_errorPages == null)
139: _errorPages = new HashMap();
140: _errorPages.put(exception.getName(), uri);
141: }
142:
143: /* ------------------------------------------------------------ */
144: /**
145: */
146: public void addErrorPage(int code, String uri) {
147: if (_errorPages == null)
148: _errorPages = new HashMap();
149: _errorPages.put(HttpStatus.CACHE.get(code).toString(), uri);
150: }
151:
152: /* ------------------------------------------------------------ */
153: protected void doStart() throws Exception {
154: super .doStart();
155: _servletContext = ContextHandler.getCurrentContext();
156: }
157:
158: /* ------------------------------------------------------------ */
159: protected void doStop() throws Exception {
160: // TODO Auto-generated method stub
161: super.doStop();
162: }
163:
164: }
|