Coverage Report - org.apache.tapestry.util.io.GzipUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
GzipUtil
89% 
100% 
4.667
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 package org.apache.tapestry.util.io;
 15  
 
 16  
 import org.apache.tapestry.web.WebRequest;
 17  
 
 18  
 
 19  
 /**
 20  
  * Encapsulates logic related to various gzip compression schemes and their rules
 21  
  * as they apply to different browsers.
 22  
  *
 23  
  * @author jkuhnert
 24  
  */
 25  
 public final class GzipUtil
 26  
 {
 27  
     private static final float MIN_IE_VERSION = 7.0f;
 28  
 
 29  
     private static final String MSIE_6_COMPATIBLE_STRING = "SV1";
 30  
 
 31  
     /* defeat instantiation */
 32  0
     private GzipUtil() { }
 33  
 
 34  
     /**
 35  
      * Determines if gzip compression is appropriate/possible based on the User Agent and 
 36  
      * other limiting factors. IE versions < 6.1 are known to not work with gzip compression reliably. 
 37  
      *
 38  
      * @return True, if this request can be served in gzip format. False otherwise.
 39  
      */
 40  
     public static boolean isGzipCapable(WebRequest request)
 41  
     {
 42  10
         String encoding = request.getHeader("Accept-Encoding");
 43  10
         if (encoding == null || encoding.indexOf("gzip") < 0)
 44  0
             return false;
 45  
 
 46  
         // Handle IE specific hacks
 47  
 
 48  10
         String userAgent = request.getHeader("User-Agent");
 49  10
         int ieIndex = (userAgent != null) ? userAgent.indexOf("MSIE") : -1;
 50  10
         if (ieIndex > -1) {
 51  
 
 52  5
             float version = -1;
 53  
 
 54  
             try {
 55  5
                 version = Float.parseFloat(userAgent.substring(ieIndex + 4, ieIndex + 8));
 56  5
             } catch (NumberFormatException nf) {nf.printStackTrace();}
 57  
 
 58  5
             if (version >= MIN_IE_VERSION)
 59  1
                 return true;
 60  
 
 61  4
             if (userAgent.indexOf(MSIE_6_COMPATIBLE_STRING) > -1)
 62  1
                 return true;
 63  
 
 64  
             // else false
 65  
 
 66  3
             return false;
 67  
         }
 68  
 
 69  5
         return true;
 70  
     }
 71  
 
 72  
     /**
 73  
      * Based on the given type of content, determines if compression is appropriate. The biggest
 74  
      * thing it does is make sure that image content isn't compressed as that kind of content
 75  
      * is already compressed fairly well.
 76  
      *
 77  
      * @param contentType
 78  
      *          The content type to check. (ie "text/javascript","text/html", etc..)
 79  
      *
 80  
      * @return True if compression is appropriate for the content specified, false otherwise.
 81  
      */
 82  
     public static boolean shouldCompressContentType(String contentType)
 83  
     {
 84  8
         if (contentType == null)
 85  1
             return false;
 86  
 
 87  7
         return contentType.indexOf("javascript") > -1
 88  
                || contentType.indexOf("css") > -1
 89  
                || contentType.indexOf("html") > -1
 90  
                || contentType.indexOf("text") > -1;
 91  
     }
 92  
 }