alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (ErrorReportValve.java)

This example Glassfish source code file (ErrorReportValve.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

http, httpresponse, httpservletresponse, httpservletresponse, illegalstateexception, io, ioexception, request, response, response, servlet, servletexception, servletexception, servletrequest, string, string, stringbuilder, throwable, throwable, util

The Glassfish ErrorReportValve.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 *
 *
 * This file incorporates work covered by the following copyright and
 * permission notice:
 *
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed 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 org.apache.catalina.HttpResponse;
import org.apache.catalina.Logger;
import org.apache.catalina.Request;
import org.apache.catalina.Response;
import org.apache.catalina.util.RequestUtil;
import org.apache.catalina.util.ServerInfo;
import org.apache.catalina.util.StringManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Locale;

/**
 * <p>Implementation of a Valve that outputs HTML error pages.

* * <p>This Valve should be attached at the Host level, although it will work * if attached to a Context.</p> * * <p>HTML code from the Cocoon 2 project.

* * @author Remy Maucherat * @author Craig R. McClanahan * @author <a href="mailto:nicolaken@supereva.it">Nicola Ken Barozzi Aisa * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi * @version $Revision: 1.19 $ $Date: 2007/05/05 05:32:41 $ */ public class ErrorReportValve extends ValveBase { private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger( ErrorReportValve.class.getName()); /** * The descriptive information related to this implementation. */ private static final String info = "org.apache.catalina.valves.ErrorReportValve/1.0"; /** * The StringManager for this package. */ protected static final StringManager sm = StringManager.getManager(Constants.Package); // ----------------------------------------------------- Instance Variables /** * The debugging detail level for this component. */ private int debug = 0; // ------------------------------------------------------------- Properties /** * Return descriptive information about this Valve implementation. */ public String getInfo() { return (info); } // --------------------------------------------------------- Public Methods /** * Invoke the next Valve in the sequence. When the invoke returns, check * the response state, and output an error report is necessary. * * @param request The servlet request to be processed * @param response The servlet response to be created * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ public int invoke(Request request, Response response) throws IOException, ServletException { // Perform the request return INVOKE_NEXT; } public void postInvoke(Request request, Response response) throws IOException, ServletException { ServletRequest sreq = (ServletRequest) request; Throwable throwable = (Throwable) sreq.getAttribute(RequestDispatcher.ERROR_EXCEPTION); ServletResponse sresp = (ServletResponse) response; if (sresp.isCommitted()) { return; } if (throwable != null) { // The response is an error response.setError(); // START PWC 6254469 // Save (and later restore) the response encoding, because the // following call to reset() will reset it to the default // encoding (ISO-8859-1). String responseCharEnc = sresp.getCharacterEncoding(); // END PWC 6254469 HttpServletResponse sresponse = (HttpServletResponse) response; // START IT 13858 Collection<String> cookieHeaders = sresponse.getHeaders("Set-Cookie"); // END IT 13858 // Reset the response (if possible) try { sresp.reset(); } catch (IllegalStateException e) { ; } // START PWC 6254469 /* * Restore the previously saved response encoding only if it is * different from the default (ISO-8859-1). This is important so * that a subsequent call to ServletResponse.setLocale() has an * opportunity to set it so it corresponds to the resource bundle * locale (see 6412710) */ if (responseCharEnc != null && !responseCharEnc.equals( com.sun.grizzly.tcp.Constants.DEFAULT_CHARACTER_ENCODING)) { sresp.setCharacterEncoding(responseCharEnc); } // END PWC 6254469 // START IT 13858 for (String cookieHeader : cookieHeaders) { sresponse.addHeader("Set-Cookie", cookieHeader); } // END IT 13858 /* GlassFish 6386229 if (sresponse instanceof HttpServletResponse) ((HttpServletResponse) sresponse).sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR); */ // START GlassFish 6386229 sresponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // END GlassFish 6386229 } response.setSuspended(false); try { report(request, response, throwable); } catch (Throwable tt) { ; } } /** * Return a String rendering of this object. */ public String toString() { StringBuilder sb = new StringBuilder("ErrorReportValve["); sb.append(container.getName()); sb.append("]"); return (sb.toString()); } // ------------------------------------------------------ Protected Methods /** * Prints out an error report. * * @param request The request being processed * @param response The response being generated * @param throwable The exception that occurred (which possibly wraps * a root cause exception */ protected void report(Request request, Response response, Throwable throwable) throws IOException { /* GlassFish 6386229 // Do nothing on non-HTTP responses if (!(response instanceof HttpResponse)) return; */ HttpResponse hresponse = (HttpResponse) response; /* GlassFish 6386229 if (!(response instanceof HttpServletResponse)) return; */ HttpServletResponse hres = (HttpServletResponse) response; int statusCode = hresponse.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; Throwable rootCause = null; if (throwable != null) { if (throwable instanceof ServletException) rootCause = ((ServletException) throwable).getRootCause(); } String message = RequestUtil.filter(hresponse.getMessage()); /* S1AS 4878272 if (message == null) message = ""; */ // BEGIN S1AS 4878272 if (message == null) { message = RequestUtil.filter(hresponse.getDetailMessage()); if (message == null) { message = ""; } } // END S1AS 4878272 // Do nothing if there is no report for the specified status code String report = null; try { /* SJSAS 6412710 report = sm.getString("http." + statusCode, message); */ // START SJSAS 6412710 report = sm.getString("http." + statusCode, message, hres.getLocale()); // END SJSAS 6412710 } catch (Throwable t) { ; } if (report == null) return; String errorPage = makeErrorPage(statusCode, message, throwable, rootCause, report, hres); // START SJSAS 6412710 /* * If throwable is not null, we've already preserved any non-default * response encoding in postInvoke(), so that the throwable's exception * message can be delivered to the client without any loss of * information. The following call to ServletResponse.setLocale() * will not override the response encoding in this case. * For all other cases, the response encoding will be set according to * the resource bundle locale. */ hres.setLocale(sm.getResourceBundleLocale(hres.getLocale())); // END SJSAS 6412710 /* PWC 6254469 // set the charset part of content type before getting the writer */ try { hres.setContentType("text/html"); /* PWC 6254469 hres.setCharacterEncoding("UTF-8"); */ } catch (Throwable t) { if (debug >= 1) log("status.setContentType", t); } try { 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(errorPage); } } catch (IOException e) { ; } catch (IllegalStateException e) { ; } } /** * Log a message on the Logger associated with our Container (if any). * * @param message Message to be logged */ protected void log(String message) { Logger logger = container.getLogger(); if (logger != null) { logger.log(this.toString() + ": " + message); } else { log.info(this.toString() + ": " + message); } } /** * Log a message on the Logger associated with our Container (if any). * * @param message Message to be logged * @param t Associated exception */ protected void log(String message, Throwable t) { Logger logger = container.getLogger(); if (logger != null) { logger.log(this.toString() + ": " + message, t, Logger.WARNING); } else { log.log(java.util.logging.Level.WARNING, this.toString() + ": " + message, t); } } public static String makeErrorPage(int statusCode, String message, Throwable throwable, Throwable rootCause, String report, HttpServletResponse response) { // START SJSAS 6412710 Locale responseLocale = response.getLocale(); // END SJSAS 6412710 String serverInfo = ServerInfo.getPublicServerInfo(); StringBuilder sb = new StringBuilder(); sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""); sb.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); sb.append("<html>"); if (serverInfo != null && !serverInfo.isEmpty()) { sb.append(serverInfo).append(" - "); } /* 6412710 sb.append(sm.getString("errorReportValve.errorReport")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.errorReport", responseLocale)); // END SJSAS 6412710 sb.append("</title>"); sb.append("<style type=\"text/css\"><!--"); sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS); sb.append("--></style> "); sb.append("</head><body>"); sb.append("<h1>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.statusHeader", "" + statusCode, message)).append("</h1>"); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.statusHeader", "" + statusCode, message, responseLocale)).append("</h1>"); // END SJSAS 6412710 sb.append("<hr/>"); sb.append("<p><b>type</b> "); if (throwable != null) { /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.exceptionReport")); */ // START SJJAS 6412710 sb.append(sm.getString("errorReportValve.exceptionReport", responseLocale)); // END SJSAS 6412710 } else { /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.statusReport")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.statusReport", responseLocale)); // END SJSAS 6412710 } sb.append("</p>"); sb.append("<p><b>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.message")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.message", responseLocale)); // END SJSAS 6412710 sb.append("</b>"); sb.append(message).append("</p>"); sb.append("<p><b>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.description")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.description", responseLocale)); // END SJSAS 6412710 sb.append("</b>"); sb.append(report); sb.append("</p>"); // exception class name or stacktrace can reveal the underlying product // name, so do not include it if product name property has been cleared. if (throwable != null && serverInfo != null && !serverInfo.isEmpty()) { /* GlassFish 823 String stackTrace = JdkCompat.getJdkCompat() .getPartialServletStackTrace(throwable); */ sb.append("<p><b>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.exception")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.exception", responseLocale)); // END SJSAS 6412710 sb.append("</b> <pre>"); /* SJSAS 6387790 sb.append(stackTrace); */ /* GlassFish 823 // START SJSAS 6387790 sb.append(RequestUtil.filter(stackTrace)); // END SJSAS 6387790 */ // START GlassFish 823 sb.append(RequestUtil.filter(String.valueOf(throwable))); // END GlassFish 823 sb.append("</pre></p>"); while (rootCause != null) { /* GlassFish 823 stackTrace = JdkCompat.getJdkCompat() .getPartialServletStackTrace(rootCause); */ sb.append("<p><b>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.rootCause")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.rootCause", responseLocale)); // END SJSAS 6412710 sb.append("</b> <pre>"); /* SJSAS 6387790 sb.append(stackTrace); */ /* GlassFish 823 // START SJSAS 6387790 sb.append(RequestUtil.filter(stackTrace)); // END SJSAS 6387790 */ // START GlassFish 823 sb.append(RequestUtil.filter(String.valueOf(rootCause))); // END GlassFish 823 sb.append("</pre></p>"); /* GlassFish 823 // In case root cause is somehow heavily nested try { rootCause = (Throwable)PropertyUtils.getProperty (rootCause, "rootCause"); } catch (ClassCastException e) { rootCause = null; } catch (IllegalAccessException e) { rootCause = null; } catch (NoSuchMethodException e) { rootCause = null; } catch (java.lang.reflect.InvocationTargetException e) { rootCause = null; } */ // START GlassFish 823 rootCause = rootCause.getCause(); // END GlassFish 823 } sb.append("<p><b>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.note")); */ // START SJSAS 6412710 sb.append(sm.getString("errorReportValve.note", responseLocale)); // END SJAS 6412710 sb.append("</b> <u>"); /* SJSAS 6412710 sb.append(sm.getString("errorReportValve.rootCauseInLogs", ServerInfo.getServerInfo())); */ // START SJSAS 6412710 if (serverInfo != null && !serverInfo.isEmpty()) { sb.append(sm.getString("errorReportValve.rootCauseInLogs", serverInfo, responseLocale)); } else { sb.append(sm.getString("errorReportValve.rootCauseInLogs", "server", responseLocale)); } // END SJSAS 6412710 sb.append("</u></p>"); } sb.append("<hr/>"); if (serverInfo != null && !serverInfo.isEmpty()) { sb.append("<h3>").append(serverInfo).append("</h3>"); } sb.append("</body></html>"); return sb.toString(); } } </pre> <div id="after_source_code"> <h2>Other Glassfish examples (source code examples)</h2> <p>Here is a short list of links related to this Glassfish ErrorReportValve.java source code file:</p> <ul> <li><a href="/java/jwarehouse"><img src="/images/scw/find.png" border="0"> The search page</a></li> <li><a href="index.shtml"><img src="/images/scw/folder.png" border="0"> Other Glassfish source code examples at this package level</a></li> <li><a href="/java/jwarehouse/about.shtml"><img src="/images/scw/information.png" border="0"> Click here to learn more about this project</a></li> </ul> </div> </td> </tr> </table> </div> </div> <div style="padding-top: 1em; width: 310px; margin-left: auto; margin-right: auto; table {border-collapse: collapse; border: none;}; tr {border-collapse: collapse; border: none; text-align: center;};"> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td colspan="2" style="border-collapse: collapse; border: none; text-align: center;};"> <em>... this post is sponsored by my books ...</em> </td> </tr> <tr> <td width="150" style="border-collapse: collapse; border: none; text-align: center;};"> <a href="https://kbhr.co/ckbk-v2"><img src="/images/books/scala-cookbook-v2-cover-220h.jpg" title="The Scala Cookbook, by Alvin Alexander" height="220" /> <br /><span style="opacity: 0.4;">#1 New Release!</span></a> </td> <td width="150" style="border-collapse: collapse; border: none; text-align: center; padding-left: 8px;"> <a href="http://kbhr.co/fps-book"><img src="/images/books/functional-programming-simplified-small.jpg" title="Functional Programming, Simplified, by Alvin Alexander" height="220" /> <br /><span style="opacity: 0.4;">FP Best Seller</span></a> </td> </tr> </table> <p> </p> </div> <div id="whats_new"> <h2>new blog posts</h2> <div id="whats_new_list"> <ul> <li><a class="whats_new_link" href="/photos/als-oasis">Al's Oasis</a></li> <li><a class="whats_new_link" href="/photos/window-poet">Window of the Poet (painting)</a></li> <li><a class="whats_new_link" href="/personal/ezoic-ads-vs-google-adsense-2024">Ezoic ads vs Google AdSense (2024, website advertising revenue/partner)</a></li> <br/> <li><a class="whats_new_link" href="/misc/dream-vacation-for-meditator-meditation">A dream vacation for the meditator in your life</a></li> <li><a class="whats_new_link" href="/scala/zio-http-netty-AnnotatedNoRouteToHostException-null-solution">ZIO HTTP: Netty AnnotatedNoRouteToHostException null solution</a></li> <li><a class="whats_new_link" href="/misc/inside-shinzens-brain-how-zen-master-experiences-daily-life-world-enlightened">Inside Shinzen'''s Brain: How A Zen Master Experiences his Daily Life</a></li> <br/> <li><a class="whats_new_link" href="/misc/shinzen-young-meditating-daily-life-arising-disappearing-the-source-love">Shinzen Young on meditating in his daily life: arising, disappearing, and The Source, and love</a></li> <li><a class="whats_new_link" href="/scala/zio-zlayer-very-simple-example-dependency-injection-services">ZIO ZLayer: A simple '''Hello, world''' example (dependency injection, services)</a></li> <li><a class="whats_new_link" href="/misc/trying-find-way-love-everyone-in-world-dont-like-dark-night-soul">Trying to find a way to love everyone in a world ...</a></li> <br/> <br/> </div> </ul> </div> <p> </p> <p align="center"><font color="#000000" size="2" face="Verdana,Arial">Copyright 1998-2021 Alvin Alexander, alvinalexander.com<br/> All Rights Reserved.<br/> <br/> A percentage of advertising revenue from<br/> pages under the <a href="/java/jwarehouse">/java/jwarehouse</a> URI on this website is<br/> paid back to open source projects.</p> <script> shuffle(books); var div = document.getElementById("leftcol"); var pre = '<div style="margin: 0; padding-right: 1.6em"><h2 align="center">favorite books</h2>'; var post = '</div>'; if (adblock) { var str = books.slice(0,3).join(" "); div.insertAdjacentHTML('beforeend', pre + str + post); } else { var str = books.slice(0,1).join(" "); div.insertAdjacentHTML('beforeend', pre + str + post); } </script> <p style="padding-bottom: 80px;"> </p> </body>