1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.etag;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.servlet.HttpHeaders;
19  import com.liferay.portal.kernel.servlet.StringServletResponse;
20  import com.liferay.portal.kernel.util.StringPool;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  /**
26   * <a href="ETagUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Shuyang Zhou
30   */
31  public class ETagUtil {
32  
33      public static boolean processETag(
34          HttpServletRequest request, HttpServletResponse response,
35          byte[] bytes) {
36  
37          return _processETag(
38              request, response, _hashCode(bytes, 0, bytes.length));
39      }
40  
41      public static boolean processETag(
42          HttpServletRequest request, HttpServletResponse response, byte[] bytes,
43          int length) {
44  
45          return _processETag(request, response, _hashCode(bytes, 0, length));
46      }
47  
48      public static boolean processETag(
49          HttpServletRequest request, HttpServletResponse response, byte[] bytes,
50          int offset, int length) {
51  
52          return _processETag(
53              request, response, _hashCode(bytes, offset, length));
54      }
55  
56      public static boolean processETag(
57          HttpServletRequest request, HttpServletResponse response, String s) {
58  
59          return _processETag(request, response, s.hashCode());
60      }
61  
62      public static boolean processETag(
63          HttpServletRequest request, HttpServletResponse response,
64          StringServletResponse stringResponse) {
65  
66          if (stringResponse.isCalledGetOutputStream()) {
67              UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
68                  stringResponse.getUnsyncByteArrayOutputStream();
69  
70              return processETag(
71                  request, response,
72                  unsyncByteArrayOutputStream.unsafeGetByteArray(),
73                  unsyncByteArrayOutputStream.size());
74          }
75          else {
76              return processETag(request, response, stringResponse.getString());
77          }
78      }
79  
80      private static int _hashCode(byte[] data, int offset, int length) {
81          int hashCode = 0;
82  
83          for (int i = 0; i < length; i++) {
84              hashCode = 31 * hashCode + data[offset++];
85          }
86  
87          return hashCode;
88      }
89  
90      private static boolean _processETag(
91          HttpServletRequest request, HttpServletResponse response,
92          int hashCode) {
93  
94          String eTag = StringPool.QUOTE.concat(
95              Integer.toHexString(hashCode)).concat(StringPool.QUOTE);
96  
97          response.setHeader(HttpHeaders.ETAG, eTag);
98  
99          String ifNoneMatch = request.getHeader(HttpHeaders.IF_NONE_MATCH);
100 
101         if (eTag.equals(ifNoneMatch)) {
102             response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
103             response.setContentLength(0);
104 
105             return true;
106         }
107         else {
108             return false;
109         }
110     }
111 
112 }