1 67 68 69 package org.eclipse.tomcat.internal; 70 71 72 import java.io.*; 73 import java.util.*; 74 75 import javax.servlet.*; 76 import javax.servlet.http.*; 77 78 import org.apache.catalina.*; 79 import org.apache.catalina.util.*; 80 import org.apache.catalina.valves.*; 81 import org.eclipse.core.runtime.Platform; 82 83 84 92 93 public class EclipseErrorReportValve 94 extends ValveBase { 95 96 97 99 100 103 private int debug = 0; 104 105 106 109 private static final String info = 110 "org.eclipse.tomcat.internal.EclipseErrorReportValve"; 112 113 116 protected static StringManager sm = 117 StringManager.getManager(Constants.Package); 118 119 120 122 123 126 public String getInfo() { 127 128 return (info); 129 130 } 131 132 133 135 136 148 public void invoke(Request request, Response response, 149 ValveContext context) 150 throws IOException, ServletException { 151 152 context.invokeNext(request, response); 154 155 ServletRequest sreq = (ServletRequest) request; 156 Throwable throwable = 157 (Throwable ) sreq.getAttribute(Globals.EXCEPTION_ATTR); 158 159 ServletResponse sresp = (ServletResponse) response; 160 if (sresp.isCommitted()) { 161 return; 162 } 163 164 if (throwable != null) { 165 166 response.setError(); 168 169 try { 171 sresp.reset(); 172 } catch (IllegalStateException e) { 173 ; 174 } 175 176 ServletResponse sresponse = (ServletResponse) response; 177 if (sresponse instanceof HttpServletResponse) 178 ((HttpServletResponse) sresponse).sendError 179 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 180 181 } 182 183 response.setSuspended(false); 184 185 try { 186 report(request, response, throwable); 187 } catch (Throwable tt) { 188 } 190 191 } 192 193 194 197 public String toString() { 198 199 StringBuffer sb = new StringBuffer ("EclipseErrorReportValve["); sb.append(container.getName()); 201 sb.append("]"); return (sb.toString()); 203 204 } 205 206 207 209 210 218 protected void report(Request request, Response response, 219 Throwable throwable) 220 throws IOException { 221 222 if (!(response instanceof HttpResponse)) 224 return; 225 HttpResponse hresponse = (HttpResponse) response; 226 if (!(response instanceof HttpServletResponse)) 227 return; 228 HttpServletResponse hres = (HttpServletResponse) response; 229 int statusCode = hresponse.getStatus(); 230 String message = RequestUtil.filter(hresponse.getMessage()); 231 if (message == null) 232 message = ""; 234 if (statusCode < 400) 236 return; 237 238 247 248 Throwable rootCause = null; 249 250 if (throwable != null) { 251 252 if (throwable instanceof ServletException) 253 rootCause = ((ServletException) throwable).getRootCause(); 254 255 } 256 257 String report = null; 259 try { 260 report = sm.getString("http." + statusCode, message); } catch (Throwable t) { 262 ; 263 } 264 if (report == null) 265 return; 266 267 StringBuffer sb = new StringBuffer (); 268 269 sb.append("<html><head><title>"); sb.append(sm.getString("errorReportValve.errorReport")); sb.append("</title>"); if (statusCode == 404) { 273 sb.append("</head><body>"); sb.append(TomcatResources.noDocument); 275 } else { 276 sb.append("<STYLE><!--"); sb.append("H1{font-family : sans-serif,Arial,Tahoma;color : white;background-color : black;} "); sb.append("H3{font-family : sans-serif,Arial,Tahoma;color : white;background-color : black;} "); sb.append("BODY{font-family : sans-serif,Arial,Tahoma;color : black;background-color : white;} "); sb.append("B{color : white;background-color : black;} "); sb.append("HR{color : black;} "); sb.append("--></STYLE> "); sb.append("</head><body>"); sb.append("<h1>"); sb.append(sm.getString("errorReportValve.statusHeader", "" + statusCode, message)).append("</h1>"); sb.append("<HR size=\"1\" noshade>"); sb.append("<p><b>type</b> "); if (throwable != null) { 290 sb.append(sm.getString("errorReportValve.exceptionReport")); } else { 292 sb.append(sm.getString("errorReportValve.statusReport")); } 294 sb.append("</p>"); sb.append("<p><b>"); sb.append(sm.getString("errorReportValve.message")); sb.append("</b> <u>"); sb.append(message).append("</u></p>"); sb.append("<p><b>"); sb.append(sm.getString("errorReportValve.description")); sb.append("</b> <u>"); sb.append(report); 303 sb.append("</u></p>"); 305 if (throwable != null) { 306 boolean selfHostingMode = false; 307 String [] args = Platform.getCommandLineArgs(); 308 for (int i = 0; i < args.length; i++) { 309 if ("-pdelaunch".equals(args[i])) { selfHostingMode = true; 311 break; 312 } 313 } 314 if (selfHostingMode) { 315 StringWriter stackTrace = new StringWriter(); 316 throwable.printStackTrace(new PrintWriter(stackTrace)); 317 sb.append("<p><b>"); sb.append(sm.getString("errorReportValve.exception")); sb.append("</b> <pre>"); sb.append(RequestUtil.filter(stackTrace.toString())); 321 sb.append("</pre></p>"); if (rootCause != null) { 323 stackTrace = new StringWriter(); 324 rootCause.printStackTrace(new PrintWriter(stackTrace)); 325 sb.append("<p><b>"); sb.append(sm.getString("errorReportValve.rootCause")); sb.append("</b> <pre>"); sb.append(RequestUtil.filter(stackTrace.toString())); 329 sb.append("</pre></p>"); } 331 } 332 } 333 334 } 335 sb.append("</body></html>"); 337 try { 338 339 Writer writer = response.getReporter(); 340 341 if (writer != null) { 342 343 Locale locale = Locale.getDefault(); 344 345 try { 346 hres.setContentType("text/html"); hres.setLocale(locale); 348 } catch (Throwable t) { 349 if (debug >= 1) 350 log("status.setContentType", t); } 352 353 writer.write(sb.toString()); 356 writer.flush(); 357 358 } 359 360 } catch (IOException e) { 361 ; 362 } catch (IllegalStateException e) { 363 ; 364 } 365 366 } 367 368 369 374 protected void log(String message) { 375 376 Logger logger = container.getLogger(); 377 if (logger != null) 378 logger.log(this.toString() + ": " + message); else 380 System.out.println(this.toString() + ": " + message); 382 } 383 384 385 391 protected void log(String message, Throwable throwable) { 392 393 Logger logger = container.getLogger(); 394 if (logger != null) 395 logger.log(this.toString() + ": " + message, throwable); else { 397 System.out.println(this.toString() + ": " + message); throwable.printStackTrace(System.out); 399 } 400 401 } 402 403 404 } 405 | Popular Tags |