1 2 24 25 26 27 28 29 package com.lutris.appserver.server.httpPresentation; 30 31 import java.io.ByteArrayOutputStream ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.PrintWriter ; 35 36 import com.lutris.appserver.server.Application; 37 import com.lutris.logging.LogChannel; 38 import com.lutris.logging.Logger; 39 40 43 class ExceptionHandler { 44 47 private ExceptionHandler() { 48 } 49 50 54 private static void sendExceptionPage(HttpPresentationComms comms, 55 Application application, 56 Throwable except) 57 throws HttpPresentationException, IOException { 58 59 String title = "Error in " + application.getName(); 60 61 comms.response.setStatus(HttpPresentationResponse.SC_INTERNAL_SERVER_ERROR); 62 comms.response.setContentType("text/html"); 63 64 HttpPresentationOutputStream out = comms.response.getOutputStream(); 65 66 out.println("<HTML><TITLE>"); 67 out.println(title); 68 out.println("</TITLE>"); 69 70 out.println("<BODY BGCOLOR=white>"); 71 out.println("<H2><P><CENTER><FONT COLOR=red> - " + title + " - </FONT></CENTER></H2>"); 72 73 if (except instanceof java.io.FileNotFoundException ) { 75 out.println("<B>File not found</B>: " + 76 comms.request.getRequestURI()); 77 } else if (except instanceof DirectoryException) { 78 out.println("<B>Directory access error</B>: " + 79 comms.request.getRequestURI()); 80 } else if (except instanceof java.lang.IllegalAccessException ) { 81 out.println("<B>Illegal access</B>: " + 82 comms.request.getRequestURI()); 83 } else if ((except instanceof java.lang.ClassNotFoundException ) 84 || (except instanceof java.lang.NoClassDefFoundError )) { 85 out.println("<B>Class not found: " + except.getMessage() + "</B>"); 86 } else { 87 out.println("<B>Reason: </B>" + except.getClass() + 89 ": " + except.getMessage()); 90 } 91 92 out.println("<P>"); 94 out.println("<B>Java Call Stack:</B><P><FONT size=-1><PRE>"); 95 ByteArrayOutputStream b = new ByteArrayOutputStream (); PrintWriter p = new PrintWriter (b); 97 except.printStackTrace(p); 98 p.flush(); 99 out.println(b.toString()); 100 out.println("</PRE></FONT>\n"); 101 102 out.println("<FORM>"); 104 out.println("<CENTER><INPUT TYPE=BUTTON VALUE=\"back\""); 105 out.println("onClick=\"history.go(-1)\"></CENTER>\n"); 106 out.println("</FORM>"); 107 108 out.println("</BODY>"); 110 out.println("</HTML>"); 111 comms.response.flush(); 112 } 113 114 115 119 protected static void logDisplayException(HttpPresentationComms comms, 120 LogChannel logChannel, 121 long requestId, 122 Application application, 123 Throwable except, 124 String presObjPath) { 125 126 131 try { 132 if (HttpPresentationIOException.isClientIOException(except)) { 133 logChannel.write(Logger.DEBUG, "RID:" + requestId 134 + ": Client appeared to drop connection: " 135 + presObjPath); 136 sendExceptionPage(comms, application, except); 137 } else { 138 logChannel.write(Logger.ERROR, "RID:" + requestId 139 + ": Unhandled Application error in " 140 + application.getName(), except); 141 sendExceptionPage(comms, application, except); 142 } 143 } catch (IllegalStateException except2) { 144 } catch (IOException except2) { 146 } catch (HttpPresentationException except2) { 148 } 150 } 151 152 178 protected static String getErrorHandler(String presObjPath, 179 String requestedPresObjPath, 180 boolean whileHandlingError, 181 PresentationLoader loader, 182 LogChannel logChannel, 183 long requestId) { 184 String handlerDir; 185 186 logChannel.write(Logger.DEBUG, "RID:" +requestId 187 + ": Searching for error handler for " 188 + requestedPresObjPath); 189 190 if (!whileHandlingError) { 192 handlerDir = (new File (presObjPath)).getParent(); 194 } else { 195 String currentDir = (new File (presObjPath)).getParent(); 197 if (currentDir == null) { 198 handlerDir = null; 199 } else { 200 handlerDir = (new File (currentDir)).getParent(); 201 if (handlerDir == null) { 203 logChannel.write(Logger.DEBUG, "RID:" + requestId 204 + ": No error handler found for: " 205 + presObjPath); 206 return null; 207 } 208 } 209 } 210 211 while (true) { 213 String handlerPath = (new File (handlerDir, "ErrorHandler.po")).getPath(); 215 logChannel.write(Logger.DEBUG, "RID:" + requestId 216 + ": Checking for error handler: " + handlerPath); 217 try { 218 loader.loadPresentation(handlerPath); 219 return handlerPath; } catch (ClassNotFoundException noClass) { 221 if (handlerDir == null) { 222 break; } 224 String currentDir = (new File (handlerPath)).getParent(); 225 if (currentDir == null) { 226 handlerDir = null; 227 } else { 228 handlerDir = (new File (currentDir)).getParent(); 229 } 230 } catch (Throwable except) { 231 logChannel.write(Logger.DEBUG, "RID:" + requestId 232 + ": Error while loading error handler: " 233 + handlerPath, except); 234 } 235 } 236 237 logChannel.write(Logger.DEBUG, "RID:" + requestId 238 + ": No error handler found for: " + presObjPath); 239 return null; 240 } 241 242 243 247 private static void sendHtml(HttpPresentationComms comms, 248 String title, 249 String htmlText) 250 throws HttpPresentationException, IOException { 251 252 comms.response.setContentType("text/html"); 253 HttpPresentationOutputStream out = comms.response.getOutputStream(); 254 255 out.println("<HTML>"); 256 out.println("<TITLE>" + title + "</TITLE>"); 257 out.println("<BODY>"); 258 out.println(htmlText); 259 out.println("</BODY>"); 260 out.println("</HTML>"); 261 comms.response.flush(); 262 } 263 264 265 279 protected static void sendUnauthorizedPage(HttpPresentationComms comms, 280 LogChannel logChannel, 281 long requestId, 282 Application application, 283 String presObjPath, 284 PageUnauthorizedException unauthExcept) 285 throws HttpPresentationException, IOException { 286 287 try { 288 logChannel.write(Logger.DEBUG, "RID:" + requestId 289 + ": Authentication required: " + presObjPath); 290 291 comms.response.setHeader("WWW-authenticate", 292 "basic realm=\"" + 293 unauthExcept.getRelm() +"\""); 294 comms.response.setStatus(HttpPresentationResponse.SC_UNAUTHORIZED); 295 sendHtml(comms, 296 unauthExcept.getHtmlTitle(), 297 unauthExcept.getHtmlText()); 298 comms.response.flush(); 299 } catch (Exception except) { 300 HttpPresentationException presExcept = 302 new HttpPresentationException("Exception while sending page unauthorized response", except); 303 logDisplayException(comms, logChannel, requestId, application, presExcept, presObjPath); 304 } 305 } 306 307 311 protected static void maxRedirectsReached(HttpPresentationComms comms, 312 LogChannel logChannel, 313 long requestId, 314 int maxRedirectErrorLoops, 315 Application application, 316 String presObjPath) 317 throws HttpPresentationException, IOException { 318 319 String msg = "Maximum number of server redirects or ErrorHandler " 320 + "calls per request reached (" + maxRedirectErrorLoops 321 + "); probable endless loop: " + presObjPath; 322 HttpPresentationException except = 323 new HttpPresentationException(msg, comms.exception); 324 logDisplayException(comms, logChannel, requestId, application, except, presObjPath); 325 } 326 327 331 protected static void processUnhandledException(HttpPresentationComms comms, 332 LogChannel logChannel, 333 long requestId, 334 Application application, 335 String presObjPath, 336 Throwable except) { 337 338 try { 339 if ((except instanceof ClassNotFoundException ) 340 || (except instanceof NoClassDefFoundError ) 341 || (except instanceof FilePresentationException)) { 342 343 logChannel.write(Logger.WARNING, "RID:" + requestId 344 + ": page not found: " + presObjPath); 345 comms.response.setStatus(HttpPresentationResponse.SC_NOT_FOUND); 346 sendHtml(comms, "Page Not Found", 347 "<B>Page was not found on this server</B>"); 348 } else { 349 logDisplayException(comms, logChannel, requestId, application, except, presObjPath); 350 } 351 } catch (Throwable except2) { 352 } 354 } 355 } 356 | Popular Tags |