Author: musachy
Date: Mon Dec 1 08:47:29 2008
New Revision: 722124
URL: http://svn.apache.org/viewvc?rev=722124&view=rev
Log:
WW-2625 Add allowCaching attribute to StreamResult
Thanks to Al Sutton for the patch.
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=722124&r1=722123&r2=722124&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 Dec 1 08:47:29 2008
@@ -61,6 +61,8 @@
* <li><b>bufferSize</b> - the size of the buffer to copy from input to output
* (default = <code>1024</code>).</li>
*
+ * <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>
*
* <p>These parameters can also be set by exposing a similarly named getter
method on your Action. For example, you can
@@ -94,6 +96,7 @@
protected String inputName = "inputStream";
protected InputStream inputStream;
protected int bufferSize = 1024;
+ protected boolean allowCaching = true;
public StreamResult() {
super();
@@ -103,6 +106,24 @@
this.inputStream = in;
}
+ /**
+ * @return Returns the whether or not the client should be requested to
allow caching of the data stream.
+ */
+ public boolean getAllowCaching() {
+ return allowCaching;
+ }
+
+ /**
+ * Set allowCaching to <tt>false</tt> to indicate that the client should
be requested not to cache the data stream.
+ * This is set to <tt>false</tt> by default
+ *
+ * @param allowCaching Enable caching.
+ */
+ public void setAllowCaching(boolean allowCaching) {
+ this.allowCaching = allowCaching;
+ }
+
+
/**
* @return Returns the bufferSize.
*/
@@ -222,6 +243,12 @@
oResponse.addHeader("Content-Disposition",
conditionalParse(contentDisposition, invocation));
}
+ // Set the cache control headers if neccessary
+ if (!allowCaching) {
+ oResponse.addHeader("Pragma", "no-cache");
+ oResponse.addHeader("Cache-Control", "no-cache");
+ }
+
// Get the outputstream
oOutput = oResponse.getOutputStream();
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=722124&r1=722123&r2=722124&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 Dec 1 08:47:29 2008
@@ -93,6 +93,26 @@
assertEquals("inline", response.getHeader("Content-disposition"));
}
+ public void testAllowCacheDefault() throws Exception {
+ result.setInputName("streamForImage");
+
+ result.doExecute("helloworld", mai);
+
+ //check that that headers are not set by default
+ assertNull(response.getHeader("Pragma"));
+ assertNull(response.getHeader("Cache-Control"));
+ }
+
+ public void testAllowCacheFalse() throws Exception {
+ result.setInputName("streamForImage");
+ result.setAllowCaching(false);
+ result.doExecute("helloworld", mai);
+
+ //check that that headers are not set by default
+ assertEquals("no-cache", response.getHeader("Pragma"));
+ assertEquals("no-cache", response.getHeader("Cache-Control"));
+ }
+
public void testStreamResultNoDefault() throws Exception {
// it's not easy to test using easymock as we use getOutputStream on
HttpServletResponse.
result.setParse(false);