コード例 #1
0
ファイル: Rest.java プロジェクト: seventy-eight/web-core
  private void generateException(Request request, PrintWriter writer, Throwable e, String message) {
    logger.error("Generating error: {}", e.getMessage());
    try {
      VelocityContext vc = new VelocityContext();
      vc.put("stacktrace", e.getStackTrace());
      vc.put("message", message);

      Core core = (Core) request.getContext().get("core");

      org.seventyeight.web.model.Error error = new org.seventyeight.web.model.Error((Exception) e);

      request
          .getContext()
          .put(
              "content",
              core.getTemplateManager()
                  .getRenderer(request)
                  .setContext(vc)
                  .renderObject(error, "view.vm"));
      request.getContext().put("title", message);
      writer.print(
          core.getTemplateManager().getRenderer(request).render("org/seventyeight/web/main.vm"));
    } catch (Exception ec) {
      request.getContext().put("content", "Error while displaying exception");
    }
  }
コード例 #2
0
  private void handleItemType(ItemType type, Request request, Response response)
      throws ActionHandlerException {
    if (request.getRequestParts().length > 2) {
      String name = request.getRequestParts()[2];
      AbstractItem item = null;
      try {
        item = type.getItem(name, request.getDB());
      } catch (CouldNotLoadItemException e) {
        throw new ActionHandlerException(e);
      }

      /* Authorization */
      checkAuthorization(
          item, request.getUser(), Authorizer.Authorization.get(request.isRequestPost()));

      request.getContext().put("title", item.getDisplayName());

      if (item instanceof Actionable) {
        actions(item, 3, request, response);
      } else {
        if (request.getRequestParts().length > 2) {
          throw new ActionHandlerException("No such action, " + request.getRequestURI());
        } else {
          executeThing(request, response, item, "index");
        }
      }

    } else {
      /* TODO, what? */
    }
  }
コード例 #3
0
  private void executeThing(Request request, Response response, Item item, String urlName)
      throws ActionHandlerException {
    logger.debug("EXECUTE: " + item + ", " + urlName);

    /* First try to find a view, if not a POST */
    try {
      logger.debug("Item: " + item + " -> " + urlName);
      executeMethod(item, request, response, urlName);
      return;
    } catch (Exception e) {
      e.printStackTrace();
      logger.debug(item + " does not not have " + urlName + ", " + e.getMessage());
    }

    logger.debug("TRYING VIEW FILE");

    if (!request.isRequestPost()) {
      try {
        request
            .getContext()
            .put(
                "content",
                SeventyEight.getInstance()
                    .getTemplateManager()
                    .getRenderer(request)
                    .renderObject(item, urlName + ".vm"));
        response
            .getWriter()
            .print(
                SeventyEight.getInstance()
                    .getTemplateManager()
                    .getRenderer(request)
                    .render(request.getTemplate()));
        return;
      } catch (Exception e) {
        logger.debug("Unable to view " + urlName + " for " + item + ": " + e.getMessage());
        throw new ActionHandlerException(e);
      }
    }
  }
コード例 #4
0
 public Renderer(Request request) {
   this.theme = request.getTheme();
   // this.locale = request.getLocale();
   this.context = request.getContext();
   this.db = request.getDB();
 }
コード例 #5
0
ファイル: Rest.java プロジェクト: seventy-eight/web-core
  public void doRequest(HttpServletRequest rqs, HttpServletResponse rsp)
      throws ServletException, IOException {
    // PrintWriter out = response.getWriter();

    StopWatch sw = new StopWatch();
    sw.reset();

    sw.start(rqs.getRequestURI());

    Core core = (Core) getServletContext().getAttribute("core");

    logger.debug("Query  : {]", rqs.getQueryString());
    logger.debug("URI    : {}", rqs.getRequestURI());
    logger.debug("METHOD : {}", rqs.getMethod());
    logger.debug("CORE   : {}", core);

    /* Instantiating request and response */
    Request request = new Request(rqs, core.getDefaultTemplate());
    request.setLocaleFromCookie("language");

    Response response = new Response(rsp);
    response.setCharacterEncoding("UTF-8");

    request.setStopWatch(sw);

    logger.debug("[Parameters] {}", rqs.getParameterMap());

    /* Instantiating context */
    VelocityContext vc = new VelocityContext();
    vc.put("title", "");

    request.setContext(vc);
    request.getContext().put("request", request);
    request.setRequestParts(rqs.getRequestURI().split("/"));
    logger.debug("------ {} -----", Arrays.asList(request.getRequestParts()));

    vc.put("currentUrl", rqs.getRequestURI());

    request.setUser(core.getAnonymousUser());
    request.setTheme(core.getDefaultTheme());

    request.getStopWatch().stop(rqs.getRequestURI());

    if (request.getRequestParts().length > 0
        && request.getRequestParts()[0].equalsIgnoreCase("static")) {
      try {
        ((Autonomous) core.getRoot().getChild("static")).autonomize(request, response);
      } catch (Throwable e) {
        throw new ServletException(e);
      }
    } else {
      request.getStopWatch().start("Authentication");

      logger.debug("THE USER: {}", request.getUser());
      try {
        logger.debug("AUTHENTICATING");
        core.getAuthentication().authenticate(request, response);
      } catch (AuthenticationException e) {
        logger.warn("Unable to authenticate", e);
      }

      logger.debug("THE USER: {}", request.getUser());
      request.getStopWatch().stop("Getting user");

      request.getStopWatch().stop("Authentication");
      request.getStopWatch().start("Render page");

      try {
        // Render the page
        Runner runner = core.render(request);
        runner.injectContext(request);
        runner.run(response);
        request.getUser().setSeen();
      } catch (CoreException e) {
        e.printStackTrace();
        if (response.isRenderingMain()) {
          response.renderError(request, e);
        } else {
          response.sendError(e.getCode(), e.getMessage());
        }
      } catch (Throwable e) {
        logger.error("CAUGHT ERROR");
        e.printStackTrace();
        generateException(request, rsp.getWriter(), e, e.getMessage());
      }
    }

    sw.stop();
    logger.info("Request response: {}", response.getResponse());

    // Render the bottom
    /*
    if( response.isRenderingMain() ) {
        try {
            vc.put( "seconds", sw.getSeconds() );
            response.getWriter().print( core.getTemplateManager().getRenderer( request ).render( "org/seventyeight/web/bottomPage.vm" ) );
        } catch( TemplateException e ) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    */

    // logger.info( sw.print( 1000 ) );
    System.out.println(sw.print(10000));
  }