hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r463914 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/entity/ main/java/org/apache/http/impl/ main/java/org/apache/http/impl/entity/ test/java/org/apache/http/impl/entity/
Date Sat, 14 Oct 2006 11:15:31 GMT
Author: olegk
Date: Sat Oct 14 04:15:28 2006
New Revision: 463914

URL: http://svn.apache.org/viewvc?view=rev&rev=463914
Log:
Factored out the content length strategy code from the DefaultEntityDeserializer and DefaultEntitySerializer classes. ContentLengthStrategy implementations can be used with the non-blocking HTTP transport in HttpCore NIO 

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java
      - copied, changed from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java
      - copied, changed from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java
      - copied, changed from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestStrictContentLengthStrategy.java
      - copied, changed from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestAllEntityImpl.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java?view=auto&rev=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java Sat Oct 14 04:15:28 2006
@@ -0,0 +1,52 @@
+/*
+ * $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.entity;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+
+/**
+ * Represents a strategy to determine the content length based on the properties
+ * of an HTTP message.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public interface ContentLengthStrategy {
+
+    public static final int IDENTITY         = -1;
+    public static final int CHUNKED          = -2;
+    
+    long determineLength(HttpMessage message) throws HttpException;
+            
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java Sat Oct 14 04:15:28 2006
@@ -34,6 +34,8 @@
 
 import org.apache.http.impl.entity.DefaultEntityDeserializer;
 import org.apache.http.impl.entity.DefaultEntitySerializer;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
 import org.apache.http.impl.io.SocketHttpDataReceiver;
 import org.apache.http.impl.io.SocketHttpDataTransmitter;
 import org.apache.http.io.HttpDataReceiver;
@@ -103,8 +105,10 @@
         setHttpDataTransmitter(transmitter);
         setMaxHeaderCount(params.getIntParameter(HttpConnectionParams.MAX_HEADER_COUNT, -1));
         setResponseFactory(new DefaultHttpResponseFactory());
-        setEntitySerializer(new DefaultEntitySerializer());
-        setEntityDeserializer(new DefaultEntityDeserializer());
+        setEntitySerializer(new DefaultEntitySerializer(
+                new StrictContentLengthStrategy()));
+        setEntityDeserializer(new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy()));
         this.open = true;
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java Sat Oct 14 04:15:28 2006
@@ -34,6 +34,8 @@
 
 import org.apache.http.impl.entity.DefaultEntityDeserializer;
 import org.apache.http.impl.entity.DefaultEntitySerializer;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
 import org.apache.http.impl.io.SocketHttpDataReceiver;
 import org.apache.http.impl.io.SocketHttpDataTransmitter;
 import org.apache.http.io.HttpDataReceiver;
@@ -101,8 +103,10 @@
         setHttpDataTransmitter(transmitter);
         setMaxHeaderCount(params.getIntParameter(HttpConnectionParams.MAX_HEADER_COUNT, -1));
         setRequestFactory(new DefaultHttpRequestFactory());
-        setEntitySerializer(new DefaultEntitySerializer());
-        setEntityDeserializer(new DefaultEntityDeserializer());
+        setEntitySerializer(new DefaultEntitySerializer(
+                new StrictContentLengthStrategy()));
+        setEntityDeserializer(new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy()));
         this.open = true;
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java Sat Oct 14 04:15:28 2006
@@ -32,151 +32,22 @@
 import java.io.IOException;
 
 import org.apache.http.Header;
-import org.apache.http.HeaderElement;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMessage;
-import org.apache.http.HttpEntity;
-import org.apache.http.ProtocolException;
 import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.entity.ContentLengthStrategy;
 import org.apache.http.entity.EntityDeserializer;
 import org.apache.http.io.ChunkedInputStream;
 import org.apache.http.io.ContentLengthInputStream;
 import org.apache.http.io.HttpDataInputStream;
 import org.apache.http.io.HttpDataReceiver;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HTTP;
 
 /**
- * Default implementation of an entity generator.
- * <p>
- * This entity generator comforms to the entity transfer rules outlined in the 
- * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec4.4">Section 4.4</a>, 
- * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>, 
- * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">Section 14.41</a>
- * and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec14.13">Section 14.13</a>
- * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
- * </p>
- * <h>4.4 Message Length</h>
- * <p>
- * The transfer-length of a message is the length of the message-body as it appears in the 
- * message; that is, after any transfer-codings have been applied. When a message-body is 
- * included with a message, the transfer-length of that body is determined by one of the 
- * following (in order of precedence):
- * </p>
- * <p>
- * 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, 
- * and 304 responses and any response to a HEAD request) is always terminated by the first 
- * empty line after the header fields, regardless of the entity-header fields present in the 
- * message.
- * </p>
- * <p>
- * 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other 
- * than "identity", then the transfer-length is defined by use of the "chunked" transfer-
- * coding (section 3.6), unless the message is terminated by closing the connection.
- * </p>
- * <p>
- * 3.If a Content-Length header field (section 14.13) is present, its decimal value in 
- * OCTETs represents both the entity-length and the transfer-length. The Content-Length 
- * header field MUST NOT be sent if these two lengths are different (i.e., if a 
- * Transfer-Encoding
- * </p>
- * <pre>
- *    header field is present). If a message is received with both a
- *    Transfer-Encoding header field and a Content-Length header field,
- *    the latter MUST be ignored.
- * </pre>
- * <p>
- * 4.If the message uses the media type "multipart/byteranges", and the ransfer-length is not 
- * otherwise specified, then this self- elimiting media type defines the transfer-length. 
- * This media type UST NOT be used unless the sender knows that the recipient can arse it; the 
- * presence in a request of a Range header with ultiple byte- range specifiers from a 1.1 
- * client implies that the lient can parse multipart/byteranges responses.
- * </p>
- * <pre>
- *     A range header might be forwarded by a 1.0 proxy that does not
- *     understand multipart/byteranges; in this case the server MUST
- *     delimit the message using methods defined in items 1,3 or 5 of
- *     this section.
- * </pre>
- * <p>
- * 5.By the server closing the connection. (Closing the connection cannot be used to indicate 
- * the end of a request body, since that would leave no possibility for the server to send back 
- * a response.)
- * </p>
- * <p>
- * For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body 
- * MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1 
- * compliant. If a request contains a message-body and a Content-Length is not given, the 
- * server SHOULD respond with 400 (bad request) if it cannot determine the length of the 
- * message, or with 411 (length required) if it wishes to insist on receiving a valid 
- * Content-Length.
- * </p>
- * <p>All HTTP/1.1 applications that receive entities MUST accept the "chunked" transfer-coding 
- * (section 3.6), thus allowing this mechanism to be used for messages when the message 
- * length cannot be determined in advance. 
- * </p>
- * <h>3.6 Transfer Codings</h>
- * <p>
- * Transfer-coding values are used to indicate an encoding transformation that 
- * has been, can be, or may need to be applied to an entity-body in order to ensure 
- * "safe transport" through the network. This differs from a content coding in that 
- * the transfer-coding is a property of the message, not of the original entity.
- * </p>
- * <pre>
- * transfer-coding         = "chunked" | transfer-extension
- * transfer-extension      = token *( ";" parameter )
- * </pre>
- * <p>
- * Parameters are in the form of attribute/value pairs.
- * </p>
- * <pre>
- * parameter               = attribute "=" value
- * attribute               = token
- * value                   = token | quoted-string
- * </pre>
+ * Default implementation of an entity deserializer.
  * <p>
- * All transfer-coding values are case-insensitive. HTTP/1.1 uses transfer-coding values in 
- * the TE header field (section 14.39) and in the Transfer-Encoding header field (section 14.41).
- * </p>
- * <p>
- * Whenever a transfer-coding is applied to a message-body, the set of transfer-codings MUST 
- * include "chunked", unless the message is terminated by closing the connection. When the 
- * "chunked" transfer-coding is used, it MUST be the last transfer-coding applied to the 
- * message-body. The "chunked" transfer-coding MUST NOT be applied more than once to a 
- * message-body. These rules allow the recipient to determine the transfer-length of the 
- * message (section 4.4).
- * </p>
- * <h>14.41 Transfer-Encoding</h>
- * <p>
- * The Transfer-Encoding general-header field indicates what (if any) type of transformation has 
- * been applied to the message body in order to safely transfer it between the sender and the 
- * recipient. This differs from the content-coding in that the transfer-coding is a property of 
- * the message, not of the entity.
- * </p>
- * <pre>
- *   Transfer-Encoding       = "Transfer-Encoding" ":" 1#transfer-coding
- * </pre>
- * <p>
- * If multiple encodings have been applied to an entity, the transfer- codings MUST be listed in 
- * the order in which they were applied. Additional information about the encoding parameters 
- * MAY be provided by other entity-header fields not defined by this specification.
- * </p> 
- * <h>14.13 Content-Length</h>
- * <p>
- * The Content-Length entity-header field indicates the size of the entity-body, in decimal 
- * number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of 
- * the entity-body that would have been sent had the request been a GET.
- * </p>
- * <pre>
- *   Content-Length    = "Content-Length" ":" 1*DIGIT
- * </pre>
- * <p>
- * Applications SHOULD use this field to indicate the transfer-length of the message-body, 
- * unless this is prohibited by the rules in section 4.4. 
- * </p>
- * <p>
- * This entity generator currently supports only "chunked" and "identitiy" transfer-coding</a>
+ * This entity deserializer currently supports only "chunked" and "identitiy" transfer-coding</a>
  * </p>
  * 
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
@@ -187,8 +58,14 @@
  */
 public class DefaultEntityDeserializer implements EntityDeserializer {
 
-    public DefaultEntityDeserializer() {
+    private final ContentLengthStrategy lenStrategy;
+    
+    public DefaultEntityDeserializer(final ContentLengthStrategy lenStrategy) {
         super();
+        if (lenStrategy == null) {
+            throw new IllegalArgumentException("Content length strategy may not be null");
+        }
+        this.lenStrategy = lenStrategy;
     }
 
     protected BasicHttpEntity doDeserialize(
@@ -203,80 +80,26 @@
 
         BasicHttpEntity entity = new BasicHttpEntity();
         
-        HttpParams params = message.getParams(); 
-        boolean strict = params.isParameterTrue(HttpProtocolParams.STRICT_TRANSFER_ENCODING);
-        
-        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
-        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
-        Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
-        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
-        // We use Transfer-Encoding if present and ignore Content-Length.
-        // RFC2616, 4.4 item number 3
-        if (transferEncodingHeader != null) {
-            HeaderElement[] encodings = transferEncodingHeader.getElements();
-            if (strict) {
-                // Currently only chunk and identity are supported
-                for (int i = 0; i < encodings.length; i++) {
-                    String encoding = encodings[i].getName();
-                    if (encoding != null && !encoding.equals("") 
-                        && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
-                        && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
-                        throw new ProtocolException("Unsupported transfer encoding: " + encoding);
-                    }
-                }
-            }
-            // The chunked encoding must be the last one applied RFC2616, 14.41
-            int len = encodings.length;
-            if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
-                entity.setChunked(false);
-                entity.setContentLength(-1);
-                entity.setContent(new HttpDataInputStream(datareceiver));                            
-            } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
-                    encodings[len - 1].getName()))) { 
-                entity.setChunked(true);
-                entity.setContentLength(-1);
-                entity.setContent(new ChunkedInputStream(datareceiver));
-            } else {
-                if (strict) {
-                    throw new ProtocolException("Chunk-encoding must be the last one applied");
-                }
-                entity.setChunked(false);
-                entity.setContentLength(-1);
-                entity.setContent(new HttpDataInputStream(datareceiver));                            
-            }
-        } else if (contentLengthHeader != null) {
-            long contentlen = -1;
-            Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
-            if (strict && headers.length > 1) {
-                throw new ProtocolException("Multiple content length headers");
-            }
-            for (int i = headers.length - 1; i >= 0; i--) {
-                Header header = headers[i];
-                try {
-                    contentlen = Long.parseLong(header.getValue());
-                    break;
-                } catch (NumberFormatException e) {
-                    if (strict) {
-                        throw new ProtocolException("Invalid content length: " + header.getValue());
-                    }
-                }
-                // See if we can have better luck with another header, if present
-            }
-            entity.setChunked(false);
-            entity.setContentLength(contentlen);
-            if (contentlen >= 0) {
-                entity.setContent(new ContentLengthInputStream(datareceiver, contentlen));
-            } else {
-                entity.setContent(new HttpDataInputStream(datareceiver));
-            }
-        } else {
+        long len = this.lenStrategy.determineLength(message);
+        if (len == ContentLengthStrategy.CHUNKED) {
+            entity.setChunked(true);
+            entity.setContentLength(-1);
+            entity.setContent(new ChunkedInputStream(datareceiver));
+        } else if (len == ContentLengthStrategy.IDENTITY) {
             entity.setChunked(false);
             entity.setContentLength(-1);
             entity.setContent(new HttpDataInputStream(datareceiver));                            
+        } else {
+            entity.setChunked(false);
+            entity.setContentLength(len);
+            entity.setContent(new ContentLengthInputStream(datareceiver, len));
         }
+        
+        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
         if (contentTypeHeader != null) {
             entity.setContentType(contentTypeHeader);    
         }
+        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
         if (contentEncodingHeader != null) {
             entity.setContentEncoding(contentEncodingHeader);    
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java Sat Oct 14 04:15:28 2006
@@ -32,21 +32,21 @@
 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;
-import org.apache.http.HttpVersion;
-import org.apache.http.ProtocolException;
+import org.apache.http.entity.ContentLengthStrategy;
 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.protocol.HTTP;
 
 /**
- * Default implementation of an entity writer on the client side.
+ * Default implementation of an entity serializer.
+ * <p>
+ * This entity serializer currently supports only "chunked" and "identitiy" transfer-coding</a>
+ * </p>
  * 
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
@@ -56,40 +56,26 @@
  */
 public class DefaultEntitySerializer implements EntitySerializer {
 
-    public DefaultEntitySerializer() {
+    private final ContentLengthStrategy lenStrategy;
+    
+    public DefaultEntitySerializer(final ContentLengthStrategy lenStrategy) {
         super();
+        if (lenStrategy == null) {
+            throw new IllegalArgumentException("Content length strategy may not be null");
+        }
+        this.lenStrategy = lenStrategy;
     }
-    
+
     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);
-            }
+        long len = this.lenStrategy.determineLength(message);
+        if (len == ContentLengthStrategy.CHUNKED) {
+            return new ChunkedOutputStream(datatransmitter);
+        } else if (len == ContentLengthStrategy.IDENTITY) {
+            return new IdentityOutputStream(datatransmitter);
         } else {
-            return new IdentityOutputStream(datatransmitter); 
+            return new ContentLengthOutputStream(datatransmitter, len);
         }
     }
 

Copied: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java (from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java?view=diff&rev=463914&p1=jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java&r1=451782&p2=jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntityDeserializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java Sat Oct 14 04:15:28 2006
@@ -29,33 +29,26 @@
 
 package org.apache.http.impl.entity;
 
-import java.io.IOException;
-
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMessage;
-import org.apache.http.HttpEntity;
 import org.apache.http.ProtocolException;
-import org.apache.http.entity.BasicHttpEntity;
-import org.apache.http.entity.EntityDeserializer;
-import org.apache.http.io.ChunkedInputStream;
-import org.apache.http.io.ContentLengthInputStream;
-import org.apache.http.io.HttpDataInputStream;
-import org.apache.http.io.HttpDataReceiver;
+import org.apache.http.entity.ContentLengthStrategy;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HTTP;
 
 /**
- * Default implementation of an entity generator.
+ * The lax implementation of the content length strategy.
  * <p>
- * This entity generator comforms to the entity transfer rules outlined in the 
+ * This strategy conforms to the entity transfer rules outlined in  
  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec4.4">Section 4.4</a>, 
  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>, 
  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">Section 14.41</a>
  * and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec14.13">Section 14.13</a>
- * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
+ * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>, but is lenient 
+ * about unsupported transfer codecs and malformed content-length headers.
  * </p>
  * <h>4.4 Message Length</h>
  * <p>
@@ -175,9 +168,6 @@
  * Applications SHOULD use this field to indicate the transfer-length of the message-body, 
  * unless this is prohibited by the rules in section 4.4. 
  * </p>
- * <p>
- * This entity generator currently supports only "chunked" and "identitiy" transfer-coding</a>
- * </p>
  * 
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
@@ -185,29 +175,20 @@
  * 
  * @since 4.0
  */
-public class DefaultEntityDeserializer implements EntityDeserializer {
+public class LaxContentLengthStrategy implements ContentLengthStrategy {
 
-    public DefaultEntityDeserializer() {
+    public LaxContentLengthStrategy() {
         super();
     }
 
-    protected BasicHttpEntity doDeserialize(
-            final HttpDataReceiver datareceiver,
-            final HttpMessage message) throws HttpException, IOException {
-        if (datareceiver == null) {
-            throw new IllegalArgumentException("HTTP data receiver may not be null");
-        }
+    public long determineLength(final HttpMessage message) throws HttpException {
         if (message == null) {
             throw new IllegalArgumentException("HTTP message may not be null");
         }
 
-        BasicHttpEntity entity = new BasicHttpEntity();
-        
         HttpParams params = message.getParams(); 
         boolean strict = params.isParameterTrue(HttpProtocolParams.STRICT_TRANSFER_ENCODING);
         
-        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
-        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
         Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
         Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
         // We use Transfer-Encoding if present and ignore Content-Length.
@@ -228,21 +209,15 @@
             // The chunked encoding must be the last one applied RFC2616, 14.41
             int len = encodings.length;
             if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
-                entity.setChunked(false);
-                entity.setContentLength(-1);
-                entity.setContent(new HttpDataInputStream(datareceiver));                            
+                return IDENTITY;                            
             } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
                     encodings[len - 1].getName()))) { 
-                entity.setChunked(true);
-                entity.setContentLength(-1);
-                entity.setContent(new ChunkedInputStream(datareceiver));
+                return CHUNKED;
             } else {
                 if (strict) {
                     throw new ProtocolException("Chunk-encoding must be the last one applied");
                 }
-                entity.setChunked(false);
-                entity.setContentLength(-1);
-                entity.setContent(new HttpDataInputStream(datareceiver));                            
+                return IDENTITY;                            
             }
         } else if (contentLengthHeader != null) {
             long contentlen = -1;
@@ -262,31 +237,14 @@
                 }
                 // See if we can have better luck with another header, if present
             }
-            entity.setChunked(false);
-            entity.setContentLength(contentlen);
             if (contentlen >= 0) {
-                entity.setContent(new ContentLengthInputStream(datareceiver, contentlen));
+                return contentlen;
             } else {
-                entity.setContent(new HttpDataInputStream(datareceiver));
+                return IDENTITY;
             }
         } else {
-            entity.setChunked(false);
-            entity.setContentLength(-1);
-            entity.setContent(new HttpDataInputStream(datareceiver));                            
-        }
-        if (contentTypeHeader != null) {
-            entity.setContentType(contentTypeHeader);    
+            return IDENTITY;
         }
-        if (contentEncodingHeader != null) {
-            entity.setContentEncoding(contentEncodingHeader);    
-        }
-        return entity;
     }
         
-    public HttpEntity deserialize(
-            final HttpDataReceiver datareceiver,
-            final HttpMessage message) throws HttpException, IOException {
-        return doDeserialize(datareceiver, message);
-    }
-    
 }

Copied: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java (from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java?view=diff&rev=463914&p1=jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java&r1=451782&p2=jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/DefaultEntitySerializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java Sat Oct 14 04:15:28 2006
@@ -29,24 +29,142 @@
 
 package org.apache.http.impl.entity;
 
-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;
 import org.apache.http.HttpVersion;
 import org.apache.http.ProtocolException;
-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.entity.ContentLengthStrategy;
 import org.apache.http.protocol.HTTP;
 
 /**
- * Default implementation of an entity writer on the client side.
+ * The strict implementation of the content length strategy.
+ * <p>
+ * This entity generator comforms to the entity transfer rules outlined in the 
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec4.4">Section 4.4</a>, 
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a>, 
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41">Section 14.41</a>
+ * and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec14.13">Section 14.13</a>
+ * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>
+ * </p>
+ * <h>4.4 Message Length</h>
+ * <p>
+ * The transfer-length of a message is the length of the message-body as it appears in the 
+ * message; that is, after any transfer-codings have been applied. When a message-body is 
+ * included with a message, the transfer-length of that body is determined by one of the 
+ * following (in order of precedence):
+ * </p>
+ * <p>
+ * 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, 
+ * and 304 responses and any response to a HEAD request) is always terminated by the first 
+ * empty line after the header fields, regardless of the entity-header fields present in the 
+ * message.
+ * </p>
+ * <p>
+ * 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other 
+ * than "identity", then the transfer-length is defined by use of the "chunked" transfer-
+ * coding (section 3.6), unless the message is terminated by closing the connection.
+ * </p>
+ * <p>
+ * 3.If a Content-Length header field (section 14.13) is present, its decimal value in 
+ * OCTETs represents both the entity-length and the transfer-length. The Content-Length 
+ * header field MUST NOT be sent if these two lengths are different (i.e., if a 
+ * Transfer-Encoding
+ * </p>
+ * <pre>
+ *    header field is present). If a message is received with both a
+ *    Transfer-Encoding header field and a Content-Length header field,
+ *    the latter MUST be ignored.
+ * </pre>
+ * <p>
+ * 4.If the message uses the media type "multipart/byteranges", and the ransfer-length is not 
+ * otherwise specified, then this self- elimiting media type defines the transfer-length. 
+ * This media type UST NOT be used unless the sender knows that the recipient can arse it; the 
+ * presence in a request of a Range header with ultiple byte- range specifiers from a 1.1 
+ * client implies that the lient can parse multipart/byteranges responses.
+ * </p>
+ * <pre>
+ *     A range header might be forwarded by a 1.0 proxy that does not
+ *     understand multipart/byteranges; in this case the server MUST
+ *     delimit the message using methods defined in items 1,3 or 5 of
+ *     this section.
+ * </pre>
+ * <p>
+ * 5.By the server closing the connection. (Closing the connection cannot be used to indicate 
+ * the end of a request body, since that would leave no possibility for the server to send back 
+ * a response.)
+ * </p>
+ * <p>
+ * For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body 
+ * MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1 
+ * compliant. If a request contains a message-body and a Content-Length is not given, the 
+ * server SHOULD respond with 400 (bad request) if it cannot determine the length of the 
+ * message, or with 411 (length required) if it wishes to insist on receiving a valid 
+ * Content-Length.
+ * </p>
+ * <p>All HTTP/1.1 applications that receive entities MUST accept the "chunked" transfer-coding 
+ * (section 3.6), thus allowing this mechanism to be used for messages when the message 
+ * length cannot be determined in advance. 
+ * </p>
+ * <h>3.6 Transfer Codings</h>
+ * <p>
+ * Transfer-coding values are used to indicate an encoding transformation that 
+ * has been, can be, or may need to be applied to an entity-body in order to ensure 
+ * "safe transport" through the network. This differs from a content coding in that 
+ * the transfer-coding is a property of the message, not of the original entity.
+ * </p>
+ * <pre>
+ * transfer-coding         = "chunked" | transfer-extension
+ * transfer-extension      = token *( ";" parameter )
+ * </pre>
+ * <p>
+ * Parameters are in the form of attribute/value pairs.
+ * </p>
+ * <pre>
+ * parameter               = attribute "=" value
+ * attribute               = token
+ * value                   = token | quoted-string
+ * </pre>
+ * <p>
+ * All transfer-coding values are case-insensitive. HTTP/1.1 uses transfer-coding values in 
+ * the TE header field (section 14.39) and in the Transfer-Encoding header field (section 14.41).
+ * </p>
+ * <p>
+ * Whenever a transfer-coding is applied to a message-body, the set of transfer-codings MUST 
+ * include "chunked", unless the message is terminated by closing the connection. When the 
+ * "chunked" transfer-coding is used, it MUST be the last transfer-coding applied to the 
+ * message-body. The "chunked" transfer-coding MUST NOT be applied more than once to a 
+ * message-body. These rules allow the recipient to determine the transfer-length of the 
+ * message (section 4.4).
+ * </p>
+ * <h>14.41 Transfer-Encoding</h>
+ * <p>
+ * The Transfer-Encoding general-header field indicates what (if any) type of transformation has 
+ * been applied to the message body in order to safely transfer it between the sender and the 
+ * recipient. This differs from the content-coding in that the transfer-coding is a property of 
+ * the message, not of the entity.
+ * </p>
+ * <pre>
+ *   Transfer-Encoding       = "Transfer-Encoding" ":" 1#transfer-coding
+ * </pre>
+ * <p>
+ * If multiple encodings have been applied to an entity, the transfer- codings MUST be listed in 
+ * the order in which they were applied. Additional information about the encoding parameters 
+ * MAY be provided by other entity-header fields not defined by this specification.
+ * </p> 
+ * <h>14.13 Content-Length</h>
+ * <p>
+ * The Content-Length entity-header field indicates the size of the entity-body, in decimal 
+ * number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of 
+ * the entity-body that would have been sent had the request been a GET.
+ * </p>
+ * <pre>
+ *   Content-Length    = "Content-Length" ":" 1*DIGIT
+ * </pre>
+ * <p>
+ * Applications SHOULD use this field to indicate the transfer-length of the message-body, 
+ * unless this is prohibited by the rules in section 4.4. 
+ * </p>
  * 
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  *
@@ -54,15 +172,16 @@
  * 
  * @since 4.0
  */
-public class DefaultEntitySerializer implements EntitySerializer {
+public class StrictContentLengthStrategy implements ContentLengthStrategy {
 
-    public DefaultEntitySerializer() {
+    public StrictContentLengthStrategy() {
         super();
     }
     
-    protected OutputStream doSerialize(
-            final HttpDataTransmitter datatransmitter,
-            final HttpMessage message) throws HttpException, IOException {
+    public long determineLength(final HttpMessage message) throws HttpException {
+        if (message == null) {
+            throw new IllegalArgumentException("HTTP message may not be null");
+        }
         Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
         Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
         if (transferEncodingHeader != null) {
@@ -73,9 +192,9 @@
                             "Chunked transfer encoding not allowed for " + 
                             message.getHttpVersion());
                 }
-                return new ChunkedOutputStream(datatransmitter);
+                return CHUNKED;
             } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
-                return new IdentityOutputStream(datatransmitter);
+                return IDENTITY;
             } else {
                 throw new ProtocolException(
                         "Unsupported transfer encoding: " + s);
@@ -84,31 +203,13 @@
             String s = contentLengthHeader.getValue();
             try {
                 long len = Long.parseLong(s);
-                return new ContentLengthOutputStream(datatransmitter, len);
+                return len;
             } catch (NumberFormatException e) {
                 throw new ProtocolException("Invalid content length: " + s);
             }
         } else {
-            return new IdentityOutputStream(datatransmitter); 
+            return IDENTITY; 
         }
     }
 
-    public void serialize(
-            final HttpDataTransmitter datatransmitter,
-            final HttpMessage message,
-            final HttpEntity entity) throws HttpException, IOException {
-        if (datatransmitter == null) {
-            throw new IllegalArgumentException("HTTP data transmitter may not be null");
-        }
-        if (message == null) {
-            throw new IllegalArgumentException("HTTP message may not be null");
-        }
-        if (entity == null) {
-            throw new IllegalArgumentException("HTTP entity may not be null");
-        }
-        OutputStream outstream = doSerialize(datatransmitter, message);
-        entity.writeTo(outstream);
-        outstream.close();
-    }
-    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestAllEntityImpl.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestAllEntityImpl.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestAllEntityImpl.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestAllEntityImpl.java Sat Oct 14 04:15:28 2006
@@ -40,6 +40,8 @@
         TestSuite suite = new TestSuite();
         suite.addTest(TestDefaultEntitySerializer.suite());
         suite.addTest(TestDefaultEntityDeserializer.suite());
+        suite.addTest(TestLaxContentLengthStrategy.suite());
+        suite.addTest(TestStrictContentLengthStrategy.suite());
         return suite;
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java Sat Oct 14 04:15:28 2006
@@ -66,7 +66,8 @@
     }
 
     public void testIllegalGenerateArg() throws Exception {
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         try {
             entitygen.deserialize(null, null);
             fail("IllegalArgumentException should have been thrown");
@@ -90,7 +91,8 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "identity, chunked");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -116,7 +118,8 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "identity");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -132,7 +135,8 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "whatever; param=value, chunked");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -158,7 +162,8 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "chunked, identity");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -183,7 +188,8 @@
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "0");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(0, entity.getContentLength());
@@ -201,7 +207,8 @@
         message.addHeader("Content-Length", "0");
         message.addHeader("Content-Length", "0");
         message.addHeader("Content-Length", "1");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(1, entity.getContentLength());
@@ -230,7 +237,8 @@
         message.addHeader("Content-Length", "1");
         message.addHeader("Content-Length", "yyy");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(1, entity.getContentLength());
@@ -258,7 +266,8 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "yyy");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -286,7 +295,8 @@
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -312,7 +322,8 @@
 
         // lenient mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertEquals(-1, entity.getContentLength());
@@ -329,7 +340,8 @@
         HttpMessage message = new HttpMessageMockup();
 
         message.addHeader("Content-Type", "stuff");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertNotNull(entity.getContentType());
@@ -341,7 +353,8 @@
         HttpMessage message = new HttpMessageMockup();
 
         message.addHeader("Content-Encoding", "what not");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
+        EntityDeserializer entitygen = new DefaultEntityDeserializer(
+                new LaxContentLengthStrategy());
         HttpEntity entity = entitygen.deserialize(datareceiver, message);
         assertNotNull(entity);
         assertNotNull(entity.getContentEncoding());

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java?view=diff&rev=463914&r1=463913&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java Sat Oct 14 04:15:28 2006
@@ -66,7 +66,8 @@
     }
 
     public void testIllegalGenerateArg() throws Exception {
-        EntitySerializer entitywriter = new DefaultEntitySerializer();
+        EntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         try {
             entitywriter.serialize(null, null, null);
             fail("IllegalArgumentException should have been thrown");
@@ -92,7 +93,8 @@
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "Chunked");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
         assertNotNull(outstream);
         assertTrue(outstream instanceof ChunkedOutputStream);
@@ -103,7 +105,8 @@
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "Identity");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
         assertNotNull(outstream);
         assertTrue(outstream instanceof IdentityOutputStream);
@@ -114,7 +117,8 @@
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "whatever");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         try {
             entitywriter.doSerialize(datatransmitter, message);
             fail("ProtocolException should have been thrown");
@@ -130,7 +134,8 @@
                 HttpVersion.HTTP_1_0);
         message.addHeader("Transfer-Encoding", "chunked");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         try {
             entitywriter.doSerialize(datatransmitter, message);
             fail("ProtocolException should have been thrown");
@@ -143,7 +148,8 @@
         HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Content-Length", "100");
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
         assertNotNull(outstream);
         assertTrue(outstream instanceof ContentLengthOutputStream);
@@ -154,7 +160,8 @@
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Content-Length", "whatever");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         try {
             entitywriter.doSerialize(datatransmitter, message);
             fail("ProtocolException should have been thrown");
@@ -166,7 +173,8 @@
     public void testEntityNoContentDelimiter() throws Exception {
         HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
         HttpMessage message = new HttpMessageMockup();
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
         assertNotNull(outstream);
         assertTrue(outstream instanceof IdentityOutputStream);
@@ -180,7 +188,8 @@
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Content-Length", Integer.toString(content.length));
         
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
+        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer(
+                new StrictContentLengthStrategy());
         entitywriter.serialize(datatransmitter, message, entity);
         
         byte[] data = datatransmitter.getData();

Copied: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java (from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java?view=diff&rev=463914&p1=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java&r1=451782&p2=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntityDeserializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java Sat Oct 14 04:15:28 2006
@@ -28,28 +28,19 @@
 
 package org.apache.http.impl.entity;
 
-import java.io.InputStream;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
 
-import org.apache.http.HttpEntity;
 import org.apache.http.HttpMessage;
 import org.apache.http.ProtocolException;
-import org.apache.http.entity.EntityDeserializer;
-import org.apache.http.impl.entity.DefaultEntityDeserializer;
-import org.apache.http.io.ChunkedInputStream;
-import org.apache.http.io.ContentLengthInputStream;
-import org.apache.http.io.HttpDataInputStream;
-import org.apache.http.io.HttpDataReceiver;
-import org.apache.http.mockup.HttpDataReceiverMockup;
+import org.apache.http.entity.ContentLengthStrategy;
 import org.apache.http.mockup.HttpMessageMockup;
 import org.apache.http.params.HttpProtocolParams;
 
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-public class TestDefaultEntityDeserializer extends TestCase {
+public class TestLaxContentLengthStrategy extends TestCase {
 
-    public TestDefaultEntityDeserializer(String testName) {
+    public TestLaxContentLengthStrategy(String testName) {
         super(testName);
     }
 
@@ -65,24 +56,8 @@
         junit.textui.TestRunner.main(testCaseName);
     }
 
-    public void testIllegalGenerateArg() throws Exception {
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        try {
-            entitygen.deserialize(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            entitygen.deserialize(new HttpDataReceiverMockup(new byte[] {}) , null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-    }
-
     public void testEntityWithTransferEncoding() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup("0\r\n", "US-ASCII");
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         
         // lenient mode 
@@ -90,25 +65,15 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "identity, chunked");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertTrue(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ChunkedInputStream);
+        assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
 
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
-        entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertTrue(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ChunkedInputStream);
+        assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
     }
 
     public void testEntityWithIdentityTransferEncoding() throws Exception {
-        HttpDataReceiver datareceiver = 
-        	new HttpDataReceiverMockup(new byte[] {});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         
         // lenient mode 
@@ -116,15 +81,11 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "identity");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
+        assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
     }
 
     public void testEntityWithUnsupportedTransferEncoding() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup("0\r\n", "US-ASCII");
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         
         // lenient mode 
@@ -132,17 +93,12 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "whatever; param=value, chunked");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertTrue(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ChunkedInputStream);
+        assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
 
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -150,7 +106,7 @@
     }
 
     public void testChunkedTransferEncodingMustBeLast() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup("0\r\n", "US-ASCII");
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         
         // lenient mode 
@@ -158,17 +114,12 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Transfer-Encoding", "chunked, identity");
         message.addHeader("Content-Length", "plain wrong");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        assertFalse(entity.getContent() instanceof ChunkedInputStream);
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
 
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -176,23 +127,18 @@
     }
 
     public void testEntityWithContentLength() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         
         // lenient mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "0");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(0, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        assertTrue(entity.getContent() instanceof ContentLengthInputStream);
+        assertEquals(0, lenStrategy.determineLength(message));
     }
 
     public void testEntityWithMultipleContentLength() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
 
         // lenient mode 
@@ -201,19 +147,12 @@
         message.addHeader("Content-Length", "0");
         message.addHeader("Content-Length", "0");
         message.addHeader("Content-Length", "1");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        InputStream instream = entity.getContent();
-        assertNotNull(instream);
-        assertTrue(instream instanceof ContentLengthInputStream);
+        assertEquals(1, lenStrategy.determineLength(message));
         
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -221,7 +160,7 @@
     }
     
     public void testEntityWithMultipleContentLengthSomeWrong() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
 
         // lenient mode 
@@ -230,19 +169,12 @@
         message.addHeader("Content-Length", "1");
         message.addHeader("Content-Length", "yyy");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        InputStream instream = entity.getContent();
-        assertNotNull(instream);
-        assertTrue(instream instanceof ContentLengthInputStream);
+        assertEquals(1, lenStrategy.determineLength(message));
         
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -250,7 +182,7 @@
     }
     
     public void testEntityWithMultipleContentLengthAllWrong() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
 
         // lenient mode 
@@ -258,20 +190,12 @@
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "yyy");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        InputStream instream = entity.getContent();
-        assertNotNull(instream);
-        assertFalse(instream instanceof ContentLengthInputStream);
-        assertTrue(instream instanceof HttpDataInputStream);
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
         
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -279,27 +203,19 @@
     }
 
     public void testEntityWithInvalidContentLength() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
 
         // lenient mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
         message.addHeader("Content-Type", "unknown");
         message.addHeader("Content-Length", "xxx");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        InputStream instream = entity.getContent();
-        assertNotNull(instream);
-        assertFalse(instream instanceof ContentLengthInputStream);
-        assertTrue(instream instanceof HttpDataInputStream);
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
         
         // strict mode 
         message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
         try {
-            entitygen.deserialize(datareceiver, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -307,46 +223,12 @@
     }
 
     public void testEntityNeitherContentLengthNorTransferEncoding() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
 
         // lenient mode 
-        message.getParams().setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertEquals(-1, entity.getContentLength());
-        assertFalse(entity.isChunked());
-        InputStream instream = entity.getContent();
-        assertNotNull(instream);
-        assertFalse(instream instanceof ContentLengthInputStream);
-        assertFalse(instream instanceof ChunkedInputStream);
-        assertTrue(instream instanceof HttpDataInputStream);
-    }
-
-    public void testEntityContentType() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
-        HttpMessage message = new HttpMessageMockup();
-
-        message.addHeader("Content-Type", "stuff");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertNotNull(entity.getContentType());
-        assertEquals("stuff", entity.getContentType().getValue());
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
     }
 
-    public void testEntityContentEncoding() throws Exception {
-        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
-        HttpMessage message = new HttpMessageMockup();
-
-        message.addHeader("Content-Encoding", "what not");
-        EntityDeserializer entitygen = new DefaultEntityDeserializer();
-        HttpEntity entity = entitygen.deserialize(datareceiver, message);
-        assertNotNull(entity);
-        assertNotNull(entity.getContentEncoding());
-        assertEquals("what not", entity.getContentEncoding().getValue());
-    }
-    
 }
 

Copied: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestStrictContentLengthStrategy.java (from r451782, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestStrictContentLengthStrategy.java?view=diff&rev=463914&p1=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java&r1=451782&p2=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestStrictContentLengthStrategy.java&r2=463914
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestDefaultEntitySerializer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/entity/TestStrictContentLengthStrategy.java Sat Oct 14 04:15:28 2006
@@ -28,8 +28,6 @@
 
 package org.apache.http.impl.entity;
 
-import java.io.OutputStream;
-
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -37,19 +35,13 @@
 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.entity.ContentLengthStrategy;
 import org.apache.http.mockup.HttpMessageMockup;
 import org.apache.http.params.HttpProtocolParams;
 
-public class TestDefaultEntitySerializer extends TestCase {
+public class TestStrictContentLengthStrategy extends TestCase {
 
-    public TestDefaultEntitySerializer(String testName) {
+    public TestStrictContentLengthStrategy(String testName) {
         super(testName);
     }
 
@@ -65,58 +57,29 @@
         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();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "Chunked");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
-        OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
-        assertNotNull(outstream);
-        assertTrue(outstream instanceof ChunkedOutputStream);
+        assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
     }
 
     public void testEntityWithIdentityTransferEncoding() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "Identity");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
-        OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
-        assertNotNull(outstream);
-        assertTrue(outstream instanceof IdentityOutputStream);
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
     }
     
     public void testEntityWithInvalidTransferEncoding() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Transfer-Encoding", "whatever");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
         try {
-            entitywriter.doSerialize(datatransmitter, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -124,15 +87,14 @@
     }
     
     public void testEntityWithInvalidChunkEncodingAndHTTP10() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, 
                 HttpVersion.HTTP_1_0);
         message.addHeader("Transfer-Encoding", "chunked");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
         try {
-            entitywriter.doSerialize(datatransmitter, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -140,23 +102,19 @@
     }
     
     public void testEntityWithContentLength() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Content-Length", "100");
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
-        OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
-        assertNotNull(outstream);
-        assertTrue(outstream instanceof ContentLengthOutputStream);
+        assertEquals(100, lenStrategy.determineLength(message));
     }
     
     public void testEntityWithInvalidContentLength() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
         message.addHeader("Content-Length", "whatever");
 
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
         try {
-            entitywriter.doSerialize(datatransmitter, message);
+            lenStrategy.determineLength(message);
             fail("ProtocolException should have been thrown");
         } catch (ProtocolException ex) {
             // expected
@@ -164,28 +122,10 @@
     }
 
     public void testEntityNoContentDelimiter() throws Exception {
-        HttpDataTransmitter datatransmitter = new HttpDataTransmitterMockup();
+        ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
         HttpMessage message = new HttpMessageMockup();
-        DefaultEntitySerializer entitywriter = new DefaultEntitySerializer();
-        OutputStream outstream = entitywriter.doSerialize(datatransmitter, message);
-        assertNotNull(outstream);
-        assertTrue(outstream instanceof IdentityOutputStream);
+        assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
     }
         
-    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("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);
-    }
 }
 



Mime
View raw message