Coverage Report - org.apache.tapestry.util.io.GzipUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
GzipUtil
0%
0/19
0%
0/22
6.333
 
 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  
 public final class GzipUtil
 24  
 {
 25  
     private static final float MIN_IE_VERSION = 7.0f;
 26  
 
 27  
     private static final String MSIE_6_COMPATIBLE_STRING = "SV1";
 28  
 
 29  
     /* defeat instantiation */
 30  0
     private GzipUtil() { }
 31  
 
 32  
     /**
 33  
      * Determines if gzip compression is appropriate/possible based on the User Agent and 
 34  
      * other limiting factors. IE versions < 6.1 are known to not work with gzip compression reliably. 
 35  
      *
 36  
      * @param request The current web request to check the headers of.
 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  0
         String encoding = request.getHeader("Accept-Encoding");
 43  
 
 44  0
         if (encoding == null || encoding.indexOf("gzip") < 0)
 45  0
             return false;
 46  
 
 47  
         // Handle IE specific hacks
 48  
 
 49  0
         String userAgent = request.getHeader("User-Agent");
 50  0
         int ieIndex = (userAgent != null) ? userAgent.indexOf("MSIE") : -1;
 51  0
         if (ieIndex > -1)
 52  
         {
 53  0
             float version = -1;
 54  
 
 55  
             try
 56  
             {
 57  0
                 version = Float.parseFloat(userAgent.substring(ieIndex + 4, ieIndex + 8));
 58  0
             } catch (NumberFormatException nf) {nf.printStackTrace();}
 59  
 
 60  0
             if (version >= MIN_IE_VERSION)
 61  0
                 return true;
 62  
 
 63  0
             if (userAgent.indexOf(MSIE_6_COMPATIBLE_STRING) > -1)
 64  0
                 return true;
 65  
 
 66  
             // else false
 67  
 
 68  0
             return false;
 69  
         }
 70  
 
 71  0
         return true;
 72  
     }
 73  
 
 74  
     /**
 75  
      * Based on the given type of content, determines if compression is appropriate. The biggest
 76  
      * thing it does is make sure that image content isn't compressed as that kind of content
 77  
      * is already compressed fairly well.
 78  
      *
 79  
      * @param contentType
 80  
      *          The content type to check. (ie "text/javascript","text/html", etc..)
 81  
      *
 82  
      * @return True if compression is appropriate for the content specified, false otherwise.
 83  
      */
 84  
     public static boolean shouldCompressContentType(String contentType)
 85  
     {
 86  0
         if (contentType == null)
 87  0
             return false;
 88  
 
 89  0
         return contentType.indexOf("javascript") > -1
 90  
                || contentType.indexOf("css") > -1
 91  
                || contentType.indexOf("html") > -1
 92  
                || contentType.indexOf("text") > -1;
 93  
     }
 94  
 }