001    /*
002     * Copyright 2003-2008 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     *
016     * You are receiving this code free of charge, which represents many hours of
017     * effort from other individuals and corporations.  As a responsible member 
018     * of the community, you are asked (but not required) to donate any 
019     * enhancements or improvements back to the community under a similar open 
020     * source license.  Thank you. -TMN
021     */
022    package groovyx.net.http;
023    
024    import groovyx.net.http.ContentEncoding.Type;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    import org.apache.http.client.HttpClient;
030    import org.apache.http.impl.client.AbstractHttpClient;
031    
032    /**
033     * Keeps track of available content-encoding handlers.
034     * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
035     */
036    public class ContentEncodingRegistry {
037    
038            protected Map<String,ContentEncoding> availableEncoders = getDefaultEncoders();
039            
040            /**
041             * This implementation adds a {@link GZIPEncoding} and {@link DeflateEncoding}
042             * handler to the registry.  Override this method to provide a different set 
043             * of defaults. 
044             * @return a map to content-encoding strings to {@link ContentEncoding} handlers.
045             */
046            protected Map<String,ContentEncoding> getDefaultEncoders() {
047                    Map<String, ContentEncoding> map = new HashMap<String, ContentEncoding>();
048                    map.put( Type.GZIP.toString(), new GZIPEncoding() );
049                    map.put( Type.DEFLATE.toString(), new DeflateEncoding() );
050                    return map;
051            }       
052    
053            /**
054             * Add the request and response interceptors to the {@link HttpClient}, 
055             * which will provide transparent decoding of the given content-encoding 
056             * types.  This method is called by HTTPBuilder and probably should not need 
057             * be modified by sub-classes.
058             * @param client client on which to set the request and response interceptors
059             * @param encodings encoding name (either a {@link ContentEncoding.Type} or 
060             *   a <code>content-encoding</code> string.
061             */
062            void setInterceptors( final AbstractHttpClient client, Object... encodings ) {
063                    // remove any encoding interceptors that are already set
064                    client.removeRequestInterceptorByClass( ContentEncoding.RequestInterceptor.class );
065                    client.removeResponseInterceptorByClass( ContentEncoding.ResponseInterceptor.class );
066                    
067                    for ( Object encName : encodings ) {
068                            ContentEncoding enc = availableEncoders.get( encName.toString() );
069                            if ( enc == null ) continue;
070                            client.addRequestInterceptor( enc.getRequestInterceptor() );
071                            client.addResponseInterceptor( enc.getResponseInterceptor() );
072                    }
073            }
074    }