You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by we...@apache.org on 2009/09/21 20:32:16 UTC

svn commit: r817363 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/dispatcher/StreamResult.java test/java/org/apache/struts2/dispatcher/StreamResultTest.java

Author: wesw
Date: Mon Sep 21 18:32:16 2009
New Revision: 817363

URL: http://svn.apache.org/viewvc?rev=817363&view=rev
Log:
WW-3187
Users can now specify a charset on the stream result

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java?rev=817363&r1=817362&r2=817363&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java Mon Sep 21 18:32:16 2009
@@ -63,8 +63,13 @@
  *
  * <li><b>allowCaching</b> if set to 'false' it will set the headers 'Pragma' and 'Cache-Control'
  * to 'no-cahce', and prevent client from caching the content. (default = <code>true</code>)
- * </ul>
  *
+ * <li><b>contentCharSet</b> if set to a string, ';charset=value' will be added to the
+ * content-type header, where value is the string set. If set to an expression, the result
+ * of evaluating the expression will be used. If not set, then no charset will be set on
+ * the header</li>
+ * </ul>
+ * 
  * <p>These parameters can also be set by exposing a similarly named getter method on your Action.  For example, you can
  * provide <code>getContentType()</code> to override that parameter for the current action.</p>
  *
@@ -93,6 +98,7 @@
     protected String contentType = "text/plain";
     protected String contentLength;
     protected String contentDisposition = "inline";
+    protected String contentCharSet ;
     protected String inputName = "inputStream";
     protected InputStream inputStream;
     protected int bufferSize = 1024;
@@ -181,6 +187,20 @@
     }
 
     /**
+     * @return Returns the charset specified by the user
+     */
+    public String getContentCharSet() {
+        return contentCharSet;
+    }
+
+    /**
+     * @param contentCharSet the charset to use on the header when sending the stream
+     */
+    public void setContentCharSet(String contentCharSet) {
+        this.contentCharSet = contentCharSet;
+    }
+
+    /**
      * @return Returns the inputName.
      */
     public String getInputName() {
@@ -200,7 +220,7 @@
     protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
 
         // Override any parameters using values on the stack
-        resolveParamsFromStack(invocation.getStack());
+        resolveParamsFromStack(invocation.getStack(), invocation);
 
         OutputStream oOutput = null;
 
@@ -217,11 +237,21 @@
                 throw new IllegalArgumentException(msg);
             }
 
+            /*
+            if (contentCharSet != null && contentCharSet.startsWith("${")) {
+                contentCharSet = (String)invocation.getStack().findValue(contentCharSet, String.class);
+            }
+            */
             // Find the Response in context
             HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
 
             // Set the content type
-            oResponse.setContentType(conditionalParse(contentType, invocation));
+            if (contentCharSet != null ) {
+                oResponse.setContentType(conditionalParse(contentType, invocation)+";charset="+contentCharSet);
+            }
+            else {
+                oResponse.setContentType(conditionalParse(contentType, invocation));
+            }
 
             // Set the content length
             if (contentLength != null) {
@@ -254,7 +284,7 @@
 
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Streaming result [" + inputName + "] type=[" + contentType + "] length=[" + contentLength +
-                    "] content-disposition=[" + contentDisposition + "]");
+                    "] content-disposition=[" + contentDisposition + "] charset=[" + contentCharSet + "]");
             }
 
             // Copy input to output
@@ -280,7 +310,7 @@
      *
      * @param stack The current value stack
      */
-    protected void resolveParamsFromStack(ValueStack stack) {
+    protected void resolveParamsFromStack(ValueStack stack, ActionInvocation invocation) {
         String disposition = stack.findString("contentDisposition");
         if (disposition != null) {
             setContentDisposition(disposition);
@@ -305,6 +335,10 @@
         if (bufferSize != null) {
             setBufferSize(bufferSize.intValue());
         }
+
+        if (contentCharSet != null ) {
+            contentCharSet = conditionalParse(contentCharSet, invocation);
+        }
     }
 
 }

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java?rev=817363&r1=817362&r2=817363&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java Mon Sep 21 18:32:16 2009
@@ -93,6 +93,38 @@
         assertEquals("inline", response.getHeader("Content-disposition"));
     }
 
+    public void testStreamResultWithCharSet() throws Exception {
+        result.setInputName("streamForImage");
+        result.setContentCharSet("ISO-8859-1");
+        result.doExecute("helloworld", mai);
+
+        assertEquals(String.valueOf(contentLength), result.getContentLength());
+        assertEquals("text/plain", result.getContentType());
+        assertEquals("streamForImage", result.getInputName());
+        assertEquals(1024, result.getBufferSize()); // 1024 is default
+        assertEquals("inline", result.getContentDisposition());
+        assertEquals("text/plain;charset=ISO-8859-1", response.getContentType());
+        assertEquals(contentLength, response.getContentLength());
+        assertEquals("inline", response.getHeader("Content-disposition"));
+    }
+
+    public void testStreamResultWithCharSet2() throws Exception {
+        result.setParse(true);
+        result.setInputName("streamForImage");
+        result.setContentCharSet("${contentCharSetMethod}");
+
+        result.doExecute("helloworld", mai);
+
+        assertEquals(String.valueOf(contentLength), result.getContentLength());
+        assertEquals("text/plain", result.getContentType());
+        assertEquals("streamForImage", result.getInputName());
+        assertEquals(1024, result.getBufferSize()); // 1024 is default
+        assertEquals("inline", result.getContentDisposition());
+        assertEquals("text/plain;charset=UTF-8", response.getContentType());
+        assertEquals(contentLength, response.getContentLength());
+        assertEquals("inline", response.getHeader("Content-disposition"));
+    }
+
     public void testAllowCacheDefault() throws Exception {
         result.setInputName("streamForImage");
 
@@ -240,6 +272,10 @@
         public String getStreamForImageAsString() {
             return "streamForImage";
         }
+
+        public String getContentCharSetMethod() {
+            return "UTF-8";
+        }
     }
 
 }