click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r949701 - /click/trunk/click/framework/src/org/apache/click/Partial.java
Date Mon, 31 May 2010 09:57:55 GMT
Author: sabob
Date: Mon May 31 09:57:55 2010
New Revision: 949701

URL: http://svn.apache.org/viewvc?rev=949701&view=rev
Log:
support multiple headers and properly close writer and outputstream

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=949701&r1=949700&r2=949701&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/Partial.java (original)
+++ click/trunk/click/framework/src/org/apache/click/Partial.java Mon May 31 09:57:55 2010
@@ -23,15 +23,18 @@ import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringReader;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.click.util.ClickUtils;
 
 /**
+ * TODO rename partial as its used for both PageAction and Ajax?
+ *
  * Partial encapsulates a fragment of an HTTP response. A Partial can be used
  * to stream back a String or byte array to the browser.
  *
@@ -73,6 +76,9 @@ public class Partial {
     /** The Partial writer buffer size. */
     private static final int WRITER_BUFFER_SIZE = 256;
 
+    /** The Partial output buffer size. */
+    private static final int OUTPUT_BUFFER_SIZE = 4 * 1024;
+
     // -------------------------------------------------------- Variables
 
     /** The content to render. */
@@ -218,6 +224,18 @@ public class Partial {
     }
 
     /**
+     * Returns a boolean indicating whether the named response header has
+     * already been set.
+     *
+     * @param name the header name
+     * @return true if the named response header has already been set, false
+     * otherwise
+     */
+    public boolean containsHeader(String name) {
+        return getHeaders().containsKey(name);
+    }
+
+    /**
      * Return the map of response header values.
      *
      * @return the map of response header values
@@ -230,6 +248,41 @@ public class Partial {
     }
 
     /**
+     * Sets a response header with the given name and value. If the header had
+     * already been set, the new value overwrites the previous one.
+     *
+     * @param name the name of the header
+     * @param value the header value
+     */
+    public void setHeader(String name, String value) {
+        getHeaders().put(name, value);
+    }
+
+    /**
+     * Adds a response header with the given name and value. This method allows
+     * response headers to have multiple values.
+     *
+     * @param name the name of the header
+     * @param value the header value
+     */
+    public void addHeader(String name, String value) {
+        Map<String, Object> headersMap = getHeaders();
+        Object currValue = headersMap.get(name);
+        if (currValue == null) {
+            headersMap.put(name, value);
+
+        } else if (currValue instanceof List) {
+            ((List) currValue).add(value);
+
+        } else {
+            List list = new ArrayList();
+            list.add(currValue);
+            list.add(value);
+            headersMap.put(name, list);
+        }
+    }
+
+    /**
      * Set the content to stream back to the client.
      *
      * @param content the content to stream back to the client
@@ -313,14 +366,18 @@ public class Partial {
                 while (-1 != (len = reader.read(buffer))) {
                     writer.write(buffer, 0, len);
                 }
+                writer.flush();
+                writer.close();
 
             } else if (inputStream != null) {
-                byte[] buffer = new byte[WRITER_BUFFER_SIZE];
+                byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
                 int len = 0;
                 OutputStream outputStream = response.getOutputStream();
                 while (-1 != (len = inputStream.read(buffer))) {
                     outputStream.write(buffer, 0, len);
                 }
+                outputStream.flush();
+                outputStream.close();
             }
 
         } catch (Exception e) {
@@ -347,30 +404,42 @@ public class Partial {
         }
     }
 
-    private void setResponseHeaders(HttpServletResponse response, Map headers) {
+    private void setResponseHeaders(HttpServletResponse response, Map<String, Object>
headers) {
 
-        for (Iterator i = headers.entrySet().iterator(); i.hasNext();) {
-            Map.Entry entry = (Map.Entry) i.next();
-            String name = entry.getKey().toString();
+        for (Map.Entry<String, Object> entry : headers.entrySet()) {
+            String name = entry.getKey();
             Object value = entry.getValue();
 
-            if (value instanceof String) {
-                String strValue = (String) value;
-                if (!strValue.equalsIgnoreCase("Content-Encoding")) {
-                    response.setHeader(name, strValue);
+            if (value instanceof List) {
+                for (Object obj : (List) value) {
+                    setResponseHeader(response, name, obj);
                 }
 
-            } else if (value instanceof Date) {
-                long time = ((Date) value).getTime();
-                response.setDateHeader(name, time);
-
             } else {
-                int intValue = ((Integer) value).intValue();
-                response.setIntHeader(name, intValue);
+                setResponseHeader(response, name, value);
             }
         }
     }
 
+    private void setResponseHeader(HttpServletResponse response, String name,
+        Object value) {
+
+        if (value instanceof String) {
+            String strValue = (String) value;
+            if (!strValue.equalsIgnoreCase("Content-Encoding")) {
+                response.setHeader(name, strValue);
+            }
+
+        } else if (value instanceof Date) {
+            long time = ((Date) value).getTime();
+            response.setDateHeader(name, time);
+
+        } else {
+            int intValue = ((Integer) value).intValue();
+            response.setIntHeader(name, intValue);
+        }
+    }
+
     private void prepare(Context context) {
         HttpServletResponse response = context.getResponse();
         applyHeaders(response);



Mime
View raw message