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 2006/02/05 16:53:39 UTC
svn commit: r375049 - in /jakarta/httpcomponents/trunk/http-core/src:
contrib/org/apache/http/contrib/compress/ java/org/apache/http/
java/org/apache/http/entity/ java/org/apache/http/util/
test/org/apache/http/impl/ test/org/apache/http/util/
Author: olegk
Date: Sun Feb 5 07:53:38 2006
New Revision: 375049
URL: http://svn.apache.org/viewcvs?rev=375049&view=rev
Log:
Changelog:
- Non repeatable entities are made to throw an IllegalStateException, if an attempt is made to consume the content more than once
Contributed by Roland Weber
Reviewed by Oleg Kalnichevski
Modified:
jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/compress/GzipDecompressingEntity.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpEntity.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestEntityUtils.java
Modified: jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/compress/GzipDecompressingEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/compress/GzipDecompressingEntity.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/compress/GzipDecompressingEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/compress/GzipDecompressingEntity.java Sun Feb 5 07:53:38 2006
@@ -47,26 +47,22 @@
*/
public class GzipDecompressingEntity extends HttpEntityWrapper {
- private InputStream instream = null;
-
public GzipDecompressingEntity(final HttpEntity entity) {
super(entity);
}
- public InputStream getContent() throws IOException {
- if (this.instream == null) {
- this.instream = new GZIPInputStream(wrappedEntity.getContent());
- }
- return this.instream;
+ public InputStream getContent()
+ throws IOException, IllegalStateException {
+
+ // the wrapped entity's getContent() decides about repeatability
+ InputStream wrappedin = wrappedEntity.getContent();
+
+ return new GZIPInputStream(wrappedin);
}
-
+
public long getContentLength() {
+ // length of ungzipped content not known in advance
return -1;
}
-
- public boolean isRepeatable() {
- // not repeatable, GZIPInputStream is created only once
- return false;
- }
-
-} // class GzipDecompressingEntity
\ No newline at end of file
+
+} // class GzipDecompressingEntity
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpEntity.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpEntity.java Sun Feb 5 07:53:38 2006
@@ -130,10 +130,17 @@
* Creates a new InputStream object of the entity.
* It is a programming error
* to return the same InputStream object more than once.
+ * Entities that are not {@link #isRepeatable repeatable}
+ * will throw an exception if this method is called multiple times.
+ *
* @return a new input stream that returns the entity data.
+ *
* @throws IOException if the stream could not be created
+ * @throws IllegalStateException
+ * if this entity is not repeatable and the stream
+ * has already been obtained previously
*/
- InputStream getContent() throws IOException;
+ InputStream getContent() throws IOException, IllegalStateException;
/**
* Writes the entity content to the output stream.
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java Sun Feb 5 07:53:38 2006
@@ -33,9 +33,6 @@
import java.io.InputStream;
import java.io.OutputStream;
-import org.apache.http.Header;
-import org.apache.http.HttpEntity;
-
/**
* <p>
* </p>
@@ -45,62 +42,82 @@
*
* @since 4.0
*/
-public class BasicHttpEntity implements HttpEntity {
-
- private Header contentType = null;
- private Header contentEncoding = null;
- private InputStream content = null;
- private long length = -1;
- private boolean chunked = false;
-
+public class BasicHttpEntity extends AbstractHttpEntity {
+
+ private InputStream content;
+ private boolean contentObtained;
+ private long length;
+
+ /**
+ * Creates a new basic entity.
+ * The content is initially missing, the content length
+ * is set to a negative number.
+ */
public BasicHttpEntity() {
super();
+ length = -1;
}
+ // non-javadoc, see interface HttpEntity
public long getContentLength() {
return this.length;
}
- public Header getContentType() {
- return this.contentType;
- }
-
- public Header getContentEncoding() {
- return this.contentEncoding;
- }
-
- public InputStream getContent() {
+ /**
+ * Obtains the content, once only.
+ *
+ * @return the content, if this is the first call to this method
+ * since {@link #setContent setContent} has been called
+ *
+ * @throws IllegalStateException
+ * if the content has been obtained before, or
+ * has not yet been provided
+ */
+ public InputStream getContent()
+ throws IllegalStateException {
+
+ if (this.content == null)
+ throw new IllegalStateException("content has not been provided");
+ if (contentObtained)
+ throw new IllegalStateException("content is not repeatable");
+
+ contentObtained = true;
return this.content;
- }
-
- public boolean isChunked() {
- return this.chunked;
- }
-
+
+ } // getContent
+
+ /**
+ * Tells that this entity is not repeatable.
+ *
+ * @return <code>false</code>
+ */
public boolean isRepeatable() {
return false;
}
-
- public void setChunked(boolean b) {
- this.chunked = b;
- }
-
+
+ /**
+ * Specifies the length of the content.
+ *
+ * @param len the number of bytes in the content, or
+ * a negative number to indicate an unknown length
+ */
public void setContentLength(long len) {
this.length = len;
}
-
- public void setContentType(final Header contentType) {
- this.contentType = contentType;
- }
-
- public void setContentEncoding(final Header contentEncoding) {
- this.contentEncoding = contentEncoding;
- }
-
+
+ /**
+ * Specifies the content.
+ *
+ * @param instream the stream to return with the next call to
+ * {@link #getContent getContent}
+ */
public void setContent(final InputStream instream) {
- this.content = instream;
+ this.content = instream;
+ if (instream != null)
+ contentObtained = false;
}
-
+
+ // non-javadoc, see interface HttpEntity
public void writeTo(final OutputStream outstream) throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output stream may not be null");
@@ -115,7 +132,6 @@
outstream.write(tmp, 0, l);
}
}
-
// non-javadoc, see interface HttpEntity
public boolean isStreaming() {
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java Sun Feb 5 07:53:38 2006
@@ -101,7 +101,8 @@
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
- if (entity.getContent() == null) {
+ InputStream instream = entity.getContent();
+ if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
@@ -118,7 +119,7 @@
if (charset == null) {
charset = HTTP.DEFAULT_CONTENT_CHARSET;
}
- Reader reader = new InputStreamReader(entity.getContent(), charset);
+ Reader reader = new InputStreamReader(instream, charset);
CharArrayBuffer buffer = new CharArrayBuffer(i);
char[] tmp = new char[1024];
int l;
Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java Sun Feb 5 07:53:38 2006
@@ -28,6 +28,8 @@
package org.apache.http.impl;
+import java.io.InputStream;
+
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpMutableMessage;
@@ -205,8 +207,9 @@
assertNotNull(entity);
assertEquals(1, entity.getContentLength());
assertFalse(entity.isChunked());
- assertNotNull(entity.getContent());
- assertTrue(entity.getContent() instanceof ContentLengthInputStream);
+ InputStream instream = entity.getContent();
+ assertNotNull(instream);
+ assertTrue(instream instanceof ContentLengthInputStream);
// strict mode
message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
@@ -233,8 +236,9 @@
assertNotNull(entity);
assertEquals(1, entity.getContentLength());
assertFalse(entity.isChunked());
- assertNotNull(entity.getContent());
- assertTrue(entity.getContent() instanceof ContentLengthInputStream);
+ InputStream instream = entity.getContent();
+ assertNotNull(instream);
+ assertTrue(instream instanceof ContentLengthInputStream);
// strict mode
message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
@@ -260,9 +264,10 @@
assertNotNull(entity);
assertEquals(-1, entity.getContentLength());
assertFalse(entity.isChunked());
- assertNotNull(entity.getContent());
- assertFalse(entity.getContent() instanceof ContentLengthInputStream);
- assertTrue(entity.getContent() instanceof HttpDataInputStream);
+ InputStream instream = entity.getContent();
+ assertNotNull(instream);
+ assertFalse(instream instanceof ContentLengthInputStream);
+ assertTrue(instream instanceof HttpDataInputStream);
// strict mode
message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
@@ -287,9 +292,10 @@
assertNotNull(entity);
assertEquals(-1, entity.getContentLength());
assertFalse(entity.isChunked());
- assertNotNull(entity.getContent());
- assertFalse(entity.getContent() instanceof ContentLengthInputStream);
- assertTrue(entity.getContent() instanceof HttpDataInputStream);
+ InputStream instream = entity.getContent();
+ assertNotNull(instream);
+ assertFalse(instream instanceof ContentLengthInputStream);
+ assertTrue(instream instanceof HttpDataInputStream);
// strict mode
message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
@@ -312,10 +318,11 @@
assertNotNull(entity);
assertEquals(-1, entity.getContentLength());
assertFalse(entity.isChunked());
- assertNotNull(entity.getContent());
- assertFalse(entity.getContent() instanceof ContentLengthInputStream);
- assertFalse(entity.getContent() instanceof ChunkedInputStream);
- assertTrue(entity.getContent() instanceof HttpDataInputStream);
+ InputStream instream = entity.getContent();
+ assertNotNull(instream);
+ assertFalse(instream instanceof ContentLengthInputStream);
+ assertFalse(instream instanceof ChunkedInputStream);
+ assertTrue(instream instanceof HttpDataInputStream);
}
}
Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestEntityUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestEntityUtils.java?rev=375049&r1=375048&r2=375049&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestEntityUtils.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestEntityUtils.java Sun Feb 5 07:53:38 2006
@@ -29,6 +29,7 @@
package org.apache.http.util;
+import java.io.InputStream;
import java.io.ByteArrayInputStream;
import org.apache.http.Header;
@@ -68,8 +69,7 @@
}
public void testEmptyContentToByteArray() throws Exception {
- BasicHttpEntity httpentity = new BasicHttpEntity();
- httpentity.setContent(null);
+ NullHttpEntity httpentity = new NullHttpEntity();
byte[] bytes = EntityUtils.toByteArray(httpentity);
assertNotNull(bytes);
assertEquals(0, bytes.length);
@@ -125,7 +125,7 @@
public void testNullContentTypeGetContentCharset() throws Exception {
BasicHttpEntity httpentity = new BasicHttpEntity();
- httpentity.setContentType(null);
+ httpentity.setContentType((Header)null);
assertNull(EntityUtils.getContentCharSet(httpentity));
}
@@ -151,8 +151,7 @@
}
public void testEmptyContentToString() throws Exception {
- BasicHttpEntity httpentity = new BasicHttpEntity();
- httpentity.setContent(null);
+ NullHttpEntity httpentity = new NullHttpEntity();
String s = EntityUtils.toString(httpentity);
assertNotNull(s);
assertEquals("", s);
@@ -237,5 +236,22 @@
String s = EntityUtils.toString(httpentity, "ISO-8859-1");
assertEquals(content, s);
}
-
-}
+
+ /**
+ * Helper class that returns <code>null</code> as the content.
+ */
+ public static class NullHttpEntity extends BasicHttpEntity {
+
+ // default constructor
+ /**
+ * Obtains no content.
+ * This method disables the state checks in the base class.
+ *
+ * @return <code>null</code>
+ */
+ public InputStream getContent() {
+ return null;
+ }
+ } // class NullEntity
+
+} // class TestEntityUtils