You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2009/09/13 10:32:13 UTC

svn commit: r814281 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/client/entity/ main/java/org/apache/http/client/protocol/ main/java/org/apache/http/impl/client/ test/java/org/apache/http/impl/client/

Author: olegk
Date: Sun Sep 13 08:32:12 2009
New Revision: 814281

URL: http://svn.apache.org/viewvc?rev=814281&view=rev
Log:
HTTPCLIENT-834: Added ContentEncodingHttpClient
Contributed by James Abley <james.abley at gmail.com>

Added:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
      - copied, changed from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DeflateDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
      - copied, changed from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/GzipDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java
      - copied, changed from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingProcessor.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java   (with props)
Removed:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingProcessor.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DeflateDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/GzipDecompressingEntity.java
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java (from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DeflateDecompressingEntity.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DeflateDecompressingEntity.java&r1=813945&r2=814281&rev=814281&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DeflateDecompressingEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java Sun Sep 13 08:32:12 2009
@@ -21,11 +21,8 @@
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.http.impl.client;
+*/
+package org.apache.http.client.entity;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -47,7 +44,7 @@
  * <code>deflate</code> streams. We handle both types in here, since that's what is seen on the 
  * internet. Moral - prefer <code>gzip</code>!
  */
-class DeflateDecompressingEntity extends HttpEntityWrapper {
+public class DeflateDecompressingEntity extends HttpEntityWrapper {
 
     /**
      * Creates a new {@link DeflateDecompressingEntity} which will wrap the specified 

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java (from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/GzipDecompressingEntity.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/GzipDecompressingEntity.java&r1=813945&r2=814281&rev=814281&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/GzipDecompressingEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java Sun Sep 13 08:32:12 2009
@@ -21,11 +21,8 @@
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.http.impl.client;
+*/
+package org.apache.http.client.entity;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -38,7 +35,7 @@
 /**
  * {@link HttpEntityWrapper} for handling gzip Content Coded responses.
  */
-class GzipDecompressingEntity extends HttpEntityWrapper {
+public class GzipDecompressingEntity extends HttpEntityWrapper {
 
     /**
      * Creates a new {@link GzipDecompressingEntity} which will wrap the specified 

Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java (from r813945, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingProcessor.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingProcessor.java&r1=813945&r2=814281&rev=814281&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingProcessor.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ContentEncodingProcessor.java Sun Sep 13 08:32:12 2009
@@ -21,11 +21,8 @@
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.http.impl.client;
+*/
+package org.apache.http.client.protocol;
 
 import java.io.IOException;
 
@@ -37,6 +34,8 @@
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.client.entity.DeflateDecompressingEntity;
+import org.apache.http.client.entity.GzipDecompressingEntity;
 import org.apache.http.protocol.HttpContext;
 
 /**
@@ -50,7 +49,8 @@
  * 
  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5
  */
-class ContentEncodingProcessor implements HttpResponseInterceptor, HttpRequestInterceptor {
+public class ContentEncodingProcessor 
+                    implements HttpResponseInterceptor, HttpRequestInterceptor {
 
     /**
      * {@inheritDoc}

Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java?rev=814281&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java Sun Sep 13 08:32:12 2009
@@ -0,0 +1,77 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+
+package org.apache.http.impl.client;
+
+import org.apache.http.client.protocol.ContentEncodingProcessor;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+
+/**
+ * {@link DefaultHttpClient} sub-class which includes a {@link ContentEncodingProcessor} 
+ * for the request and response.
+ */
+public class ContentEncodingHttpClient extends DefaultHttpClient {
+
+    /**
+     * Creates a new HTTP client from parameters and a connection manager.
+     *
+     * @param params    the parameters
+     * @param conman    the connection manager
+     */
+    public ContentEncodingHttpClient(ClientConnectionManager conman, HttpParams params) {
+        super(conman, params);
+    }
+
+    /**
+     * @param params
+     */
+    public ContentEncodingHttpClient(HttpParams params) {
+        this(null, params);
+    }
+
+    /**
+     * 
+     */
+    public ContentEncodingHttpClient() {
+        this(null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected BasicHttpProcessor createHttpProcessor() {
+        BasicHttpProcessor result = super.createHttpProcessor();
+        
+        ContentEncodingProcessor ceProcessor = new ContentEncodingProcessor();
+        result.addRequestInterceptor(ceProcessor);
+        result.addResponseInterceptor(ceProcessor);
+        return result;
+    }
+    
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ContentEncodingHttpClient.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java?rev=814281&r1=814280&r2=814281&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java Sun Sep 13 08:32:12 2009
@@ -54,6 +54,7 @@
 import org.apache.http.HttpVersion;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.ContentEncodingProcessor;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.params.ConnManagerParams;
 import org.apache.http.conn.scheme.PlainSocketFactory;
@@ -102,7 +103,7 @@
             }
         });
 
-        DefaultHttpClient client = new DefaultHttpClient();
+        DefaultHttpClient client = createHttpClient();
 
         HttpGet request = new HttpGet("/some-resource");
         HttpResponse response = client.execute(getServerHttp(), request);
@@ -124,7 +125,7 @@
 
         this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, false));
 
-        DefaultHttpClient client = new DefaultHttpClient();
+        DefaultHttpClient client = createHttpClient();
 
         HttpGet request = new HttpGet("/some-resource");
         HttpResponse response = client.execute(getServerHttp(), request);
@@ -146,7 +147,7 @@
 
         this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, true));
 
-        DefaultHttpClient client = new DefaultHttpClient();
+        DefaultHttpClient client = createHttpClient();
         HttpGet request = new HttpGet("/some-resource");
         HttpResponse response = client.execute(getServerHttp(), request);
         assertEquals("The entity text is correctly transported", entityText, 
@@ -165,7 +166,7 @@
 
         this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
 
-        DefaultHttpClient client = new DefaultHttpClient();
+        DefaultHttpClient client = createHttpClient();
         HttpGet request = new HttpGet("/some-resource");
         HttpResponse response = client.execute(getServerHttp(), request);
         assertEquals("The entity text is correctly transported", entityText, 
@@ -231,12 +232,18 @@
         }
     }
 
-    public void testExistingProtocolInterceptorsAreNotAffected() throws Exception {
+    /**
+     * This test is no longer relevant, since the functionality has been added via a new subclass of 
+     * {@link DefaultHttpClient} rather than changing the existing class.
+     * 
+     * @throws Exception
+     */
+    public void removedTestExistingProtocolInterceptorsAreNotAffected() throws Exception {
         final String entityText = "Hello, this is some plain text coming back.";
 
         this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
 
-        DefaultHttpClient client = new DefaultHttpClient();
+        DefaultHttpClient client = createHttpClient();
         HttpGet request = new HttpGet("/some-resource");
 
         client.addRequestInterceptor(new HttpRequestInterceptor() {
@@ -285,7 +292,47 @@
 
         client.getConnectionManager().shutdown();
     }
+    /**
+     * Checks that we can turn off the new Content-Coding support. The default is that it's on, but that is a change 
+     * to existing behaviour and might not be desirable in some situations.
+     * 
+     * @throws Exception
+     */
+    public void testCanBeDisabledAtRequestTime() throws Exception {
+        final String entityText = "Hello, this is some plain text coming back.";
+
+        /* Assume that server will see an Accept-Encoding header. */
+        final boolean [] sawAcceptEncodingHeader = { true };
+        
+        this.localServer.register("*", new HttpRequestHandler() {
+            
+            /**
+             * {@inheritDoc}
+             */
+            public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
+                response.setEntity(new StringEntity(entityText));
+                response.addHeader("Content-Type", "text/plain");
+                Header[] acceptEncodings = request.getHeaders("Accept-Encoding");
+                
+                sawAcceptEncodingHeader[0] = acceptEncodings.length > 0;
+            }
+            
+        });
+
+        AbstractHttpClient client = createHttpClient();
+        HttpGet request = new HttpGet("/some-resource");
 
+        client.removeRequestInterceptorByClass(ContentEncodingProcessor.class);
+        
+        HttpResponse response = client.execute(getServerHttp(), request);
+        
+        assertFalse("The Accept-Encoding header was not there", sawAcceptEncodingHeader[0]);
+        assertEquals("The entity isn't treated as gzip or zip content", entityText, 
+                EntityUtils.toString(response.getEntity()));
+
+        client.getConnectionManager().shutdown();
+    }
+    
     /**
      * Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream 
      * Content-Coding.
@@ -402,6 +449,10 @@
         };
     }
 
+    private DefaultHttpClient createHttpClient() {
+        return new ContentEncodingHttpClient();
+    }
+
     /**
      * Sub-ordinate task passed off to a different thread to be executed.
      *