1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.filters.etag;
16  
17  import com.liferay.portal.kernel.util.ParamUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.servlet.filters.BasePortalFilter;
20  import com.liferay.util.servlet.filters.CacheResponse;
21  import com.liferay.util.servlet.filters.CacheResponseData;
22  import com.liferay.util.servlet.filters.CacheResponseUtil;
23  
24  import javax.servlet.FilterChain;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  /**
29   * <a href="ETagFilter.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Eduardo Lundgren
32   * @author Brian Wing Shun Chan
33   * @author Raymond Augé
34   */
35  public class ETagFilter extends BasePortalFilter {
36  
37      protected void processFilter(
38              HttpServletRequest request, HttpServletResponse response,
39              FilterChain filterChain)
40          throws Exception {
41  
42          boolean etag = ParamUtil.getBoolean(request, _ETAG, true);
43  
44          if (etag) {
45              CacheResponse cacheResponse = new CacheResponse(
46                  response, StringPool.UTF8);
47  
48              processFilter(
49                  ETagFilter.class, request, cacheResponse, filterChain);
50  
51              CacheResponseData cacheResponseData = new CacheResponseData(
52                  cacheResponse.unsafeGetData(), cacheResponse.getContentLength(),
53                  cacheResponse.getContentType(), cacheResponse.getHeaders());
54  
55              if (!ETagUtil.processETag(
56                      request, response, cacheResponse.unsafeGetData(),
57                      cacheResponse.getContentLength())) {
58  
59                  CacheResponseUtil.write(response, cacheResponseData);
60              }
61          }
62          else {
63              processFilter(
64                  ETagFilter.class, request, response, filterChain);
65          }
66      }
67  
68      private static final String _ETAG = "etag";
69  
70  }