@Override
  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    request.setCharacterEncoding(httpEncoding);
    response.setCharacterEncoding(httpEncoding);

    String path = RequestUtils.getPathInfo(request);

    if (httpCache == false) {
      ResponseUtils.setBufferOff(response);
    }

    if (bypassRequestUrls != null && bypassRequestUrls.accept(request, path)) {
      chain.doFilter(request, response);
      return;
    }

    HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());

    if (corsRequestProcessor != null) {
      corsRequestProcessor.setHeaders(request, response);
      if (httpMethod == HttpMethod.OPTIONS) {
        response.setStatus(HttpServletResponse.SC_OK);
        return;
      }
    }

    RouteInfo route = router.lookup(request, path, httpMethod);
    request = fileUploadResolver.transform(request);
    RequestContext ctx = new RequestContext(request, response, path, httpMethod, route);

    try {
      if (route == null || route == RouteInfo.NOT_FOUND) {
        throw new ActionNotFoundException(path);
      }

      InterceptorChainImpl interceptorChain =
          new InterceptorChainImpl(WebConfig.getInterceptors(), ctx);
      interceptorChain.invoke();

      ResultInfo result = interceptorChain.getResult();
      if (result != null) {
        ResultHandler<Object> handler = resultHandlerResolver.lookup(result.getResultClass());
        handler.handle(ctx, result.getResultObject());
      }
    } catch (Exception e) {
      handleError(ctx, e);
    } finally {
      if (ctx != null) {
        ctx.destory();
      }
    }
  }
Example #2
0
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    if (null != request.getAttribute("FLAG")) {
      chain.doFilter(request, response);
      return;
    }
    request.setAttribute("FLAG", FLAG);

    String encoding = this.encoding;
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    String resourcePath = req.getServletPath();
    RequestContext requestContext =
        new RequestContext(config.getServletContext(), req, resourcePath);
    request.setAttribute("context", requestContext);

    for (Decorator decorator : decorators) {
      int returned = decorator.parseRequest(req, res, requestContext);
      switch (returned) {
        case Decorator.OK:
          // do nothing
          break;
        case Decorator.REDIRECT:
          res.sendRedirect(requestContext.getResourcePath());
          return;
        case Decorator.MOVED_PERMANENTLY:
          res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
          res.setHeader("Location", requestContext.getResourcePath());
          return;
        default:
          // do nothing
      }
    }

    if (pattern.matcher(requestContext.getResourcePath()).matches()) {
      WebContent content = new WebContent();
      if (requestContext.getResourcePath().startsWith("/WEB-INF/admin/")) {
        encoding = "UTF-8";
      }
      ByteArrayResponseWrapper wrapper = new ByteArrayResponseWrapper(res);
      request.getRequestDispatcher(requestContext.getResourcePath()).include(req, wrapper);
      wrapper.finish();
      content.setWholeContent(wrapper.getString(encoding));
      response.setContentType(wrapper.getContentType());
      content.finish();

      for (Decorator decorator : decorators) {
        decorator.decorate(requestContext, content, content);
        content.finish();
      }
      byte[] theContent = content.toString().getBytes(encoding);
      response.setContentLength(theContent.length);
      response.getOutputStream().write(theContent);
      return;
    }
    request.getRequestDispatcher(requestContext.getResourcePath()).forward(req, res);
  }
  private void handleError(RequestContext ctx, Exception e) throws IOException, ServletException {
    ctx.getRequest().setAttribute(ExceptionHandler.KEY_IN_REQUEST, e);

    if (exceptionHandler != null) {
      try {
        exceptionHandler.handleError(ctx, e);
        return;
      } catch (Exception ex) {
        e = ex;
      }
    }

    if (e instanceof RuntimeException) {
      throw (RuntimeException) e;
    }
    if (e instanceof IOException) {
      throw (IOException) e;
    }
    if (e instanceof ServletException) {
      throw (ServletException) e;
    }

    throw new ServletException(e);
  }