Beispiel #1
0
  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");
    }
  }
  public void actions(Item item, int uriStart, Request request, Response response)
      throws ActionHandlerException {

    int i = uriStart;
    int l = request.getRequestParts().length;
    Action action = null;
    Item lastItem = item;
    String urlName = "index";
    for (; i < l; i++) {
      urlName = request.getRequestParts()[i];
      logger.debug("Url name is " + urlName);

      lastItem = item;
      action = null;

      if (item instanceof Actionable) {
        for (Action a : ((Actionable) item).getActions()) {
          logger.debug("Action is " + a);
          if (a.getUrlName().equals(urlName)) {
            action = a;
            break;
          }
        }
        item = (Item) action;
      } else {
        i++;
        break;
      }

      if (action == null) {
        logger.debug("Action was null, breaking");
        i++;
        break;
      }
    }

    logger.debug("[Action method] " + urlName + " -> " + action + "/" + lastItem);

    if (action != null) {
      /* Last sub space was an action, do its index method */
      logger.debug("Action was NOT null");
      executeThing(request, response, action, "index");
    } else {
      // i:3  l:3
      // if( i == l - 1 ) {
      if (i == l) {
        /* We came to an end */
        logger.debug("Action was null");
        executeThing(request, response, lastItem, urlName);

      } else {
        throw new ActionHandlerException(urlName + " not defined for " + lastItem);
      }
    }
  }
  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? */
    }
  }
  private void executeMethod(Item item, Request request, Response response, String actionMethod)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException,
          NoSuchJsonElementException {
    logger.debug("METHOD: " + item + ", " + actionMethod);

    Method method = getRequestMethod(item, actionMethod, request.isRequestPost());
    logger.debug("FOUND METHOD: " + method);

    if (request.isRequestPost()) {
      JsonObject json = null;
      try {
        json = JsonUtils.getJsonFromRequest(request);
      } catch (Exception e) {
        logger.debug(e.getMessage());
      }
      method.invoke(item, request, response, json);
    } else {
      method.invoke(item, request, response);
    }
  }
  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);
      }
    }
  }
 public Renderer(Request request) {
   this.theme = request.getTheme();
   // this.locale = request.getLocale();
   this.context = request.getContext();
   this.db = request.getDB();
 }
Beispiel #7
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));
  }