001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.valves;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.catalina.Globals;
027: import org.apache.catalina.connector.Request;
028: import org.apache.catalina.connector.Response;
029: import org.apache.catalina.util.RequestUtil;
030: import org.apache.catalina.util.ServerInfo;
031: import org.apache.catalina.util.StringManager;
032:
033: /**
034: * <p>Implementation of a Valve that outputs HTML error pages.</p>
035: *
036: * <p>This Valve should be attached at the Host level, although it will work
037: * if attached to a Context.</p>
038: *
039: * <p>HTML code from the Cocoon 2 project.</p>
040: *
041: * @author Remy Maucherat
042: * @author Craig R. McClanahan
043: * @author <a href="mailto:nicolaken@supereva.it">Nicola Ken Barozzi</a> Aisa
044: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
045: * @author Yoav Shapira
046: * @version $Revision: 543307 $ $Date: 2007-06-01 01:08:24 +0200 (ven., 01 juin 2007) $
047: */
048:
049: public class ErrorReportValve extends ValveBase {
050:
051: // ----------------------------------------------------- Instance Variables
052:
053: /**
054: * The descriptive information related to this implementation.
055: */
056: private static final String info = "org.apache.catalina.valves.ErrorReportValve/1.0";
057:
058: /**
059: * The StringManager for this package.
060: */
061: protected static StringManager sm = StringManager
062: .getManager(Constants.Package);
063:
064: // ------------------------------------------------------------- Properties
065:
066: /**
067: * Return descriptive information about this Valve implementation.
068: */
069: public String getInfo() {
070:
071: return (info);
072:
073: }
074:
075: // --------------------------------------------------------- Public Methods
076:
077: /**
078: * Invoke the next Valve in the sequence. When the invoke returns, check
079: * the response state, and output an error report is necessary.
080: *
081: * @param request The servlet request to be processed
082: * @param response The servlet response to be created
083: *
084: * @exception IOException if an input/output error occurs
085: * @exception ServletException if a servlet error occurs
086: */
087: public void invoke(Request request, Response response)
088: throws IOException, ServletException {
089:
090: // Perform the request
091: getNext().invoke(request, response);
092:
093: Throwable throwable = (Throwable) request
094: .getAttribute(Globals.EXCEPTION_ATTR);
095:
096: if (response.isCommitted()) {
097: return;
098: }
099:
100: if (throwable != null) {
101:
102: // The response is an error
103: response.setError();
104:
105: // Reset the response (if possible)
106: try {
107: response.reset();
108: } catch (IllegalStateException e) {
109: ;
110: }
111:
112: response
113: .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
114:
115: }
116:
117: response.setSuspended(false);
118:
119: try {
120: report(request, response, throwable);
121: } catch (Throwable tt) {
122: ;
123: }
124:
125: }
126:
127: // ------------------------------------------------------ Protected Methods
128:
129: /**
130: * Prints out an error report.
131: *
132: * @param request The request being processed
133: * @param response The response being generated
134: * @param throwable The exception that occurred (which possibly wraps
135: * a root cause exception
136: */
137: protected void report(Request request, Response response,
138: Throwable throwable) {
139:
140: // Do nothing on non-HTTP responses
141: int statusCode = response.getStatus();
142:
143: // Do nothing on a 1xx, 2xx and 3xx status
144: // Do nothing if anything has been written already
145: if ((statusCode < 400) || (response.getContentCount() > 0))
146: return;
147:
148: String message = RequestUtil.filter(response.getMessage());
149: if (message == null)
150: message = "";
151:
152: // Do nothing if there is no report for the specified status code
153: String report = null;
154: try {
155: report = sm.getString("http." + statusCode, message);
156: } catch (Throwable t) {
157: ;
158: }
159: if (report == null)
160: return;
161:
162: StringBuffer sb = new StringBuffer();
163:
164: sb.append("<html><head><title>");
165: sb.append(ServerInfo.getServerInfo()).append(" - ");
166: sb.append(sm.getString("errorReportValve.errorReport"));
167: sb.append("</title>");
168: sb.append("<style><!--");
169: sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS);
170: sb.append("--></style> ");
171: sb.append("</head><body>");
172: sb.append("<h1>");
173: sb.append(
174: sm.getString("errorReportValve.statusHeader", ""
175: + statusCode, message)).append("</h1>");
176: sb.append("<HR size=\"1\" noshade=\"noshade\">");
177: sb.append("<p><b>type</b> ");
178: if (throwable != null) {
179: sb.append(sm.getString("errorReportValve.exceptionReport"));
180: } else {
181: sb.append(sm.getString("errorReportValve.statusReport"));
182: }
183: sb.append("</p>");
184: sb.append("<p><b>");
185: sb.append(sm.getString("errorReportValve.message"));
186: sb.append("</b> <u>");
187: sb.append(message).append("</u></p>");
188: sb.append("<p><b>");
189: sb.append(sm.getString("errorReportValve.description"));
190: sb.append("</b> <u>");
191: sb.append(report);
192: sb.append("</u></p>");
193:
194: if (throwable != null) {
195:
196: String stackTrace = getPartialServletStackTrace(throwable);
197: sb.append("<p><b>");
198: sb.append(sm.getString("errorReportValve.exception"));
199: sb.append("</b> <pre>");
200: sb.append(RequestUtil.filter(stackTrace));
201: sb.append("</pre></p>");
202:
203: int loops = 0;
204: Throwable rootCause = throwable.getCause();
205: while (rootCause != null && (loops < 10)) {
206: stackTrace = getPartialServletStackTrace(rootCause);
207: sb.append("<p><b>");
208: sb.append(sm.getString("errorReportValve.rootCause"));
209: sb.append("</b> <pre>");
210: sb.append(RequestUtil.filter(stackTrace));
211: sb.append("</pre></p>");
212: // In case root cause is somehow heavily nested
213: rootCause = rootCause.getCause();
214: loops++;
215: }
216:
217: sb.append("<p><b>");
218: sb.append(sm.getString("errorReportValve.note"));
219: sb.append("</b> <u>");
220: sb.append(sm.getString("errorReportValve.rootCauseInLogs",
221: ServerInfo.getServerInfo()));
222: sb.append("</u></p>");
223:
224: }
225:
226: sb.append("<HR size=\"1\" noshade=\"noshade\">");
227: sb.append("<h3>").append(ServerInfo.getServerInfo()).append(
228: "</h3>");
229: sb.append("</body></html>");
230:
231: try {
232: try {
233: response.setContentType("text/html");
234: response.setCharacterEncoding("utf-8");
235: } catch (Throwable t) {
236: if (container.getLogger().isDebugEnabled())
237: container.getLogger().debug(
238: "status.setContentType", t);
239: }
240: Writer writer = response.getReporter();
241: if (writer != null) {
242: // If writer is null, it's an indication that the response has
243: // been hard committed already, which should never happen
244: writer.write(sb.toString());
245: }
246: } catch (IOException e) {
247: ;
248: } catch (IllegalStateException e) {
249: ;
250: }
251:
252: }
253:
254: /**
255: * Print out a partial servlet stack trace (truncating at the last
256: * occurrence of javax.servlet.).
257: */
258: protected String getPartialServletStackTrace(Throwable t) {
259: StringBuffer trace = new StringBuffer();
260: trace.append(t.toString()).append('\n');
261: StackTraceElement[] elements = t.getStackTrace();
262: int pos = elements.length;
263: for (int i = 0; i < elements.length; i++) {
264: if ((elements[i].getClassName()
265: .startsWith("org.apache.catalina.core.ApplicationFilterChain"))
266: && (elements[i].getMethodName()
267: .equals("internalDoFilter"))) {
268: pos = i;
269: }
270: }
271: for (int i = 0; i < pos; i++) {
272: if (!(elements[i].getClassName()
273: .startsWith("org.apache.catalina.core."))) {
274: trace.append('\t').append(elements[i].toString())
275: .append('\n');
276: }
277: }
278: return trace.toString();
279: }
280:
281: }
|