Coverage Report - groovyx.net.http.ContentEncodingRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentEncodingRegistry
100%
14/14
75%
3/4
0
 
 1  
 /*
 2  
  * Copyright 2003-2008 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * You are receiving this code free of charge, which represents many hours of
 17  
  * effort from other individuals and corporations.  As a responsible member 
 18  
  * of the community, you are asked (but not required) to donate any 
 19  
  * enhancements or improvements back to the community under a similar open 
 20  
  * source license.  Thank you. -TMN
 21  
  */
 22  
 package groovyx.net.http;
 23  
 
 24  
 import groovyx.net.http.ContentEncoding.Type;
 25  
 
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.http.impl.client.AbstractHttpClient;
 30  
 
 31  
 import sun.net.www.http.HttpClient;
 32  
 
 33  
 
 34  
 /**
 35  
  * Keeps track of available content-encoding handlers.
 36  
  * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
 37  
  */
 38  15
 public class ContentEncodingRegistry {
 39  
 
 40  15
         protected Map<String,ContentEncoding> availableEncoders = getDefaultEncoders();
 41  
         
 42  
         /**
 43  
          * This implementation adds a {@link GZIPEncoding} and {@link DeflateEncoding}
 44  
          * handler to the registry.  Override this method to provide a different set 
 45  
          * of defaults. 
 46  
          * @return a map to content-encoding strings to {@link ContentEncoding} handlers.
 47  
          */
 48  
         protected Map<String,ContentEncoding> getDefaultEncoders() {
 49  15
                 Map<String, ContentEncoding> map = new HashMap<String, ContentEncoding>();
 50  15
                 map.put( Type.GZIP.toString(), new GZIPEncoding() );
 51  15
                 map.put( Type.DEFLATE.toString(), new DeflateEncoding() );
 52  15
                 return map;
 53  
         }        
 54  
 
 55  
         /**
 56  
          * Add the request and response interceptors to the {@link HttpClient}, 
 57  
          * which will provide transparent decoding of the given content-encoding 
 58  
          * types.  This method is called by HTTPBuilder and probably should not need 
 59  
          * be modified by sub-classes.
 60  
          * @param client client on which to set the request and response interceptors
 61  
          * @param encodings encoding name (either a {@link ContentEncoding.Type} or 
 62  
          *   a <code>content-encoding</code> string.
 63  
          */
 64  
         void setInterceptors( final AbstractHttpClient client, Object... encodings ) {
 65  
                 // remove any encoding interceptors that are already set
 66  15
                 client.removeRequestInterceptorByClass( ContentEncoding.RequestInterceptor.class );
 67  15
                 client.removeResponseInterceptorByClass( ContentEncoding.ResponseInterceptor.class );
 68  
                 
 69  45
                 for ( Object encName : encodings ) {
 70  30
                         ContentEncoding enc = availableEncoders.get( encName.toString() );
 71  30
                         if ( enc == null ) continue;
 72  30
                         client.addRequestInterceptor( enc.getRequestInterceptor() );
 73  30
                         client.addResponseInterceptor( enc.getResponseInterceptor() );
 74  
                 }
 75  15
         }
 76  
 }