You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/06/28 01:17:40 UTC

svn commit: r958435 - /click/trunk/click/framework/src/org/apache/click/Partial.java

Author: sabob
Date: Sun Jun 27 23:17:40 2010
New Revision: 958435

URL: http://svn.apache.org/viewvc?rev=958435&view=rev
Log:
added support for streaming bytes to Partial

Modified:
    click/trunk/click/framework/src/org/apache/click/Partial.java

Modified: click/trunk/click/framework/src/org/apache/click/Partial.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/Partial.java?rev=958435&r1=958434&r2=958435&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/Partial.java (original)
+++ click/trunk/click/framework/src/org/apache/click/Partial.java Sun Jun 27 23:17:40 2010
@@ -18,6 +18,7 @@
  */
 package org.apache.click;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
@@ -32,8 +33,6 @@ import org.apache.click.util.ClickUtils;
 /**
  * TODO rename partial as its used for both PageAction and Ajax?
  *
- * TODO add support for rendering a given template
- *
  * Partial encapsulates a fragment of an HTTP response. A Partial can be used
  * to stream back a String or byte array to the browser.
  *
@@ -81,7 +80,10 @@ public class Partial {
     // -------------------------------------------------------- Variables
 
     /** The content to render. */
-    private Object content;
+    private String content;
+
+    /** The content as a byte array. */
+    private byte[] bytes;
 
     /** The servlet response reader. */
     private Reader reader;
@@ -168,29 +170,34 @@ public class Partial {
 
     /**
      * Construct the Partial for the given content and content type.
-     * <p/>
-     * At rendering time the partial invokes the Object's <tt>toString()</tt>
-     * method and streams the resulting <tt>String</tt> back to the client.
      *
      * @param content the content to stream back to the client
      * @param contentType the response content type
      */
-    public Partial(Object content, String contentType) {
+    public Partial(String content, String contentType) {
         this.content = content;
         this.contentType = contentType;
     }
 
     /**
+     * Construct the Partial for the given byte array and content type.
+     *
+     * @param bytes the byte array to stream back to the client
+     * @param contentType the response content type
+     */
+    public Partial(byte[] bytes, String contentType) {
+        this.bytes = bytes;
+        this.contentType = contentType;
+    }
+
+    /**
      * Construct the Partial for the given content. The
      * <tt>{@link javax.servlet.http.HttpServletResponse#setContentType(java.lang.String) response content type}</tt>
      * will default to {@link #TEXT}.
-     * <p/>
-     * At rendering time the partial invokes the Object's <tt>toString()</tt>
-     * method and streams the resulting <tt>String</tt> back to the client.
      *
      * @param content the content to stream back to the client
      */
-    public Partial(Object content) {
+    public Partial(String content) {
         this.content = content;
         this.contentType = TEXT;
     }
@@ -199,7 +206,6 @@ public class Partial {
      * Construct a new empty Partial. The
      * <tt>{@link javax.servlet.http.HttpServletResponse#setContentType(java.lang.String) response content type}</tt>
      * will default to {@link #TEXT}.
-     *
      */
     public Partial() {
         this.contentType = TEXT;
@@ -267,7 +273,7 @@ public class Partial {
      *
      * @param content the content to stream back to the client
      */
-    public void setContent(Object content) {
+    public void setContent(String content) {
         this.content = content;
     }
 
@@ -276,11 +282,30 @@ public class Partial {
      *
      * @return the content to stream back to the client
      */
-    public Object getContent() {
+    public String getContent() {
         return content;
     }
 
     /**
+     * Set the byte array to stream back to the client.
+     *
+     * @param bytes the byte array to stream back to the client
+     */
+    public void setBytes(byte[] bytes, String contentType) {
+        this.bytes = bytes;
+        this.contentType = contentType;
+    }
+
+    /**
+     * Return the byte array to stream back to the client.
+     *
+     * @return the byte array to stream back to the client
+     */
+    public byte[] getBytes() {
+        return bytes;
+    }
+
+    /**
      * Set the content to stream back to the client.
      *
      * @param inputStream the inputStream to stream back to the client
@@ -377,7 +402,13 @@ public class Partial {
 
         HttpServletResponse response = context.getResponse();
 
+        Reader reader = getReader();
+        InputStream inputStream = getInputStream();
+
         try {
+            String content = getContent();
+            byte[] bytes = getBytes();
+
             String template = getTemplate();
             if (template != null) {
                 Map<String, Object> templateModel = getModel();
@@ -385,10 +416,12 @@ public class Partial {
                     templateModel = new HashMap<String, Object>();
                 }
                 String result = context.renderTemplate(template, templateModel);
-                this.reader = new StringReader(result);
+                reader = new StringReader(result);
 
             } else if (content != null) {
-                this.reader = new StringReader(content.toString());
+                reader = new StringReader(content);
+            } else if (bytes != null) {
+                inputStream = new ByteArrayInputStream(bytes);
             }
 
             if (reader != null) {