001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet.filters.etag;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.servlet.HttpHeaders;
019    import com.liferay.portal.kernel.servlet.StringServletResponse;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Shuyang Zhou
028     */
029    public class ETagUtil {
030    
031            public static boolean processETag(
032                    HttpServletRequest request, HttpServletResponse response,
033                    byte[] bytes) {
034    
035                    return _processETag(
036                            request, response, _hashCode(bytes, 0, bytes.length));
037            }
038    
039            public static boolean processETag(
040                    HttpServletRequest request, HttpServletResponse response, byte[] bytes,
041                    int length) {
042    
043                    return _processETag(request, response, _hashCode(bytes, 0, length));
044            }
045    
046            public static boolean processETag(
047                    HttpServletRequest request, HttpServletResponse response, byte[] bytes,
048                    int offset, int length) {
049    
050                    return _processETag(
051                            request, response, _hashCode(bytes, offset, length));
052            }
053    
054            public static boolean processETag(
055                    HttpServletRequest request, HttpServletResponse response, String s) {
056    
057                    return _processETag(request, response, s.hashCode());
058            }
059    
060            public static boolean processETag(
061                    HttpServletRequest request, HttpServletResponse response,
062                    StringServletResponse stringResponse) {
063    
064                    if (stringResponse.isCalledGetOutputStream()) {
065                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
066                                    stringResponse.getUnsyncByteArrayOutputStream();
067    
068                            return processETag(
069                                    request, response,
070                                    unsyncByteArrayOutputStream.unsafeGetByteArray(),
071                                    unsyncByteArrayOutputStream.size());
072                    }
073                    else {
074                            return processETag(request, response, stringResponse.getString());
075                    }
076            }
077    
078            private static int _hashCode(byte[] data, int offset, int length) {
079                    int hashCode = 0;
080    
081                    for (int i = 0; i < length; i++) {
082                            hashCode = 31 * hashCode + data[offset++];
083                    }
084    
085                    return hashCode;
086            }
087    
088            private static boolean _processETag(
089                    HttpServletRequest request, HttpServletResponse response,
090                    int hashCode) {
091    
092                    String eTag = StringPool.QUOTE.concat(
093                            Integer.toHexString(hashCode)).concat(StringPool.QUOTE);
094    
095                    response.setHeader(HttpHeaders.ETAG, eTag);
096    
097                    String ifNoneMatch = request.getHeader(HttpHeaders.IF_NONE_MATCH);
098    
099                    if (eTag.equals(ifNoneMatch)) {
100                            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
101                            response.setContentLength(0);
102    
103                            return true;
104                    }
105                    else {
106                            return false;
107                    }
108            }
109    
110    }