abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r433505 - /incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java
Date Tue, 22 Aug 2006 04:35:22 GMT
Author: jmsnell
Date: Mon Aug 21 21:35:22 2006
New Revision: 433505

URL: http://svn.apache.org/viewvc?rev=433505&view=rev
Log:
The old implementation always used chunked requests.  Some proxies and servers, however,
require the Content-Length header and no chunking of the request.  Add a flag to the
BaseRequestEntity utility class to enable non-chunked requests.

Modified:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java?rev=433505&r1=433504&r2=433505&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java
(original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/BaseRequestEntity.java
Mon Aug 21 21:35:22 2006
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.protocol.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
@@ -34,22 +35,53 @@
 public class BaseRequestEntity 
   implements RequestEntity {
 
-  Base base = null;
+  private Base base = null;
+  private byte[] buf = null;
+  private boolean use_chunked = true;
   
   public BaseRequestEntity(Base base) {
     this.base = base;
   }
   
+  public BaseRequestEntity(Base base, boolean use_chunked) {
+    this(base);
+    this.use_chunked = use_chunked;
+  }
+  
   public boolean isRepeatable() {
     return true;
   }
 
   public void writeRequest(OutputStream out) throws IOException {
-    base.writeTo(out);
+    if (use_chunked)
+      base.writeTo(out);
+    else {
+      // if we're not using chunked requests, the getContentLength method
+      // has likely already been called and we want to just go ahead and
+      // use the buffered output rather than reserialize
+      if (buf == null) getContentLength();  // ensures that the content is buffered
+      out.write(buf);
+      out.flush();
+    }
   }
 
   public long getContentLength() {
-    return -1;  // chunk the response
+    if (use_chunked)
+      return -1;  // chunk the response
+    else {
+      // this is ugly, but some proxies and server configurations (e.g. gdata)
+      // require that requests contain the Content-Length header.  The only
+      // way to get that is to serialize the document into a byte array, which
+      // we buffer into memory.
+      if (buf == null) {
+        try {
+          ByteArrayOutputStream out = new ByteArrayOutputStream();
+          base.writeTo(out);
+          buf = out.toByteArray();
+        } catch (Exception e) {}
+      }
+      return buf.length;
+    }
   }
 
   public String getContentType() {



Mime
View raw message