001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: * /*
039: * Created on Feb 3, 2005 5:15:34 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.exceptions;
044:
045: import java.io.PrintWriter;
046: import java.io.StringWriter;
047: import java.io.Writer;
048:
049: import net.jforum.JForumExecutionContext;
050: import net.jforum.SessionFacade;
051: import net.jforum.context.RequestContext;
052: import net.jforum.util.preferences.ConfigKeys;
053: import net.jforum.util.preferences.SystemGlobals;
054:
055: import org.apache.log4j.Logger;
056:
057: import freemarker.template.SimpleHash;
058: import freemarker.template.Template;
059:
060: /**
061: * @author Rafael Steil
062: * @version $Id: ExceptionWriter.java,v 1.14 2007/10/10 04:54:20 rafaelsteil Exp $
063: */
064: public class ExceptionWriter {
065: private static Logger logger = Logger
066: .getLogger(ExceptionWriter.class);
067:
068: public void handleExceptionData(Throwable t, Writer w,
069: RequestContext request) {
070: StringWriter strWriter = new StringWriter();
071: PrintWriter writer = new PrintWriter(strWriter);
072: t.printStackTrace(writer);
073:
074: String currentUrl = this .extractCurrentUrl(request);
075:
076: writer.write(currentUrl);
077: writer.close();
078:
079: try {
080: logger.error(strWriter);
081:
082: String message = "";
083: Throwable cause = t.getCause();
084:
085: while (cause != null) {
086: message = cause.toString();
087: cause = cause.getCause();
088: }
089:
090: if (message == null || message.equals("")) {
091: message = t.getMessage();
092: }
093:
094: if (message == null || message.equals("")) {
095: message = t.toString();
096: }
097:
098: boolean canViewStackTrace = !SystemGlobals
099: .getBoolValue(ConfigKeys.STACKTRACE_MODERATORS_ONLY)
100: || (SessionFacade.isLogged() && SessionFacade
101: .getUserSession().isModerator());
102:
103: String filter = "[<>]";
104: String stackTrace = canViewStackTrace ? strWriter
105: .toString()
106: : "Only moderators can view stack trace.";
107:
108: stackTrace = stackTrace.replaceAll(filter, "");
109: message = message.replaceAll(filter, "");
110:
111: SimpleHash templateContext = JForumExecutionContext
112: .getTemplateContext();
113:
114: templateContext.put("stackTrace", stackTrace);
115: templateContext.put("message", message);
116:
117: Template template = JForumExecutionContext.templateConfig()
118: .getTemplate("exception.html");
119: template.process(templateContext, w);
120: } catch (Exception e) {
121: strWriter = new StringWriter();
122: writer = new PrintWriter(strWriter);
123: e.printStackTrace(writer);
124: writer.close();
125: logger.error(strWriter);
126: }
127: }
128:
129: private String extractCurrentUrl(RequestContext request) {
130: return request == null ? "" : new StringBuffer().append(
131: "\nURL is: ").append(request.getRequestURI()).append(
132: '?').append(request.getQueryString()).toString();
133: }
134: }
|