import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionInvocation; import com.opensymphony.xwork.Result; /** * Implements an XWork Result that takes an InputStream object available from a chained * Action and redirects it to the browser. * * The following declaration must be added to the xwork.xml file after the * element: * * * * * * To use the stream result type add the following as part of the action declaration: * * * image/jpeg * imageStream * 1024 * * * contentType = the stream mime-type as sent to the web browser * 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 (defaul = 1024) * * @author mcrawford */ public class StreamResult implements Result { protected String contentType = "text/plain"; protected String inputName = "inputStream"; protected int bufferSize = 1024; /** * @return Returns the bufferSize. */ public int getBufferSize() { return(bufferSize); } /** * @param bufferSize The bufferSize to set. */ public void setBufferSize(int iBufferSize) { bufferSize = iBufferSize; } /** * @return Returns the contentType. */ public String getContentType() { return(contentType); } /** * @param contentType The contentType to set. */ public void setContentType(String sContentType) { contentType = sContentType; } /** * @return Returns the inputName. */ public String getInputName() { return(inputName); } /** * @param inputName The inputName to set. */ public void setInputName(String sInputName) { inputName = sInputName; } /** * @see com.opensymphony.xwork.Result#execute(com.opensymphony.xwork.ActionInvocation) */ public void execute(ActionInvocation oInvocation) throws Exception { // Find the inputstream from the invocation variable stack InputStream oInput = (InputStream) oInvocation.getStack().findValue(inputName); // Find the Response in context HttpServletResponse oResponse = ServletActionContext.getResponse(); // Set the content type oResponse.setContentType(contentType); // Get the outputstream OutputStream oOutput = oResponse.getOutputStream(); // Copy input to output byte[] oBuff = new byte[bufferSize]; int iSize= 0; while (-1 != (iSize = oInput.read(oBuff))) { oOutput.write(oBuff, 0, iSize); } // Flush oOutput.flush(); } }