1 28 29 package com.caucho.filters; 30 31 import com.caucho.log.Log; 32 import com.caucho.server.connection.AbstractResponseStream; 33 import com.caucho.server.connection.CauchoResponse; 34 import com.caucho.server.connection.ResponseWrapper; 35 import com.caucho.server.connection.ResponseWriter; 36 import com.caucho.server.connection.ServletOutputStreamImpl; 37 import com.caucho.vfs.FlushBuffer; 38 39 import javax.servlet.ServletOutputStream ; 40 import javax.servlet.ServletResponse ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.io.PrintWriter ; 45 import java.io.UnsupportedEncodingException ; 46 import java.util.Locale ; 47 import java.util.logging.Logger ; 48 49 52 public class CauchoResponseWrapper extends ResponseWrapper 53 implements CauchoResponse { 54 private static final Logger log = Log.open(CauchoResponseWrapper.class); 55 56 private FlushBuffer _flushBuffer; 57 58 private final FilterWrapperResponseStream _originalStream; 59 protected AbstractResponseStream _stream; 60 61 private ResponseWriter _writer; 62 private ServletOutputStreamImpl _os; 63 64 private boolean _hasStream; 65 66 private boolean _hasError; 67 68 public CauchoResponseWrapper() 69 { 70 _os = new ServletOutputStreamImpl(); 71 _writer = new ResponseWriter(); 72 73 _originalStream = new FilterWrapperResponseStream(); 74 } 75 76 public CauchoResponseWrapper(HttpServletResponse response) 77 { 78 this(); 79 80 init(response); 81 } 82 83 86 public void init(HttpServletResponse response) 87 { 88 setResponse(response); 89 90 _stream = _originalStream; 91 _os.init(_originalStream); 92 _writer.init(_originalStream); 93 94 _hasStream = false; 95 _hasError = false; 96 97 _originalStream.init(this); 98 105 106 _originalStream.start(); 107 } 108 109 112 public void close() 113 throws IOException 114 { 115 if (_stream != null) 116 _stream.close(); 117 else 118 _originalStream.close(); 119 } 120 121 public ServletResponse getResponse() 122 { 123 return _response; 124 } 125 126 129 public void setContentType(String value) 130 { 131 _response.setContentType(value); 132 133 try { 134 _stream.setEncoding(getCharacterEncoding()); 135 } catch (UnsupportedEncodingException e) { 136 } 137 } 138 139 142 public void setResponseStream(AbstractResponseStream stream) 143 { 144 _stream = stream; 145 146 _os.init(stream); 147 _writer.init(stream); 148 } 149 150 153 public AbstractResponseStream getResponseStream() 154 { 155 return _stream; 156 } 157 158 161 public boolean isCauchoResponseStream() 162 { 163 return true; 164 } 165 166 169 public ServletOutputStream getOutputStream() throws IOException 170 { 171 return _os; 172 } 173 174 177 public PrintWriter getWriter() throws IOException 178 { 179 return _writer; 180 } 181 182 185 protected OutputStream getStream() throws IOException 186 { 187 return _response.getOutputStream(); 188 } 189 190 193 public void setFlushBuffer(FlushBuffer flushBuffer) 194 { 195 _flushBuffer = flushBuffer; 196 } 197 198 201 public FlushBuffer getFlushBuffer() 202 { 203 return _flushBuffer; 204 } 205 206 public void flushBuffer() 207 throws IOException 208 { 209 if (_flushBuffer != null) 210 _flushBuffer.flushBuffer(); 211 212 _stream.flushBuffer(); 213 214 } 216 217 public void resetBuffer() 218 { 219 if (_stream != null) 220 _stream.clearBuffer(); 221 222 _response.resetBuffer(); 223 } 224 225 public void clearBuffer() 226 { 227 resetBuffer(); 228 } 229 230 public void setLocale(Locale locale) 231 { 232 _response.setLocale(locale); 233 234 try { 235 _stream.setLocale(_response.getLocale()); 236 } catch (UnsupportedEncodingException e) { 237 } 238 } 239 240 243 244 public String getHeader(String key) 245 { 246 return null; 247 } 248 249 public boolean disableHeaders(boolean disable) 250 { 251 return false; 252 } 253 254 public int getRemaining() 255 { 256 262 return _stream.getRemaining(); 263 } 264 265 269 public void setForbidForward(boolean forbid) 270 { 271 } 272 273 277 public boolean getForbidForward() 278 { 279 return false; 280 } 281 282 283 286 public void setHasError(boolean hasError) 287 { 288 _hasError = hasError; 289 } 290 291 294 public boolean hasError() 295 { 296 return _hasError; 297 } 298 299 302 public void killCache() 303 { 304 if (_response instanceof CauchoResponse) 305 ((CauchoResponse) _response).killCache(); 306 } 307 308 public void setSessionId(String id) 309 { 310 if (_response instanceof CauchoResponse) 311 ((CauchoResponse) _response).setSessionId(id); 312 } 313 314 public void setPrivateCache(boolean isPrivate) 315 { 316 if (_response instanceof CauchoResponse) 317 ((CauchoResponse) _response).setPrivateCache(isPrivate); 318 } 319 320 public void setNoCache(boolean isPrivate) 321 { 322 if (_response instanceof CauchoResponse) 323 ((CauchoResponse) _response).setNoCache(isPrivate); 324 } 325 } 326 | Popular Tags |