001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet.filters.etag;
016    
017    import com.liferay.portal.kernel.servlet.RestrictedByteBufferCacheServletResponse;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.servlet.filters.BasePortalFilter;
020    import com.liferay.portal.util.PropsValues;
021    
022    import java.nio.ByteBuffer;
023    
024    import javax.servlet.FilterChain;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Eduardo Lundgren
030     * @author Brian Wing Shun Chan
031     * @author Raymond Aug??
032     * @author Shuyang Zhou
033     */
034    public class ETagFilter extends BasePortalFilter {
035    
036            @Override
037            public boolean isFilterEnabled(
038                    HttpServletRequest request, HttpServletResponse response) {
039    
040                    if (ParamUtil.getBoolean(request, _ETAG, true)) {
041                            return true;
042                    }
043                    else {
044                            return false;
045                    }
046            }
047    
048            protected boolean isEligibleForEtag(int status) {
049                    if ((status >= HttpServletResponse.SC_OK) &&
050                            (status < HttpServletResponse.SC_MULTIPLE_CHOICES)) {
051    
052                            return true;
053                    }
054                    else {
055                            return false;
056                    }
057            }
058    
059            @Override
060            protected void processFilter(
061                            HttpServletRequest request, HttpServletResponse response,
062                            FilterChain filterChain)
063                    throws Exception {
064    
065                    RestrictedByteBufferCacheServletResponse
066                            restrictedByteBufferCacheServletResponse =
067                                    new RestrictedByteBufferCacheServletResponse(
068                                            response, PropsValues.ETAG_RESPONSE_SIZE_MAX);
069    
070                    processFilter(
071                            ETagFilter.class, request, restrictedByteBufferCacheServletResponse,
072                            filterChain);
073    
074                    if (!restrictedByteBufferCacheServletResponse.isOverflowed()) {
075                            ByteBuffer byteBuffer =
076                                    restrictedByteBufferCacheServletResponse.getByteBuffer();
077    
078                            if (!isEligibleForEtag(
079                                            restrictedByteBufferCacheServletResponse.getStatus()) ||
080                                    !ETagUtil.processETag(request, response, byteBuffer)) {
081    
082                                    restrictedByteBufferCacheServletResponse.finishResponse();
083    
084                                    restrictedByteBufferCacheServletResponse.flushCache();
085                            }
086                    }
087            }
088    
089            private static final String _ETAG = "etag";
090    
091    }