Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  * ====================================================================
  20.  *
  21.  * This software consists of voluntary contributions made by many
  22.  * individuals on behalf of the Apache Software Foundation.  For more
  23.  * information on the Apache Software Foundation, please see
  24.  * <http://www.apache.org/>.
  25.  *
  26.  */
  27.  
  28. package org.apache.http.examples.client;
  29.  
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.util.zip.GZIPInputStream;
  33.  
  34. import org.apache.http.Header;
  35. import org.apache.http.HeaderElement;
  36. import org.apache.http.HttpEntity;
  37. import org.apache.http.HttpException;
  38. import org.apache.http.HttpRequest;
  39. import org.apache.http.HttpRequestInterceptor;
  40. import org.apache.http.HttpResponse;
  41. import org.apache.http.HttpResponseInterceptor;
  42. import org.apache.http.client.methods.HttpGet;
  43. import org.apache.http.entity.HttpEntityWrapper;
  44. import org.apache.http.impl.client.DefaultHttpClient;
  45. import org.apache.http.protocol.HttpContext;
  46. import org.apache.http.util.EntityUtils;
  47.  
  48. /**
  49.  * Demonstration of the use of protocol interceptors to transparently
  50.  * modify properties of HTTP messages sent / received by the HTTP client.
  51.  * <p/>
  52.  * In this particular case HTTP client is made capable of transparent content
  53.  * GZIP compression by adding two protocol interceptors: a request interceptor
  54.  * that adds 'Accept-Encoding: gzip' header to all outgoing requests and
  55.  * a response interceptor that automatically expands compressed response
  56.  * entities by wrapping them with a uncompressing decorator class. The use of
  57.  * protocol interceptors makes content compression completely transparent to
  58.  * the consumer of the {@link org.apache.http.client.HttpClient HttpClient}
  59.  * interface.
  60.  */
  61. public class ClientGZipContentCompression {
  62.  
  63.     public final static void main(String[] args) throws Exception {
  64.         DefaultHttpClient httpclient = new DefaultHttpClient();
  65.  
  66.         httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
  67.            
  68.             public void process(
  69.                     final HttpRequest request,
  70.                     final HttpContext context) throws HttpException, IOException {
  71.                 if (!request.containsHeader("Accept-Encoding")) {
  72.                     request.addHeader("Accept-Encoding", "gzip");
  73.                 }
  74.             }
  75.  
  76.         });
  77.        
  78.         httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
  79.            
  80.             public void process(
  81.                     final HttpResponse response,
  82.                     final HttpContext context) throws HttpException, IOException {
  83.                 HttpEntity entity = response.getEntity();
  84.                 Header ceheader = entity.getContentEncoding();
  85.                 if (ceheader != null) {
  86.                     HeaderElement[] codecs = ceheader.getElements();
  87.                     for (int i = 0; i < codecs.length; i++) {
  88.                         if (codecs[i].getName().equalsIgnoreCase("gzip")) {
  89.                             response.setEntity(
  90.                                     new GzipDecompressingEntity(response.getEntity()));
  91.                             return;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.            
  97.         });
  98.        
  99.         HttpGet httpget = new HttpGet("http://www.apache.org/");
  100.        
  101.         // Execute HTTP request
  102.         System.out.println("executing request " + httpget.getURI());
  103.         HttpResponse response = httpclient.execute(httpget);
  104.  
  105.         System.out.println("----------------------------------------");
  106.         System.out.println(response.getStatusLine());
  107.         System.out.println(response.getLastHeader("Content-Encoding"));
  108.         System.out.println(response.getLastHeader("Content-Length"));
  109.         System.out.println("----------------------------------------");
  110.  
  111.         HttpEntity entity = response.getEntity();
  112.        
  113.         if (entity != null) {
  114.             String content = EntityUtils.toString(entity);
  115.             System.out.println(content);
  116.             System.out.println("----------------------------------------");
  117.             System.out.println("Uncompressed size: "+content.length());
  118.         }
  119.  
  120.         // When HttpClient instance is no longer needed,
  121.         // shut down the connection manager to ensure
  122.         // immediate deallocation of all system resources
  123.         httpclient.getConnectionManager().shutdown();        
  124.     }
  125.  
  126.     static class GzipDecompressingEntity extends HttpEntityWrapper {
  127.  
  128.         public GzipDecompressingEntity(final HttpEntity entity) {
  129.             super(entity);
  130.         }
  131.    
  132.         @Override
  133.         public InputStream getContent()
  134.             throws IOException, IllegalStateException {
  135.  
  136.             // the wrapped entity's getContent() decides about repeatability
  137.             InputStream wrappedin = wrappedEntity.getContent();
  138.  
  139.             return new GZIPInputStream(wrappedin);
  140.         }
  141.  
  142.         @Override
  143.         public long getContentLength() {
  144.             // length of ungzipped content is not known
  145.             return -1;
  146.         }
  147.  
  148.     }
  149.    
  150. }
  151.  
  152.