1 19 package org.openharmonise.rm; 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 25 import javax.servlet.http.*; 26 27 import org.openharmonise.commons.net.*; 28 import org.openharmonise.rm.config.*; 29 30 36 public class HarmoniseExceptionHandler { 37 40 static final String EMAIL_ADDRESS_PNAME = "ERROR_EMAIL_ADDRESS"; 41 44 static final String EMAIL_ERRORS_PNAME = "EMAIL_ERRORS"; 45 48 static final String REDIRECT_URL_PNAME = "ERROR_REDIRECT_URL"; 49 52 static final String USE_REDIRECT_PNAME = "REDIRECT_ON_ERROR"; 53 56 static final String ERRORS_FROMADDRESS = "ErrorMail@simulacramedia.com"; 57 58 61 private String [] m_error_email_toaddresses = new String [1]; 62 65 private boolean m_bEmailErrors = false; 66 69 private String m_reporting_URL = "Not known"; 70 73 private String m_originator_email_address = ERRORS_FROMADDRESS; 74 77 private String m_exception_date_string = "Not known"; 78 81 private String m_additional_info = "None provided"; 82 85 private boolean m_use_redirect = false; 86 89 private String m_redirect_URL = "/errors/404.htm"; 90 93 protected String m_sImportance = null; 94 95 101 public HarmoniseExceptionHandler() throws Exception { 102 String error_email_toaddress = 103 ConfigSettings.getProperty(EMAIL_ADDRESS_PNAME); 104 m_error_email_toaddresses[0] = error_email_toaddress; 105 106 m_use_redirect = 107 ConfigSettings.getBoolProperty( 108 USE_REDIRECT_PNAME, 109 "false"); 110 111 m_redirect_URL = 112 ConfigSettings.getProperty( 113 REDIRECT_URL_PNAME, 114 m_redirect_URL); 115 116 m_bEmailErrors = 117 ConfigSettings.getBoolProperty( 118 EMAIL_ERRORS_PNAME, 119 "false"); 120 121 this.updateExceptionDate(); 122 } 123 124 136 public void handle( 137 String additional_info, 138 Throwable e, 139 HttpServletRequest request, 140 HttpServletResponse response, 141 PrintWriter out) 142 throws Exception { 143 m_additional_info = additional_info; 144 145 m_reporting_URL = request.getRequestURI(); 146 147 String exception_description = describeException(request, e); 148 149 if (m_bEmailErrors) { 150 emailError(exception_description, e); 151 } 153 156 if (m_use_redirect) { 157 response.sendRedirect(m_redirect_URL); 158 } else { 159 if (out != null) { 160 out.println("<pre>"); 161 out.println(exception_description); 162 out.println("</pre>"); 163 } 164 } 165 } 166 167 175 public void emailError(String exception_description, Throwable e) 176 throws Exception { 177 emailError(exception_description, e, false); 178 } 179 180 189 public void emailError( 190 String exception_description, 191 Throwable e, 192 boolean bEmphasize) 193 throws Exception { 194 String sSubject = 195 "Error for " 196 + m_reporting_URL 197 + " [" 198 + m_exception_date_string 199 + "]"; 200 201 emailError(sSubject, exception_description, e, bEmphasize); 202 } 203 204 213 public void emailError( 214 String sSubject, 215 String exception_description, 216 Throwable e) 217 throws Exception { 218 emailError(sSubject, exception_description, e, false); 219 } 220 221 231 public void emailError( 232 String sSubject, 233 String exception_description, 234 Throwable e, 235 boolean bEmphasize) 236 throws Exception { 237 238 if (bEmphasize == true) { 239 sSubject = sSubject.toUpperCase(); 240 } 241 242 Email email = 243 new Email( 244 ConfigSettings.getProperty("EMAIL_HOST"), 245 m_error_email_toaddresses[0], 246 m_originator_email_address, 247 sSubject, 248 exception_description); 249 250 if (m_sImportance != null) { 251 email.setImportance(m_sImportance); 252 } 253 254 for (int i = 0; i < m_error_email_toaddresses.length; i++) { 255 email.SetEmailAddress(m_error_email_toaddresses[i]); 256 email.send(); 257 } 258 259 } 260 261 265 private void updateExceptionDate() { 266 SimpleDateFormat formatter = 267 new SimpleDateFormat("EEEE yyyyy-MMMM-dd 'at' hh:mm:ss zzzz"); 268 269 java.util.Date current_time = new java.util.Date (); 270 m_exception_date_string = formatter.format(current_time); 271 } 272 273 278 public void setImportance(String sImportance) { 279 this.m_sImportance = sImportance; 280 } 281 282 288 public String describeException(Exception e) { 289 StringBuffer strbuf = new StringBuffer (); 290 String EOL = "\n"; 291 292 strbuf.append("[").append(m_reporting_URL).append("]").append(EOL); 293 strbuf.append(m_exception_date_string).append(EOL); 294 strbuf.append(EOL); 295 296 strbuf.append("The following error occured:").append(EOL); 297 strbuf.append(" '").append(e).append("'").append(EOL); 298 strbuf.append(EOL); 299 300 strbuf.append("Additional information:").append(EOL); 302 strbuf.append(m_additional_info).append(EOL); 303 strbuf.append(EOL); 304 305 strbuf.append("-- Stacktrace:").append(EOL); 307 ; 308 strbuf.append(getStackTraceAsString(e)); 309 310 return (strbuf.toString()); 311 } 312 313 321 public String describeException(HttpServletRequest request, Throwable e) { 322 StringBuffer strbuf = new StringBuffer (); 323 String EOL = "\n"; 324 325 strbuf.append("[").append(m_reporting_URL).append("]").append(EOL); 326 strbuf.append(m_exception_date_string).append(EOL); 327 strbuf.append(EOL); 328 329 strbuf.append("The following error occured:").append(EOL); 330 strbuf.append(" '").append(e).append("'").append(EOL); 331 strbuf.append(EOL); 332 333 strbuf.append("Additional information:").append(EOL); 335 strbuf.append(m_additional_info).append(EOL); 336 strbuf.append(EOL); 337 338 strbuf.append("-- Request:\n"); 340 strbuf.append("URI :").append(request.getRequestURI()).append(EOL); 341 strbuf.append("Query :?").append(request.getQueryString()).append(EOL); 342 strbuf.append("Path info :").append(request.getPathInfo()).append(EOL); 343 strbuf.append("User IP :").append(request.getRemoteAddr()).append(EOL); 344 strbuf.append("Remote Host :").append(request.getRemoteHost()).append( 345 EOL); 346 strbuf.append("Remote User :").append(request.getRemoteUser()).append( 347 EOL); 348 strbuf.append("Request method :").append(request.getMethod()).append( 349 EOL); 350 strbuf.append("Servlet Path :").append( 351 request.getServletPath()).append( 352 EOL); 353 354 strbuf.append(EOL); 355 356 strbuf.append("-- Header Info:").append(EOL); 358 359 Enumeration header_names = request.getHeaderNames(); 360 361 while (header_names.hasMoreElements()) { 362 String name = (String ) header_names.nextElement(); 363 strbuf.append(" "); 364 strbuf.append(name); 365 strbuf.append(": "); 366 strbuf.append(request.getHeader(name)); 367 strbuf.append(EOL); 368 } 369 370 strbuf.append(EOL); 371 372 Cookie[] cookies = request.getCookies(); 374 375 if (cookies != null) { 376 strbuf.append("-- Cookies:").append(EOL); 377 378 for (int i = 0; i < cookies.length; i++) { 379 Cookie this_cookie = cookies[i]; 380 strbuf.append(" "); 381 strbuf.append(this_cookie.getName()); 382 strbuf.append(": "); 383 strbuf.append(this_cookie.getValue()); 384 strbuf.append(EOL); 385 } 386 } 387 388 strbuf.append(EOL); 389 390 strbuf.append("-- Stacktrace:").append(EOL); 392 ; 393 strbuf.append(getStackTraceAsString(e)); 394 395 return (strbuf.toString()); 396 } 397 398 404 private static String getStackTraceAsString(Throwable e) { 405 StringWriter sw = new StringWriter(); 406 PrintWriter pw = new PrintWriter(sw); 407 e.printStackTrace(pw); 408 pw.close(); 409 410 return sw.toString(); 411 } 412 } | Popular Tags |