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 2005/04/07 21:55:04 UTC

svn commit: r160447 - in jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity: ./ BufferedHttpEntity.java EntityConsumer.java

Author: olegk
Date: Thu Apr  7 12:55:03 2005
New Revision: 160447

URL: http://svn.apache.org/viewcvs?view=rev&rev=160447
Log:
Decorator class intended to transparently buffer enitiy content

Added:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java   (with props)
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java   (with props)

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java?view=auto&rev=160447
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java Thu Apr  7 12:55:03 2005
@@ -0,0 +1,93 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  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.
+ * ====================================================================
+ *
+ * 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.entity;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.http.HttpEntity;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class BufferedHttpEntity implements HttpEntity {
+    
+    private final HttpEntity source;
+    private final byte[] buffer;
+    
+    public BufferedHttpEntity(final HttpEntity entity) throws IOException {
+        super();
+        if (entity == null) {
+            throw new IllegalArgumentException("HTTP entity may not be null");
+        }
+        this.source = entity;
+        if (entity.isChunked() || !entity.isRepeatable() ) {
+            this.buffer = EntityConsumer.toByteArray(entity);
+        } else {
+            this.buffer = null;
+        }
+    }
+
+    public long getContentLength() {
+        if (this.buffer != null) {
+            return this.buffer.length;
+        } else {
+            return this.source.getContentLength();
+        }
+    }
+
+    public String getContentType() {
+        return this.source.getContentType();
+    }
+    
+    public InputStream getInputStream() {
+        if (this.buffer != null) {
+            return new ByteArrayInputStream(this.buffer);
+        } else {
+            return this.source.getInputStream();
+        }
+    }
+    
+    public boolean isChunked() {
+        return false;
+    }
+    
+    public boolean isRepeatable() {
+        return true;
+    }
+
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/BufferedHttpEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java?view=auto&rev=160447
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java Thu Apr  7 12:55:03 2005
@@ -0,0 +1,155 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  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.
+ * ====================================================================
+ *
+ * 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.entity;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.apache.http.HeaderElement;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.params.HttpProtocolParams;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class EntityConsumer {
+    
+    private final HttpResponse response;
+    
+    public EntityConsumer(final HttpResponse response) {
+        super();
+        if (response == null) {
+            throw new IllegalArgumentException("HTTP response may not be null");
+        }
+        this.response = response;
+    }
+
+    public static byte[] toByteArray(final HttpEntity entity) throws IOException {
+        if (entity == null) {
+            throw new IllegalArgumentException("HTTP entity may not be null");
+        }
+        InputStream instream = entity.getInputStream();
+        if (instream == null) {
+            return new byte[] {};
+        }
+        if (entity.getContentLength() > Integer.MAX_VALUE) {
+            throw new IllegalStateException("HTTP entity too large to be buffered in memory");
+        }
+        int i = (int)entity.getContentLength();
+        if (i == -1) {
+            i = 0;
+        }
+        ByteArrayOutputStream outstream = new ByteArrayOutputStream(i);
+        byte[] tmp = new byte[2048];
+        int l;
+        while((l = instream.read(tmp)) != -1) {
+            outstream.write(tmp, 0, l);
+        }
+        return outstream.toByteArray();
+    }
+        
+    public static String getContentCharSet(final HttpEntity entity) {
+        if (entity == null) {
+            throw new IllegalArgumentException("HTTP entity may not be null");
+        }
+        String charset = null;
+        if (entity.getContentType() != null) { 
+            HeaderElement values[] = HeaderElement.parseElements(entity.getContentType());
+            if (values.length > 0) {
+                NameValuePair param = values[0].getParameterByName("charset");
+                if (param != null) {
+                    charset = param.getValue();
+                }
+            }
+        }
+        return charset;
+    }
+
+    public static String toString(
+            final HttpEntity entity, final String defaultCharset) throws IOException {
+        if (entity == null) {
+            throw new IllegalArgumentException("HTTP entity may not be null");
+        }
+        if (entity.getInputStream() == null) {
+            return "";
+        }
+        if (entity.getContentLength() > Integer.MAX_VALUE) {
+            throw new IllegalStateException("HTTP entity too large to be buffered in memory");
+        }
+        String charset = getContentCharSet(entity);
+        if (charset == null) {
+            charset = defaultCharset;
+        }
+        if (charset == null) {
+            charset = "ISO-8859-1";
+        }
+        Reader reader = new InputStreamReader(entity.getInputStream(), charset);
+        StringBuffer buffer = new StringBuffer(); 
+        char[] tmp = new char[1024];
+        int l;
+        while((l = reader.read(tmp)) != -1) {
+            buffer.append(tmp, 0, l);
+        }
+        return buffer.toString();
+    }
+
+    public static String toString(final HttpEntity entity) throws IOException {
+        return toString(entity, null);
+    }
+
+    public byte[] asByteArray() throws IOException {
+        HttpEntity entity = this.response.getEntity();
+        if (entity == null) {
+            return new byte[] {};
+        }
+        return toByteArray(entity);
+    }
+    
+    public String asString() throws IOException {
+        HttpEntity entity = this.response.getEntity();
+        if (entity == null) {
+            return "";
+        }
+        HttpProtocolParams params = new HttpProtocolParams(this.response.getParams());
+        return toString(entity, params.getContentCharset());
+    }
+    
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/EntityConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain