| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package groovyx.net.http; |
| 23 | |
|
| 24 | |
import java.io.IOException; |
| 25 | |
|
| 26 | |
import org.apache.http.Header; |
| 27 | |
import org.apache.http.HeaderElement; |
| 28 | |
import org.apache.http.HttpEntity; |
| 29 | |
import org.apache.http.HttpException; |
| 30 | |
import org.apache.http.HttpRequest; |
| 31 | |
import org.apache.http.HttpRequestInterceptor; |
| 32 | |
import org.apache.http.HttpResponse; |
| 33 | |
import org.apache.http.HttpResponseInterceptor; |
| 34 | |
import org.apache.http.protocol.HttpContext; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 10 | public abstract class ContentEncoding { |
| 41 | |
|
| 42 | |
public static final String ACCEPT_ENC_HDR = "Accept-Encoding"; |
| 43 | |
public static final String CONTENT_ENC_HDR = "Content-Encoding"; |
| 44 | |
|
| 45 | |
protected abstract String getContentEncoding(); |
| 46 | |
protected abstract HttpEntity wrapResponseEntity( HttpEntity raw ); |
| 47 | |
|
| 48 | |
public HttpRequestInterceptor getRequestInterceptor() { |
| 49 | 10 | return new RequestInterceptor(); |
| 50 | |
} |
| 51 | |
|
| 52 | |
public HttpResponseInterceptor getResponseInterceptor() { |
| 53 | 10 | return new ResponseInterceptor(); |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 4 | public static enum Type { |
| 60 | 1 | GZIP, |
| 61 | 1 | COMPRESS, |
| 62 | 1 | DEFLATE; |
| 63 | |
|
| 64 | |
|
| 65 | |
@Override public String toString() { |
| 66 | 40 | return this.name().toLowerCase(); |
| 67 | |
} |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 10 | protected class RequestInterceptor implements HttpRequestInterceptor { |
| 76 | |
public void process( final HttpRequest req, |
| 77 | |
final HttpContext context ) throws HttpException, IOException { |
| 78 | |
|
| 79 | |
|
| 80 | 10 | String encoding = getContentEncoding(); |
| 81 | 10 | if ( !req.containsHeader( ACCEPT_ENC_HDR ) ) |
| 82 | 5 | req.addHeader( ACCEPT_ENC_HDR, encoding ); |
| 83 | |
|
| 84 | |
else { |
| 85 | 5 | StringBuilder values = new StringBuilder(); |
| 86 | 10 | for ( Header h : req.getHeaders( ACCEPT_ENC_HDR ) ) |
| 87 | 5 | values.append( h.getValue() ).append( "," ); |
| 88 | |
|
| 89 | 5 | String encList = (!values.toString().contains( encoding )) ? values |
| 90 | |
.append( encoding ).toString() |
| 91 | |
: values.toString().substring( 0, values.lastIndexOf( "," ) ); |
| 92 | |
|
| 93 | 5 | req.setHeader( ACCEPT_ENC_HDR, encList ); |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | 10 | } |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 10 | protected class ResponseInterceptor implements HttpResponseInterceptor { |
| 106 | |
public void process( final HttpResponse response, final HttpContext context ) |
| 107 | |
throws HttpException, IOException { |
| 108 | |
|
| 109 | 10 | if ( hasEncoding( response, getContentEncoding() ) ) |
| 110 | 1 | response.setEntity( wrapResponseEntity( response.getEntity() ) ); |
| 111 | 10 | } |
| 112 | |
|
| 113 | |
protected boolean hasEncoding( final HttpResponse response, final String encoding ) { |
| 114 | 10 | HttpEntity entity = response.getEntity(); |
| 115 | 10 | Header ceheader = entity.getContentEncoding(); |
| 116 | 10 | if ( ceheader == null ) return false; |
| 117 | |
|
| 118 | 2 | HeaderElement[] codecs = ceheader.getElements(); |
| 119 | 3 | for ( int i = 0; i < codecs.length; i++ ) |
| 120 | 2 | if ( codecs[i].getName().equalsIgnoreCase( encoding ) ) |
| 121 | 1 | return true; |
| 122 | |
|
| 123 | 1 | return false; |
| 124 | |
} |
| 125 | |
} |
| 126 | |
} |