Beispiel #1
0
 public Location getLocation(Object obj) {
   Location loc = LocationUtils.getLocation(obj);
   if (loc == null) {
     return Location.UNKNOWN;
   }
   return loc;
 }
Beispiel #2
0
  /**
   * Send an HTTP error response code.
   *
   * @param request the HttpServletRequest object.
   * @param response the HttpServletResponse object.
   * @param code the HttpServletResponse error code (see {@link
   *     javax.servlet.http.HttpServletResponse} for possible error codes).
   * @param e the Exception that is reported.
   * @param ctx the ServletContext object.
   */
  public void sendError(
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext ctx,
      int code,
      Exception e) {
    Boolean devModeOverride = FilterDispatcher.getDevModeOverride();
    if (devModeOverride != null ? devModeOverride : devMode) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Exception occurred during processing request: #0", e, e.getMessage());
      }
      try {
        FreemarkerManager mgr = getContainer().getInstance(FreemarkerManager.class);

        freemarker.template.Configuration config = mgr.getConfiguration(ctx);
        Template template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl");

        List<Throwable> chain = new ArrayList<Throwable>();
        Throwable cur = e;
        chain.add(cur);
        while ((cur = cur.getCause()) != null) {
          chain.add(cur);
        }

        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("exception", e);
        data.put("unknown", Location.UNKNOWN);
        data.put("chain", chain);
        data.put("locator", new Locator());

        Writer writer = new StringWriter();
        template.process(data, writer);

        response.setContentType("text/html");
        response.getWriter().write(writer.toString());
        response.getWriter().close();
      } catch (Exception exp) {
        try {
          if (LOG.isDebugEnabled()) {
            LOG.debug("Cannot show problem report!", exp);
          }
          response.sendError(
              code,
              "Unable to show problem report:\n" + exp + "\n\n" + LocationUtils.getLocation(exp));
        } catch (IOException ex) {
          // we're already sending an error, not much else we can do if more stuff breaks
        }
      }
    } else {
      try {
        if (LOG.isErrorEnabled()) {
          LOG.error("Exception occurred during processing request: #0", e, e.getMessage());
        }
        // WW-1977: Only put errors in the request when code is a 500 error
        if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
          // send a http error response to use the servlet defined error handler
          // make the exception availible to the web.xml defined error page
          request.setAttribute("javax.servlet.error.exception", e);

          // for compatibility
          request.setAttribute("javax.servlet.jsp.jspException", e);
        }

        // send the error response
        response.sendError(code, e.getMessage());
      } catch (IOException e1) {
        // we're already sending an error, not much else we can do if more stuff breaks
      }
    }
  }