From c3ce93739ba4e88fa6ee362fdd32b5dece245884 Mon Sep 17 00:00:00 2001 From: "Donald J. Brown" Date: Sat, 3 Nov 2007 01:18:14 +0000 Subject: [PATCH] Adding retrieval of result parameters from stack for stream result WW-1281 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@591531 13f79535-47bb-0310-9956-ffa450edef68 --- .../struts2/dispatcher/StreamResult.java | 39 +++++++++++++++++++ .../struts2/dispatcher/StreamResultTest.java | 4 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java b/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java index 581c470b8..9bda0db64 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/StreamResult.java @@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletResponse; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; +import com.opensymphony.xwork2.util.ValueStack; /** * @@ -61,6 +62,9 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory; * * * + *

These parameters can also be set by exposing a similarly named getter method on your Action. For example, you can + * provide getContentType() to override that parameter for the current action.

N + * * * * Example: @@ -173,6 +177,9 @@ public class StreamResult extends StrutsResultSupport { */ protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { + // Override any parameters using values on the stack + resolveParamsFromStack(invocation.getStack()); + OutputStream oOutput = null; try { @@ -240,4 +247,36 @@ public class StreamResult extends StrutsResultSupport { } } + /** + * Tries to lookup the parameters on the stack. Will override any existing parameters + * + * @param stack The current value stack + */ + protected void resolveParamsFromStack(ValueStack stack) { + String disposition = stack.findString("contentDisposition"); + if (disposition != null) { + setContentDisposition(disposition); + } + + String contentType = stack.findString("contentType"); + if (contentType != null) { + setContentLength(contentType); + } + + String inputName = stack.findString("inputName"); + if (inputName != null) { + setInputName(inputName); + } + + String contentLength = stack.findString("contentLength"); + if (contentLength != null) { + setContentLength(contentLength); + } + + Integer bufferSize = (Integer) stack.findValue("bufferSize", Integer.class); + if (bufferSize != null) { + setBufferSize(bufferSize.intValue()); + } + } + } diff --git a/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java b/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java index aff9174b5..361ca7a3c 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/StreamResultTest.java @@ -81,14 +81,14 @@ public class StreamResultTest extends StrutsTestCase { result.doExecute("helloworld", mai); - assertEquals(null, result.getContentLength()); + assertEquals("1185", 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", response.getContentType()); - assertEquals(0, response.getContentLength()); + assertEquals(1185, response.getContentLength()); assertEquals("inline", response.getHeader("Content-disposition")); }