1 29 30 package com.caucho.server.connection; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.servlet.ServletOutputStream ; 36 import javax.servlet.ServletResponse ; 37 import java.io.IOException ; 38 import java.io.OutputStream ; 39 import java.io.PrintWriter ; 40 import java.io.Writer ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 public class IncludeResponseStream extends ToByteResponseStream { 45 static final Logger log = Log.open(IncludeResponseStream.class); 46 47 static final L10N L = new L10N(IncludeResponseStream.class); 48 49 private final AbstractHttpResponse _response; 50 51 private boolean _isCauchoResponseStream; 52 private AbstractResponseStream _nextResponseStream; 53 54 private ServletResponse _next; 55 private ServletOutputStream _os; 56 private PrintWriter _writer; 57 58 private OutputStream _cacheStream; 59 private Writer _cacheWriter; 60 61 public IncludeResponseStream(AbstractHttpResponse response) 62 { 63 _response = response; 64 } 65 66 public void init(ServletResponse next) 67 { 68 _next = next; 69 70 if (_os != null || _writer != null) 71 throw new IllegalStateException (); 72 73 if (_next instanceof CauchoResponse) { 74 CauchoResponse response = (CauchoResponse) next; 75 76 _isCauchoResponseStream = response.isCauchoResponseStream(); 77 78 if (_isCauchoResponseStream) 79 _nextResponseStream = response.getResponseStream(); 80 } 81 else 82 _isCauchoResponseStream = false; 83 } 84 85 public void start() 86 { 87 super.start(); 88 89 try { 90 setEncoding(_next.getCharacterEncoding()); 91 } catch (Throwable e) { 92 log.log(Level.FINE, e.toString(), e); 93 } 94 } 95 96 99 public boolean isCauchoResponseStream() 100 { 101 return _isCauchoResponseStream; 102 } 103 104 107 public void setCauchoResponseStream(boolean isCaucho) 108 { 109 _isCauchoResponseStream = isCaucho; 110 } 111 112 115 public void setByteCacheStream(OutputStream cacheStream) 116 { 117 _cacheStream = cacheStream; 118 } 119 120 123 public void setCharCacheStream(Writer cacheWriter) 124 { 125 _cacheWriter = cacheWriter; 126 } 127 128 131 protected void flushCharBuffer() 132 throws IOException 133 { 134 if (_isCauchoResponseStream && _nextResponseStream == null) { 135 super.flushCharBuffer(); 137 138 return; 139 } 140 141 try { 142 if (_writer == null) 143 _writer = _next.getWriter(); 144 } catch (Throwable e) { 145 log.log(Level.FINER, e.toString(), e); 146 } 147 148 if (_writer != null) { 149 int charLength = getCharOffset(); 150 setCharOffset(0); 151 char []buffer = getCharBuffer(); 152 153 _response.startCaching(false); 154 155 _writer.write(buffer, 0, charLength); 156 157 if (_cacheWriter != null) 158 _cacheWriter.write(buffer, 0, charLength); 159 } 160 else 161 super.flushCharBuffer(); 162 } 163 164 167 public void setBufferOffset(int offset) 168 throws IOException 169 { 170 super.setBufferOffset(offset); 171 172 if (! _isCauchoResponseStream) 173 flushByteBuffer(); 174 } 175 176 183 public void write(int ch) 184 throws IOException 185 { 186 flushCharBuffer(); 187 188 if (_isCauchoResponseStream) { 189 super.write(ch); 190 } 191 else { 192 if (_os == null) 193 _os = _next.getOutputStream(); 194 195 _os.write(ch); 196 } 197 } 198 199 206 public void write(byte []buf, int offset, int length) 207 throws IOException 208 { 209 flushCharBuffer(); 210 211 if (false && _isCauchoResponseStream) super.write(buf, offset, length); 213 else { 214 if (_os == null) 215 _os = _next.getOutputStream(); 216 217 _os.write(buf, offset, length); 218 } 219 } 220 221 228 protected void writeNext(byte []buf, int offset, int length, boolean isEnd) 229 throws IOException 230 { 231 try { 232 if (_response != null) 233 _response.writeHeaders(null, -1); 234 235 if (length == 0) 236 return; 237 238 if (_cacheStream != null) 239 _cacheStream.write(buf, offset, length); 240 241 if (_os == null) 242 _os = _next.getOutputStream(); 243 244 _os.write(buf, offset, length); 245 } catch (IOException e) { 246 if (_next instanceof CauchoResponse) 247 ((CauchoResponse) _next).killCache(); 248 249 if (_response != null) 250 _response.killCache(); 251 252 throw e; 253 } 254 } 255 256 259 public void flushByte() 260 throws IOException 261 { 262 flushBuffer(); 263 264 if (_os == null) 265 _os = _next.getOutputStream(); 266 267 _os.flush(); 268 } 269 270 273 public void flushChar() 274 throws IOException 275 { 276 flushBuffer(); 277 278 if (_writer == null) 279 _writer = _next.getWriter(); 280 281 _writer.flush(); 282 } 283 284 287 public void finish() 288 throws IOException 289 { 290 flushBuffer(); 291 292 299 300 _os = null; 301 _writer = null; 302 _next = null; 303 304 _cacheStream = null; 305 _cacheWriter = null; 306 } 307 308 311 public void close() 312 throws IOException 313 { 314 super.close(); 315 316 323 324 _os = null; 325 _writer = null; 326 _next = null; 327 328 _cacheStream = null; 329 _cacheWriter = null; 330 } 331 } 332 | Popular Tags |