コード例 #1
0
  protected void doPoemRequest(Melati melati) throws Exception {
    ServletTemplateContext templateContext = melati.getServletTemplateContext();
    // If we have an multipart form, we use a different template context
    // which allows us to access the uploaded files as well as fields.
    // This used to be in prePoemSession, but the use case was pretty thin,
    // the main Adaptor is PoemFileFormDataAdaptor, which needs to be in session.
    String contentType = melati.getRequest().getHeader("content-type");
    if (contentType != null
        && contentType.length() >= 19
        && contentType.substring(0, 19).equalsIgnoreCase("multipart/form-data")) {
      templateContext = new MultipartTemplateContext(melati, templateContext);
    }

    templateContext.put("melati", melati);
    templateContext.put("ml", melati.getMarkupLanguage());

    String templateName = doTemplateRequest(melati, templateContext);

    // only expand a template if we have one (it could be a redirect)
    if (templateName != null) {
      templateName = addExtension(templateName);
      templateEngine.expandTemplate(melati.getWriter(), templateName, templateContext);
    }
  }
コード例 #2
0
  /**
   * Send an error message.
   *
   * <p>Single call to the templet loader giving purpose (error) and Exception class.
   *
   * <p>This will look in the purpose directory, the standard templet directory and the classpath,
   * in that order, for a templet. This can no longer fail with NotFoundException, as the Object
   * templet will always be found (or this is a broken installation).
   *
   * @param melati the {@link Melati}
   * @param e the {@link Exception} to report
   */
  public void error(Melati melati, Exception e) {
    melati.getResponse().setStatus(httpStatusCode(e));
    ServletTemplateContext templateContext = melati.getServletTemplateContext();
    // If this a DB error which has occurred prior to
    // the establishment of a template context
    if (templateContext == null) {
      super.error(melati, e);
    } else

    // has it been trapped already, if so, we don't need to relog it here
    if (!(e instanceof TrappedException)) {
      try {
        // log it
        e.printStackTrace(System.err);
        // and put it on the page
        MelatiWriter mw = melati.getWriter();
        // get rid of anything that has been written so far
        mw.reset();
        templateContext.put("melati", melati);
        templateContext.put("ml", melati.getMarkupLanguage());
        templateContext.put("object", e);
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        templateContext.put("error", sw);
        templateContext.put("sysAdminName", getSysAdminName());
        templateContext.put("sysAdminEmail", getSysAdminEmail());

        Template errorTemplate;
        errorTemplate =
            melati
                .getConfig()
                .getTempletLoader()
                .templet(
                    melati.getTemplateEngine(), melati.getMarkupLanguage(), "error", e.getClass());
        templateEngine.expandTemplate(mw, errorTemplate, templateContext);
        melati.write();
      } catch (Exception f) {
        System.err.println("Error finding/writing error template:");
        f.printStackTrace();
        super.error(melati, e);
      }
    }
  }