Exemple #1
0
 public synchronized void debug(Throwable t) {
   TeaStackTraceLine[] lines = getTeaStackTraceLines(t);
   if (lines == null) {
     super.debug(t);
   } else {
     if (isEnabled() && isDebugEnabled()) {
       dispatchLogTeaStackTrace(new TeaLogEvent(this, LogEvent.DEBUG_TYPE, lines));
     }
   }
 }
Exemple #2
0
  /**
   * Creates a transaction from the provided request and response and then processes that
   * transaction by executing the target template.
   *
   * @param request the user's http request
   * @param response the user's http response
   */
  private boolean processTemplate(ApplicationRequest appRequest, ApplicationResponse appResponse)
      throws IOException {

    // check if redirect or erroring out
    if (appResponse.isRedirectOrError()) {
      return false;
    }

    // set initial content type and helper attributes
    appResponse.setContentType("text/html");
    appRequest.setAttribute(this.getClass().getName(), this);

    // lookup template
    Template template = (Template) appRequest.getTemplate();

    // process as resource if no template available
    if (template == null) {
      if (!processResource(appRequest, appResponse)) {
        appResponse.sendError(404);
        return false;
      }
      return true;
    }

    long endTime = 0L;
    long startTime = 0L;
    long contentLength = 0;

    TemplateStats templateStats = null;
    if (mInstrumentationEnabled) {
      templateStats = mTeaServletRequestStats.getStats(template.getName());
    }

    try {
      Object[] params = null;
      try {
        if (templateStats != null) {
          templateStats.incrementServicing();
        }

        // Fill in the parameters to pass to the template.
        Class<?>[] paramTypes = template.getParameterTypes();
        if (paramTypes.length == 0) {
          params = NO_PARAMS;
        } else {
          params = new Object[paramTypes.length];
          String[] paramNames = template.getParameterNames();
          for (int i = 0; i < paramNames.length; i++) {
            String paramName = paramNames[i];
            if (paramName == null) {
              continue;
            }

            Class<?> paramType = paramTypes[i];

            if (!paramType.isArray()) {
              String value = appRequest.getParameter(paramName);
              if (value == null || paramType == String.class) {
                params[i] = value;
              } else {
                params[i] = convertParameter(value, paramType);
              }
            } else {
              String[] values = appRequest.getParameterValues(paramName);
              if (values == null || paramType == String[].class) {
                params[i] = values;
              } else {
                paramType = paramType.getComponentType();
                Object converted = Array.newInstance(paramType, values.length);
                params[i] = converted;
                for (int j = 0; j < values.length; j++) {
                  Array.set(converted, j, convertParameter(values[j], paramType));
                }
              }
            }
          }
        }

        startTime = System.currentTimeMillis();
        try {
          try {
            appRequest.getTemplate().execute(appResponse.getHttpContext(), params);
          } catch (ContextCreationException cce) {
            // unwrap the inner exception
            throw (Exception) cce.getUndeclaredThrowable();
          }
        } catch (AbortTemplateException e) {
          if (DEBUG) {
            mLog.debug("Template execution aborted!");
            mLog.debug(e);
          }
        } catch (RuntimeException e) {
          if (getEngine().getTemplateSource().isExceptionGuardianEnabled()) {
            // Just log the error and use what the template wrote out.
            mLog.error(e);
          } else {
            throw new ServletException(e);
          }
        } catch (IOException e) {
          // TODO: shouldn't we be throwing this as a ServletException?
          //       otherwise its not logged to the TeaLog.
          throw e;
        } catch (ServletException e) {
          throw e;
        } catch (Exception e) {
          throw new ServletException(e);
        }
        // TODO: shouldn't we be catching errors and not just exceptions?
        //       otherwise its not logged to the TeaLog.
        finally {
          endTime = System.currentTimeMillis();
          if (appRequest instanceof TeaServletStats) {
            long duration = endTime - startTime;
            ((TeaServletStats) appRequest).setTemplateDuration(duration);
          }
        }

        if (DEBUG) {
          mLog.debug("Finished executing template");
        }
      } catch (ServletException e) {
        // Log exception
        StringBuffer msg = new StringBuffer();
        msg.append("Error processing request for ");
        msg.append(appRequest.getRequestURI());
        if (appRequest.getQueryString() != null) {
          msg.append('?');
          msg.append(appRequest.getQueryString());
        }
        mLog.error(msg.toString());

        Throwable t = e;
        while (t instanceof ServletException) {
          e = (ServletException) t;
          if (e.getRootCause() != null) {
            String message = e.getMessage();
            if (message != null && message.length() > 0) {
              mLog.error(message);
            }
            mLog.error(t = e.getRootCause());
          } else {
            mLog.error(e);
            break;
          }
        }

        // Internal server error unless header is already set
        if (!appResponse.isRedirectOrError()) {
          String displayMessage = e.getLocalizedMessage();
          if (displayMessage == null || displayMessage.length() == 0) {
            appResponse.sendError(ApplicationResponse.SC_INTERNAL_SERVER_ERROR);
          } else {
            appResponse.sendError(ApplicationResponse.SC_INTERNAL_SERVER_ERROR, displayMessage);
          }
        }
      }
      contentLength = appResponse.getResponseBuffer().getByteCount();
      appResponse.finish();
      if (templateStats != null) {
        templateStats.decrementServicing();
        templateStats.log(startTime, endTime, contentLength, params);
      }
    } catch (Exception e) {
      if (templateStats != null) {
        templateStats.decrementServicing();
      }
    }
    return true;
  }
Exemple #3
0
 TeaServletEngine getEngine() {
   if (mEngine == null) {
     mLog.debug("the engine aint there");
   }
   return mEngine;
 }