Пример #1
0
  @Override
  protected void processFilter(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws Exception {

    request.setAttribute(SKIP_FILTER, Boolean.TRUE);

    String key = getCacheKey(request);

    long companyId = PortalInstances.getCompanyId(request);

    CacheResponseData cacheResponseData = CacheUtil.getCacheResponseData(companyId, key);

    if (cacheResponseData == null) {
      if (!isCacheableData(companyId, request)) {
        if (_log.isDebugEnabled()) {
          _log.debug("Request is not cacheable " + key);
        }

        processFilter(CacheFilter.class, request, response, filterChain);

        return;
      }

      if (_log.isInfoEnabled()) {
        _log.info("Caching request " + key);
      }

      BufferCacheServletResponse bufferCacheServletResponse =
          new BufferCacheServletResponse(response);

      processFilter(CacheFilter.class, request, bufferCacheServletResponse, filterChain);

      cacheResponseData = new CacheResponseData(bufferCacheServletResponse);

      LastPath lastPath = (LastPath) request.getAttribute(WebKeys.LAST_PATH);

      if (lastPath != null) {
        cacheResponseData.setAttribute(WebKeys.LAST_PATH, lastPath);
      }

      // Cache the result if and only if there is a result and the request
      // is cacheable. We have to test the cacheability of a request twice
      // because the user could have been authenticated after the initial
      // test.

      String cacheControl =
          GetterUtil.getString(bufferCacheServletResponse.getHeader(HttpHeaders.CACHE_CONTROL));

      if ((bufferCacheServletResponse.getStatus() == HttpServletResponse.SC_OK)
          && !cacheControl.contains(HttpHeaders.PRAGMA_NO_CACHE_VALUE)
          && isCacheableRequest(request)
          && isCacheableResponse(bufferCacheServletResponse)) {

        CacheUtil.putCacheResponseData(companyId, key, cacheResponseData);
      }
    } else {
      LastPath lastPath = (LastPath) cacheResponseData.getAttribute(WebKeys.LAST_PATH);

      if (lastPath != null) {
        HttpSession session = request.getSession();

        session.setAttribute(WebKeys.LAST_PATH, lastPath);
      }
    }

    CacheResponseUtil.write(response, cacheResponseData);
  }