001 // Copyright 2009, 2010, 2012 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 org.apache.tapestry5.SymbolConstants; 018 import org.apache.tapestry5.internal.TapestryInternalUtils; 019 import org.apache.tapestry5.ioc.annotations.Symbol; 020 import org.apache.tapestry5.services.ResponseCompressionAnalyzer; 021 import org.apache.tapestry5.services.assets.CompressionAnalyzer; 022 023 import javax.servlet.http.HttpServletRequest; 024 import java.util.Collection; 025 026 public class ResponseCompressionAnalyzerImpl implements ResponseCompressionAnalyzer 027 { 028 private final HttpServletRequest request; 029 030 private final boolean gzipCompressionEnabled; 031 032 private final CompressionAnalyzer analyzer; 033 034 // configuration is left here for partial compatibility with end-user modules that contribute a value; they 035 // should contribute to CompressionAnalyzer instead. 036 public ResponseCompressionAnalyzerImpl(HttpServletRequest request, CompressionAnalyzer analyzer, @Deprecated 037 Collection<String> configuration, @Symbol(SymbolConstants.GZIP_COMPRESSION_ENABLED) 038 boolean gzipCompressionEnabled) 039 { 040 this.request = request; 041 this.analyzer = analyzer; 042 this.gzipCompressionEnabled = gzipCompressionEnabled; 043 } 044 045 public boolean isGZipSupported() 046 { 047 if (!gzipCompressionEnabled) 048 { 049 return false; 050 } 051 052 // TAP5-1880: 053 if (request.getProtocol() == "HTTP/1.0") 054 { 055 return false; 056 } 057 058 String supportedEncodings = request.getHeader("Accept-Encoding"); 059 060 if (supportedEncodings == null) 061 { 062 return false; 063 } 064 065 for (String encoding : TapestryInternalUtils.splitAtCommas(supportedEncodings)) 066 { 067 if (encoding.equalsIgnoreCase("gzip")) 068 { 069 return true; 070 } 071 } 072 073 return false; 074 } 075 076 public boolean isCompressable(String contentType) 077 { 078 return analyzer.isCompressable(contentType); 079 } 080 }