Example #1
0
  /**
   * This is the main dispatching method, handling all requests. It parses request, extracts
   * controller and action information, invokes action and forwards to rendering the template.
   */
  public void handleRequest(HttpServletRequest req, HttpServletResponse res) {

    Context ctx = null;
    try {
      log.debug("about to handle request " + req.getServletPath());
      /* Convert to multipart request if it has multipart content (fileuploads). */
      if (HttpMultipartServletRequest.isMultipartContent(req)) {
        /* maxFileSize is defined in mega-bytes */
        int maxFileSize =
            1024 * 1024 * (new Integer(getInitParam(PARAM_MAXFILEUPLOADSIZE, "1")).intValue());
        HttpMultipartServletRequest mreq = new HttpMultipartServletRequest(req, maxFileSize, -1);
        req = mreq;
      }
      req.setCharacterEncoding(
          "UTF-8"); // must set this before getParameter() to get the correct encoding
      /* create a context for the controller */
      HttpSession session = req.getSession(true); // get session, create one if none exists
      Locale defaultLocale = computeDefaultLocale(supportedLanguages, req.getLocales());
      ctx =
          Context.createInstance(this, session, req, res, defaultLocale); // create context instance
      ctx.put(
          CONTEXT_KEY,
          ctx); // put context into itself - some tools may need a context passed to them
      Target target = handleAction(ctx, req.getServletPath(), ACTION_INVOCATION_BY_URL);
      if (target != null) {
        log.debug("about to go to target " + target);
        TestTimer targetTimer = new TestTimer("target");
        target.go();
        targetTimer.done();
        log.debug("returned from target");
      }
    } catch (Throwable e) { // catch all sorts of exceptions
      log.fatalException(e);
      try {
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      } catch (IOException e1) {
        log.fatalException(e);
      }
    } finally {
      Context.destroyInstance();
    }
  }
Example #2
0
  /**
   * Executes action derived from specified path.
   *
   * @see #handleAction(Context, String)
   * @param ctx A context
   * @param path A path to an action
   * @param invocationMode One of ACTION_INVOKATION_XXX - During execution, the specified value can
   *     be accessed in request context using the ACTION_INVOKATION_MODE_KEY. This may come handy
   *     for those who need to know if an action was invoked by an url or programatically
   * @return The target returned by the action or some error target in case of an error.
   * @throws IOException
   */
  protected Target handleAction(Context ctx, String path, String invocationMode)
      throws IOException {

    /* put invokation mode in request context */
    ctx.put(ACTION_INVOCATION_MODE_KEY, invocationMode);

    String[] ca = extractControllerAndActionNames(path);
    String ctrlName = ca[0];
    String actionName = ca[1];

    TestTimer timer = new TestTimer("execute");
    try {
      return executeAction(ctx, ctrlName, actionName);
    } catch (NoSuchActionException nme) {
      log.warn(nme.getMessage());
      ctx.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
      return null;
    } catch (HttpErrorException hee) {
      log.warn(
          "HttpErrorException "
              + hee.getStatusCode()
              + " :\""
              + ctrlName
              + "/"
              + actionName
              + "\"");
      ctx.getResponse().sendError(hee.getStatusCode());
      return null;
    } catch (Exception e) {
      String msg = "error doing action \"" + actionName + "\"";
      log.fatal(msg);
      log.fatalException(e);
      ctx.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return null;
    } finally {
      timer.done();
    }
  }