1 28 29 package com.caucho.server.connection; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.FreeList; 33 import com.caucho.vfs.FlushBuffer; 34 35 import javax.servlet.ServletOutputStream ; 36 import javax.servlet.ServletResponse ; 37 import javax.servlet.http.Cookie ; 38 import javax.servlet.http.HttpServletResponse ; 39 import java.io.IOException ; 40 import java.io.PrintWriter ; 41 import java.io.UnsupportedEncodingException ; 42 import java.util.logging.Logger ; 43 44 public class ResponseAdapter extends ResponseWrapper 45 implements CauchoResponse { 46 private static final Logger log = Log.open(ResponseAdapter.class); 47 48 private static final FreeList<ResponseAdapter> _freeList = 49 new FreeList<ResponseAdapter>(32); 50 51 protected RequestAdapter _request; 52 53 protected FlushBuffer _flushBuffer; 54 55 protected AbstractResponseStream _originalResponseStream; 56 protected AbstractResponseStream _responseStream; 57 58 private ServletOutputStreamImpl _os; 59 private ResponseWriter _writer; 60 61 private boolean _hasOutputStream; 62 private boolean _hasError; 63 64 private ResponseAdapter() 65 { 66 } 67 68 protected ResponseAdapter(HttpServletResponse response) 69 { 70 setResponse(response); 71 72 _originalResponseStream = createWrapperResponseStream(); 73 74 _os = new ServletOutputStreamImpl(); 75 _writer = new ResponseWriter(); 76 } 77 78 81 public static ResponseAdapter create(HttpServletResponse response) 82 { 83 ResponseAdapter resAdapt = _freeList.allocate(); 84 85 if (resAdapt == null) 86 resAdapt = new ResponseAdapter(response); 87 else 88 resAdapt.setResponse(response); 89 90 resAdapt.init(response); 91 92 return resAdapt; 93 } 94 95 void setRequest(RequestAdapter request) 96 { 97 _request = request; 98 } 99 100 protected AbstractResponseStream createWrapperResponseStream() 101 { 102 return new WrapperResponseStream(); 103 } 104 105 public void init(HttpServletResponse response) 106 { 107 setResponse(response); 108 _hasError = false; 109 110 _responseStream = _originalResponseStream; 111 if (_originalResponseStream instanceof WrapperResponseStream) { 112 WrapperResponseStream wrapper = (WrapperResponseStream) _originalResponseStream; 113 wrapper.init(response); 114 } 115 116 _originalResponseStream.start(); 117 118 _os.init(_originalResponseStream); 119 _writer.init(_originalResponseStream); 120 } 121 122 public AbstractResponseStream getResponseStream() 123 { 124 return _responseStream; 125 } 126 127 public boolean isCauchoResponseStream() 128 { 129 return false; 130 } 131 132 public void setResponseStream(AbstractResponseStream responseStream) 133 { 134 _responseStream = responseStream; 135 136 _os.init(responseStream); 137 _writer.init(responseStream); 138 } 139 140 public boolean isTop() 141 { 142 return false; 143 } 144 145 public void resetBuffer() 146 { 147 super.resetBuffer(); 148 149 _responseStream.clearBuffer(); 150 } 151 152 public void sendRedirect(String url) 153 throws IOException 154 { 155 resetBuffer(); 156 157 super.sendRedirect(url); 158 } 159 160 public ServletOutputStream getOutputStream() throws IOException 161 { 162 return _os; 163 } 164 165 168 public void setFlushBuffer(FlushBuffer flushBuffer) 169 { 170 _flushBuffer = flushBuffer; 171 } 172 173 176 public FlushBuffer getFlushBuffer() 177 { 178 return _flushBuffer; 179 } 180 181 public PrintWriter getWriter() throws IOException 182 { 183 return _writer; 184 } 185 186 public void setContentType(String value) 187 { 188 super.setContentType(value); 189 190 try { 191 _responseStream.setEncoding(getCharacterEncoding()); 192 } catch (UnsupportedEncodingException e) { 193 } 194 } 195 196 public void addCookie(Cookie cookie) 197 { 198 if (_request != null) 199 _request.setHasCookie(); 200 201 super.addCookie(cookie); 202 } 203 204 207 208 public String getHeader(String key) 209 { 210 return null; 211 } 212 213 public boolean disableHeaders(boolean disable) 214 { 215 return false; 216 } 217 218 public int getRemaining() 219 { 220 return _responseStream.getRemaining(); 221 } 222 223 227 public void setForbidForward(boolean forbid) 228 { 229 } 230 231 235 public boolean getForbidForward() 236 { 237 return false; 238 } 239 240 241 244 public void setHasError(boolean hasError) 245 { 246 _hasError = hasError; 247 } 248 249 252 public boolean hasError() 253 { 254 return _hasError; 255 } 256 257 260 public void killCache() 261 { 262 if (getResponse() instanceof CauchoResponse) 263 ((CauchoResponse) getResponse()).killCache(); 264 } 265 266 269 public void setPrivateCache(boolean isPrivate) 270 { 271 if (getResponse() instanceof CauchoResponse) 272 ((CauchoResponse) getResponse()).setPrivateCache(isPrivate); 273 } 274 275 278 public void setNoCache(boolean isPrivate) 279 { 280 if (getResponse() instanceof CauchoResponse) 281 ((CauchoResponse) getResponse()).setNoCache(isPrivate); 282 } 283 284 public void setSessionId(String id) 285 { 286 if (getResponse() instanceof CauchoResponse) 287 ((CauchoResponse) getResponse()).setSessionId(id); 288 } 289 290 public void finish() 291 throws IOException 292 { 293 if (_responseStream != null) 294 _responseStream.flushBuffer(); 295 296 _responseStream = _originalResponseStream; 297 } 298 299 public void close() 300 throws IOException 301 { 302 ServletResponse response = getResponse(); 303 304 AbstractResponseStream responseStream = _responseStream; 305 _responseStream = _originalResponseStream; 306 307 if (responseStream != null) 308 responseStream.close(); 309 310 if (_originalResponseStream != responseStream) 311 _originalResponseStream.close(); 312 313 if (response instanceof CauchoResponse) { 314 ((CauchoResponse) response).close(); 315 } 316 331 } 332 333 public static void free(ResponseAdapter resAdapt) 334 { 335 resAdapt.free(); 336 337 _freeList.free(resAdapt); 338 } 339 340 343 protected void free() 344 { 345 _request = null; 346 _responseStream = null; 347 348 setResponse(null); 349 } 350 } 351 | Popular Tags |