Author: tmjee Date: Wed Aug 2 08:27:02 2006 New Revision: 428027 URL: http://svn.apache.org/viewvc?rev=428027&view=rev Log: WW-1406 - Exception thrown when contentLength expression supplied in stream result xwork.xml definition
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=428027&r1=428026&r2=428027&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 Wed Aug 2 08:27:02 2006 @@ -79,7 +79,7 @@ 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; @@ -115,14 +115,14 @@ /** * @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; } @@ -154,7 +154,7 @@ this.inputName = inputName; } - /* (non-Javadoc) + /** * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation) */ protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { @@ -180,8 +180,18 @@ 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 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=428027&r1=428026&r2=428027&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 Wed Aug 2 08:27:02 2006 @@ -74,7 +74,7 @@ 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 @@ -90,13 +90,13 @@ 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()); @@ -107,19 +107,48 @@ 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); - result.setInputName("${top.streamForImage}"); + // ${...} 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(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("${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); + // 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(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("streamForImage", result.getInputName()); assertEquals(128, result.getBufferSize()); @@ -128,7 +157,6 @@ assertEquals("image/jpeg", response.getContentType()); assertEquals(contentLength, response.getContentLength()); assertEquals("filename=\"logo.png\"", response.getHeader("Content-disposition")); -*/ } protected void setUp() throws Exception { @@ -149,6 +177,8 @@ ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response); } + + protected void tearDown() { response = null; @@ -164,7 +194,8 @@ // 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 { @@ -175,6 +206,10 @@ URL url = ClassLoaderUtil.getResource("log4j.properties", StreamResultTest.class); File file = new File(new URI(url.toString())); return file.length(); + } + + public String getStreamForImageAsString() { + return "streamForImage"; } }