comparison src/org/eclipse/jetty/server/Response.java @ 999:74b9daf2826c

simplify Response
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Oct 2016 00:59:46 -0600
parents 688c39c50ee3
children 0114d373748e
comparison
equal deleted inserted replaced
998:bf176925e00f 999:74b9daf2826c
55 /** Response. 55 /** Response.
56 * <p> 56 * <p>
57 * Implements {@link javax.servlet.http.HttpServletResponse} from the <code>javax.servlet.http</code> package. 57 * Implements {@link javax.servlet.http.HttpServletResponse} from the <code>javax.servlet.http</code> package.
58 * </p> 58 * </p>
59 */ 59 */
60 public class Response implements HttpServletResponse 60 public final class Response implements HttpServletResponse
61 { 61 {
62 private static final Logger LOG = LoggerFactory.getLogger(Response.class); 62 private static final Logger LOG = LoggerFactory.getLogger(Response.class);
63 63
64 64
65 public static final int 65 public static final int
79 * will be set as HTTP ONLY. 79 * will be set as HTTP ONLY.
80 */ 80 */
81 public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__"; 81 public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__";
82 82
83 private final AbstractHttpConnection _connection; 83 private final AbstractHttpConnection _connection;
84 private int _status=SC_OK; 84 private int _status = SC_OK;
85 private String _reason; 85 private String _reason;
86 private Locale _locale; 86 private Locale _locale;
87 private String _mimeType; 87 private String _mimeType;
88 private CachedBuffer _cachedMimeType; 88 private CachedBuffer _cachedMimeType;
89 private String _characterEncoding; 89 private String _characterEncoding;
90 private boolean _explicitEncoding; 90 private boolean _explicitEncoding;
91 private String _contentType; 91 private String _contentType;
92 private volatile int _outputState; 92 private volatile int _outputState;
93 private PrintWriter _writer; 93 private PrintWriter _writer;
94 94
95 /* ------------------------------------------------------------ */
96 /**
97 *
98 */
99 public Response(AbstractHttpConnection connection) 95 public Response(AbstractHttpConnection connection)
100 { 96 {
101 _connection=connection; 97 _connection = connection;
102 } 98 }
103 99
104 100
105 /* ------------------------------------------------------------ */ 101 void recycle()
106 /* 102 {
107 * @see javax.servlet.ServletResponse#reset() 103 _status = SC_OK;
108 */ 104 _reason = null;
109 protected void recycle() 105 _locale = null;
110 { 106 _mimeType = null;
111 _status=SC_OK; 107 _cachedMimeType = null;
112 _reason=null; 108 _characterEncoding = null;
113 _locale=null; 109 _explicitEncoding = false;
114 _mimeType=null; 110 _contentType = null;
115 _cachedMimeType=null; 111 _writer = null;
116 _characterEncoding=null; 112 _outputState = NONE;
117 _explicitEncoding=false; 113 }
118 _contentType=null; 114
119 _writer=null;
120 _outputState=NONE;
121 }
122
123 /* ------------------------------------------------------------ */
124 /*
125 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
126 */
127 public void addCookie(HttpCookie cookie) 115 public void addCookie(HttpCookie cookie)
128 { 116 {
129 _connection._responseFields.addSetCookie(cookie); 117 _connection._responseFields.addSetCookie(cookie);
130 } 118 }
131 119
132 /* ------------------------------------------------------------ */ 120 @Override
133 /*
134 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
135 */
136 public void addCookie(Cookie cookie) 121 public void addCookie(Cookie cookie)
137 { 122 {
138 String comment=cookie.getComment(); 123 String comment=cookie.getComment();
139 boolean http_only=false; 124 boolean http_only=false;
140 125
158 cookie.getSecure(), 143 cookie.getSecure(),
159 http_only || cookie.isHttpOnly(), 144 http_only || cookie.isHttpOnly(),
160 cookie.getVersion()); 145 cookie.getVersion());
161 } 146 }
162 147
163 /* ------------------------------------------------------------ */ 148 @Override
164 /*
165 * @see javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String)
166 */
167 public boolean containsHeader(String name) 149 public boolean containsHeader(String name)
168 { 150 {
169 return _connection._responseFields.containsKey(name); 151 return _connection._responseFields.containsKey(name);
170 } 152 }
171 153
172 /* ------------------------------------------------------------ */ 154 @Override
173 /*
174 * @see javax.servlet.http.HttpServletResponse#encodeURL(java.lang.String)
175 */
176 public String encodeURL(String url) 155 public String encodeURL(String url)
177 { 156 {
178 throw new UnsupportedOperationException(); 157 throw new UnsupportedOperationException();
179 } 158 }
180 159
181 /* ------------------------------------------------------------ */ 160 @Override
182 /**
183 * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL(java.lang.String)
184 */
185 public String encodeRedirectURL(String url) 161 public String encodeRedirectURL(String url)
186 { 162 {
187 return encodeURL(url); 163 throw new UnsupportedOperationException();
188 } 164 }
189 165
190 /* ------------------------------------------------------------ */ 166 @Override
191 @Deprecated 167 @Deprecated
192 public String encodeUrl(String url) 168 public String encodeUrl(String url)
193 { 169 {
194 return encodeURL(url); 170 throw new UnsupportedOperationException();
195 } 171 }
196 172
197 /* ------------------------------------------------------------ */ 173 @Override
198 @Deprecated 174 @Deprecated
199 public String encodeRedirectUrl(String url) 175 public String encodeRedirectUrl(String url)
200 { 176 {
201 return encodeRedirectURL(url); 177 throw new UnsupportedOperationException();
202 } 178 }
203 179
204 /* ------------------------------------------------------------ */ 180 @Override
205 /*
206 * @see javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String)
207 */
208 public void sendError(int code, String message) throws IOException 181 public void sendError(int code, String message) throws IOException
209 { 182 {
210 if (isCommitted()) 183 if (isCommitted())
211 LOG.warn("Committed before "+code+" "+message); 184 LOG.warn("Committed before "+code+" "+message);
212 185
213 resetBuffer(); 186 resetBuffer();
214 _characterEncoding=null; 187 _characterEncoding = null;
215 setHeader(HttpHeaders.EXPIRES,null); 188 setHeader(HttpHeaders.EXPIRES,null);
216 setHeader(HttpHeaders.LAST_MODIFIED,null); 189 setHeader(HttpHeaders.LAST_MODIFIED,null);
217 setHeader(HttpHeaders.CACHE_CONTROL,null); 190 setHeader(HttpHeaders.CACHE_CONTROL,null);
218 setHeader(HttpHeaders.CONTENT_TYPE,null); 191 setHeader(HttpHeaders.CONTENT_TYPE,null);
219 setHeader(HttpHeaders.CONTENT_LENGTH,null); 192 setHeader(HttpHeaders.CONTENT_LENGTH,null);
220 193
221 _outputState=NONE; 194 _outputState = NONE;
222 setStatus(code,message); 195 setStatus(code,message);
223 196
224 if (message==null) 197 if (message==null)
225 message=HttpStatus.getMessage(code); 198 message=HttpStatus.getMessage(code);
226 199
276 } 249 }
277 else if (code!=SC_PARTIAL_CONTENT) 250 else if (code!=SC_PARTIAL_CONTENT)
278 { 251 {
279 _connection._requestFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); 252 _connection._requestFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
280 _connection._requestFields.remove(HttpHeaders.CONTENT_LENGTH_BUFFER); 253 _connection._requestFields.remove(HttpHeaders.CONTENT_LENGTH_BUFFER);
281 _characterEncoding=null; 254 _characterEncoding = null;
282 _mimeType=null; 255 _mimeType = null;
283 _cachedMimeType=null; 256 _cachedMimeType = null;
284 } 257 }
285 258
286 complete(); 259 complete();
287 } 260 }
288 261
289 /* ------------------------------------------------------------ */ 262 @Override
290 /*
291 * @see javax.servlet.http.HttpServletResponse#sendError(int)
292 */
293 public void sendError(int sc) throws IOException 263 public void sendError(int sc) throws IOException
294 { 264 {
295 if (sc==102) 265 sendError(sc,null);
296 sendProcessing(); 266 }
297 else 267
298 sendError(sc,null); 268 @Override
299 }
300
301 /* ------------------------------------------------------------ */
302 /* Send a 102-Processing response.
303 * If the connection is a HTTP connection, the version is 1.1 and the
304 * request has a Expect header starting with 102, then a 102 response is
305 * sent. This indicates that the request still be processed and real response
306 * can still be sent. This method is called by sendError if it is passed 102.
307 * @see javax.servlet.http.HttpServletResponse#sendError(int)
308 */
309 public void sendProcessing() throws IOException
310 {
311 if (_connection.isExpecting102Processing() && !isCommitted())
312 _connection._generator.send1xx(HttpStatus.PROCESSING_102);
313 }
314
315 /* ------------------------------------------------------------ */
316 /*
317 * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)
318 */
319 public void sendRedirect(String location) throws IOException 269 public void sendRedirect(String location) throws IOException
320 { 270 {
321 if (location==null) 271 if (location==null)
322 throw new IllegalArgumentException(); 272 throw new IllegalArgumentException();
323 273
375 setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); 325 setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
376 complete(); 326 complete();
377 327
378 } 328 }
379 329
380 /* ------------------------------------------------------------ */ 330 @Override
381 /*
382 * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long)
383 */
384 public void setDateHeader(String name, long date) 331 public void setDateHeader(String name, long date)
385 { 332 {
386 _connection._responseFields.putDateField(name, date); 333 _connection._responseFields.putDateField(name, date);
387 } 334 }
388 335
389 /* ------------------------------------------------------------ */ 336 @Override
390 /*
391 * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long)
392 */
393 public void addDateHeader(String name, long date) 337 public void addDateHeader(String name, long date)
394 { 338 {
395 _connection._responseFields.addDateField(name, date); 339 _connection._responseFields.addDateField(name, date);
396 } 340 }
397 341
398 /* ------------------------------------------------------------ */ 342 @Override
399 /*
400 * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String)
401 */
402 public void setHeader(String name, String value) 343 public void setHeader(String name, String value)
403 { 344 {
404 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) 345 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name))
405 setContentType(value); 346 setContentType(value);
406 else 347 else
415 } 356 }
416 } 357 }
417 } 358 }
418 359
419 360
420 /* ------------------------------------------------------------ */ 361 @Override
421 public Collection<String> getHeaderNames() 362 public Collection<String> getHeaderNames()
422 { 363 {
423 final HttpFields fields=_connection._responseFields; 364 final HttpFields fields=_connection._responseFields;
424 return fields.getFieldNamesCollection(); 365 return fields.getFieldNamesCollection();
425 } 366 }
426 367
427 /* ------------------------------------------------------------ */ 368 @Override
428 /*
429 */
430 public String getHeader(String name) 369 public String getHeader(String name)
431 { 370 {
432 return _connection._responseFields.getStringField(name); 371 return _connection._responseFields.getStringField(name);
433 } 372 }
434 373
435 /* ------------------------------------------------------------ */ 374 @Override
436 /*
437 */
438 public Collection<String> getHeaders(String name) 375 public Collection<String> getHeaders(String name)
439 { 376 {
440 final HttpFields fields=_connection._responseFields; 377 final HttpFields fields=_connection._responseFields;
441 Collection<String> i = fields.getValuesCollection(name); 378 Collection<String> i = fields.getValuesCollection(name);
442 if (i==null) 379 if (i==null)
443 return Collections.EMPTY_LIST; 380 return Collections.EMPTY_LIST;
444 return i; 381 return i;
445 } 382 }
446 383
447 /* ------------------------------------------------------------ */ 384 @Override
448 /*
449 * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String)
450 */
451 public void addHeader(String name, String value) 385 public void addHeader(String name, String value)
452 { 386 {
453 387
454 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) 388 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name))
455 { 389 {
460 _connection._responseFields.add(name, value); 394 _connection._responseFields.add(name, value);
461 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) 395 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name))
462 _connection._generator.setContentLength(Long.parseLong(value)); 396 _connection._generator.setContentLength(Long.parseLong(value));
463 } 397 }
464 398
465 /* ------------------------------------------------------------ */ 399 @Override
466 /*
467 * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int)
468 */
469 public void setIntHeader(String name, int value) 400 public void setIntHeader(String name, int value)
470 { 401 {
471 _connection._responseFields.putLongField(name, value); 402 _connection._responseFields.putLongField(name, value);
472 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) 403 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name))
473 _connection._generator.setContentLength(value); 404 _connection._generator.setContentLength(value);
474 } 405 }
475 406
476 /* ------------------------------------------------------------ */ 407 @Override
477 /*
478 * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int)
479 */
480 public void addIntHeader(String name, int value) 408 public void addIntHeader(String name, int value)
481 { 409 {
482 _connection._responseFields.addLongField(name, value); 410 _connection._responseFields.addLongField(name, value);
483 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) 411 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name))
484 _connection._generator.setContentLength(value); 412 _connection._generator.setContentLength(value);
485 } 413 }
486 414
487 /* ------------------------------------------------------------ */ 415 @Override
488 /*
489 * @see javax.servlet.http.HttpServletResponse#setStatus(int)
490 */
491 public void setStatus(int sc) 416 public void setStatus(int sc)
492 { 417 {
493 setStatus(sc,null); 418 setStatus(sc,null);
494 } 419 }
495 420
496 /* ------------------------------------------------------------ */ 421 @Override
497 /*
498 * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
499 */
500 public void setStatus(int sc, String sm) 422 public void setStatus(int sc, String sm)
501 { 423 {
502 if (sc<=0) 424 if (sc<=0)
503 throw new IllegalArgumentException(); 425 throw new IllegalArgumentException();
504 _status=sc; 426 _status = sc;
505 _reason=sm; 427 _reason = sm;
506 } 428 }
507 429
508 /* ------------------------------------------------------------ */ 430 @Override
509 /*
510 * @see javax.servlet.ServletResponse#getCharacterEncoding()
511 */
512 public String getCharacterEncoding() 431 public String getCharacterEncoding()
513 { 432 {
514 if (_characterEncoding==null) 433 if (_characterEncoding==null)
515 _characterEncoding=StringUtil.__ISO_8859_1; 434 _characterEncoding = StringUtil.__ISO_8859_1;
516 return _characterEncoding; 435 return _characterEncoding;
517 } 436 }
518 437
519 /* ------------------------------------------------------------ */ 438 @Override
520 String getSetCharacterEncoding()
521 {
522 return _characterEncoding;
523 }
524
525 /* ------------------------------------------------------------ */
526 /*
527 * @see javax.servlet.ServletResponse#getContentType()
528 */
529 public String getContentType() 439 public String getContentType()
530 { 440 {
531 return _contentType; 441 return _contentType;
532 } 442 }
533 443
534 /* ------------------------------------------------------------ */ 444 @Override
535 /*
536 * @see javax.servlet.ServletResponse#getOutputStream()
537 */
538 public ServletOutputStream getOutputStream() throws IOException 445 public ServletOutputStream getOutputStream() throws IOException
539 { 446 {
540 if (_outputState!=NONE && _outputState!=STREAM) 447 if (_outputState!=NONE && _outputState!=STREAM)
541 throw new IllegalStateException("WRITER"); 448 throw new IllegalStateException("WRITER");
542 449
543 ServletOutputStream out = _connection.getOutputStream(); 450 ServletOutputStream out = _connection.getOutputStream();
544 _outputState=STREAM; 451 _outputState = STREAM;
545 return out; 452 return out;
546 } 453 }
547 454
548 /* ------------------------------------------------------------ */ 455 @Override
549 public boolean isWriting()
550 {
551 return _outputState==WRITER;
552 }
553
554 /* ------------------------------------------------------------ */
555 public boolean isOutputing()
556 {
557 return _outputState!=NONE;
558 }
559
560 /* ------------------------------------------------------------ */
561 /*
562 * @see javax.servlet.ServletResponse#getWriter()
563 */
564 public PrintWriter getWriter() throws IOException 456 public PrintWriter getWriter() throws IOException
565 { 457 {
566 if (_outputState!=NONE && _outputState!=WRITER) 458 if (_outputState!=NONE && _outputState!=WRITER)
567 throw new IllegalStateException("STREAM"); 459 throw new IllegalStateException("STREAM");
568 460
589 } 481 }
590 _outputState=WRITER; 482 _outputState=WRITER;
591 return _writer; 483 return _writer;
592 } 484 }
593 485
594 /* ------------------------------------------------------------ */ 486 @Override
595 /*
596 * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
597 */
598 public void setCharacterEncoding(String encoding) 487 public void setCharacterEncoding(String encoding)
599 { 488 {
600 if (this._outputState==0 && !isCommitted()) 489 if (this._outputState==0 && !isCommitted())
601 { 490 {
602 _explicitEncoding=true; 491 _explicitEncoding = true;
603 492
604 if (encoding==null) 493 if (encoding==null)
605 { 494 {
606 // Clear any encoding. 495 // Clear any encoding.
607 if (_characterEncoding!=null) 496 if (_characterEncoding!=null)
608 { 497 {
609 _characterEncoding=null; 498 _characterEncoding=null;
610 if (_cachedMimeType!=null) 499 if (_cachedMimeType!=null)
611 _contentType=_cachedMimeType.toString(); 500 _contentType = _cachedMimeType.toString();
612 else if (_mimeType!=null) 501 else if (_mimeType!=null)
613 _contentType=_mimeType; 502 _contentType = _mimeType;
614 else 503 else
615 _contentType=null; 504 _contentType = null;
616 505
617 if (_contentType==null) 506 if (_contentType==null)
618 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); 507 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
619 else 508 else
620 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 509 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
633 if(_cachedMimeType!=null) 522 if(_cachedMimeType!=null)
634 { 523 {
635 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); 524 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding);
636 if (content_type!=null) 525 if (content_type!=null)
637 { 526 {
638 _contentType=content_type.toString(); 527 _contentType = content_type.toString();
639 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); 528 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type);
640 } 529 }
641 } 530 }
642 531
643 if (_contentType==null) 532 if (_contentType==null)
667 } 556 }
668 } 557 }
669 } 558 }
670 } 559 }
671 560
672 /* ------------------------------------------------------------ */ 561 @Override
673 /*
674 * @see javax.servlet.ServletResponse#setContentLength(int)
675 */
676 public void setContentLength(int len) 562 public void setContentLength(int len)
677 { 563 {
678 // Protect from setting after committed as default handling 564 // Protect from setting after committed as default handling
679 // of a servlet HEAD request ALWAYS sets _content length, even 565 // of a servlet HEAD request ALWAYS sets _content length, even
680 // if the getHandling committed the response! 566 // if the getHandling committed the response!
701 } 587 }
702 } 588 }
703 } 589 }
704 } 590 }
705 591
706 /* ------------------------------------------------------------ */ 592 @Override
707 /*
708 * @see javax.servlet.ServletResponse#setContentLength(int)
709 */
710 public void setLongContentLength(long len)
711 {
712 // Protect from setting after committed as default handling
713 // of a servlet HEAD request ALWAYS sets _content length, even
714 // if the getHandling committed the response!
715 if (isCommitted())
716 return;
717 _connection._generator.setContentLength(len);
718 _connection._responseFields.putLongField(HttpHeaders.CONTENT_LENGTH, len);
719 }
720
721 /* ------------------------------------------------------------ */
722 /*
723 * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
724 */
725 public void setContentType(String contentType) 593 public void setContentType(String contentType)
726 { 594 {
727 if (isCommitted()) 595 if (isCommitted())
728 return; 596 return;
729 597
732 // 600 //
733 601
734 if (contentType==null) 602 if (contentType==null)
735 { 603 {
736 if (_locale==null) 604 if (_locale==null)
737 _characterEncoding=null; 605 _characterEncoding = null;
738 _mimeType=null; 606 _mimeType = null;
739 _cachedMimeType=null; 607 _cachedMimeType = null;
740 _contentType=null; 608 _contentType=null;
741 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); 609 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER);
742 } 610 }
743 else 611 else
744 { 612 {
748 if (i0>0) 616 if (i0>0)
749 { 617 {
750 // we have content type parameters 618 // we have content type parameters
751 619
752 // Extract params off mimetype 620 // Extract params off mimetype
753 _mimeType=contentType.substring(0,i0).trim(); 621 _mimeType = contentType.substring(0,i0).trim();
754 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); 622 _cachedMimeType = MimeTypes.CACHE.get(_mimeType);
755 623
756 // Look for charset 624 // Look for charset
757 int i1=contentType.indexOf("charset=",i0+1); 625 int i1=contentType.indexOf("charset=",i0+1);
758 if (i1>=0) 626 if (i1>=0)
759 { 627 {
760 _explicitEncoding=true; 628 _explicitEncoding = true;
761 int i8=i1+8; 629 int i8=i1+8;
762 int i2 = contentType.indexOf(' ',i8); 630 int i2 = contentType.indexOf(' ',i8);
763 631
764 if (_outputState==WRITER) 632 if (_outputState==WRITER)
765 { 633 {
774 _contentType=content_type.toString(); 642 _contentType=content_type.toString();
775 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); 643 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type);
776 } 644 }
777 else 645 else
778 { 646 {
779 _contentType=_mimeType+";charset="+_characterEncoding; 647 _contentType = _mimeType+";charset="+_characterEncoding;
780 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 648 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
781 } 649 }
782 } 650 }
783 else 651 else
784 { 652 {
785 _contentType=_mimeType+";charset="+_characterEncoding; 653 _contentType = _mimeType+";charset="+_characterEncoding;
786 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 654 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
787 } 655 }
788 } 656 }
789 else if (i2<0) 657 else if (i2<0)
790 { 658 {
798 } 666 }
799 } 667 }
800 else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) 668 else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' '))
801 { 669 {
802 // The params are just the char encoding 670 // The params are just the char encoding
803 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); 671 _cachedMimeType = MimeTypes.CACHE.get(_mimeType);
804 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); 672 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8));
805 673
806 if (_cachedMimeType!=null) 674 if (_cachedMimeType!=null)
807 { 675 {
808 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); 676 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding);
836 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 704 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
837 } 705 }
838 } 706 }
839 else // No encoding in the params. 707 else // No encoding in the params.
840 { 708 {
841 _cachedMimeType=null; 709 _cachedMimeType = null;
842 _contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); 710 _contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ");
843 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 711 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
844 } 712 }
845 } 713 }
846 else // No params at all 714 else // No params at all
847 { 715 {
848 _mimeType=contentType; 716 _mimeType = contentType;
849 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); 717 _cachedMimeType = MimeTypes.CACHE.get(_mimeType);
850 718
851 if (_characterEncoding!=null) 719 if (_characterEncoding!=null)
852 { 720 {
853 if (_cachedMimeType!=null) 721 if (_cachedMimeType!=null)
854 { 722 {
858 _contentType=content_type.toString(); 726 _contentType=content_type.toString();
859 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); 727 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type);
860 } 728 }
861 else 729 else
862 { 730 {
863 _contentType=_mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); 731 _contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ");
864 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 732 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
865 } 733 }
866 } 734 }
867 else 735 else
868 { 736 {
870 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); 738 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
871 } 739 }
872 } 740 }
873 else if (_cachedMimeType!=null) 741 else if (_cachedMimeType!=null)
874 { 742 {
875 _contentType=_cachedMimeType.toString(); 743 _contentType = _cachedMimeType.toString();
876 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); 744 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType);
877 } 745 }
878 else 746 else
879 { 747 {
880 _contentType=contentType; 748 _contentType=contentType;
882 } 750 }
883 } 751 }
884 } 752 }
885 } 753 }
886 754
887 /* ------------------------------------------------------------ */ 755 @Override
888 /*
889 * @see javax.servlet.ServletResponse#setBufferSize(int)
890 */
891 public void setBufferSize(int size) 756 public void setBufferSize(int size)
892 { 757 {
893 throw new UnsupportedOperationException(); 758 throw new UnsupportedOperationException();
894 } 759 }
895 760
896 /* ------------------------------------------------------------ */ 761 @Override
897 /*
898 * @see javax.servlet.ServletResponse#getBufferSize()
899 */
900 public int getBufferSize() 762 public int getBufferSize()
901 { 763 {
902 return _connection._generator.getContentBufferSize(); 764 return _connection._generator.getContentBufferSize();
903 } 765 }
904 766
905 /* ------------------------------------------------------------ */ 767 @Override
906 /*
907 * @see javax.servlet.ServletResponse#flushBuffer()
908 */
909 public void flushBuffer() throws IOException 768 public void flushBuffer() throws IOException
910 { 769 {
911 _connection.flushResponse(); 770 _connection.flushResponse();
912 } 771 }
913 772
914 /* ------------------------------------------------------------ */ 773 @Override
915 /*
916 * @see javax.servlet.ServletResponse#reset()
917 */
918 public void reset() 774 public void reset()
919 { 775 {
920 resetBuffer(); 776 resetBuffer();
921 fwdReset(); 777 fwdReset();
922 _status=200; 778 _status = 200;
923 _reason=null; 779 _reason = null;
924 780
925 HttpFields response_fields=_connection._responseFields; 781 HttpFields response_fields=_connection._responseFields;
926 782
927 response_fields.clear(); 783 response_fields.clear();
928 String connection=_connection._requestFields.getStringField(HttpHeaders.CONNECTION_BUFFER); 784 String connection=_connection._requestFields.getStringField(HttpHeaders.CONNECTION_BUFFER);
952 } 808 }
953 } 809 }
954 } 810 }
955 } 811 }
956 812
957
958 public void reset(boolean preserveCookies)
959 {
960 if (!preserveCookies)
961 reset();
962 else
963 {
964 HttpFields response_fields=_connection._responseFields;
965
966 ArrayList<String> cookieValues = new ArrayList<String>(5);
967 Enumeration<String> vals = response_fields.getValues(HttpHeaders.SET_COOKIE);
968 while (vals.hasMoreElements())
969 cookieValues.add((String)vals.nextElement());
970
971 reset();
972
973 for (String v:cookieValues)
974 response_fields.add(HttpHeaders.SET_COOKIE, v);
975 }
976 }
977 813
978 814 private void fwdReset()
979
980 /* ------------------------------------------------------------ */
981 /*
982 * @see javax.servlet.ServletResponse#reset()
983 */
984 public void fwdReset()
985 { 815 {
986 resetBuffer(); 816 resetBuffer();
987 817
988 _writer=null; 818 _writer=null;
989 _outputState=NONE; 819 _outputState=NONE;
990 } 820 }
991 821
992 /* ------------------------------------------------------------ */ 822 @Override
993 /*
994 * @see javax.servlet.ServletResponse#resetBuffer()
995 */
996 public void resetBuffer() 823 public void resetBuffer()
997 { 824 {
998 if (isCommitted()) 825 if (isCommitted())
999 throw new IllegalStateException("Committed"); 826 throw new IllegalStateException("Committed");
1000 _connection._generator.resetBuffer(); 827 _connection._generator.resetBuffer();
1001 } 828 }
1002 829
1003 /* ------------------------------------------------------------ */ 830 @Override
1004 /*
1005 * @see javax.servlet.ServletResponse#isCommitted()
1006 */
1007 public boolean isCommitted() 831 public boolean isCommitted()
1008 { 832 {
1009 return _connection._generator.isCommitted(); 833 return _connection._generator.isCommitted();
1010 } 834 }
1011 835
1012 836
1013 /* ------------------------------------------------------------ */ 837 @Override
1014 /*
1015 * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
1016 */
1017 public void setLocale(Locale locale) 838 public void setLocale(Locale locale)
1018 { 839 {
1019 if (locale == null || isCommitted()) 840 if (locale == null || isCommitted())
1020 return; 841 return;
1021 842
1022 _locale = locale; 843 _locale = locale;
1023 _connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); 844 _connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-'));
1024 } 845 }
1025 846
1026 /* ------------------------------------------------------------ */ 847 @Override
1027 /*
1028 * @see javax.servlet.ServletResponse#getLocale()
1029 */
1030 public Locale getLocale() 848 public Locale getLocale()
1031 { 849 {
1032 if (_locale==null) 850 if (_locale==null)
1033 return Locale.getDefault(); 851 return Locale.getDefault();
1034 return _locale; 852 return _locale;
1052 public String getReason() 870 public String getReason()
1053 { 871 {
1054 return _reason; 872 return _reason;
1055 } 873 }
1056 874
1057 /* ------------------------------------------------------------ */
1058 /**
1059 */
1060 public void complete() 875 public void complete()
1061 throws IOException 876 throws IOException
1062 { 877 {
1063 _connection.completeResponse(); 878 _connection.completeResponse();
1064 } 879 }
1072 if (_connection==null) 887 if (_connection==null)
1073 return -1; 888 return -1;
1074 return _connection._generator.getContentWritten(); 889 return _connection._generator.getContentWritten();
1075 } 890 }
1076 891
1077 /* ------------------------------------------------------------ */
1078 public HttpFields getHttpFields() 892 public HttpFields getHttpFields()
1079 { 893 {
1080 return _connection._responseFields; 894 return _connection._responseFields;
1081 } 895 }
1082 896
1083 /* ------------------------------------------------------------ */
1084 @Override 897 @Override
1085 public String toString() 898 public String toString()
1086 { 899 {
1087 return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+ 900 return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+
1088 _connection._responseFields.toString(); 901 _connection._responseFields.toString();
1089 } 902 }
1090 903
1091 /* ------------------------------------------------------------ */
1092 /* ------------------------------------------------------------ */
1093 /* ------------------------------------------------------------ */
1094 private static class NullOutput extends ServletOutputStream
1095 {
1096 @Override
1097 public void write(int b) throws IOException
1098 {
1099 }
1100
1101 @Override
1102 public void print(String s) throws IOException
1103 {
1104 }
1105
1106 @Override
1107 public void println(String s) throws IOException
1108 {
1109 }
1110
1111 @Override
1112 public void write(byte[] b, int off, int len) throws IOException
1113 {
1114 }
1115
1116 }
1117
1118 } 904 }