View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.workflow.web;
18  
19  
20  import java.io.ByteArrayOutputStream;
21  import java.io.CharArrayWriter;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import javax.servlet.ServletOutputStream;
25  import javax.servlet.ServletResponse;
26  import javax.servlet.ServletResponseWrapper;
27  
28  
29  /**
30   * <p>Implementation of <code>HttpServletResponseWrapper</code> for use in
31   * <code>IncludeStep23</code>.  It buffers the response characters up into
32   * a memory-resident buffer that can be converted into a String by calling
33   * <code>getContent()</code>.</p>
34   *
35   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
36   * @author Craig R. McClanahan
37   */
38  
39  public class IncludeResponse23 extends ServletResponseWrapper {
40  
41  
42      // ----------------------------------------------------------- Constructors
43  
44  
45      /**
46       * Construct a new response wrapper according to the specified parameters.
47       *
48       * @param response The servlet response we are wrapping
49       */
50      public IncludeResponse23(ServletResponse response) {
51  
52          super(response);
53  
54      }
55  
56  
57      // ----------------------------------------------------- Instance Variables
58  
59  
60      /**
61       * Accumulator for output that is generated via
62       * <code>getOutputStream()</code>.
63       */
64      protected ByteArrayOutputStream baos = null;
65  
66  
67      /**
68       * Accumulator for output that is generated via
69       * <code>getWriter()</code>.
70       */
71      protected CharArrayWriter caw = null;
72  
73  
74      // --------------------------------------------------------- Public Methods
75  
76  
77      /**
78       * Swallow any attempt to flush the response buffer.
79       */
80      public void flushBuffer() throws IOException {
81  
82          ; // No action is required
83  
84      }
85  
86  
87      /**
88       * Return the character encoding for the included response (if any).
89       */
90      public String getCharacterEncoding() {
91  
92          return (null); // FIXME - getCharacterEncoding()
93  
94      }
95  
96  
97      /**
98       * Return the response data written to this response as a String.
99       *
100      * @exception IOException if a conversion error occurs
101      */
102     public String getContent() throws IOException {
103 
104         String encoding = getCharacterEncoding();
105         if (baos != null) {
106             if (encoding == null)
107                 return (baos.toString());
108             else
109                 return (baos.toString(encoding));
110         } else if (caw != null) {
111             return (caw.toString());
112         } else {
113             return ("");
114         }
115 
116     }
117 
118 
119     /**
120      * Return a ServletOutputStream that can be used to accumulate the response
121      * data for the included resource.
122      *
123      * @exception IOException if an I/O error occurs
124      */
125     public ServletOutputStream getOutputStream() throws IOException {
126 
127         if (caw != null)
128             throw new IllegalStateException("getWriter() already called");
129         baos = new ByteArrayOutputStream();
130         //        return (new IncludeOutputStream23(this));
131         return (null); // FIXME - getOutputStream()
132 
133     }
134 
135 
136     /**
137      * Return a PrintWriter that can be used to accumulate the response data
138      * for the included resource.
139      *
140      * @exception IOException if an I/O error occurs
141      */
142     public PrintWriter getWriter() throws IOException {
143 
144         if (baos != null)
145             throw new IllegalStateException
146                 ("getOutputStream() already called");
147         caw = new CharArrayWriter();
148         //        return (new IncludeWriter23(this));
149         return (null); // FIXME - getWriter()
150 
151     }
152 
153 
154     /**
155      * Reset the response buffer and all headers.
156      */
157     public void reset() {
158 
159         resetBuffer();
160 
161     }
162 
163 
164     /**
165      * Reset the response buffer to contain no data.
166      */
167     public void resetBuffer() {
168 
169         if (baos != null)
170             baos.reset();
171         else if (caw != null)
172             caw.reset();
173 
174     }
175 
176 
177     /**
178      * Set the content type (and possibly the character encoding) of the
179      * response data.
180      *
181      * @param contentType The new content type
182      */
183     public void setContentType(String contentType) {
184 
185         ; // FIXME - setContentType()
186 
187     }
188 
189 
190     // -------------------------------------------------------- Package Methods
191 
192 
193     /**
194      * Write a sequence of bytes to our accumulator.
195      *
196      * @param b The byte array
197      */
198     void write(byte b[]) {
199 
200         baos.write(b, 0, b.length);
201 
202     }
203 
204 
205     /**
206      * Write a sequence of bytes to our accumulator.
207      *
208      * @param b The byte array
209      * @param off Starting offset
210      * @param len Number of bytes to be written
211      */
212     void write(byte b[], int off, int len) {
213 
214         baos.write(b, off, len);
215 
216     }
217 
218 
219     /**
220      * Write a sequence of characters to our accumulator.
221      *
222      * @param c The character array
223      */
224     void write(char c[]) {
225 
226         caw.write(c, 0, c.length);
227 
228     }
229 
230 
231     /**
232      * Write a sequence of characters to our accumulator.
233      *
234      * @param c The character array
235      * @param off Starting offset
236      * @param len Number of bytes to be written
237      */
238     void write(char c[], int off, int len) {
239 
240         caw.write(c, off, len);
241 
242     }
243 
244 
245     /**
246      * Write a single byte or character (based on which accumulator has
247      * been created) to our accumulator.
248      *
249      * @param value The byte or character to be written
250      */
251     void write(int value) {
252 
253         if (baos != null)
254             baos.write(value);
255         else
256             caw.write(value);
257 
258     }
259 
260 
261     /**
262      * Write a sequence of characters to our accumulator.
263      *
264      * @param s The character string
265      */
266     void write(String s) {
267 
268         caw.write(s, 0, s.length());
269 
270     }
271 
272 
273     /**
274      * Write a sequence of characters to our accumulator.
275      *
276      * @param s The character string
277      * @param off Starting offset
278      * @param len Number of characters to write
279      */
280     void write(String s, int off, int len) {
281 
282         caw.write(s, off, len);
283 
284     }
285 
286 
287 }