Users can now specify a charset on the stream result

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@817363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Wesley Wannemacher
2009-09-21 18:32:16 +00:00
parent f19092287a
commit 96768e036d
2 changed files with 75 additions and 5 deletions
@@ -63,8 +63,13 @@ import com.opensymphony.xwork2.util.ValueStack;
*
* <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 @@ public class StreamResult extends StrutsResultSupport {
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;
@@ -180,6 +186,20 @@ public class StreamResult extends StrutsResultSupport {
this.contentDisposition = contentDisposition;
}
/**
* @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.
*/
@@ -200,7 +220,7 @@ 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());
resolveParamsFromStack(invocation.getStack(), invocation);
OutputStream oOutput = null;
@@ -217,11 +237,21 @@ public class StreamResult extends StrutsResultSupport {
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 @@ public class StreamResult extends StrutsResultSupport {
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 @@ public class StreamResult extends StrutsResultSupport {
*
* @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 @@ public class StreamResult extends StrutsResultSupport {
if (bufferSize != null) {
setBufferSize(bufferSize.intValue());
}
if (contentCharSet != null ) {
contentCharSet = conditionalParse(contentCharSet, invocation);
}
}
}
@@ -93,6 +93,38 @@ public class StreamResultTest extends StrutsTestCase {
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 class StreamResultTest extends StrutsTestCase {
public String getStreamForImageAsString() {
return "streamForImage";
}
public String getContentCharSetMethod() {
return "UTF-8";
}
}
}