hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r832847 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/client/entity/ test/java/org/apache/http/impl/client/
Date Wed, 04 Nov 2009 19:16:49 GMT
Author: olegk
Date: Wed Nov  4 19:16:48 2009
New Revision: 832847

URL: http://svn.apache.org/viewvc?rev=832847&view=rev
Log:
HTTPCLIENT-834: GzipDecompressingEntity and DeflateDecompressingEntity classes now override
the#writeTo()
Contributed by James Abley <james.abley at gmail.com>

Added:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestContentCodings.java

Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java?rev=832847&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
Wed Nov  4 19:16:48 2009
@@ -0,0 +1,75 @@
+/*
+ * ====================================================================
+ *
+ *  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.client.entity;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.entity.HttpEntityWrapper;
+
+/**
+ * Common base class for decompressing {@link HttpEntity} implementations.
+ */
+abstract class DecompressingEntity extends HttpEntityWrapper {
+
+    /**
+     * Default buffer size.
+     */
+    private static final int BUFFER_SIZE = 1024 * 2;
+
+    /**
+     * Creates a new {@link DecompressingEntity}.
+     * 
+     * @param wrapped
+     *            the non-null {@link HttpEntity} to be wrapped
+     */
+    public DecompressingEntity(final HttpEntity wrapped) {
+        super(wrapped);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException {
+        if (outstream == null) {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+
+        InputStream instream = getContent();
+        
+        byte[] buffer = new byte[BUFFER_SIZE];
+        
+        int l;
+
+        while ((l = instream.read(buffer)) != -1) {
+            outstream.write(buffer, 0, l);
+        }
+    }
+
+}

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

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

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

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java?rev=832847&r1=832846&r2=832847&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
Wed Nov  4 19:16:48 2009
@@ -45,7 +45,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>!
  */
-public class DeflateDecompressingEntity extends HttpEntityWrapper {
+public class DeflateDecompressingEntity extends DecompressingEntity {
 
     /**
      * Creates a new {@link DeflateDecompressingEntity} which will wrap the specified 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java?rev=832847&r1=832846&r2=832847&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
Wed Nov  4 19:16:48 2009
@@ -22,7 +22,7 @@
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  *
-*/
+ */
 package org.apache.http.client.entity;
 
 import java.io.IOException;
@@ -36,7 +36,7 @@
 /**
  * {@link HttpEntityWrapper} for handling gzip Content Coded responses.
  */
-public class GzipDecompressingEntity extends HttpEntityWrapper {
+public class GzipDecompressingEntity extends DecompressingEntity {
 
     /**
      * Creates a new {@link GzipDecompressingEntity} which will wrap the specified 

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=832847&r1=832846&r2=832847&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
Wed Nov  4 19:16:48 2009
@@ -322,6 +322,52 @@
 
         client.getConnectionManager().shutdown();
     }
+
+    /**
+     * Test that the returned {@link HttpEntity} in the response correctly overrides 
+     * {@link HttpEntity#writeTo(OutputStream)} for gzip-encoding.
+     * 
+     * @throws Exception
+     */
+    public void testHttpEntityWriteToForGzip() throws Exception {
+        final String entityText = "Hello, this is some plain text coming back.";
+
+        this.localServer.register("*", createGzipEncodingRequestHandler(entityText));
+
+        DefaultHttpClient client = createHttpClient();
+        HttpGet request = new HttpGet("/some-resource");
+        HttpResponse response = client.execute(getServerHttp(), request);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        
+        response.getEntity().writeTo(out);
+        
+        assertEquals(entityText, out.toString("utf-8"));
+
+        client.getConnectionManager().shutdown();
+    }
+    
+    /**
+     * Test that the returned {@link HttpEntity} in the response correctly overrides 
+     * {@link HttpEntity#writeTo(OutputStream)} for deflate-encoding.
+     * 
+     * @throws Exception
+     */
+    public void testHttpEntityWriteToForDeflate() throws Exception {
+        final String entityText = "Hello, this is some plain text coming back.";
+
+        this.localServer.register("*", createDeflateEncodingRequestHandler(entityText, true));
+
+        DefaultHttpClient client = createHttpClient();
+        HttpGet request = new HttpGet("/some-resource");
+        HttpResponse response = client.execute(getServerHttp(), request);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        
+        response.getEntity().writeTo(out);
+        
+        assertEquals(entityText, out.toString("utf-8"));
+
+        client.getConnectionManager().shutdown();
+    }
     
     /**
      * Creates a new {@link HttpRequestHandler} that will attempt to provide a deflate stream




Mime
View raw message