FileDocCategorySizeDatePackage
StreamResult.javaAPI DocExample8136Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

StreamResult

public class StreamResult extends StrutsResultSupport
A custom Result type for send 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).
  • contentDispostion - the content disposition header value for specifing the file name (default = inline, values are typically 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).
Example:

<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>

Fields Summary
private static final long
serialVersionUID
protected static final Log
log
public static final String
DEFAULT_PARAM
protected String
contentType
protected String
contentLength
protected String
contentDisposition
protected String
inputName
protected InputStream
inputStream
protected int
bufferSize
Constructors Summary
public StreamResult()


      
        super();
    
public StreamResult(InputStream in)

        this.inputStream = in;
    
Methods Summary
protected voiddoExecute(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)

see
org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)


        OutputStream oOutput = null;

        try {
            if (inputStream == null) {
                // Find the inputstream from the invocation variable stack
                inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
            }

            if (inputStream == null) {
                String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
                    "Check the <param name=\"inputName\"> tag specified for this action.");
                log.error(msg);
                throw new IllegalArgumentException(msg);
            }

            // Find the Response in context
            HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);

            // Set the content type
            oResponse.setContentType(conditionalParse(contentType, invocation));

            // Set the content length
            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
            if (contentDisposition != null) {
                oResponse.addHeader("Content-disposition", conditionalParse(contentDisposition, invocation));
            }

            // Get the outputstream
            oOutput = oResponse.getOutputStream();

            if (log.isDebugEnabled()) {
                log.debug("Streaming result [" + inputName + "] type=[" + contentType + "] length=[" + contentLength +
                    "] content-disposition=[" + contentDisposition + "]");
            }

            // Copy input to output
            log.debug("Streaming to output buffer +++ START +++");
            byte[] oBuff = new byte[bufferSize];
            int iSize;
            while (-1 != (iSize = inputStream.read(oBuff))) {
                oOutput.write(oBuff, 0, iSize);
            }
            log.debug("Streaming to output buffer +++ END +++");

            // Flush
            oOutput.flush();
        }
        finally {
            if (inputStream != null) inputStream.close();
            if (oOutput != null) oOutput.close();
        }
    
public intgetBufferSize()

return
Returns the bufferSize.

        return (bufferSize);
    
public java.lang.StringgetContentDisposition()

return
Returns the Content-disposition header value.

        return contentDisposition;
    
public java.lang.StringgetContentLength()

return
Returns the contentLength.

        return contentLength;
    
public java.lang.StringgetContentType()

return
Returns the contentType.

        return (contentType);
    
public java.lang.StringgetInputName()

return
Returns the inputName.

        return (inputName);
    
public voidsetBufferSize(int bufferSize)

param
bufferSize The bufferSize to set.

        this.bufferSize = bufferSize;
    
public voidsetContentDisposition(java.lang.String contentDisposition)

param
contentDisposition the Content-disposition header value to use.

        this.contentDisposition = contentDisposition;
    
public voidsetContentLength(java.lang.String contentLength)

param
contentLength The contentLength to set.

        this.contentLength = contentLength;
    
public voidsetContentType(java.lang.String contentType)

param
contentType The contentType to set.

        this.contentType = contentType;
    
public voidsetInputName(java.lang.String inputName)

param
inputName The inputName to set.

        this.inputName = inputName;