Package org.apache.struts2.result
Class StreamResult
java.lang.Object
org.apache.struts2.result.StrutsResultSupport
org.apache.struts2.result.StreamResult
- All Implemented Interfaces:
Serializable,Result,StrutsStatics
A custom Result type for sending raw data (via an InputStream) directly to the
HttpServletResponse. Very useful for allowing users to download content.
This result type takes the following parameters:
- contentType - the stream mime-type as sent to the web browser
(default =
text/plain). - contentLength - the stream length in bytes (the browser displays a progress bar).
- contentDisposition - the content disposition header value for
specifing the file name (default =
inline, values are typically attachment;filename="document.pdf". - inputName - the name of the InputStream property from the chained
action (default =
inputStream). - bufferSize - the size of the buffer to copy from input to output
(default =
1024). - allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control'
to 'no-cache', and prevent client from caching the content. (default =
true) - contentCharSet 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
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.
<result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <param name="contentDisposition">attachment;filename="document.pdf"</param> <param name="bufferSize">1024</param> </result>
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected intprotected Stringprotected Stringprotected Stringprotected Stringstatic final intstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected Stringprotected InputStreamprotected static final org.apache.logging.log4j.LoggerFields inherited from class org.apache.struts2.result.StrutsResultSupport
DEFAULT_URL_ENCODING, parseLocationFields inherited from interface org.apache.struts2.StrutsStatics
ACTION_MAPPING, HTTP_REQUEST, HTTP_RESPONSE, PAGE_CONTEXT, SERVLET_CONTEXT, SERVLET_DISPATCHER, STRUTS_ACTION_TAG_INVOCATION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidapplyContentLength(jakarta.servlet.http.HttpServletResponse response, ActionInvocation invocation) Applies the content-length header to the response.protected voidapplyResponseHeaders(jakarta.servlet.http.HttpServletResponse response, ActionInvocation invocation) Applies all response headers including content-type, charset, content-length, content-disposition, and cache control headers.protected voiddoExecute(String finalLocation, ActionInvocation invocation) Executes the result given a final location (jsp page, action, etc) and the action invocation (the state in which the action was executed).booleanintprotected booleanisAcceptableExpression(String expression) Checks if expression doesn't contain vulnerable codeprotected voidresolveInputStream(ActionInvocation invocation) Resolves the input stream from the action invocation.voidsetAllowCaching(boolean allowCaching) Set allowCaching to false to indicate that the client should be requested not to cache the data stream.voidsetBufferSize(int bufferSize) voidsetContentCharSet(String contentCharSet) voidsetContentDisposition(String contentDisposition) voidsetContentLength(String contentLength) voidsetContentType(String contentType) voidsetInputName(String inputName) voidsetNotExcludedAcceptedPatterns(NotExcludedAcceptedPatternsChecker notExcludedAcceptedPatterns) protected voidstreamContent(InputStream input, OutputStream output) Streams content from the input stream to the output stream.Methods inherited from class org.apache.struts2.result.StrutsResultSupport
conditionalParse, conditionalParseCollection, execute, getLastFinalLocation, getLocation, setEncode, setLocation, setParse
-
Field Details
-
LOG
protected static final org.apache.logging.log4j.Logger LOG -
DEFAULT_PARAM
- See Also:
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE- See Also:
-
DEFAULT_CONTENT_TYPE
- See Also:
-
DEFAULT_CONTENT_DISPOSITION
- See Also:
-
DEFAULT_INPUT_NAME
- See Also:
-
contentType
-
contentLength
-
contentDisposition
-
contentCharSet
-
inputName
-
inputStream
-
bufferSize
protected int bufferSize -
allowCaching
protected boolean allowCaching
-
-
Constructor Details
-
StreamResult
public StreamResult() -
StreamResult
-
-
Method Details
-
setNotExcludedAcceptedPatterns
public void setNotExcludedAcceptedPatterns(NotExcludedAcceptedPatternsChecker notExcludedAcceptedPatterns) -
getAllowCaching
public boolean getAllowCaching()- Returns:
- Returns the whether or not the client should be requested to allow caching of the data stream.
-
setAllowCaching
public void setAllowCaching(boolean allowCaching) Set allowCaching to false to indicate that the client should be requested not to cache the data stream. This is set to false by default- Parameters:
allowCaching- Enable caching.
-
getBufferSize
public int getBufferSize()- Returns:
- Returns the bufferSize.
-
setBufferSize
public void setBufferSize(int bufferSize) - Parameters:
bufferSize- The bufferSize to set.
-
getContentType
- Returns:
- Returns the contentType.
-
setContentType
- Parameters:
contentType- The contentType to set.
-
getContentLength
- Returns:
- Returns the contentLength.
-
setContentLength
- Parameters:
contentLength- The contentLength to set.
-
getContentDisposition
- Returns:
- Returns the Content-disposition header value.
-
setContentDisposition
- Parameters:
contentDisposition- the Content-disposition header value to use.
-
getContentCharSet
- Returns:
- Returns the charset specified by the user
-
setContentCharSet
- Parameters:
contentCharSet- the charset to use on the header when sending the stream
-
getInputName
- Returns:
- Returns the inputName.
-
setInputName
- Parameters:
inputName- The inputName to set.
-
doExecute
Description copied from class:StrutsResultSupportExecutes the result given a final location (jsp page, action, etc) and the action invocation (the state in which the action was executed). Subclasses must implement this class to handle custom logic for result handling.- Specified by:
doExecutein classStrutsResultSupport- Parameters:
finalLocation- the location (jsp page, action, etc) to go to.invocation- the execution state of the action.- Throws:
Exception- if an error occurs while executing the result.- See Also:
-
resolveInputStream
Resolves the input stream from the action invocation.This method can be overridden by subclasses to provide custom stream sources (e.g., from database, cloud storage, or generated content).
- Parameters:
invocation- the action invocation- Throws:
IllegalArgumentException- if the input stream cannot be found
-
applyResponseHeaders
protected void applyResponseHeaders(jakarta.servlet.http.HttpServletResponse response, ActionInvocation invocation) Applies all response headers including content-type, charset, content-length, content-disposition, and cache control headers.This method can be overridden by subclasses to add custom headers (e.g., ETag, X-Custom-Header) or modify caching behavior.
- Parameters:
response- the HTTP responseinvocation- the action invocation
-
applyContentLength
protected void applyContentLength(jakarta.servlet.http.HttpServletResponse response, ActionInvocation invocation) Applies the content-length header to the response.This method can be overridden by subclasses for custom length calculation or to skip setting the header for chunked transfer encoding.
- Parameters:
response- the HTTP responseinvocation- the action invocation
-
streamContent
Streams content from the input stream to the output stream.This method can be overridden by subclasses to implement custom streaming behavior such as progress tracking, compression, or encryption.
- Parameters:
input- the input stream to read fromoutput- the output stream to write to- Throws:
IOException- if an I/O error occurs
-
isAcceptableExpression
Checks if expression doesn't contain vulnerable code- Parameters:
expression- of result- Returns:
- true|false
- Since:
- 6.0.0
-