All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovyx.net.http.ContentEncoding Maven / Gradle / Ivy

Go to download

A builder-style HTTP client API, including authentication, and extensible handling of common content-types such as JSON and XML. It is built on top of Apache's HttpClient.

There is a newer version: 0.7.1
Show newest version
/*
 * Copyright 2008-2011 Thomas Nichols.  http://blog.thomnichols.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * You are receiving this code free of charge, which represents many hours of
 * effort from other individuals and corporations.  As a responsible member
 * of the community, you are encouraged (but not required) to donate any
 * enhancements or improvements back to the community under a similar open
 * source license.  Thank you. -TMN
 */
package groovyx.net.http;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.protocol.HttpContext;

/**
 * Base class for handing content-encoding.
 * @author Tom Nichols
 */
public abstract class ContentEncoding {

    public static final String ACCEPT_ENC_HDR = "Accept-Encoding";
    public static final String CONTENT_ENC_HDR = "Content-Encoding";

    protected abstract String getContentEncoding();
    protected abstract HttpEntity wrapResponseEntity( HttpEntity raw );

    public HttpRequestInterceptor getRequestInterceptor() {
        return new RequestInterceptor();
    }

    public HttpResponseInterceptor getResponseInterceptor() {
        return new ResponseInterceptor();
    }

    /**
     * Enumeration of common content-encodings.
     */
    public static enum Type {
        GZIP,
        COMPRESS,
        DEFLATE;

        /** Prints the value as it should appear in an HTTP header */
        @Override public String toString() {
            return this.name().toLowerCase();
        }
    }

    /**
     * Request interceptor that adds the correct Accept header
     * to the outgoing request.
     * @author Tom Nichols
     */
    protected class RequestInterceptor implements HttpRequestInterceptor {
        public void process( final HttpRequest req,
                final HttpContext context ) throws HttpException, IOException {

            // set the Accept-Encoding header:
            String encoding = getContentEncoding();
            if ( !req.containsHeader( ACCEPT_ENC_HDR ) )
                req.addHeader( ACCEPT_ENC_HDR, encoding );

            else {
                StringBuilder values = new StringBuilder();
                for ( Header h : req.getHeaders( ACCEPT_ENC_HDR ) )
                    values.append( h.getValue() ).append( "," );

                String encList = (!values.toString().contains( encoding )) ? values
                        .append( encoding ).toString()
                        : values.toString().substring( 0, values.lastIndexOf( "," ) );

                req.setHeader( ACCEPT_ENC_HDR, encList );
            }

            //TODO compress request and add content-encoding header.
        }
    }

    /**
     * Response interceptor that filters the response stream to decode the
     * compressed content before it is passed on to the parser.
     * @author Tom Nichols
     */
    protected class ResponseInterceptor implements HttpResponseInterceptor {
        public void process( final HttpResponse response, final HttpContext context )
                throws HttpException, IOException {

            if ( hasEncoding( response, getContentEncoding() ) )
                response.setEntity( wrapResponseEntity( response.getEntity() ) );
        }

        protected boolean hasEncoding( final HttpResponse response, final String encoding ) {
            HttpEntity entity = response.getEntity();
            if ( entity == null ) return false;
            Header ceHeader = entity.getContentEncoding();
            if ( ceHeader == null ) return false;

            HeaderElement[] codecs = ceHeader.getElements();
            for ( int i = 0; i < codecs.length; i++ )
                if ( encoding.equalsIgnoreCase( codecs[i].getName() ) )
                    return true;

            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy