Commits

tmjee  committed da472d0

- Exception thrown when contentLength expression supplied in stream result xwork.xml definition

Issue number: WW-1332
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2669573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 4e23f44

Comments (0)

Files changed (2)

File src/java/com/opensymphony/webwork/dispatcher/StreamResult.java

  *
  * @author mcrawford
  * @author Rainer Hermanns
+ * @author tm_jee
  */
 public class StreamResult extends WebWorkResultSupport {
-    protected static final Log log = LogFactory.getLog(StreamResult.class);
+	
+	private static final long serialVersionUID = -6039988588327688623L;
+
+	protected static final Log log = LogFactory.getLog(StreamResult.class);
 
     protected String contentType = "text/plain";
-    protected int contentLength;
+    protected String contentLength;
     protected String contentDisposition = "inline";
     protected String inputName = "inputStream";
     protected int bufferSize = 1024;
     /**
      * @return Returns the contentLength.
      */
-    public int getContentLength() {
+    public String getContentLength() {
         return contentLength;
     }
 
     /**
      * @param contentLength The contentLength to set.
      */
-    public void setContentLength(int contentLength) {
+    public void setContentLength(String contentLength) {
         this.contentLength = contentLength;
     }
 
         OutputStream oOutput = null;
 
         try {
-            // Find the inputstream from the invocation variable stack
+        	// Find the inputstream from the invocation variable stack
             oInput = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
 
             if (oInput == null) {
             oResponse.setContentType(conditionalParse(contentType, invocation));
 
             // Set the content length
-            if (contentLength != 0) {
-                 oResponse.setContentLength(contentLength);
+            if (contentLength != null) {
+            	String _contentLength = conditionalParse(contentLength, invocation);
+            	int _contentLengthAsInt = -1;
+            	try {
+            		_contentLengthAsInt = Integer.parseInt(_contentLength);
+            		if (_contentLengthAsInt >= 0) {
+                		oResponse.setContentLength(_contentLengthAsInt);
+                	}
+            	}
+            	catch(NumberFormatException e) {
+            		log.warn("failed to recongnize "+_contentLength+" as a number, contentLength header will not be set", e);
+            	}
             }
 
             // Set the content-disposition

File src/test/com/opensymphony/webwork/dispatcher/StreamResultTest.java

  * Unit test for {@link StreamResult}.
  *
  * @author Claus Ibsen
+ * @author tm_jee
  */
 public class StreamResultTest extends TestCase {
 
 
         result.doExecute("helloworld", mai);
 
-        assertEquals(0, result.getContentLength());
+        assertEquals(null, result.getContentLength());
         assertEquals("text/plain", result.getContentType());
         assertEquals("streamForImage", result.getInputName());
         assertEquals(1024, result.getBufferSize()); // 1024 is default
         result.setParse(false);
         result.setInputName("streamForImage");
         result.setBufferSize(128);
-        result.setContentLength(contentLength);
+        result.setContentLength(String.valueOf(contentLength));
         result.setContentDisposition("filename=\"logo.png\"");
         result.setContentType("image/jpeg");
 
         result.doExecute("helloworld", mai);
 
-        assertEquals(contentLength, result.getContentLength());
+        assertEquals(String.valueOf(contentLength), result.getContentLength());
         assertEquals("image/jpeg", result.getContentType());
         assertEquals("streamForImage", result.getInputName());
         assertEquals(128, result.getBufferSize());
         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
     }
 
-    public void testStreamResultParse() throws Exception {
-        // TODO: There is a bug in StreamResult with parse = true
-/*
+    public void testStreamResultParse1() throws Exception {
+    	///////////////////
+        result.setParse(true);
+        // ${...} conditionalParse of Result, returns String, 
+        // which gets evaluated to the stack, that's how it works.
+        // We use ${streamForImageAsString} that returns "streamForImage"
+        // which is a property that returns an InputStream object.
+        result.setInputName("${streamForImageAsString}");
+        result.setBufferSize(128);
+        result.setContentLength(String.valueOf(contentLength));
+        result.setContentDisposition("filename=\"logo.png\"");
+        result.setContentType("image/jpeg");
+
+        result.doExecute("helloworld", mai);
+
+        assertEquals(String.valueOf(contentLength), result.getContentLength());
+        assertEquals("image/jpeg", result.getContentType());
+        assertEquals("${streamForImageAsString}", result.getInputName());
+        assertEquals(128, result.getBufferSize());
+        assertEquals("filename=\"logo.png\"", result.getContentDisposition());
+
+        assertEquals("image/jpeg", response.getContentType());
+        assertEquals(contentLength, response.getContentLength());
+        assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
+    }
+    
+    public void testStreamResultParse2() throws Exception {
+    	///////////////////
         result.setParse(true);
-        result.setInputName("${top.streamForImage}");
+        // This time we dun use ${...}, so streamForImage will
+        // be evaluated to the stack, which should reaturn an
+        // InputStream object, cause there's such a property in 
+        // the action object itself.
+        result.setInputName("streamForImage");
         result.setBufferSize(128);
-        result.setContentLength(contentLength);
+        result.setContentLength(String.valueOf(contentLength));
         result.setContentDisposition("filename=\"logo.png\"");
         result.setContentType("image/jpeg");
 
         result.doExecute("helloworld", mai);
 
-        assertEquals(contentLength, result.getContentLength());
+        assertEquals(String.valueOf(contentLength), result.getContentLength());
         assertEquals("image/jpeg", result.getContentType());
         assertEquals("streamForImage", result.getInputName());
         assertEquals(128, result.getBufferSize());
         assertEquals("image/jpeg", response.getContentType());
         assertEquals(contentLength, response.getContentLength());
         assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition"));
-*/
     }
 
     protected void setUp() throws Exception {
             // just use src/test/log4j.properties as test file 
             URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class);
             File file = new File(new URI(url.toString()));
-            return new FileInputStream(file);
+            FileInputStream fis = new FileInputStream(file);
+            return fis;
         }
 
         public String execute() throws Exception {
             File file = new File(new URI(url.toString()));
             return file.length();
         }
+        
+        public String getStreamForImageAsString() {
+        	return "streamForImage";
+        }
     }
 
 }