1 17 18 19 package org.apache.catalina.valves; 20 21 22 import java.io.IOException ; 23 import java.io.Writer ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.catalina.Globals; 31 import org.apache.catalina.connector.Request; 32 import org.apache.catalina.connector.Response; 33 import org.apache.catalina.util.RequestUtil; 34 import org.apache.catalina.util.ServerInfo; 35 import org.apache.catalina.util.StringManager; 36 import org.apache.tomcat.util.IntrospectionUtils; 37 38 53 54 public class ErrorReportValve 55 extends ValveBase { 56 57 58 60 61 64 private static final String info = 65 "org.apache.catalina.valves.ErrorReportValve/1.0"; 66 67 68 71 protected static StringManager sm = 72 StringManager.getManager(Constants.Package); 73 74 75 77 78 81 public String getInfo() { 82 83 return (info); 84 85 } 86 87 88 90 91 101 public void invoke(Request request, Response response) 102 throws IOException , ServletException { 103 104 getNext().invoke(request, response); 106 107 ServletRequest sreq = (ServletRequest ) request; 108 Throwable throwable = 109 (Throwable ) sreq.getAttribute(Globals.EXCEPTION_ATTR); 110 111 ServletResponse sresp = (ServletResponse ) response; 112 if (sresp.isCommitted()) { 113 return; 114 } 115 116 if (throwable != null) { 117 118 response.setError(); 120 121 try { 123 sresp.reset(); 124 } catch (IllegalStateException e) { 125 ; 126 } 127 128 response.sendError 129 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 130 131 } 132 133 response.setSuspended(false); 134 135 try { 136 report(request, response, throwable); 137 } catch (Throwable tt) { 138 ; 139 } 140 141 } 142 143 144 146 147 155 protected void report(Request request, Response response, 156 Throwable throwable) { 157 158 int statusCode = response.getStatus(); 160 161 if ((statusCode < 400) || (response.getContentCount() > 0)) 164 return; 165 166 Throwable rootCause = null; 167 168 if (throwable != null) { 169 170 if (throwable instanceof ServletException ) 171 rootCause = ((ServletException ) throwable).getRootCause(); 172 173 } 174 175 String message = RequestUtil.filter(response.getMessage()); 176 if (message == null) 177 message = ""; 178 179 String report = null; 181 try { 182 report = sm.getString("http." + statusCode, message); 183 } catch (Throwable t) { 184 ; 185 } 186 if (report == null) 187 return; 188 189 StringBuffer sb = new StringBuffer (); 190 191 sb.append("<html><head><title>"); 192 sb.append(ServerInfo.getServerInfo()).append(" - "); 193 sb.append(sm.getString("errorReportValve.errorReport")); 194 sb.append("</title>"); 195 sb.append("<style><!--"); 196 sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS); 197 sb.append("--></style> "); 198 sb.append("</head><body>"); 199 sb.append("<h1>"); 200 sb.append(sm.getString("errorReportValve.statusHeader", 201 "" + statusCode, message)).append("</h1>"); 202 sb.append("<HR size=\"1\" noshade=\"noshade\">"); 203 sb.append("<p><b>type</b> "); 204 if (throwable != null) { 205 sb.append(sm.getString("errorReportValve.exceptionReport")); 206 } else { 207 sb.append(sm.getString("errorReportValve.statusReport")); 208 } 209 sb.append("</p>"); 210 sb.append("<p><b>"); 211 sb.append(sm.getString("errorReportValve.message")); 212 sb.append("</b> <u>"); 213 sb.append(message).append("</u></p>"); 214 sb.append("<p><b>"); 215 sb.append(sm.getString("errorReportValve.description")); 216 sb.append("</b> <u>"); 217 sb.append(report); 218 sb.append("</u></p>"); 219 220 if (throwable != null) { 221 222 String stackTrace = getPartialServletStackTrace(throwable); 223 sb.append("<p><b>"); 224 sb.append(sm.getString("errorReportValve.exception")); 225 sb.append("</b> <pre>"); 226 sb.append(RequestUtil.filter(stackTrace)); 227 sb.append("</pre></p>"); 228 229 int loops = 0; 230 while (rootCause != null && (loops < 10)) { 231 stackTrace = getPartialServletStackTrace(rootCause); 232 sb.append("<p><b>"); 233 sb.append(sm.getString("errorReportValve.rootCause")); 234 sb.append("</b> <pre>"); 235 sb.append(RequestUtil.filter(stackTrace)); 236 sb.append("</pre></p>"); 237 rootCause = rootCause.getCause(); 239 loops++; 240 } 241 242 sb.append("<p><b>"); 243 sb.append(sm.getString("errorReportValve.note")); 244 sb.append("</b> <u>"); 245 sb.append(sm.getString("errorReportValve.rootCauseInLogs", 246 ServerInfo.getServerInfo())); 247 sb.append("</u></p>"); 248 249 } 250 251 sb.append("<HR size=\"1\" noshade=\"noshade\">"); 252 sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>"); 253 sb.append("</body></html>"); 254 255 try { 256 try { 257 response.setContentType("text/html"); 258 response.setCharacterEncoding("utf-8"); 259 } catch (Throwable t) { 260 if (container.getLogger().isDebugEnabled()) 261 container.getLogger().debug("status.setContentType", t); 262 } 263 Writer writer = response.getReporter(); 264 if (writer != null) { 265 writer.write(sb.toString()); 268 } 269 } catch (IOException e) { 270 ; 271 } catch (IllegalStateException e) { 272 ; 273 } 274 275 } 276 277 278 282 protected String getPartialServletStackTrace(Throwable t) { 283 StringBuffer trace = new StringBuffer (); 284 trace.append(t.toString()).append('\n'); 285 StackTraceElement [] elements = t.getStackTrace(); 286 int pos = elements.length; 287 for (int i = 0; i < elements.length; i++) { 288 if ((elements[i].getClassName().startsWith 289 ("org.apache.catalina.core.ApplicationFilterChain")) 290 && (elements[i].getMethodName().equals("internalDoFilter"))) { 291 pos = i; 292 } 293 } 294 for (int i = 0; i < pos; i++) { 295 if (!(elements[i].getClassName().startsWith 296 ("org.apache.catalina.core."))) { 297 trace.append('\t').append(elements[i].toString()).append('\n'); 298 } 299 } 300 return trace.toString(); 301 } 302 303 } 304 | Popular Tags |