You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/11/21 00:44:44 UTC

svn commit: r596881 - in /incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client: AbderaClient.java EntityProvider.java util/ContentProviderRequestEntity.java

Author: jmsnell
Date: Tue Nov 20 15:44:43 2007
New Revision: 596881

URL: http://svn.apache.org/viewvc?rev=596881&view=rev
Log:
EntityProvider makes it possible to use the new StreamWriter interface on 
the client side with POST and PUT operations.

Added:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/EntityProvider.java
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/ContentProviderRequestEntity.java
Modified:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java?rev=596881&r1=596880&r2=596881&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java Tue Nov 20 15:44:43 2007
@@ -38,6 +38,7 @@
 import org.apache.abdera.protocol.client.cache.CachedResponse;
 import org.apache.abdera.protocol.client.cache.lru.LRUCache;
 import org.apache.abdera.protocol.client.util.BaseRequestEntity;
+import org.apache.abdera.protocol.client.util.ContentProviderRequestEntity;
 import org.apache.abdera.protocol.client.util.MethodHelper;
 import org.apache.abdera.protocol.client.util.SimpleSSLProtocolSocketFactory;
 import org.apache.abdera.protocol.error.Error;
@@ -136,6 +137,20 @@
   
   public ClientResponse post(
     String uri, 
+    EntityProvider provider, 
+    RequestOptions options) {
+      return post(
+        uri, 
+        new ContentProviderRequestEntity(
+          abdera,
+          provider,
+          options.isUseChunked()
+        ), 
+        options);
+  }
+  
+  public ClientResponse post(
+    String uri, 
     RequestEntity entity, 
     RequestOptions options) {
       return execute("POST", uri, entity, options);
@@ -160,7 +175,21 @@
       }
       return execute("POST", uri, new BaseRequestEntity(base, options.isUseChunked()), options);
   }
-    
+
+  public ClientResponse put(
+    String uri, 
+    EntityProvider provider, 
+    RequestOptions options) {
+      return put(
+        uri, 
+        new ContentProviderRequestEntity(
+          abdera,
+          provider,
+          options.isUseChunked()
+        ), 
+        options);
+  }
+  
   public ClientResponse put(
     String uri, 
     RequestEntity entity, 

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/EntityProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/EntityProvider.java?rev=596881&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/EntityProvider.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/EntityProvider.java Tue Nov 20 15:44:43 2007
@@ -0,0 +1,44 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client;
+
+import org.apache.abdera.writer.StreamWriter;
+
+/**
+ * An EntityProvider is used to serialize client requests using the StreamWriter
+ * interface.  The EntityProvider interface can be implemented by client applications
+ * to provide an efficient means of serializing non-FOM objects to Atom/XML. 
+ */
+public interface EntityProvider {
+
+  /**
+   * Write to the specified StreamWriter
+   */
+  void writeTo(StreamWriter sw);
+ 
+  /**
+   * True if the serialization is repeatable. 
+   */
+  boolean isRepeatable();
+  
+  /**
+   * Return the mime content type of the serialized entity
+   */
+  String getContentType();
+  
+}

Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/ContentProviderRequestEntity.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/ContentProviderRequestEntity.java?rev=596881&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/ContentProviderRequestEntity.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/ContentProviderRequestEntity.java Tue Nov 20 15:44:43 2007
@@ -0,0 +1,111 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.protocol.client.EntityProvider;
+import org.apache.commons.httpclient.methods.RequestEntity;
+
+public class ContentProviderRequestEntity 
+  implements RequestEntity {
+  
+  private final Abdera abdera;
+  private final EntityProvider provider;
+  private byte[] buf = null;
+  private boolean use_chunked = true;
+  private boolean auto_indent = false;
+  private String encoding = "UTF-8";
+  
+  public ContentProviderRequestEntity(
+    Abdera abdera,
+    EntityProvider provider, 
+    boolean use_chunked) {
+      this.abdera = abdera;
+      this.use_chunked = use_chunked;
+      this.provider = provider;
+  }
+  
+  private void write(OutputStream out) {
+    provider.writeTo(
+      abdera.newStreamWriter()
+            .setOutputStream(out, encoding)
+            .setAutoIndent(auto_indent));
+  }
+  
+  public long getContentLength() {
+    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();
+          write(out);
+          buf = out.toByteArray();
+        } catch (Exception e) {}
+      }
+      return buf.length;
+    }
+  }
+  
+  public String getContentType() {
+    return provider.getContentType();
+  }
+  
+  public boolean isRepeatable() {
+    return provider.isRepeatable();
+  }
+  
+  public void writeRequest(
+    OutputStream out) 
+      throws IOException {
+    if (use_chunked)
+      write(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 boolean isAutoIndent() {
+    return auto_indent;
+  }
+  
+  public void setAutoIndent(boolean auto_indent) {
+    this.auto_indent = auto_indent;
+  }
+  
+  public String getEncoding() {
+    return encoding;
+  }
+  
+  public void setEncoding(String encoding) {
+    this.encoding = encoding;
+  }
+}