Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.valves;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.io.Writer;
 
 

Implementation of a Valve that outputs HTML error pages.

This Valve should be attached at the Host level, although it will work if attached to a Context.

HTML code from the Cocoon 2 project.

Author(s):
Remy Maucherat
Craig R. McClanahan
Nicola Ken Barozzi Aisa
Stefano Mazzocchi
Yoav Shapira
Version:
$Revision: 1458 $ $Date: 2010-05-01 01:47:43 +0200 (Sat, 01 May 2010) $
 
 
 public class ErrorReportValve
     extends ValveBase {
 
 
     // ----------------------------------------------------- Instance Variables
 

    
The descriptive information related to this implementation.
 
     private static final String info =
         "org.apache.catalina.valves.ErrorReportValve/1.0";
 
 
     // ------------------------------------------------------------- Properties
 

    
Return descriptive information about this Valve implementation.
 
     public String getInfo() {
 
         return ();
 
     }
 
 
     // --------------------------------------------------------- Public Methods
 

    
Invoke the next Valve in the sequence. When the invoke returns, check the response state, and output an error report is necessary.

Parameters:
request The servlet request to be processed
response The servlet response to be created
Throws:
java.io.IOException if an input/output error occurs
javax.servlet.ServletException if a servlet error occurs
 
     public void invoke(Request requestResponse response)
         throws IOExceptionServletException {
 
         // Perform the request
         getNext().invoke(requestresponse);
 
         Throwable throwable =
            (Throwablerequest.getAttribute(.);
        if (response.isCommitted()) {
            return;
        }
        if (throwable != null) {
            // The response is an error
            response.setError();
            // Reset the response (if possible)
            try {
                response.reset();
            } catch (IllegalStateException e) {
                ;
            }
            response.sendError
                (.);
        }
        response.setSuspended(false);
        try {
            report(requestresponsethrowable);
        } catch (Throwable tt) {
            ;
        }
    }
    // ------------------------------------------------------ Protected Methods


    
Prints out an error report.

Parameters:
request The request being processed
response The response being generated
throwable The exception that occurred (which possibly wraps a root cause exception
    protected void report(Request requestResponse response,
                          Throwable throwable) {
        // Do nothing on non-HTTP responses
        int statusCode = response.getStatus();
        // Do nothing on a 1xx, 2xx and 3xx status
        // Do nothing if anything has been written already
        if ((statusCode < 400) || (response.getContentCount() > 0))
            return;
        String message = RequestUtil.filter(response.getMessage());
        if (message == null) {
            if (throwable != null) {
                String exceptionMessage = throwable.getMessage();
                if (exceptionMessage != null && exceptionMessage.length() > 0) {
                    message = RequestUtil.filter((new Scanner(exceptionMessage)).nextLine());
                }
            }
            if (message == null) {
                message = "";
            }
        }
        // Do nothing if there is no report for the specified status code
        String report = null;
        switch (statusCode) {
        case 404: report = .http404(); break;
        case 500: report = .http500(); break;
        case 400: report = .http400(); break;
        case 403: report = .http403(); break;
        case 401: report = .http401(); break;
        case 402: report = .http402(); break;
        case 405: report = .http405(); break;
        case 406: report = .http406(); break;
        case 407: report = .http407(); break;
        case 408: report = .http408(); break;
        case 409: report = .http409(); break;
        case 410: report = .http410(); break;
        case 411: report = .http411(); break;
        case 412: report = .http412(); break;
        case 413: report = .http413(); break;
        case 414: report = .http414(); break;
        case 415: report = .http415(); break;
        case 416: report = .http416(); break;
        case 417: report = .http417(); break;
        case 422: report = .http422(); break;
        case 423: report = .http423(); break;
        case 424: report = .http424(); break;
        case 426: report = .http426(); break;
        case 428: report = .http428(); break;
        case 429: report = .http429(); break;
        case 431: report = .http431(); break;
        case 501: report = .http501(); break;
        case 502: report = .http502(); break;
        case 503: report = .http503(); break;
        case 504: report = .http504(); break;
        case 505: report = .http505(); break;
        case 506: report = .http506(); break;
        case 507: report = .http507(); break;
        case 508: report = .http508(); break;
        case 510: report = .http510(); break;
        case 511: report = .http511(); break;
        default:
            return;
        }
        if (report == null)
            return;
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head><title>");
        sb.append(ServerInfo.getServerInfo()).append(" - ");
        sb.append(.errorReport());
        sb.append("</title>");
        sb.append("<style><!--");
        sb.append("--></style> ");
        sb.append("</head><body>");
        sb.append("<h1>");
        sb.append(.statusHeader(statusCodemessage)).append("</h1>");
        sb.append("<HR size=\"1\" noshade=\"noshade\">");
        sb.append("<p><b>");
        sb.append(.statusType());
        sb.append("</b> ");
        if (throwable != null) {
            sb.append(.exceptionReport());
        } else {
            sb.append(.statusReport());
        }
        sb.append("</p>");
        sb.append("<p><b>");
        sb.append(.statusMessage());
        sb.append("</b> <u>");
        sb.append(message).append("</u></p>");
        sb.append("<p><b>");
        sb.append(.statusDescritpion());
        sb.append("</b> <u>");
        sb.append(report);
        sb.append("</u></p>");
        if (throwable != null) {
            String stackTrace = getPartialServletStackTrace(throwable);
            sb.append("<p><b>");
            sb.append(.statusException());
            sb.append("</b> <pre>");
            sb.append(RequestUtil.filter(stackTrace));
            sb.append("</pre></p>");
            int loops = 0;
            Throwable rootCause = throwable.getCause();
            while (rootCause != null && (loops < 10)) {
                stackTrace = getPartialServletStackTrace(rootCause);
                sb.append("<p><b>");
                sb.append(.statusRootCause());
                sb.append("</b> <pre>");
                sb.append(RequestUtil.filter(stackTrace));
                sb.append("</pre></p>");
                // In case root cause is somehow heavily nested
                rootCause = rootCause.getCause();
                loops++;
            }
            sb.append("<p><b>");
            sb.append(.statusNote());
            sb.append("</b> <u>");
            sb.append(.statusRootCauseInLogs(ServerInfo.getServerInfo()));
            sb.append("</u></p>");
        }
        sb.append("<HR size=\"1\" noshade=\"noshade\">");
        sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>");
        sb.append("</body></html>");
        try {
            try {
                response.setContentType("text/html");
                response.setCharacterEncoding("utf-8");
            } catch (Throwable t) {
                if (.getLogger().isDebugEnabled())
                    .getLogger().debug("status.setContentType"t);
            }
            Writer writer = response.getReporter();
            if (writer != null) {
                // If writer is null, it's an indication that the response has
                // been hard committed already, which should never happen
                writer.write(sb.toString());
            }
        } catch (IOException e) {
            ;
        } catch (IllegalStateException e) {
            ;
        }
        
    }


    
Print out a partial servlet stack trace (truncating at the last occurrence of javax.servlet.).
        StringBuilder trace = new StringBuilder();
        trace.append(t.toString()).append('\n');
        StackTraceElement[] elements = t.getStackTrace();
        int pos = elements.length;
        for (int i = 0; i < elements.lengthi++) {
            if ((elements[i].getClassName().startsWith
                 ("org.apache.catalina.core.ApplicationFilterChain"))
                && (elements[i].getMethodName().equals("internalDoFilter"))) {
                pos = i;
            }
        }
        for (int i = 0; i < posi++) {
            if (!(elements[i].getClassName().startsWith
                  ("org.apache.catalina.core."))) {
                trace.append('\t').append(elements[i].toString()).append('\n');
            }
        }
        return trace.toString();
    }
New to GrepCode? Check out our FAQ X