| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ContentEncodingRegistry |
|
| 0.0;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.client.HttpClient; | |
| 30 | import org.apache.http.impl.client.AbstractHttpClient; | |
| 31 | ||
| 32 | /** | |
| 33 | * Keeps track of available content-encoding handlers. | |
| 34 | * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a> | |
| 35 | */ | |
| 36 | 5 | public class ContentEncodingRegistry { |
| 37 | ||
| 38 | 5 | protected Map<String,ContentEncoding> availableEncoders = getDefaultEncoders(); |
| 39 | ||
| 40 | /** | |
| 41 | * This implementation adds a {@link GZIPEncoding} and {@link DeflateEncoding} | |
| 42 | * handler to the registry. Override this method to provide a different set | |
| 43 | * of defaults. | |
| 44 | * @return a map to content-encoding strings to {@link ContentEncoding} handlers. | |
| 45 | */ | |
| 46 | protected Map<String,ContentEncoding> getDefaultEncoders() { | |
| 47 | 5 | Map<String, ContentEncoding> map = new HashMap<String, ContentEncoding>(); |
| 48 | 5 | map.put( Type.GZIP.toString(), new GZIPEncoding() ); |
| 49 | 5 | map.put( Type.DEFLATE.toString(), new DeflateEncoding() ); |
| 50 | 5 | return map; |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * Add the request and response interceptors to the {@link HttpClient}, | |
| 55 | * which will provide transparent decoding of the given content-encoding | |
| 56 | * types. This method is called by HTTPBuilder and probably should not need | |
| 57 | * be modified by sub-classes. | |
| 58 | * @param client client on which to set the request and response interceptors | |
| 59 | * @param encodings encoding name (either a {@link ContentEncoding.Type} or | |
| 60 | * a <code>content-encoding</code> string. | |
| 61 | */ | |
| 62 | void setInterceptors( final AbstractHttpClient client, Object... encodings ) { | |
| 63 | // remove any encoding interceptors that are already set | |
| 64 | 5 | client.removeRequestInterceptorByClass( ContentEncoding.RequestInterceptor.class ); |
| 65 | 5 | client.removeResponseInterceptorByClass( ContentEncoding.ResponseInterceptor.class ); |
| 66 | ||
| 67 | 15 | for ( Object encName : encodings ) { |
| 68 | 10 | ContentEncoding enc = availableEncoders.get( encName.toString() ); |
| 69 | 10 | if ( enc == null ) continue; |
| 70 | 10 | client.addRequestInterceptor( enc.getRequestInterceptor() ); |
| 71 | 10 | client.addResponseInterceptor( enc.getResponseInterceptor() ); |
| 72 | } | |
| 73 | 5 | } |
| 74 | } |