1 48 49 50 package com.caucho.portal.generic.context; 51 52 import com.caucho.portal.generic.FastPrintWriter; 53 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.io.PrintWriter ; 57 import java.io.UnsupportedEncodingException ; 58 import java.util.Locale ; 59 import java.util.logging.Level ; 60 import java.util.logging.Logger ; 61 62 69 public class AbstractResponseHandler implements ResponseHandler 70 { 71 protected static final Logger log = 72 Logger.getLogger(AbstractResponseHandler.class.getName()); 73 74 private PrintWriter _internalWriter; 75 private OutputStream _internalOutputStream; 76 77 private ResponseHandler _successor; 78 79 protected PrintWriter _writer; 80 protected OutputStream _outputStream; 81 82 protected PrintWriter _writerOut; 83 protected OutputStream _outputStreamOut; 84 85 private Exception _errorCause; 86 87 88 public AbstractResponseHandler() 89 { 90 } 91 92 public AbstractResponseHandler(ResponseHandler successor) 93 { 94 open(successor); 95 } 96 97 public void open(ResponseHandler successor) 98 { 99 if (_successor != null) 100 throw new IllegalStateException ("already open"); 101 102 _successor = successor; 103 } 104 105 public void finish() 106 throws IOException 107 { 108 Exception errorCause = _errorCause; 109 110 _writerOut = null; 111 _outputStreamOut = null; 112 _successor = null; 113 _errorCause = null; 114 115 if (errorCause != null) { 116 if (errorCause instanceof IOException ) 117 throw (IOException ) errorCause; 118 else { 119 IOException ex = new IOException (); 120 ex.initCause(errorCause); 121 throw ex; 122 } 123 } 124 125 } 126 127 public ResponseHandler getSuccessor() 128 { 129 return _successor; 130 } 131 132 public void setProperty(String name, String value) 133 { 134 _successor.setProperty(name, value); 135 } 136 137 public void addProperty(String name, String value) 138 { 139 _successor.addProperty(name, value); 140 } 141 142 public void setCharacterEncoding(String enc) 143 throws UnsupportedEncodingException 144 { 145 if (_writer == null && _outputStream == null) 146 _successor.setCharacterEncoding(enc); 147 } 148 149 public String getCharacterEncoding() 150 { 151 return _successor.getCharacterEncoding(); 152 } 153 154 public void setContentType(String contentType) 155 { 156 if (_writer == null && _outputStream == null) 157 _successor.setContentType(contentType); 158 } 159 160 public String getContentType() 161 { 162 return _successor.getContentType(); 163 } 164 165 public void setLocale(Locale locale) 166 { 167 if (_writer == null && _outputStream == null) 168 _successor.setLocale(locale); 169 } 170 171 public Locale getLocale() 172 { 173 return _successor.getLocale(); 174 } 175 176 public boolean isCommitted() 177 { 178 return _successor.isCommitted(); 179 } 180 181 public PrintWriter getWriter() 182 throws IOException 183 { 184 checkErrorOrFail(); 185 186 if (_writer != null) 187 return _writer; 188 189 if (_outputStream != null) 190 throw new IllegalStateException ("getOutputStream() already called"); 191 192 if (getContentType() == null) 193 throw new IllegalStateException ( 194 "response.setContentType() must be called before getWriter()"); 195 196 try { 197 PrintWriter writerOut = _successor.getWriter(); 198 199 if (_internalWriter == null) 200 _internalWriter = new GenericPrintWriter(this); 201 202 _writerOut = writerOut; 203 _writer = _internalWriter; 204 205 } 206 catch (Exception ex) { 207 setError(ex); 208 } 209 210 checkErrorOrFail(); 211 212 213 return _writer; 214 } 215 216 protected PrintWriter getUnderlyingWriter() 217 { 218 return _writerOut; 219 } 220 221 public OutputStream getOutputStream() 222 throws IOException 223 { 224 checkErrorOrFail(); 225 226 if (_outputStream != null) 227 return _outputStream; 228 229 if (_writer != null) 230 throw new IllegalStateException ("getWriter() already called"); 231 232 if (getContentType() == null) 233 throw new IllegalStateException ( 234 "response.setContentType() must be called before getOutputStream()"); 235 236 boolean fail = true; 237 238 try { 239 OutputStream outputStreamOut = _successor.getOutputStream(); 240 241 if (_internalOutputStream == null) 242 _internalOutputStream = new GenericOutputStream(this); 243 244 _outputStreamOut = outputStreamOut; 245 _outputStream = _internalOutputStream; 246 247 fail = false; 248 } 249 catch (Exception ex) { 250 setError(ex); 251 } 252 253 checkErrorOrFail(); 254 255 return _outputStream; 256 } 257 258 protected OutputStream getUnderlyingOutputStream() 259 { 260 return _outputStreamOut; 261 } 262 263 266 protected void setError(Exception cause) 267 { 268 if (cause == null) 269 throw new NullPointerException (); 270 271 if (_errorCause != null) { 272 _errorCause = cause; 273 log.log(Level.FINEST, _errorCause.toString(), cause); 274 } 275 } 276 277 280 public Exception getErrorCause() 281 { 282 return _errorCause; 283 } 284 285 public boolean isError() 286 { 287 return _errorCause != null; 288 } 289 290 protected void checkErrorOrFail() 291 throws IOException 292 { 293 if (_errorCause != null) { 294 if (_errorCause instanceof IOException ) 295 throw (IOException ) _errorCause; 296 else { 297 IOException ex = new IOException (); 298 ex.initCause(_errorCause); 299 throw ex; 300 } 301 } 302 } 303 304 public void setBufferSize(int bufferSize) 305 { 306 _successor.setBufferSize(bufferSize); 307 } 308 309 public int getBufferSize() 310 { 311 return _successor.getBufferSize(); 312 } 313 314 318 public void reset() 319 { 320 } 321 322 326 public void resetBuffer() 327 { 328 } 329 330 334 public void flushBuffer() 335 throws IOException 336 { 337 checkErrorOrFail(); 338 } 339 340 343 protected void print(char buf[], int off, int len) 344 throws IOException 345 { 346 if (len == 0) 347 return; 348 349 checkErrorOrFail(); 350 351 _writerOut.write(buf, off, len); 352 } 353 354 357 protected void print(String str, int off, int len) 358 throws IOException 359 { 360 if (len == 0) 361 return; 362 363 checkErrorOrFail(); 364 365 _writerOut.write(str, off, len); 366 } 367 368 371 protected void print(char c) 372 throws IOException 373 { 374 checkErrorOrFail(); 375 376 _writerOut.write((int)c); 377 } 378 379 382 protected void write(byte[] buf, int off, int len) 383 throws IOException 384 { 385 checkErrorOrFail(); 386 387 _outputStreamOut.write(buf, off, len); 388 } 389 390 393 protected void write(byte b) 394 throws IOException 395 { 396 checkErrorOrFail(); 397 398 _outputStreamOut.write((int)b); 399 } 400 401 private static class GenericPrintWriter 402 extends FastPrintWriter 403 { 404 private AbstractResponseHandler _successor; 405 406 public GenericPrintWriter(AbstractResponseHandler successor) 407 { 408 _successor = successor; 409 } 410 411 protected void setError() 412 { 413 _successor.setError(new IOException ()); 414 } 415 416 protected void setError(Exception errorCause) 417 { 418 _successor.setError(errorCause); 419 } 420 421 public boolean checkError() 422 { 423 return _successor.getErrorCause() != null; 424 } 425 426 public Exception getErrorCause() 427 { 428 return _successor.getErrorCause(); 429 } 430 431 432 public void writeOut(char buf[], int off, int len) 433 throws IOException 434 { 435 _successor.print(buf, off, len); 436 } 437 438 public void writeOut(String str, int off, int len) 439 throws IOException 440 { 441 _successor.print(str, off, len); 442 } 443 444 public void writeOut(char c) 445 throws IOException 446 { 447 _successor.print((char)c); 448 } 449 450 public void close() 451 { 452 } 453 } 454 455 static private class GenericOutputStream extends OutputStream 456 { 457 private AbstractResponseHandler _successor; 458 459 public GenericOutputStream(AbstractResponseHandler successor) 460 { 461 _successor = successor; 462 } 463 464 public void flush() 465 throws IOException 466 { 467 _successor.flushBuffer(); 468 } 469 470 public void write(byte[] buf) 471 throws IOException 472 { 473 _successor.write(buf, 0, buf.length); 474 } 475 476 public void write(byte[] buf, int off, int len) 477 throws IOException 478 { 479 _successor.write(buf, off, len); 480 } 481 482 public void write(int b) 483 throws IOException 484 { 485 _successor.write((byte)b); 486 } 487 488 public void close() 489 { 490 } 491 } 492 } 493 | Popular Tags |