001    /**
002     * Copyright (c) 2000-2012 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.ByteBufferServletResponse;
018    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.servlet.filters.BasePortalFilter;
021    import com.liferay.util.servlet.filters.CacheResponseUtil;
022    
023    import javax.servlet.FilterChain;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Eduardo Lundgren
029     * @author Brian Wing Shun Chan
030     * @author Raymond Augé
031     * @author Shuyang Zhou
032     */
033    public class ETagFilter extends BasePortalFilter {
034    
035            @Override
036            public boolean isFilterEnabled(
037                    HttpServletRequest request, HttpServletResponse response) {
038    
039                    if (ParamUtil.getBoolean(request, _ETAG, true)) {
040                            return true;
041                    }
042                    else {
043                            return false;
044                    }
045            }
046    
047            @Override
048            protected void processFilter(
049                            HttpServletRequest request, HttpServletResponse response,
050                            FilterChain filterChain)
051                    throws Exception {
052    
053                    ByteBufferServletResponse byteBufferResponse =
054                            new ByteBufferServletResponse(response);
055    
056                    processFilter(
057                            ETagFilter.class, request, byteBufferResponse, filterChain);
058    
059                    if (!ETagUtil.processETag(request, response, byteBufferResponse)) {
060                            CacheResponseUtil.setHeaders(
061                                    response, byteBufferResponse.getHeaders());
062    
063                            ServletResponseUtil.write(
064                                    response, byteBufferResponse.getByteBuffer());
065                    }
066            }
067    
068            private static final String _ETAG = "etag";
069    
070    }