abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r596873 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/util/ core/src/main/java/org/apache/abdera/writer/ examples/src/main/java/org/apache/abdera/examples/appserver/ server/src/main/java/org/apache/abdera/protocol/...
Date Tue, 20 Nov 2007 23:17:04 GMT
Author: jmsnell
Date: Tue Nov 20 15:17:03 2007
New Revision: 596873

URL: http://svn.apache.org/viewvc?rev=596873&view=rev
Log:
Provide an example of using the StreamWriterResponseContext.  
Provide utility methods on StreamWriter to indicate that a collection accepts entries and
nothing, etc.  

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/StreamWriterResponseContext.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java?rev=596873&r1=596872&r2=596873&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbstractStreamWriter.java
Tue Nov 20 15:17:03 2007
@@ -567,6 +567,14 @@
     return this;
   }
   
+  public StreamWriter writeAcceptsEntry() {
+    return writeAccepts("application/atom+xml;type=entry");
+  }
+  
+  public StreamWriter writeAcceptsNothing() {
+    return writeAccepts("");
+  }
+  
   public StreamWriter startCategories() {
     return startCategories(false,null);
   }

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java?rev=596873&r1=596872&r2=596873&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
Tue Nov 20 15:17:03 2007
@@ -705,6 +705,16 @@
   StreamWriter writeAccepts(String... accepts);
   
   /**
+   * Writes an app:accept element indicating that entries are accepted 
+   */
+  StreamWriter writeAcceptsEntry();
+  
+  /**
+   * Writes an app:accept element indicating that nothing is accepted
+   */
+  StreamWriter writeAcceptsNothing();
+  
+  /**
    * Start an app:categories element
    */
   StreamWriter startCategories();

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java?rev=596873&r1=596872&r2=596873&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
Tue Nov 20 15:17:03 2007
@@ -17,19 +17,18 @@
 */
 package org.apache.abdera.examples.appserver;
 
+import java.io.IOException;
 import java.util.Date;
 
 import javax.activation.MimeType;
 
 import org.apache.abdera.Abdera;
 import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Base;
-import org.apache.abdera.model.Collection;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
-import org.apache.abdera.model.Service;
-import org.apache.abdera.model.Workspace;
 import org.apache.abdera.parser.ParseException;
 import org.apache.abdera.parser.Parser;
 import org.apache.abdera.protocol.server.Provider;
@@ -40,34 +39,22 @@
 import org.apache.abdera.protocol.server.impl.AbstractResponseContext;
 import org.apache.abdera.protocol.server.impl.BaseResponseContext;
 import org.apache.abdera.protocol.server.impl.EmptyResponseContext;
+import org.apache.abdera.protocol.server.impl.StreamWriterResponseContext;
 import org.apache.abdera.util.EntityTag;
 import org.apache.abdera.util.MimeTypeHelper;
-import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.writer.StreamWriter;
 
 public class SimpleProvider 
   extends AbstractProvider
   implements Provider {
 
   private EntityTag service_etag = new EntityTag("simple");
-  private Document<Service> service_doc;
   private Document<Feed> feed_doc;
   
   public SimpleProvider() {
     super(10);
   }
   
-  private Document<Service> init_service_doc(Abdera abdera) {
-    Factory factory = abdera.getFactory();
-    Service service = factory.newService();
-    Workspace workspace = service.addWorkspace("Simple");
-    try {
-      Collection collection = workspace.addCollection("Simple", "atom/feed");
-      collection.setAccept("entry");
-      collection.addCategories().setFixed(false);
-    } catch (Exception e) {}
-    return service.getDocument();
-  }
-  
   private Document<Feed> init_feed_doc(Abdera abdera) {
     Factory factory = abdera.getFactory();
     Feed feed = factory.newFeed();
@@ -82,13 +69,6 @@
     return feed.getDocument();
   }
   
-  private synchronized Document<Service> get_service_doc(Abdera abdera) {
-    if (service_doc == null) {
-      service_doc = init_service_doc(abdera);
-    }
-    return service_doc;
-  }
-  
   private synchronized Document<Feed> get_feed_doc(Abdera abdera) {
     if (feed_doc == null) {
       feed_doc = init_feed_doc(abdera);
@@ -96,16 +76,42 @@
     return feed_doc;
   }
   
+  /**
+   * Demonstrates how to use the StreamWriterResponseContext to serialize
+   * a response document.  The alternative is to use the BaseResponseContext
+   * which uses a FOM Object Model.  The StreamWriterResponseContext is faster
+   * and requires less memory since it streams directly to the outputstream
+   * or writer
+   */
   public ResponseContext getService(
     RequestContext request) {
       Abdera abdera = request.getAbdera();
-      Document<Service> service = get_service_doc(abdera);
-      AbstractResponseContext rc; 
-      rc = new BaseResponseContext<Document<Service>>(service); 
+      AbstractResponseContext rc = 
+        new StreamWriterResponseContext(abdera) {
+          protected void writeTo(
+            StreamWriter sw) 
+              throws IOException {
+            sw.startDocument()
+              .startWorkspace()
+              .writeTitle("Simple")
+              .startCollection("atom/feed")
+              .writeTitle("Simple")
+              .writeAcceptsEntry()
+              .startCategories(false)
+              .endCategories()
+              .endCollection()
+              .endWorkspace()
+              .endDocument();
+          }
+        }; 
       rc.setEntityTag(service_etag);
       return rc;
   }
-  
+
+  /**
+   * Demonstates how to use the BaseResponseContext to serialize a response
+   * document.
+   */
   public ResponseContext getFeed(
     RequestContext request) {
       Abdera abdera = request.getAbdera();

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/StreamWriterResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/StreamWriterResponseContext.java?rev=596873&r1=596872&r2=596873&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/StreamWriterResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/StreamWriterResponseContext.java
Tue Nov 20 15:17:03 2007
@@ -37,6 +37,7 @@
   private final Abdera abdera;
   private final String sw;
   private boolean autoindent;
+  private String encoding = "UTF-8";
   
   /**
    * Create a new StreamWriterResponseContext
@@ -82,7 +83,7 @@
   public void writeTo(
     OutputStream out) 
       throws IOException {
-    writeTo(new OutputStreamWriter(out,"UTF-8"));
+    writeTo(new OutputStreamWriter(out,encoding));
   }
 
   /**
@@ -135,5 +136,23 @@
    */
   public boolean getAutoIndent() {
     return this.autoindent;
+  }
+
+  public boolean hasEntity() {
+    return true;
+  }
+
+  /**
+   * Return the character set encoding used when writing to an outputstream
+   */
+  public String getEncoding() {
+    return encoding;
+  }
+  
+  /**
+   * Set the character set encoding used when writing to an outputstream
+   */
+  public void setEncoding(String encoding) {
+    this.encoding = encoding;
   }
 }



Mime
View raw message