001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.exceptionprocessor;
021:
022: import java.io.IOException;
023: import java.util.Properties;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029: import javax.xml.transform.Templates;
030: import javax.xml.transform.TransformerConfigurationException;
031: import javax.xml.transform.TransformerException;
032: import javax.xml.transform.stream.StreamResult;
033:
034: import org.apache.log4j.Logger;
035: import org.w3c.dom.Document;
036:
037: import de.schlund.pfixxml.PfixServletRequest;
038: import de.schlund.pfixxml.exceptionprocessor.util.ExceptionDataValue;
039: import de.schlund.pfixxml.exceptionprocessor.util.ExceptionDataValueHelper;
040: import de.schlund.pfixxml.exceptionprocessor.util.TextCreatorVisitor;
041: import de.schlund.pfixxml.exceptionprocessor.util.XMLCreatorVisitor;
042: import de.schlund.pfixxml.resources.ResourceUtil;
043: import de.schlund.pfixxml.targets.TargetGenerationException;
044: import de.schlund.pfixxml.util.Xml;
045: import de.schlund.pfixxml.util.Xslt;
046: import de.schlund.pfixxml.util.XsltVersion;
047:
048: /**
049: * @author jh
050: *
051: * Get a DOM tree from the passed exception, transform it with a
052: * given stylesheet and write the result to the passed outputstream.
053: */
054: public class UniversalExceptionProcessor implements ExceptionProcessor {
055:
056: private static final String ERROR_STYLESHEET = "core/xsl/errorrepresentation.xsl";
057: private static final Logger LOG = Logger
058: .getLogger(UniversalExceptionProcessor.class);
059:
060: /* (non-Javadoc)
061: * @see de.schlund.pfixxml.exceptionprocessor.ExceptionProcessor#processException(java.lang.Throwable, de.schlund.pfixxml.exceptionprocessor.ExceptionConfig, de.schlund.pfixxml.PfixServletRequest, javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
062: */
063: public void processException(Throwable exception,
064: ExceptionConfig exConfig, PfixServletRequest pfixReq,
065: ServletContext servletContext, HttpServletRequest req,
066: HttpServletResponse res, Properties props)
067: throws IOException, ServletException {
068: Document doc = null;
069: String text = null;
070:
071: LOG.error("Processing throwable: ", exception);
072:
073: if (exception instanceof TargetGenerationException) {
074: TargetGenerationException tex = (TargetGenerationException) exception;
075: doc = tex.toXMLRepresentation();
076: text = tex.toStringRepresentation();
077: } else {
078: ExceptionDataValue data = ExceptionDataValueHelper
079: .createExceptionDataValue(exception, pfixReq);
080: XMLCreatorVisitor xv = new XMLCreatorVisitor();
081: data.accept(xv);
082: TextCreatorVisitor tv = new TextCreatorVisitor();
083: data.accept(tv);
084: doc = data.getXMLPresentation();
085: text = data.getTextBody();
086: }
087:
088: LOG.error(text);
089:
090: if (LOG.isDebugEnabled()) {
091: LOG.debug("Got following DOM for error-representation: "
092: + Xml.serialize(doc, true, false));
093: }
094:
095: doc = Xml.parse(XsltVersion.XSLT1, doc);
096:
097: Templates stvalue;
098:
099: try {
100: stvalue = Xslt
101: .loadTemplates(
102: XsltVersion.XSLT1,
103: ResourceUtil
104: .getFileResourceFromDocroot(ERROR_STYLESHEET));
105: } catch (TransformerConfigurationException e) {
106: throw new ServletException(e);
107: }
108:
109: try {
110: Xslt.transform(doc, stvalue, null, new StreamResult(res
111: .getOutputStream()));
112: } catch (TransformerException e) {
113: throw new ServletException(e);
114: } catch (IOException e) {
115: throw new ServletException(e);
116: }
117: }
118:
119: }
|