001 /* 002 * Copyright 1999-2001,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.workflow.web; 018 019 020 import java.io.ByteArrayOutputStream; 021 import java.io.CharArrayWriter; 022 import java.io.IOException; 023 import java.io.PrintWriter; 024 import javax.servlet.ServletOutputStream; 025 import javax.servlet.ServletResponse; 026 import javax.servlet.ServletResponseWrapper; 027 028 029 /** 030 * <p>Implementation of <code>HttpServletResponseWrapper</code> for use in 031 * <code>IncludeStep23</code>. It buffers the response characters up into 032 * a memory-resident buffer that can be converted into a String by calling 033 * <code>getContent()</code>.</p> 034 * 035 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ 036 * @author Craig R. McClanahan 037 */ 038 039 public class IncludeResponse23 extends ServletResponseWrapper { 040 041 042 // ----------------------------------------------------------- Constructors 043 044 045 /** 046 * Construct a new response wrapper according to the specified parameters. 047 * 048 * @param response The servlet response we are wrapping 049 */ 050 public IncludeResponse23(ServletResponse response) { 051 052 super(response); 053 054 } 055 056 057 // ----------------------------------------------------- Instance Variables 058 059 060 /** 061 * Accumulator for output that is generated via 062 * <code>getOutputStream()</code>. 063 */ 064 protected ByteArrayOutputStream baos = null; 065 066 067 /** 068 * Accumulator for output that is generated via 069 * <code>getWriter()</code>. 070 */ 071 protected CharArrayWriter caw = null; 072 073 074 // --------------------------------------------------------- Public Methods 075 076 077 /** 078 * Swallow any attempt to flush the response buffer. 079 */ 080 public void flushBuffer() throws IOException { 081 082 ; // No action is required 083 084 } 085 086 087 /** 088 * Return the character encoding for the included response (if any). 089 */ 090 public String getCharacterEncoding() { 091 092 return (null); // FIXME - getCharacterEncoding() 093 094 } 095 096 097 /** 098 * Return the response data written to this response as a String. 099 * 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 }