📄 jahiaerrordisplay.java
字号:
} catch (IOException ioe) { JahiaConsole.println("JahiaErrorDisplay.DisplayException", "Dispatching caused an IOException"); JahiaConsole.printe("JahiaErrorDisplay.DisplayException", ioe); } catch (java.lang.IllegalStateException ise) { JahiaConsole.println("JahiaErrorDisplay.DisplayException", "Cannot forward because response was already committed !"); JahiaConsole.printe("JahiaErrorDisplay.DisplayException", ise); } catch (Throwable newt) { JahiaConsole.println("JahiaErrorDisplay.DisplayException", "Unknown exception during DisplayException : " + t.getMessage()); JahiaConsole.printe("JahiaErrorDisplay.DisplayException", newt); } return false; } /** * Mails an exception to the administrator according to the settings in * the Jahia installation. */ public static boolean MailException(Throwable t, HttpServletRequest request, HttpServletResponse response, JahiaPrivateSettings jSettings, int errorInt) { try { String to = null, subject = null, from = null, cc = null, bcc = null, url = null; String mailhost = null; String mailer = "Jahia Server 1.0"; String protocol = null, host = null, user = null, password = null; String record = null; // name of folder in which to record mail to = jSettings.mail_administrator; from = jSettings.mail_from; mailhost = jSettings.mail_server; if ((mailhost.equals("")) || (to.equals("")) || (from.equals(""))) { JahiaConsole.println("JahiaErrorDisplay.MailException", "Mail settings not valid, ignoring..."); return false; } if ((mailhost == null) || (to == null) || (from == null)) { JahiaConsole.println("JahiaErrorDisplay.MailException", "Mail settings not valid, ignoring..."); return false; } JahiaConsole.println("JahiaErrorDisplay.MailException", "Using settings mailhost=[" + mailhost + "] to=[" + to + "] from=[" + from + "] paranoia=" + Integer.toString(jSettings.mail_paranoia)); if (t instanceof JahiaException) { JahiaException je = (JahiaException) t; if (je.getSeverity() < jSettings.mail_paranoia) { return true; } } boolean debug = false; Properties props = System.getProperties(); if (mailhost != null) props.put("mail.smtp.host", mailhost); // Get a Session object Session session = Session.getDefaultInstance(props, null); if (debug) session.setDebug(true); // construct the message Message msg = new MimeMessage(session); if (from != null) msg.setFrom(new InternetAddress(from)); else msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); if (cc != null) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); if (bcc != null) msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); subject = "[JAHIA] Jahia Error : " + t.getMessage(); msg.setSubject(subject); StringWriter msgBodyWriter = new StringWriter(); PrintWriter strOut = new PrintWriter(msgBodyWriter); strOut.println(""); strOut.println("Your Jahia Server has generated an error. Please review the details below for additional information: "); strOut.println(""); if (t instanceof JahiaException) { JahiaException nje = (JahiaException) t; String severityMsg = "Undefined"; switch (nje.getSeverity()) { case JahiaException.WARNING : severityMsg = "WARNING"; break; case JahiaException.ERROR : severityMsg = "ERROR"; break; case JahiaException.CRITICAL : severityMsg = "CRITICAL"; break; case JahiaException.KISS_YOUR_ASS_GOODBYE : severityMsg = "FATAL"; break; } strOut.println("Severity : " + severityMsg); } strOut.println(""); strOut.println("Error : " + t.getMessage()); strOut.println(""); strOut.println("URL : " + request.getRequestURL() + " Method : " + request.getMethod()); strOut.println(""); strOut.println("Remote host : " + request.getRemoteHost() + " Remote Address : " + request.getRemoteAddr()); strOut.println(""); strOut.println("Request headers : "); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); String headerValue = request.getHeader(headerName); strOut.println(" " + headerName + ":" + headerValue); } if (errorInt == 0) { strOut.println(""); strOut.println("Stack trace : "); String stackTraceStr = stackTraceToString(t); strOut.println(stackTraceStr); } strOut.println(""); strOut.println("Depending on the severity of this error, Jahia may still be operational or not. Please check your"); strOut.println("installation as soon as possible."); strOut.println(""); strOut.println("Yours Faithfully, "); strOut.println(" Jahia Server"); msg.setText(msgBodyWriter.toString()); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); JahiaConsole.println("JahiaErrorDisplay.MailException", "Mailing to " + to + " via " + mailhost + "..."); // send the thing off Transport.send(msg); JahiaConsole.println("JahiaErrorDisplay.MailException", "Mail was sent successfully."); } catch (Throwable th) { JahiaConsole.println("JahiaErrorDisplay.MailException", "Error while sending mail : " + th.getMessage()); JahiaConsole.printe("JahiaErrorDisplay.MailException", th); return false; } return true; } /** * Converts an exception to a string, dump everything in a simple manner, * using typecasting if it is a JahiaException or a ServletException that * can contain other exception. * Warning : the result String contains return chars. * * @param t the exception to be converted to a String * @param request the http request object * @param response the http response object * * @returns A string containing the full dump with stack trace of the * exception. Return chars are inserted to make it more readable... */ private static String throwableToString(Throwable t, HttpServletRequest request, HttpServletResponse response) { StringWriter msgBodyWriter = new StringWriter(); PrintWriter strOut = new PrintWriter(msgBodyWriter); strOut.println("Your Jahia Server has generated an error. Please review the details below for additional information: "); if (t instanceof JahiaException) { JahiaException nje = (JahiaException) t; String severityMsg = "Undefined"; switch (nje.getSeverity()) { case JahiaException.WARNING : severityMsg = "WARNING"; break; case JahiaException.ERROR : severityMsg = "ERROR"; break; case JahiaException.CRITICAL : severityMsg = "CRITICAL"; break; case JahiaException.KISS_YOUR_ASS_GOODBYE : severityMsg = "FATAL"; break; } strOut.println("Severity : " + severityMsg); } strOut.println("Error : " + t.getMessage()); if ((request != null) && (response != null)) { strOut.println("URL : " + request.getRequestURL() + " Method : " + request.getMethod()); strOut.println("Remote host : " + request.getRemoteHost() + " Remote Address : " + request.getRemoteAddr()); strOut.println("Request headers : "); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); String headerValue = request.getHeader(headerName); strOut.println(" " + headerName + ":" + headerValue); } } strOut.println("Stack trace : "); String stackTraceStr = stackTraceToString(t); strOut.println(stackTraceStr); strOut.println(""); return msgBodyWriter.toString(); } /** * Converts an exception stack trace to a string, going doing into all * the embedded exceptions too to detail as much as possible the real * causes of the error. * * @param t the exception (eventually that contains other exceptions) for * which we want to convert the stack trace into a string. * * @return a string containing all the stack traces of all the exceptions * contained inside this exception, or an empty string if passed an * empty string. */ private static String stackTraceToString(Throwable t) { if (t == null) { return ""; } int level = 0; StringWriter msgBodyWriter = new StringWriter(); PrintWriter strOut = new PrintWriter(msgBodyWriter); Throwable curThrowable = t; while (curThrowable != null) { strOut.println("Cause level " + Integer.toString(level) + " : "); curThrowable.printStackTrace(strOut); if (curThrowable instanceof ServletException) { ServletException se = (ServletException) curThrowable; curThrowable = se.getRootCause(); level++; } else if (curThrowable instanceof JahiaException) { JahiaException je = (JahiaException) curThrowable; curThrowable = je.getRootCause(); level++; } else if (curThrowable instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) curThrowable; curThrowable = ite.getTargetException(); level++; } else { curThrowable = null; } } return msgBodyWriter.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -