001: /*
002: * $Id: ErrorResult.java,v 1.4 2007/09/18 08:45:08 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.server.frontend;
008:
009: import java.text.SimpleDateFormat;
010: import java.util.Date;
011: import java.io.PrintWriter;
012: import java.io.StringWriter;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.xml.transform.SourceLocator;
015: import javax.xml.transform.TransformerException;
016: import org.xins.common.text.TextUtils;
017: import org.xins.server.FunctionResult;
018:
019: /**
020: * Result for an error.
021: *
022: * @version $Revision: 1.4 $ $Date: 2007/09/18 08:45:08 $
023: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
024: */
025: class ErrorResult extends FunctionResult {
026:
027: private static SimpleDateFormat ERROR_DATE_FORMATTER = new SimpleDateFormat(
028: "yyyy.MM.dd',' HH:mm:ss ");
029:
030: /**
031: * Creates the result for a transformation error.
032: *
033: * @param exception
034: * the exception during the transformation, cannot be <code>null</code>.
035: *
036: * @param httpRequest
037: * the HTTP request, cannot be <code>null</code>.
038: */
039: ErrorResult(Exception exception, HttpServletRequest httpRequest) {
040:
041: param("error.type", "TechnicalError");
042: param("error.message", exception.getMessage());
043: StringWriter stWriter = new StringWriter(360);
044: PrintWriter printWriter = new PrintWriter(stWriter);
045: exception.printStackTrace(printWriter);
046: String stackTrace = stWriter.toString();
047: param("error.stacktrace", stackTrace);
048: String timeOfFailure = ERROR_DATE_FORMATTER.format(new Date());
049: param("error.time", timeOfFailure);
050: if (exception instanceof TransformerException) {
051: TransformerException tex = (TransformerException) exception;
052: SourceLocator locator = tex.getLocator();
053: if (locator != null) {
054: int line = locator.getLineNumber();
055: int col = locator.getColumnNumber();
056: String publicId = locator.getPublicId();
057: String systemId = locator.getSystemId();
058: String detail = "line: " + line + "; col: " + col
059: + "; public ID: " + publicId + "; system ID: "
060: + systemId;
061: param("error.location", detail);
062: }
063: }
064:
065: String command = httpRequest.getParameter("command");
066: if (!TextUtils.isEmpty(command)) {
067: param("error.command", command);
068: }
069: String action = httpRequest.getParameter("action");
070: if (!TextUtils.isEmpty(action)) {
071: param("error.action", action);
072: }
073: String query = httpRequest.getQueryString();
074: if (!TextUtils.isEmpty(query)) {
075: param("error.query", query);
076: }
077: }
078:
079: /**
080: * Return the XSLT to use to display the data of the Control command.
081: *
082: * @return
083: * the XSLT to display the Control result, never <code>null</code>.
084: */
085: static String getDefaultErrorTemplate() {
086: String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
087: + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"
088: + "<xsl:output method=\"html\" indent=\"yes\" encoding=\"US-ASCII\"\n"
089: + "doctype-public=\"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
090: + "doctype-system=\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"\n"
091: + "omit-xml-declaration=\"yes\" />\n"
092: + "<xsl:template match=\"commandresult\">\n"
093: + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
094: + "<body>\n"
095: + "<h2>\n"
096: + "A technical error occured\n"
097: + "</h2>\n"
098: + "<h3>Command</h3>\n"
099: + "<xsl:value-of select=\"parameter[@name='error.command']/text()\" />\n"
100: + "<xsl:if test=\"parameter[@name='error.action']\">\n"
101: + "<xsl:text> with the </xsl:text>\n"
102: + "<xsl:value-of select=\"parameter[@name='error.action']/text()\" />\n"
103: + "<xsl:text> action.</xsl:text>\n"
104: + "</xsl:if>\n"
105: + "<h3>Request</h3>\n"
106: + "<xsl:value-of select=\"parameter[@name='error.query']/text()\" />\n"
107: + "<h3>Error message</h3>\n"
108: + "<xsl:value-of select=\"parameter[@name='error.message']/text()\" />\n"
109: + "<xsl:if test=\"parameter[@name='error.location']\">\n"
110: + "<h3>Error location</h3>\n"
111: + "<xsl:value-of select=\"parameter[@name='error.location']/text()\" />\n"
112: + "</xsl:if>\n"
113: + "<h3>Error details</h3>\n"
114: + "<pre>\n"
115: + "<xsl:value-of select=\"parameter[@name='error.stacktrace']/text()\" />\n"
116: + "</pre>\n" + "</body>\n" + "</html>\n"
117: + "</xsl:template>\n" + "</xsl:stylesheet>";
118: return result;
119: }
120: }
|