Ejemplo n.º 1
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);
      }
    }
  }
Ejemplo n.º 2
0
  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));
  }