1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.connector;
import java.io.IOException;
import java.io.Writer;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Globals;
import org.apache.coyote.ActionCode;
import org.apache.coyote.Response;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.C2BConverter;
import org.apache.tomcat.util.buf.CharChunk;
/**
* The buffer used by Tomcat response. This is a derivative of the Tomcat 3.3
* OutputBuffer, with the removal of some of the state handling (which in
* Coyote is mostly the Processor's responsibility).
*
* @author Costin Manolache
* @author Remy Maucherat
*/
public class OutputBuffer extends Writer
implements ByteChunk.ByteOutputChannel, CharChunk.CharOutputChannel {
// -------------------------------------------------------------- Constants
public static final String DEFAULT_ENCODING =
org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
public static final int DEFAULT_BUFFER_SIZE = 8*1024;
// ----------------------------------------------------- Instance Variables
/**
* The byte buffer.
*/
private final ByteChunk bb;
/**
* The chunk buffer.
*/
private final CharChunk cb;
/**
* State of the output buffer.
*/
private boolean initial = true;
/**
* Number of bytes written.
*/
private long bytesWritten = 0;
/**
* Number of chars written.
*/
private long charsWritten = 0;
/**
* Flag which indicates if the output buffer is closed.
*/
private boolean closed = false;
/**
* Do a flush on the next operation.
*/
private boolean doFlush = false;
/**
* Byte chunk used to output bytes.
*/
private final ByteChunk outputChunk = new ByteChunk();
/**
* Char chunk used to output chars.
*/
private CharChunk outputCharChunk = new CharChunk();
/**
* Encoding to use.
*/
private String enc;
/**
* Encoder is set.
*/
private boolean gotEnc = false;
/**
* List of encoders.
*/
protected HashMap<String, C2BConverter> encoders =
new HashMap<String, C2BConverter>();
/**
* Current char to byte converter.
*/
protected C2BConverter conv;
/**
* Associated Coyote response.
*/
private Response coyoteResponse;
/**
* Suspended flag. All output bytes will be swallowed if this is true.
*/
private boolean suspended = false;
// ----------------------------------------------------------- Constructors
/**
* Default constructor. Allocate the buffer with the default buffer size.
*/
public OutputBuffer() {
this(DEFAULT_BUFFER_SIZE);
}
/**
* Alternate constructor which allows specifying the initial buffer size.
*
* @param size Buffer size to use
*/
public OutputBuffer(int size) {
bb = new ByteChunk(size);
bb.setLimit(size);
bb.setByteOutputChannel(this);
cb = new CharChunk(size);
cb.setLimit(size);
cb.setOptimizedWrite(false);
cb.setCharOutputChannel(this);
}
// ------------------------------------------------------------- Properties
/**
* Associated Coyote response.
*
* @param coyoteResponse Associated Coyote response
*/
public void setResponse(Response coyoteResponse) {
this.coyoteResponse = coyoteResponse;
}
/**
* Get associated Coyote response.
*
* @return the associated Coyote response
*/
@Deprecated
public Response getResponse() {
return this.coyoteResponse;
}
/**
* Is the response output suspended ?
*
* @return suspended flag value
*/
public boolean isSuspended() {
return this.suspended;
}
/**
* Set the suspended flag.
*
* @param suspended New suspended flag value
*/
public void setSuspended(boolean suspended) {
this.suspended = suspended;
}
/**
* Is the response output closed ?
*
* @return closed flag value
*/
public boolean isClosed() {
return this.closed;
}
// --------------------------------------------------------- Public Methods
/**
* Recycle the output buffer.
*/
public void recycle() {
initial = true;
bytesWritten = 0;
charsWritten = 0;
bb.recycle();
cb.recycle();
outputCharChunk.setChars(null, 0, 0);
closed = false;
suspended = false;
doFlush = false;
if (conv!= null) {
conv.recycle();
}
gotEnc = false;
enc = null;
}
/**
* Clear cached encoders (to save memory for Comet requests).
*/
public void clearEncoders() {
encoders.clear();
}
/**
* Close the output buffer. This tries to calculate the response size if
* the response has not been committed yet.
*
* @throws IOException An underlying IOException occurred
*/
@Override
public void close()
throws IOException {
if (closed) {
return;
}
if (suspended) {
return;
}
// If there are chars, flush all of them to the byte buffer now as bytes are used to
// calculate the content-length (if everything fits into the byte buffer, of course).
if (cb.getLength() > 0) {
cb.flushBuffer();
}
if ((!coyoteResponse.isCommitted())
&& (coyoteResponse.getContentLengthLong() == -1)) {
// If this didn't cause a commit of the response, the final content
// length can be calculated
if (!coyoteResponse.isCommitted()) {
coyoteResponse.setContentLength(bb.getLength());
}
}
if (coyoteResponse.getStatus() ==
HttpServletResponse.SC_SWITCHING_PROTOCOLS) {
doFlush(true);
} else {
doFlush(false);
}
closed = true;
// The request should have been completely read by the time the response
// is closed. Further reads of the input a) are pointless and b) really
// confuse AJP (bug 50189) so close the input buffer to prevent them.
Request req = (Request) coyoteResponse.getRequest().getNote(
CoyoteAdapter.ADAPTER_NOTES);
req.inputBuffer.close();
coyoteResponse.finish();
}
/**
* Flush bytes or chars contained in the buffer.
*
* @throws IOException An underlying IOException occurred
*/
@Override
public void flush() throws IOException {
doFlush(true);
}
/**
* Flush bytes or chars contained in the buffer.
*
* @throws IOException An underlying IOException occurred
*/
protected void doFlush(boolean realFlush) throws IOException {
if (suspended) {
return;
}
try {
doFlush = true;
if (initial) {
coyoteResponse.sendHeaders();
initial = false;
}
if (cb.getLength() > 0) {
cb.flushBuffer();
}
if (bb.getLength() > 0) {
bb.flushBuffer();
}
} finally {
doFlush = false;
}
if (realFlush) {
coyoteResponse.action(ActionCode.CLIENT_FLUSH, null);
// If some exception occurred earlier, or if some IOE occurred
// here, notify the servlet with an IOE
if (coyoteResponse.isExceptionPresent()) {
throw new ClientAbortException(coyoteResponse.getErrorException());
}
}
}
// ------------------------------------------------- Bytes Handling Methods
/**
* Sends the buffer data to the client output, checking the
* state of Response and calling the right interceptors.
*
* @param buf Byte buffer to be written to the response
* @param off Offset
* @param cnt Length
*
* @throws IOException An underlying IOException occurred
*/
@Override
public void realWriteBytes(byte buf[], int off, int cnt)
throws IOException {
if (closed) {
return;
}
if (coyoteResponse == null) {
return;
}
// If we really have something to write
if (cnt > 0) {
// real write to the adapter
outputChunk.setBytes(buf, off, cnt);
try {
coyoteResponse.doWrite(outputChunk);
} catch (IOException e) {
// An IOException on a write is almost always due to
// the remote client aborting the request. Wrap this
// so that it can be handled better by the error dispatcher.
throw new ClientAbortException(e);
}
}
}
public void write(byte b[], int off, int len) throws IOException {
if (suspended) {
return;
}
writeBytes(b, off, len);
}
private void writeBytes(byte b[], int off, int len)
throws IOException {
if (closed) {
return;
}
bb.append(b, off, len);
bytesWritten += len;
// if called from within flush(), then immediately flush
// remaining bytes
if (doFlush) {
bb.flushBuffer();
}
}
public void writeByte(int b)
throws IOException {
if (suspended) {
return;
}
bb.append((byte) b);
bytesWritten++;
}
// ------------------------------------------------- Chars Handling Methods
/**
* Convert the chars to bytes, then send the data to the client.
*
* @param buf Char buffer to be written to the response
* @param off Offset
* @param len Length
*
* @throws IOException An underlying IOException occurred
*/
@Override
public void realWriteChars(char buf[], int off, int len)
throws IOException {
outputCharChunk.setChars(buf, off, len);
while (outputCharChunk.getLength() > 0) {
conv.convert(outputCharChunk, bb);
if (bb.getLength() == 0) {
// Break out of the loop if more chars are needed to produce any output
break;
}
if (outputCharChunk.getLength() > 0) {
if (bb.getBuffer().length == bb.getEnd() && bb.getLength() < bb.getLimit()) {
// Need to expand output buffer
bb.makeSpace(outputCharChunk.getLength());
} else {
bb.flushBuffer();
}
}
}
}
@Override
public void write(int c)
throws IOException {
if (suspended) {
return;
}
cb.append((char) c);
charsWritten++;
}
@Override
public void write(char c[])
throws IOException {
if (suspended) {
return;
}
write(c, 0, c.length);
}
@Override
public void write(char c[], int off, int len)
throws IOException {
if (suspended) {
return;
}
cb.append(c, off, len);
charsWritten += len;
}
/**
* Append a string to the buffer
*/
@Override
public void write(String s, int off, int len)
throws IOException {
if (suspended) {
return;
}
charsWritten += len;
if (s == null) {
s = "null";
}
cb.append(s, off, len);
charsWritten += len;
}
@Override
public void write(String s)
throws IOException {
if (suspended) {
return;
}
if (s == null) {
s = "null";
}
cb.append(s);
charsWritten += s.length();
}
public void setEncoding(String s) {
enc = s;
}
public void checkConverter()
throws IOException {
if (!gotEnc) {
setConverter();
}
}
protected void setConverter()
throws IOException {
if (coyoteResponse != null) {
enc = coyoteResponse.getCharacterEncoding();
}
gotEnc = true;
if (enc == null) {
enc = DEFAULT_ENCODING;
}
conv = encoders.get(enc);
if (conv == null) {
if (Globals.IS_SECURITY_ENABLED){
try{
conv = AccessController.doPrivileged(
new PrivilegedExceptionAction<C2BConverter>(){
@Override
public C2BConverter run() throws IOException{
return new C2BConverter(enc);
}
}
);
}catch(PrivilegedActionException ex){
Exception e = ex.getException();
if (e instanceof IOException) {
throw (IOException)e;
}
}
} else {
conv = new C2BConverter(enc);
}
encoders.put(enc, conv);
}
}
// -------------------- BufferedOutputStream compatibility
public long getContentWritten() {
return bytesWritten + charsWritten;
}
/**
* True if this buffer hasn't been used ( since recycle() ) -
* i.e. no chars or bytes have been added to the buffer.
*/
public boolean isNew() {
return (bytesWritten == 0) && (charsWritten == 0);
}
public void setBufferSize(int size) {
if (size > bb.getLimit()) {// ??????
bb.setLimit(size);
}
}
public void reset() {
reset(false);
}
public void reset(boolean resetWriterStreamFlags) {
bb.recycle();
cb.recycle();
bytesWritten = 0;
charsWritten = 0;
if (resetWriterStreamFlags) {
gotEnc = false;
enc = null;
}
initial = true;
}
public int getBufferSize() {
return bb.getLimit();
}
}
|