Example #1
0
 /** {@inheritDoc} */
 @Override
 public Response handleRequest(FreenetRequest request, Response response) throws IOException {
   if (webInterface.getCore().getPreferences().isRequireFullAccess()
       && !request.getToadletContext().isAllowedFullAccess()) {
     return response
         .setStatusCode(403)
         .setStatusText("Forbidden")
         .setContentType("application/json")
         .write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
   }
   if (needsFormPassword()) {
     String formPassword = request.getHttpRequest().getParam("formPassword");
     if (!webInterface.getFormPassword().equals(formPassword)) {
       return response
           .setStatusCode(403)
           .setStatusText("Forbidden")
           .setContentType("application/json")
           .write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
     }
   }
   if (requiresLogin()) {
     if (getCurrentSone(request.getToadletContext(), false) == null) {
       return response
           .setStatusCode(403)
           .setStatusText("Forbidden")
           .setContentType("application/json")
           .write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
     }
   }
   try {
     JsonReturnObject jsonObject = createJsonObject(request);
     return response
         .setStatusCode(200)
         .setStatusText("OK")
         .setContentType("application/json")
         .write(objectMapper.writeValueAsString(jsonObject));
   } catch (Exception e1) {
     logger.log(Level.WARNING, "Error executing JSON page!", e1);
     return response
         .setStatusCode(500)
         .setStatusText(e1.getMessage())
         .setContentType("text/plain")
         .write(dumpStackTrace(e1));
   }
 }
  /** {@inheritDoc} */
  @Override
  public Response handleRequest(FreenetRequest request, Response response) throws IOException {
    String redirectTarget = getRedirectTarget(request);
    if (redirectTarget != null) {
      return new RedirectResponse(redirectTarget);
    }

    if (isFullAccessOnly() && !request.getToadletContext().isAllowedFullAccess()) {
      return response
          .setStatusCode(401)
          .setStatusText("Not authorized")
          .setContentType("text/html");
    }
    ToadletContext toadletContext = request.getToadletContext();
    if (request.getMethod() == Method.POST) {
      /* require form password. */
      String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
      if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
        return new RedirectResponse(invalidFormPasswordRedirectTarget);
      }
    }
    PageMaker pageMaker = toadletContext.getPageMaker();
    PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
    for (String styleSheet : getStyleSheets()) {
      pageNode.addCustomStyleSheet(styleSheet);
    }
    for (Map<String, String> linkNodeParameters : getAdditionalLinkNodes(request)) {
      HTMLNode linkNode = pageNode.headNode.addChild("link");
      for (Entry<String, String> parameter : linkNodeParameters.entrySet()) {
        linkNode.addAttribute(parameter.getKey(), parameter.getValue());
      }
    }
    String shortcutIcon = getShortcutIcon();
    if (shortcutIcon != null) {
      pageNode.addForwardLink("icon", shortcutIcon);
    }

    TemplateContext templateContext = templateContextFactory.createTemplateContext();
    templateContext.mergeContext(template.getInitialContext());
    try {
      long start = System.nanoTime();
      processTemplate(request, templateContext);
      long finish = System.nanoTime();
      logger.log(
          Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
    } catch (RedirectException re1) {
      return new RedirectResponse(re1.getTarget());
    }

    StringWriter stringWriter = new StringWriter();
    template.render(templateContext, stringWriter);
    pageNode.content.addChild("%", stringWriter.toString());

    postProcess(request, templateContext);

    return response
        .setStatusCode(200)
        .setStatusText("OK")
        .setContentType("text/html")
        .write(pageNode.outer.generate());
  }