All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xins.server.frontend.ErrorResult Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
/*
 * $Id: ErrorResult.java,v 1.6 2010/09/29 17:21:48 agoubard Exp $
 *
 * See the COPYRIGHT file for redistribution and use restrictions.
 */
package org.xins.server.frontend;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.TransformerException;
import org.xins.common.text.TextUtils;
import org.xins.server.FunctionResult;

/**
 * Result for an error.
 *
 * @version $Revision: 1.6 $ $Date: 2010/09/29 17:21:48 $
 * @author Anthony Goubard
 */
class ErrorResult extends FunctionResult {

    private static SimpleDateFormat ERROR_DATE_FORMATTER = new SimpleDateFormat("yyyy.MM.dd',' HH:mm:ss ");

   /**
    * Creates the result for a transformation error.
    *
    * @param exception
    *    the exception during the transformation, cannot be null.
    *
    * @param httpRequest
    *    the HTTP request, cannot be null.
    */
   ErrorResult(Exception exception, HttpServletRequest httpRequest) {

      param("error.type", "TechnicalError");
      param("error.message", exception.getMessage());
      StringWriter stWriter = new StringWriter(360);
      PrintWriter printWriter = new PrintWriter(stWriter);
      exception.printStackTrace(printWriter);
      String stackTrace = stWriter.toString();
      param("error.stacktrace", stackTrace);
      String timeOfFailure = ERROR_DATE_FORMATTER.format(new Date());
      param("error.time", timeOfFailure);
      if (exception instanceof TransformerException) {
         TransformerException tex = (TransformerException) exception;
         SourceLocator locator = tex.getLocator();
         if (locator != null) {
            int line = locator.getLineNumber();
            int col = locator.getColumnNumber();
            String publicId = locator.getPublicId();
            String systemId = locator.getSystemId();
            String detail = "line: " + line + "; col: " + col +
                  "; public ID: " + publicId + "; system ID: " + systemId;
            param("error.location", detail);
         }
      }

      String command = httpRequest.getParameter("command");
      if (!TextUtils.isEmpty(command)) {
         param("error.command", command);
      }
      String action = httpRequest.getParameter("action");
      if (!TextUtils.isEmpty(action)) {
         param("error.action", action);
      }
      String query = httpRequest.getQueryString();
      if (!TextUtils.isEmpty(query)) {
         param("error.query", query);
      }
   }

   /**
    * Return the XSLT to use to display the data of the Control command.
    *
    * @return
    *    the XSLT to display the Control result, never null.
    */
   static String getDefaultErrorTemplate() {
      String result = "\n" +
          "\n" +
          "\n" +
          "\n" +
          "\n" +
            "\n" +
              "

\n" + "A technical error occured\n" + "

\n" + "

Command

\n" + "\n" + "\n" + " with the \n" + "\n" + " action.\n" + "\n" + "

Request

\n" + "\n" + "

Error message

\n" + "\n" + "\n" + "

Error location

\n" + "\n" + "
\n" + "

Error details

\n" + "
\n" +
              "\n" +
              "
\n" + "\n" + "\n" + "
\n" + "
"; return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy