001    // Copyright 2009, 2010 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import java.util.Collection;
018    
019    import javax.servlet.http.HttpServletRequest;
020    
021    import org.apache.tapestry5.SymbolConstants;
022    import org.apache.tapestry5.internal.TapestryInternalUtils;
023    import org.apache.tapestry5.ioc.annotations.Symbol;
024    import org.apache.tapestry5.services.ResponseCompressionAnalyzer;
025    import org.apache.tapestry5.services.assets.CompressionAnalyzer;
026    
027    public class ResponseCompressionAnalyzerImpl implements ResponseCompressionAnalyzer
028    {
029        private final HttpServletRequest request;
030    
031        private final boolean gzipCompressionEnabled;
032    
033        private final CompressionAnalyzer analyzer;
034    
035        public ResponseCompressionAnalyzerImpl(HttpServletRequest request, CompressionAnalyzer analyzer, @Deprecated
036        Collection<String> configuration, @Symbol(SymbolConstants.GZIP_COMPRESSION_ENABLED)
037        boolean gzipCompressionEnabled)
038        {
039            this.request = request;
040            this.analyzer = analyzer;
041            this.gzipCompressionEnabled = gzipCompressionEnabled;
042        }
043    
044        public boolean isGZipSupported()
045        {
046            if (!gzipCompressionEnabled)
047                return false;
048    
049            String supportedEncodings = request.getHeader("Accept-Encoding");
050    
051            if (supportedEncodings == null)
052                return false;
053    
054            for (String encoding : TapestryInternalUtils.splitAtCommas(supportedEncodings))
055            {
056                if (encoding.equalsIgnoreCase("gzip"))
057                    return true;
058            }
059    
060            return false;
061        }
062    
063        public boolean isCompressable(String contentType)
064        {
065            return analyzer.isCompressable(contentType);
066        }
067    }