Source Code Cross Referenced for ErrorDispatcherValve.java in  » Web-Server » Rimfaxe-Web-Server » org » apache » catalina » valves » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Rimfaxe Web Server » org.apache.catalina.valves 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/valves/ErrorDispatcherValve.java,v 1.8 2001/12/02 22:19:26 remm Exp $
003:         * $Revision: 1.8 $
004:         * $Date: 2001/12/02 22:19:26 $
005:         *
006:         * ====================================================================
007:         *
008:         * The Apache Software License, Version 1.1
009:         *
010:         * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
011:         * reserved.
012:         *
013:         * Redistribution and use in source and binary forms, with or without
014:         * modification, are permitted provided that the following conditions
015:         * are met:
016:         *
017:         * 1. Redistributions of source code must retain the above copyright
018:         *    notice, this list of conditions and the following disclaimer.
019:         *
020:         * 2. Redistributions in binary form must reproduce the above copyright
021:         *    notice, this list of conditions and the following disclaimer in
022:         *    the documentation and/or other materials provided with the
023:         *    distribution.
024:         *
025:         * 3. The end-user documentation included with the redistribution, if
026:         *    any, must include the following acknowlegement:
027:         *       "This product includes software developed by the
028:         *        Apache Software Foundation (http://www.apache.org/)."
029:         *    Alternately, this acknowlegement may appear in the software itself,
030:         *    if and wherever such third-party acknowlegements normally appear.
031:         *
032:         * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033:         *    Foundation" must not be used to endorse or promote products derived
034:         *    from this software without prior written permission. For written
035:         *    permission, please contact apache@apache.org.
036:         *
037:         * 5. Products derived from this software may not be called "Apache"
038:         *    nor may "Apache" appear in their names without prior written
039:         *    permission of the Apache Group.
040:         *
041:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052:         * SUCH DAMAGE.
053:         * ====================================================================
054:         *
055:         * This software consists of voluntary contributions made by many
056:         * individuals on behalf of the Apache Software Foundation.  For more
057:         * information on the Apache Software Foundation, please see
058:         * <http://www.apache.org/>.
059:         *
060:         * [Additional notices, if required by prior licensing conditions]
061:         *
062:         */
063:
064:        package org.apache.catalina.valves;
065:
066:        import java.io.IOException;
067:        import java.util.ArrayList;
068:        import java.util.Enumeration;
069:        import java.util.Iterator;
070:        import javax.servlet.RequestDispatcher;
071:        import javax.servlet.ServletContext;
072:        import javax.servlet.ServletException;
073:        import javax.servlet.ServletRequest;
074:        import javax.servlet.ServletResponse;
075:        import javax.servlet.http.Cookie;
076:        import javax.servlet.http.HttpServletRequest;
077:        import javax.servlet.http.HttpServletResponse;
078:        import org.apache.catalina.Container;
079:        import org.apache.catalina.Context;
080:        import org.apache.catalina.Globals;
081:        import org.apache.catalina.HttpRequest;
082:        import org.apache.catalina.HttpResponse;
083:        import org.apache.catalina.Logger;
084:        import org.apache.catalina.Request;
085:        import org.apache.catalina.Response;
086:        import org.apache.catalina.Valve;
087:        import org.apache.catalina.ValveContext;
088:        import org.apache.catalina.Wrapper;
089:        import org.apache.catalina.deploy.ErrorPage;
090:        import org.apache.catalina.util.RequestUtil;
091:        import org.apache.catalina.util.StringManager;
092:
093:        /**
094:         * <p>Implementation of a Valve that handles the error dispatch (that is, will
095:         * forward to the appropriate error page if necessary).</p>
096:         *
097:         * <p>This Valve should be attached at the Host level, although it will work
098:         * if attached to a Context.</p>
099:         *
100:         * <p><b>WARNING</b>: This valve is necessary for Servlet API compliance.</p>
101:         *
102:         * @author Remy Maucherat
103:         * @author Craig R. McClanahan
104:         * @version $Revision: 1.8 $ $Date: 2001/12/02 22:19:26 $
105:         */
106:
107:        public class ErrorDispatcherValve extends ValveBase {
108:
109:            // ----------------------------------------------------- Instance Variables
110:
111:            /**
112:             * The debugging detail level for this component.
113:             */
114:            protected int debug = 0;
115:
116:            /**
117:             * The descriptive information related to this implementation.
118:             */
119:            protected static final String info = "org.apache.catalina.valves.ErrorDispatcherValve/1.0";
120:
121:            /**
122:             * The StringManager for this package.
123:             */
124:            protected static StringManager sm = StringManager
125:                    .getManager(Constants.Package);
126:
127:            // ------------------------------------------------------------- Properties
128:
129:            /**
130:             * Return descriptive information about this Valve implementation.
131:             */
132:            public String getInfo() {
133:
134:                return (info);
135:
136:            }
137:
138:            // --------------------------------------------------------- Public Methods
139:
140:            /**
141:             * Invoke the next Valve in the sequence. When the invoke returns, check 
142:             * the response state, and output an error report is necessary.
143:             *
144:             * @param request The servlet request to be processed
145:             * @param response The servlet response to be created
146:             * @param context The valve context used to invoke the next valve
147:             *  in the current processing pipeline
148:             *
149:             * @exception IOException if an input/output error occurs
150:             * @exception ServletException if a servlet error occurs
151:             */
152:            public void invoke(Request request, Response response,
153:                    ValveContext context) throws IOException, ServletException {
154:
155:                // Perform the request
156:                context.invokeNext(request, response);
157:
158:                response.setSuspended(false);
159:
160:                ServletRequest sreq = request.getRequest();
161:                Throwable t = (Throwable) sreq
162:                        .getAttribute(Globals.EXCEPTION_ATTR);
163:
164:                if (t != null) {
165:                    throwable(request, response, t);
166:                } else {
167:                    status(request, response);
168:                }
169:
170:            }
171:
172:            /**
173:             * Return a String rendering of this object.
174:             */
175:            public String toString() {
176:
177:                StringBuffer sb = new StringBuffer("ErrorDispatcherValve[");
178:                sb.append(container.getName());
179:                sb.append("]");
180:                return (sb.toString());
181:
182:            }
183:
184:            // ------------------------------------------------------ Protected Methods
185:
186:            /**
187:             * Handle the specified Throwable encountered while processing
188:             * the specified Request to produce the specified Response.  Any
189:             * exceptions that occur during generation of the exception report are
190:             * logged and swallowed.
191:             *
192:             * @param request The request being processed
193:             * @param response The response being generated
194:             * @param exception The exception that occurred (which possibly wraps
195:             *  a root cause exception
196:             */
197:            protected void throwable(Request request, Response response,
198:                    Throwable throwable) {
199:
200:                Context context = request.getContext();
201:                if (context == null)
202:                    return;
203:
204:                Throwable realError = throwable;
205:                ErrorPage errorPage = findErrorPage(context, realError);
206:                if ((errorPage == null)
207:                        && (realError instanceof  ServletException)) {
208:                    realError = ((ServletException) realError).getRootCause();
209:                    if (realError != null)
210:                        errorPage = findErrorPage(context, realError);
211:                    else
212:                        realError = throwable;
213:                }
214:
215:                if (errorPage != null) {
216:                    response.setAppCommitted(false);
217:                    ServletRequest sreq = request.getRequest();
218:                    ServletResponse sresp = response.getResponse();
219:                    sreq.setAttribute(Globals.STATUS_CODE_ATTR, new Integer(
220:                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
221:                    sreq.setAttribute(Globals.ERROR_MESSAGE_ATTR, throwable
222:                            .getMessage());
223:                    sreq.setAttribute(Globals.EXCEPTION_ATTR, throwable);
224:                    Wrapper wrapper = request.getWrapper();
225:                    if (wrapper != null)
226:                        sreq.setAttribute(Globals.SERVLET_NAME_ATTR, wrapper
227:                                .getName());
228:                    if (sreq instanceof  HttpServletRequest)
229:                        sreq.setAttribute(Globals.EXCEPTION_PAGE_ATTR,
230:                                ((HttpServletRequest) sreq).getRequestURI());
231:                    sreq.setAttribute(Globals.EXCEPTION_TYPE_ATTR, throwable
232:                            .getClass());
233:                    if (custom(request, response, errorPage)) {
234:                        try {
235:                            sresp.flushBuffer();
236:                        } catch (IOException e) {
237:                            log("Exception Processing " + errorPage, e);
238:                        }
239:                    }
240:                }
241:
242:            }
243:
244:            /**
245:             * Handle the HTTP status code (and corresponding message) generated
246:             * while processing the specified Request to produce the specified
247:             * Response.  Any exceptions that occur during generation of the error
248:             * report are logged and swallowed.
249:             *
250:             * @param request The request being processed
251:             * @param response The response being generated
252:             */
253:            protected void status(Request request, Response response) {
254:
255:                // Do nothing on non-HTTP responses
256:                if (!(response instanceof  HttpResponse))
257:                    return;
258:                HttpResponse hresponse = (HttpResponse) response;
259:                if (!(response.getResponse() instanceof  HttpServletResponse))
260:                    return;
261:                int statusCode = hresponse.getStatus();
262:                String message = RequestUtil.filter(hresponse.getMessage());
263:                if (message == null)
264:                    message = "";
265:
266:                // Handle a custom error page for this status code
267:                Context context = request.getContext();
268:                if (context == null)
269:                    return;
270:
271:                ErrorPage errorPage = context.findErrorPage(statusCode);
272:                if (errorPage != null) {
273:                    response.setAppCommitted(false);
274:                    ServletRequest sreq = request.getRequest();
275:                    ServletResponse sresp = response.getResponse();
276:                    sreq.setAttribute(Globals.STATUS_CODE_ATTR, new Integer(
277:                            statusCode));
278:                    sreq.setAttribute(Globals.ERROR_MESSAGE_ATTR, message);
279:                    Wrapper wrapper = request.getWrapper();
280:                    if (wrapper != null)
281:                        sreq.setAttribute(Globals.SERVLET_NAME_ATTR, wrapper
282:                                .getName());
283:                    if (sreq instanceof  HttpServletRequest)
284:                        sreq.setAttribute(Globals.EXCEPTION_PAGE_ATTR,
285:                                ((HttpServletRequest) sreq).getRequestURI());
286:                    if (custom(request, response, errorPage)) {
287:                        try {
288:                            sresp.flushBuffer();
289:                        } catch (IOException e) {
290:                            log("Exception Processing " + errorPage, e);
291:                        }
292:                    }
293:                }
294:
295:            }
296:
297:            /**
298:             * Find and return the ErrorPage instance for the specified exception's
299:             * class, or an ErrorPage instance for the closest superclass for which
300:             * there is such a definition.  If no associated ErrorPage instance is
301:             * found, return <code>null</code>.
302:             *
303:             * @param context The Context in which to search
304:             * @param exception The exception for which to find an ErrorPage
305:             */
306:            protected static ErrorPage findErrorPage(Context context,
307:                    Throwable exception) {
308:
309:                if (exception == null)
310:                    return (null);
311:                Class clazz = exception.getClass();
312:                String name = clazz.getName();
313:                while (!"java.lang.Object".equals(clazz)) {
314:                    ErrorPage errorPage = context.findErrorPage(name);
315:                    if (errorPage != null)
316:                        return (errorPage);
317:                    clazz = clazz.getSuperclass();
318:                    if (clazz == null)
319:                        break;
320:                    name = clazz.getName();
321:                }
322:                return (null);
323:
324:            }
325:
326:            /**
327:             * Handle an HTTP status code or Java exception by forwarding control
328:             * to the location included in the specified errorPage object.  It is
329:             * assumed that the caller has already recorded any request attributes
330:             * that are to be forwarded to this page.  Return <code>true</code> if
331:             * we successfully utilized the specified error page location, or
332:             * <code>false</code> if the default error report should be rendered.
333:             *
334:             * @param request The request being processed
335:             * @param response The response being generated
336:             * @param errorPage The errorPage directive we are obeying
337:             */
338:            protected boolean custom(Request request, Response response,
339:                    ErrorPage errorPage) {
340:
341:                if (debug >= 1)
342:                    log("Processing " + errorPage);
343:
344:                // Validate our current environment
345:                if (!(request instanceof  HttpRequest)) {
346:                    if (debug >= 1)
347:                        log(" Not processing an HTTP request --> default handling");
348:                    return (false); // NOTE - Nothing we can do generically
349:                }
350:                HttpServletRequest hreq = (HttpServletRequest) request
351:                        .getRequest();
352:                if (!(response instanceof  HttpResponse)) {
353:                    if (debug >= 1)
354:                        log("Not processing an HTTP response --> default handling");
355:                    return (false); // NOTE - Nothing we can do generically
356:                }
357:                HttpServletResponse hres = (HttpServletResponse) response
358:                        .getResponse();
359:
360:                try {
361:
362:                    // Reset the response if possible (else IllegalStateException)
363:                    hres.reset();
364:
365:                    // Forward control to the specified location
366:                    ServletContext servletContext = request.getContext()
367:                            .getServletContext();
368:                    RequestDispatcher rd = servletContext
369:                            .getRequestDispatcher(errorPage.getLocation());
370:                    rd.forward(hreq, hres);
371:
372:                    // If we forward, the response is suspended again
373:                    response.setSuspended(false);
374:
375:                    // Indicate that we have successfully processed this custom page
376:                    return (true);
377:
378:                } catch (Throwable t) {
379:
380:                    // Report our failure to process this custom page
381:                    log("Exception Processing " + errorPage, t);
382:                    return (false);
383:
384:                }
385:
386:            }
387:
388:            /**
389:             * Log a message on the Logger associated with our Container (if any).
390:             *
391:             * @param message Message to be logged
392:             */
393:            protected void log(String message) {
394:
395:                Logger logger = container.getLogger();
396:                if (logger != null)
397:                    logger.log(this .toString() + ": " + message);
398:                else
399:                    System.out.println(this .toString() + ": " + message);
400:
401:            }
402:
403:            /**
404:             * Log a message on the Logger associated with our Container (if any).
405:             *
406:             * @param message Message to be logged
407:             * @param throwable Associated exception
408:             */
409:            protected void log(String message, Throwable throwable) {
410:
411:                Logger logger = container.getLogger();
412:                if (logger != null)
413:                    logger.log(this .toString() + ": " + message, throwable);
414:                else {
415:                    System.out.println(this .toString() + ": " + message);
416:                    throwable.printStackTrace(System.out);
417:                }
418:
419:            }
420:
421:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.