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/03/14 19:46:54 UTC
svn commit: r385851 - in /jakarta/httpcomponents/trunk/http-core/src:
java/org/apache/http/impl/ java/org/apache/http/impl/entity/
test/org/apache/http/ test/org/apache/http/impl/entity/
test/org/apache/http/mockup/
Author: olegk
Date: Tue Mar 14 10:46:52 2006
New Revision: 385851
URL: http://svn.apache.org/viewcvs?rev=385851&view=rev
Log:
Changelog:
* Default entity serializer impl changed to rely on message headers when serialing an entity
instead of the HttpEntity properties which may be inconsitent with the Content-Length and
Transfer-Encoding headers contained in the message. This way, the content entity is always
guaranteed to be correctly serialized. The message headers are expected to be setup by the
request / response interceptors
* The split-up into the default client-side and the server-side serializer impls is no
longer necessary.
Added:
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java
- copied, changed from r385835, jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultClientEntitySerializer.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java (with props)
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java (with props)
Removed:
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultClientEntitySerializer.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultServerEntitySerializer.java
Modified:
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestAll.java
jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpMessageMockup.java
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java?rev=385851&r1=385850&r2=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java Tue Mar 14 10:46:52 2006
@@ -48,7 +48,7 @@
import org.apache.http.StatusLine;
import org.apache.http.entity.EntityDeserializer;
import org.apache.http.entity.EntitySerializer;
-import org.apache.http.impl.entity.DefaultClientEntitySerializer;
+import org.apache.http.impl.entity.DefaultEntitySerializer;
import org.apache.http.impl.entity.DefaultEntityDeserializer;
import org.apache.http.io.CharArrayBuffer;
import org.apache.http.io.SocketFactory;
@@ -86,7 +86,7 @@
this.localAddress = localAddress;
this.buffer = new CharArrayBuffer(128);
this.responsefactory = new DefaultHttpResponseFactory();
- this.entityserializer = new DefaultClientEntitySerializer();
+ this.entityserializer = new DefaultEntitySerializer();
this.entitydeserializer = new DefaultEntityDeserializer();
}
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java?rev=385851&r1=385850&r2=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java Tue Mar 14 10:46:52 2006
@@ -46,7 +46,7 @@
import org.apache.http.entity.EntityDeserializer;
import org.apache.http.entity.EntitySerializer;
import org.apache.http.impl.entity.DefaultEntityDeserializer;
-import org.apache.http.impl.entity.DefaultServerEntitySerializer;
+import org.apache.http.impl.entity.DefaultEntitySerializer;
import org.apache.http.io.CharArrayBuffer;
import org.apache.http.params.HttpParams;
@@ -75,7 +75,7 @@
super();
this.requestfactory = new DefaultHttpRequestFactory();
this.buffer = new CharArrayBuffer(128);
- this.entityserializer = new DefaultServerEntitySerializer();
+ this.entityserializer = new DefaultEntitySerializer();
this.entitydeserializer = new DefaultEntityDeserializer();
}
Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java?rev=385851&r1=385850&r2=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java Tue Mar 14 10:46:52 2006
@@ -191,7 +191,7 @@
super();
}
- protected BasicHttpEntity generateEntity(
+ protected BasicHttpEntity doDeserialize(
final HttpDataReceiver datareceiver,
final HttpMessage message) throws HttpException, IOException {
if (datareceiver == null) {
@@ -286,7 +286,7 @@
public HttpEntity deserialize(
final HttpDataReceiver datareceiver,
final HttpMessage message) throws HttpException, IOException {
- return generateEntity(datareceiver, message);
+ return doDeserialize(datareceiver, message);
}
}
Copied: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java (from r385835, jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultClientEntitySerializer.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java?p2=jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java&p1=jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultClientEntitySerializer.java&r1=385835&r2=385851&rev=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultClientEntitySerializer.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java Tue Mar 14 10:46:52 2006
@@ -32,6 +32,7 @@
import java.io.IOException;
import java.io.OutputStream;
+import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpMessage;
@@ -41,6 +42,8 @@
import org.apache.http.io.ChunkedOutputStream;
import org.apache.http.io.ContentLengthOutputStream;
import org.apache.http.io.HttpDataTransmitter;
+import org.apache.http.io.IdentityOutputStream;
+import org.apache.http.protocol.HTTP;
/**
* Default implementation of an entity writer on the client side.
@@ -51,11 +54,44 @@
*
* @since 4.0
*/
-public class DefaultClientEntitySerializer implements EntitySerializer {
+public class DefaultEntitySerializer implements EntitySerializer {
- public DefaultClientEntitySerializer() {
+ public DefaultEntitySerializer() {
super();
}
+
+ protected OutputStream doSerialize(
+ final HttpDataTransmitter datatransmitter,
+ final HttpMessage message) throws HttpException, IOException {
+ Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
+ Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
+ if (transferEncodingHeader != null) {
+ String s = transferEncodingHeader.getValue();
+ if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
+ if (message.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0)) {
+ throw new ProtocolException(
+ "Chunked transfer encoding not allowed for " +
+ message.getHttpVersion());
+ }
+ return new ChunkedOutputStream(datatransmitter);
+ } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
+ return new IdentityOutputStream(datatransmitter);
+ } else {
+ throw new ProtocolException(
+ "Unsupported transfer encoding: " + s);
+ }
+ } else if (contentLengthHeader != null) {
+ String s = contentLengthHeader.getValue();
+ try {
+ long len = Long.parseLong(s);
+ return new ContentLengthOutputStream(datatransmitter, len);
+ } catch (NumberFormatException e) {
+ throw new ProtocolException("Invalid content length: " + s);
+ }
+ } else {
+ return new IdentityOutputStream(datatransmitter);
+ }
+ }
public void serialize(
final HttpDataTransmitter datatransmitter,
@@ -70,18 +106,7 @@
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
- long len = entity.getContentLength();
- boolean chunked = entity.isChunked() || len < 0;
- if (chunked && message.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0)) {
- throw new ProtocolException(
- "Chunked transfer encoding not supported by " + message.getHttpVersion());
- }
- OutputStream outstream = null;
- if (chunked) {
- outstream = new ChunkedOutputStream(datatransmitter);
- } else {
- outstream = new ContentLengthOutputStream(datatransmitter, len);
- }
+ OutputStream outstream = doSerialize(datatransmitter, message);
entity.writeTo(outstream);
outstream.close();
}
Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestAll.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestAll.java?rev=385851&r1=385850&r2=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestAll.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestAll.java Tue Mar 14 10:46:52 2006
@@ -30,6 +30,7 @@
import org.apache.http.entity.TestAllEntity;
import org.apache.http.impl.TestAllImpl;
+import org.apache.http.impl.entity.TestAllEntityImpl;
import org.apache.http.io.TestAllIO;
import org.apache.http.util.TestAllUtil;
@@ -60,6 +61,7 @@
suite.addTest(TestAllIO.suite());
suite.addTest(TestAllEntity.suite());
suite.addTest(TestAllImpl.suite());
+ suite.addTest(TestAllEntityImpl.suite());
return suite;
}
Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java?rev=385851&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java Tue Mar 14 10:46:52 2006
@@ -0,0 +1,50 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * Copyright 1999-2006 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.impl.entity;
+
+import junit.framework.*;
+
+public class TestAllEntityImpl extends TestCase {
+
+ public TestAllEntityImpl(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(TestDefaultEntitySerializer.suite());
+ return suite;
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestAllEntityImpl.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+}
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestAllEntityImpl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java?rev=385851&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java Tue Mar 14 10:46:52 2006
@@ -0,0 +1,192 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * Copyright 2002-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.impl.entity;
+
+import java.io.OutputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.Header;
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.EntitySerializer;
+import org.apache.http.io.ChunkedOutputStream;
+import org.apache.http.io.ContentLengthOutputStream;
+import org.apache.http.io.HttpDataTransmitter;
+import org.apache.http.io.IdentityOutputStream;
+import org.apache.http.mockup.HttpDataTransmitterMockup;
+import org.apache.http.mockup.HttpMessageMockup;
+import org.apache.http.params.HttpProtocolParams;
+
+public class TestDefaultEntitySerializer extends TestCase {
+
+ public TestDefaultEntitySerializer(String testName) {
+ super(testName);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestDefaultEntitySerializer.class);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestDefaultEntitySerializer.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public void testIllegalGenerateArg() throws Exception {
+ EntitySerializer entitywriter = new DefaultEntitySerializer();
+ try {
+ entitywriter.serialize(null, null, null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ entitywriter.serialize(new HttpDataTransmitterMockup() , null, null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ entitywriter.serialize(new HttpDataTransmitterMockup() , new HttpMessageMockup(), null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testEntityWithChunkTransferEncoding() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Transfer-Encoding", "Chunked"));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
+ assertNotNull(outstream);
+ assertTrue(outstream instanceof ChunkedOutputStream);
+ }
+
+ public void testEntityWithIdentityTransferEncoding() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Transfer-Encoding", "Identity"));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
+ assertNotNull(outstream);
+ assertTrue(outstream instanceof IdentityOutputStream);
+ }
+
+ public void testEntityWithInvalidTransferEncoding() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Transfer-Encoding", "whatever"));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ try {
+ entitywriter.doSerialize(datatransmitter, message);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException ex) {
+ // expected
+ }
+ }
+
+ public void testEntityWithInvalidChunkEncodingAndHTTP10() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION,
+ HttpVersion.HTTP_1_0);
+ message.addHeader(new Header("Transfer-Encoding", "chunked"));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ try {
+ entitywriter.doSerialize(datatransmitter, message);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException ex) {
+ // expected
+ }
+ }
+
+ public void testEntityWithContentLength() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Content-Length", "100"));
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
+ assertNotNull(outstream);
+ assertTrue(outstream instanceof ContentLengthOutputStream);
+ }
+
+ public void testEntityWithInvalidContentLength() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Content-Length", "whatever"));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ try {
+ entitywriter.doSerialize(datatransmitter, message);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException ex) {
+ // expected
+ }
+ }
+
+ public void testEntityNoContentDelimiter() throws Exception {
+ HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
+ assertNotNull(outstream);
+ assertTrue(outstream instanceof IdentityOutputStream);
+ }
+
+ public void testEntitySerialization() throws Exception {
+ byte[] content = new byte[] {1, 2, 3, 4, 5};
+ ByteArrayEntity entity = new ByteArrayEntity(content);
+
+ HttpDataTransmitterMockup datatransmitter = new HttpDataTransmitterMockup();
+ HttpMessage message = new HttpMessageMockup();
+ message.addHeader(new Header("Content-Length", Integer.toString(content.length)));
+
+ DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+ entitywriter.serialize(datatransmitter, message, entity);
+
+ byte[] data = datatransmitter.getData();
+ assertNotNull(data);
+ assertEquals(content.length, data.length);
+ }
+}
+
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpMessageMockup.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpMessageMockup.java?rev=385851&r1=385850&r2=385851&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpMessageMockup.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpMessageMockup.java Tue Mar 14 10:46:52 2006
@@ -45,7 +45,7 @@
}
public HttpVersion getHttpVersion() {
- return (HttpVersion) this.getParams().getParameter(HttpProtocolParams.PROTOCOL_VERSION);
+ return HttpProtocolParams.getVersion(this.getParams());
}
}