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