KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > StreamResult


1 package com.opensymphony.webwork.dispatcher;
2
3 import com.opensymphony.xwork.ActionInvocation;
4
5 import javax.servlet.http.HttpServletResponse JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 /**
10  * Implements an XWork Result that takes an InputStream object available from a chained
11  * Action and redirects it to the browser.
12  *
13  * <p/>
14  *
15  * The following declaration must be added to the xwork.xml file after the &lt;package&gt;
16  * element:
17  *
18  * <p/>
19  *
20  * <pre>
21  * &lt;result-types&gt;
22  * &lt;result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/&gt;
23  * &lt;/result-types&gt;
24  * </pre>
25  *
26  * <p/>
27  *
28  * To use the stream result type add the following as part of the action declaration:
29  *
30  * <p/>
31  *
32  * <pre>
33  * &lt;result name="success" type="stream"&gt;
34  * &lt;param name="contentType"&gt;image/jpeg&lt/param&gt;
35  * &lt;param name="inputName"&gt;imageStream&lt/param&gt;
36  * &lt;param name="contentDisposition"&gt;filename="document.pdf"&lt/param&gt;
37  * &lt;param name="bufferSize"&gt;1024&lt/param&gt;
38  * &lt;/result&gt;
39  * </pre>
40  *
41  * <p/>
42  *
43  * <ul>
44  * <li>contentType - the stream mime-type as sent to the web browser</li>
45  * <li>contentLength - the stream length in bytes (the browser displays a progress bar)</li>
46  * <li>contentDispostion - the content disposition header value for specifing the file name (default = "inline", values are typically <i>filename="document.pdf"</i></li>
47  * <li>inputName - the name of the InputStream property from the chained action (default = "inputStream")</li>
48  * <li>bufferSize - the size of the buffer to copy from input to output (defaul = 1024)</li>
49  * </ul>
50  *
51  * @author mcrawford
52  */

53 public class StreamResult extends WebWorkResultSupport {
54     protected String JavaDoc contentType = "text/plain";
55     protected int contentLength;
56     protected String JavaDoc contentDisposition = "inline";
57     protected String JavaDoc inputName = "inputStream";
58     protected int bufferSize = 1024;
59
60     /**
61      * @return Returns the bufferSize.
62      */

63     public int getBufferSize() {
64         return (bufferSize);
65     }
66
67     /**
68      * @param bufferSize The bufferSize to set.
69      */

70     public void setBufferSize(int bufferSize) {
71         this.bufferSize = bufferSize;
72     }
73
74     /**
75      * @return Returns the contentType.
76      */

77     public String JavaDoc getContentType() {
78         return (contentType);
79     }
80
81     /**
82      * @param contentType The contentType to set.
83      */

84     public void setContentType(String JavaDoc contentType) {
85         this.contentType = contentType;
86     }
87
88     /**
89      * @return Returns the contentLength.
90      */

91     public int getContentLength() {
92         return contentLength;
93     }
94
95     /**
96      * @param contentLength The contentLength to set.
97      */

98     public void setContentLength(int contentLength) {
99         this.contentLength = contentLength;
100     }
101
102     /**
103      * @return Returns the Content-disposition header value.
104      */

105     public String JavaDoc getContentDisposition() {
106         return contentDisposition;
107     }
108
109     /**
110      * @param contentDisposition the Content-disposition header value to use.
111      */

112     public void setContentDisposition(String JavaDoc contentDisposition) {
113         this.contentDisposition = contentDisposition;
114     }
115
116     /**
117      * @return Returns the inputName.
118      */

119     public String JavaDoc getInputName() {
120         return (inputName);
121     }
122
123     /**
124      * @param inputName The inputName to set.
125      */

126     public void setInputName(String JavaDoc inputName) {
127         this.inputName = inputName;
128     }
129
130     /**
131      * @see com.opensymphony.xwork.Result#execute(com.opensymphony.xwork.ActionInvocation)
132      */

133     protected void doExecute(String JavaDoc finalLocation, ActionInvocation invocation) throws Exception JavaDoc {
134         // Find the inputstream from the invocation variable stack
135
InputStream JavaDoc oInput = (InputStream JavaDoc) invocation.getStack().findValue(conditionalParse(inputName, invocation));
136
137         try {
138             // Find the Response in context
139
HttpServletResponse JavaDoc oResponse = (HttpServletResponse JavaDoc) invocation.getInvocationContext().get(HTTP_RESPONSE);
140
141             // Set the content type
142
oResponse.setContentType(conditionalParse(contentType, invocation));
143
144             // Set the content length
145
if (contentLength != 0) {
146                  oResponse.setContentLength(contentLength);
147             }
148
149             // Set the content-disposition
150
if (contentDisposition != null) {
151                 oResponse.addHeader("Content-disposition", conditionalParse(contentDisposition, invocation));
152             }
153
154             // Get the outputstream
155
OutputStream JavaDoc oOutput = oResponse.getOutputStream();
156
157             // Copy input to output
158
byte[] oBuff = new byte[bufferSize];
159             int iSize = 0;
160             while (-1 != (iSize = oInput.read(oBuff))) {
161                 oOutput.write(oBuff, 0, iSize);
162             }
163
164             // Flush
165
oOutput.flush();
166         }
167         finally {
168             if (oInput != null) oInput.close();
169         }
170     }
171
172 }
173
Popular Tags