1 43 package net.jforum.exceptions; 44 45 import java.io.PrintWriter ; 46 import java.io.StringWriter ; 47 import java.io.Writer ; 48 49 import net.jforum.JForumExecutionContext; 50 import net.jforum.SessionFacade; 51 import net.jforum.util.preferences.ConfigKeys; 52 import net.jforum.util.preferences.SystemGlobals; 53 54 import org.apache.log4j.Logger; 55 56 import freemarker.template.SimpleHash; 57 import freemarker.template.Template; 58 59 63 public class ExceptionWriter 64 { 65 private static Logger logger = Logger.getLogger(ExceptionWriter.class); 66 67 public void handleExceptionData(Throwable t, Writer w) 68 { 69 StringWriter strWriter = new StringWriter (); 70 PrintWriter writer = new PrintWriter (strWriter); 71 t.printStackTrace(writer); 72 writer.close(); 73 74 try { 75 logger.error(strWriter); 76 String message = ""; 77 78 if (t.getCause() != null) { 79 message = t.getCause().getMessage(); 80 } 81 82 if (message == null) { 83 message = t.getMessage(); 84 } 85 86 if (message == null) { 87 message = t.toString(); 88 } 89 90 boolean canViewStackTrace = !SystemGlobals.getBoolValue(ConfigKeys.STACKTRACE_MODERATORS_ONLY) 91 || (SessionFacade.isLogged() 92 && SessionFacade.getUserSession().isModerator()); 93 94 String filter = "[<>]"; 95 String stackTrace = canViewStackTrace 96 ? strWriter.toString() 97 : "Only moderators can view stack trace."; 98 99 stackTrace = stackTrace.replaceAll(filter, ""); 100 message = message.replaceAll(filter, ""); 101 102 SimpleHash templateContext = JForumExecutionContext.getTemplateContext(); 103 104 templateContext.put("stackTrace", stackTrace); 105 templateContext.put("message", message); 106 107 Template template = JForumExecutionContext.templateConfig().getTemplate("exception.html"); 108 template.process(templateContext, w); 109 } 110 catch (Exception e) { 111 strWriter = new StringWriter (); 112 writer = new PrintWriter (strWriter); 113 e.printStackTrace(writer); 114 writer.close(); 115 logger.error(strWriter); 116 } 117 } 118 } 119 | Popular Tags |