mirror of
https://github.com/apache/struts.git
synced 2026-08-11 09:36:57 +00:00
WW-1406
- Exception thrown when contentLength expression supplied in stream
result xwork.xml definition
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@428027 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -79,7 +79,7 @@ public class StreamResult extends StrutsResultSupport {
|
||||
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 @@ public class StreamResult extends StrutsResultSupport {
|
||||
/**
|
||||
* @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 @@ public class StreamResult extends StrutsResultSupport {
|
||||
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 @@ public class StreamResult extends StrutsResultSupport {
|
||||
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
|
||||
|
||||
@@ -74,7 +74,7 @@ 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
|
||||
@@ -90,13 +90,13 @@ public class StreamResultTest extends TestCase {
|
||||
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 @@ public class StreamResultTest extends TestCase {
|
||||
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 @@ public class StreamResultTest extends TestCase {
|
||||
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 @@ public class StreamResultTest extends TestCase {
|
||||
|
||||
ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void tearDown() {
|
||||
response = null;
|
||||
@@ -164,7 +194,8 @@ public class StreamResultTest extends TestCase {
|
||||
// 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 {
|
||||
@@ -176,6 +207,10 @@ public class StreamResultTest extends TestCase {
|
||||
File file = new File(new URI(url.toString()));
|
||||
return file.length();
|
||||
}
|
||||
|
||||
public String getStreamForImageAsString() {
|
||||
return "streamForImage";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user