| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Wombat::Connector::ResponseBase - internal response base class | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | package My::Connector::Response; | ||
| 12 | |||
| 13 | use base qw(Wombat::Connector::ResponseBase); | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Convenience base implementation of B<Wombat::Response> and | ||
| 18 | B<Servlet::ServletResponse> which can be used for most | ||
| 19 | connectors. Only connector-specific methods need to be implemented. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Wombat::Connector::ResponseBase; | ||
| 24 | |||
| 25 | use base qw(Wombat::Response); | ||
| 26 | use fields qw(application connector contentCount error facade handle); | ||
| 27 | use fields qw(included request); | ||
| 28 | use fields qw(buffer bufferCount bufferSize characterEncoding committed); | ||
| 29 | use fields qw(contentLength contentType locale output writer); | ||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | use Servlet::Util::Exception (); | ||
| 34 | use Wombat::Connector::ResponseFacade (); | ||
| 35 | use Wombat::Connector::ResponseHandle (); | ||
| 36 | use Wombat::Globals (); | ||
| 37 | use Wombat::Util::RequestUtil (); | ||
| 38 | |||
| 39 | =head1 CONSTRUCTOR | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item new() | ||
| 44 | |||
| 45 | Construct and return a B<Wombat::Connector::ResponseBase> instance, | ||
| 46 | initializing fields appropriately. If subclasses override the | ||
| 47 | constructor, they must be sure to call | ||
| 48 | |||
| 49 | $self->SUPER::new(); | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my $self = shift; | ||
| 57 | my $facade = shift; | ||
| 58 | |||
| 59 | $self = fields::new($self) unless ref $self; | ||
| 60 | |||
| 61 | $self->recycle($facade); | ||
| 62 | |||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 ACCESSOR METHODS | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item getApplication() | ||
| 73 | |||
| 74 | Return the Application within which this Response is being generated. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub getApplication { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | return $self->{application}; | ||
| 82 | } | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =item setApplication($application) | ||
| 87 | |||
| 88 | Set the Application within which this Response is being generated. This | ||
| 89 | must be called as soon as the appropriate Application is identified. | ||
| 90 | |||
| 91 | B<Parameters:> | ||
| 92 | |||
| 93 | =over | ||
| 94 | |||
| 95 | =item $application | ||
| 96 | |||
| 97 | the B<Wombat::Application> within which the Response is being generated | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub setApplication { | ||
| 104 | my $self = shift; | ||
| 105 | my $application = shift; | ||
| 106 | |||
| 107 | $self->{application} = $application; | ||
| 108 | |||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | =pod | ||
| 113 | |||
| 114 | =item getBufferSize() | ||
| 115 | |||
| 116 | Return the buffer size (in bytes) used for the response. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub getBufferSize { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | return $self->{bufferSize}; | ||
| 124 | } | ||
| 125 | |||
| 126 | =pod | ||
| 127 | |||
| 128 | =item setBufferSize($size) | ||
| 129 | |||
| 130 | Set the preferred buffer size for the body of the response. | ||
| 131 | |||
| 132 | B<Parameters:> | ||
| 133 | |||
| 134 | =over | ||
| 135 | |||
| 136 | =item $size | ||
| 137 | |||
| 138 | the buffer size, in bytes | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | B<Throws:> | ||
| 143 | |||
| 144 | =over | ||
| 145 | |||
| 146 | =item B<Servlet::Util::IllegalStateException> | ||
| 147 | |||
| 148 | if content has been written to the buffer | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub setBufferSize { | ||
| 155 | my $self = shift; | ||
| 156 | my $size = shift; | ||
| 157 | |||
| 158 | if ($self->{bufferCount}) { | ||
| 159 | my $msg = "setBufferSize: content in buffer"; | ||
| 160 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 161 | } | ||
| 162 | if ($self->isCommitted()) { | ||
| 163 | my $msg = "setBufferSize: response is committed"; | ||
| 164 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 165 | } | ||
| 166 | |||
| 167 | $self->{bufferSize} = $size; | ||
| 168 | |||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | =pod | ||
| 173 | |||
| 174 | =item getCharacterEncoding() | ||
| 175 | |||
| 176 | Return the character encoding used in the body of this Response, or | ||
| 177 | I<ISO-8859-1> if the character encoding is unset. The character | ||
| 178 | encoding is set explicitly with C<setContentType()> or implicity with | ||
| 179 | C<setLocale()>. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub getCharacterEncoding { | ||
| 184 | my $self = shift; | ||
| 185 | |||
| 186 | return $self->{characterEncoding} || 'ISO-8859-1'; | ||
| 187 | } | ||
| 188 | |||
| 189 | =pod | ||
| 190 | |||
| 191 | =item isCommitted() | ||
| 192 | |||
| 193 | Return a flag indicating if this Response has been committed. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub isCommitted { | ||
| 198 | my $self = shift; | ||
| 199 | |||
| 200 | return $self->{committed}; | ||
| 201 | } | ||
| 202 | |||
| 203 | =pod | ||
| 204 | |||
| 205 | =item getConnector() | ||
| 206 | |||
| 207 | Return the Connector through which this Response is returned. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub getConnector { | ||
| 212 | my $self = shift; | ||
| 213 | |||
| 214 | return $self->{connector}; | ||
| 215 | } | ||
| 216 | |||
| 217 | =pod | ||
| 218 | |||
| 219 | =item setConnector($connector) | ||
| 220 | |||
| 221 | Set the Connector through which this response is returned. | ||
| 222 | |||
| 223 | B<Parameters:> | ||
| 224 | |||
| 225 | =over | ||
| 226 | |||
| 227 | =item $connector | ||
| 228 | |||
| 229 | the B<Wombat::Connector> that will return the response | ||
| 230 | |||
| 231 | =back | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | sub setConnector { | ||
| 236 | my $self = shift; | ||
| 237 | my $connector = shift; | ||
| 238 | |||
| 239 | $self->{connector} = $connector; | ||
| 240 | |||
| 241 | return 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =item getContentCount() | ||
| 247 | |||
| 248 | Return the number of bytes actually written to the output handle. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub getContentCount { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | return $self->{contentCount}; | ||
| 256 | } | ||
| 257 | |||
| 258 | =pod | ||
| 259 | |||
| 260 | =item getContentLength() | ||
| 261 | |||
| 262 | Return the content length, in bytes, that was set or calculated for | ||
| 263 | this Response. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub getContentLength { | ||
| 268 | my $self = shift; | ||
| 269 | |||
| 270 | return $self->{contentLength}; | ||
| 271 | } | ||
| 272 | |||
| 273 | =pod | ||
| 274 | |||
| 275 | =item setContentLength($len) | ||
| 276 | |||
| 277 | Set the length of the content body in this Response. | ||
| 278 | |||
| 279 | B<Parameters:> | ||
| 280 | |||
| 281 | =over | ||
| 282 | |||
| 283 | =item $len | ||
| 284 | |||
| 285 | the content length, in bytes | ||
| 286 | |||
| 287 | =back | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub setContentLength { | ||
| 292 | my $self = shift; | ||
| 293 | my $len = shift; | ||
| 294 | |||
| 295 | return 1 if $self->isCommitted(); | ||
| 296 | return 1 if $self->isIncluded(); | ||
| 297 | |||
| 298 | $self->{contentLength} = $len; | ||
| 299 | |||
| 300 | return 1; | ||
| 301 | } | ||
| 302 | |||
| 303 | =pod | ||
| 304 | |||
| 305 | =item getContentType() | ||
| 306 | |||
| 307 | Return the MIME type that was set or calculated for this response. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub getContentType { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | return $self->{contentType}; | ||
| 315 | } | ||
| 316 | |||
| 317 | =pod | ||
| 318 | |||
| 319 | =item setContentType($type) | ||
| 320 | |||
| 321 | Set the content type of this Response. If the C<charset> parameter is | ||
| 322 | specified, the character encoding of this Response is also set. | ||
| 323 | |||
| 324 | B<Parameters:> | ||
| 325 | |||
| 326 | =over | ||
| 327 | |||
| 328 | =item $type | ||
| 329 | |||
| 330 | the MIME type of the content | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub setContentType { | ||
| 337 | my $self = shift; | ||
| 338 | my $type = shift; | ||
| 339 | |||
| 340 | return 1 if $self->isCommitted(); | ||
| 341 | return 1 if $self->isIncluded(); | ||
| 342 | |||
| 343 | $self->{contentType} = $type; | ||
| 344 | |||
| 345 | my $charset = Wombat::Util::RequestUtil->parseCharacterEncoding($type); | ||
| 346 | $self->{characterEncoding} = $charset if $charset; | ||
| 347 | |||
| 348 | return 1; | ||
| 349 | } | ||
| 350 | |||
| 351 | =pod | ||
| 352 | |||
| 353 | =item isError() | ||
| 354 | |||
| 355 | Return a flag indicating whether or not this is an error response. | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | sub isError { | ||
| 360 | my $self = shift; | ||
| 361 | |||
| 362 | return $self->{error}; | ||
| 363 | } | ||
| 364 | |||
| 365 | =pod | ||
| 366 | |||
| 367 | =item setError() | ||
| 368 | |||
| 369 | Set a flag indicating that this Response is an error response. | ||
| 370 | |||
| 371 | =cut | ||
| 372 | |||
| 373 | sub setError { | ||
| 374 | my $self = shift; | ||
| 375 | |||
| 376 | $self->{error} = 1; | ||
| 377 | |||
| 378 | return 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | =pod | ||
| 382 | |||
| 383 | =item getHandle() | ||
| 384 | |||
| 385 | Return the output handle associated with this Response. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub getHandle { | ||
| 390 | my $self = shift; | ||
| 391 | |||
| 392 | return $self->{handle}; | ||
| 393 | } | ||
| 394 | |||
| 395 | =pod | ||
| 396 | |||
| 397 | =item setHandle($handle) | ||
| 398 | |||
| 399 | Set the output handle ssociated with this Response. | ||
| 400 | |||
| 401 | B<Parameters:> | ||
| 402 | |||
| 403 | =over | ||
| 404 | |||
| 405 | =item $handle | ||
| 406 | |||
| 407 | the B<IO::Handle> associated with this response | ||
| 408 | |||
| 409 | =back | ||
| 410 | |||
| 411 | =cut | ||
| 412 | |||
| 413 | sub setHandle { | ||
| 414 | my $self = shift; | ||
| 415 | my $handle = shift; | ||
| 416 | |||
| 417 | $self->{handle} = $handle; | ||
| 418 | } | ||
| 419 | |||
| 420 | =pod | ||
| 421 | |||
| 422 | =item isIncluded() | ||
| 423 | |||
| 424 | Return a flag indicating whether or not this Response is being | ||
| 425 | processed as an include. | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | sub isIncluded { | ||
| 430 | my $self = shift; | ||
| 431 | |||
| 432 | return $self->{included}; | ||
| 433 | } | ||
| 434 | |||
| 435 | =pod | ||
| 436 | |||
| 437 | =item setIncluded($flag) | ||
| 438 | |||
| 439 | Set a flag indicating whether or not this Response is being processed | ||
| 440 | as an include. | ||
| 441 | |||
| 442 | B<Parameters:> | ||
| 443 | |||
| 444 | =over | ||
| 445 | |||
| 446 | =item $flag | ||
| 447 | |||
| 448 | a boolean value indicating whether or not this response is included | ||
| 449 | |||
| 450 | =back | ||
| 451 | |||
| 452 | =cut | ||
| 453 | |||
| 454 | sub setIncluded { | ||
| 455 | my $self = shift; | ||
| 456 | my $flag = shift; | ||
| 457 | |||
| 458 | $self->{included} = $flag; | ||
| 459 | |||
| 460 | return 1; | ||
| 461 | } | ||
| 462 | |||
| 463 | =pod | ||
| 464 | |||
| 465 | =item getLocale() | ||
| 466 | |||
| 467 | Return the locale assigned to this Response. | ||
| 468 | |||
| 469 | =cut | ||
| 470 | |||
| 471 | sub getLocale { | ||
| 472 | my $self = shift; | ||
| 473 | |||
| 474 | return $self->{locale}; | ||
| 475 | } | ||
| 476 | |||
| 477 | =pod | ||
| 478 | |||
| 479 | =item setLocale($loc) | ||
| 480 | |||
| 481 | Set the locale for this Response. The character encoding for this | ||
| 482 | Response will be set to the encoding specified by the locale. | ||
| 483 | |||
| 484 | B<Parameters:> | ||
| 485 | |||
| 486 | =over | ||
| 487 | |||
| 488 | =item $loc | ||
| 489 | |||
| 490 | the locale for the response | ||
| 491 | |||
| 492 | =back | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | sub setLocale { | ||
| 497 | my $self = shift; | ||
| 498 | my $loc = shift; | ||
| 499 | |||
| 500 | return 1 if $self->isCommitted(); | ||
| 501 | return 1 if $self->isIncluded(); | ||
| 502 | |||
| 503 | $self->{locale} = $loc; | ||
| 504 | |||
| 505 | # XXX: map locale to encoding and set encoding | ||
| 506 | |||
| 507 | return 1; | ||
| 508 | } | ||
| 509 | |||
| 510 | =pod | ||
| 511 | |||
| 512 | =item getOutputHandle() | ||
| 513 | |||
| 514 | Return the B<Servlet::ServletOutputHandle> that wraps the underlying | ||
| 515 | output handle (see C<getHandle()>. The default implementation returns a | ||
| 516 | handle created by C<createOutputHandle()>. | ||
| 517 | |||
| 518 | B<Throws:> | ||
| 519 | |||
| 520 | =over | ||
| 521 | |||
| 522 | =item B<Servlet::Util::IllegalStateException> | ||
| 523 | |||
| 524 | if the C<getWriter()> method has already been called for this response | ||
| 525 | |||
| 526 | =item B<Servlet::Util::IOException> | ||
| 527 | |||
| 528 | if an input or output exception occurred | ||
| 529 | |||
| 530 | =back | ||
| 531 | |||
| 532 | =cut | ||
| 533 | |||
| 534 | sub getOutputHandle { | ||
| 535 | my $self = shift; | ||
| 536 | |||
| 537 | if ($self->{writer}) { | ||
| 538 | my $msg = "getOutputHandle: writer already obtained"; | ||
| 539 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 540 | } | ||
| 541 | |||
| 542 | $self->{output} ||= $self->createOutputHandle(); | ||
| 543 | |||
| 544 | return $self->{output}; | ||
| 545 | } | ||
| 546 | |||
| 547 | =pod | ||
| 548 | |||
| 549 | =item getRequest() | ||
| 550 | |||
| 551 | Return the Request with which this Response is associated. | ||
| 552 | |||
| 553 | =cut | ||
| 554 | |||
| 555 | sub getRequest { | ||
| 556 | my $self = shift; | ||
| 557 | |||
| 558 | return $self->{request}; | ||
| 559 | } | ||
| 560 | |||
| 561 | =pod | ||
| 562 | |||
| 563 | =item setRequest($request) | ||
| 564 | |||
| 565 | Set the Request with which this Response is associated. | ||
| 566 | |||
| 567 | B<Parameters:> | ||
| 568 | |||
| 569 | =over | ||
| 570 | |||
| 571 | =item $request | ||
| 572 | |||
| 573 | the B<Wombat::Request> with which this response is associated | ||
| 574 | |||
| 575 | =back | ||
| 576 | |||
| 577 | =cut | ||
| 578 | |||
| 579 | sub setRequest { | ||
| 580 | my $self = shift; | ||
| 581 | my $request = shift; | ||
| 582 | |||
| 583 | $self->{request} = $request; | ||
| 584 | } | ||
| 585 | |||
| 586 | =pod | ||
| 587 | |||
| 588 | =item getResponse() | ||
| 589 | |||
| 590 | Return the ServletResponse for which this object is the facade. | ||
| 591 | |||
| 592 | =cut | ||
| 593 | |||
| 594 | sub getResponse { | ||
| 595 | my $self = shift; | ||
| 596 | |||
| 597 | return $self->{facade}; | ||
| 598 | } | ||
| 599 | |||
| 600 | =pod | ||
| 601 | |||
| 602 | =item getWriter() | ||
| 603 | |||
| 604 | Return the B<XXX> that wraps the ServletOutputHandle for this | ||
| 605 | response. The default implementation returns a B<XXX> wrapped around | ||
| 606 | the handle created by C<createOutputHandle()>. | ||
| 607 | |||
| 608 | B<Throws:> | ||
| 609 | |||
| 610 | =over | ||
| 611 | |||
| 612 | =item B<Servlet::Util::UnsupportedEncodingException> | ||
| 613 | |||
| 614 | if the charset specified in C<setContentType()> cannot be used | ||
| 615 | |||
| 616 | =item B<Servlet::Util::IllegalStateException> | ||
| 617 | |||
| 618 | if the C<getOutputHandle()> method has already been called for this | ||
| 619 | request | ||
| 620 | |||
| 621 | =item B<Servlet::Util::IOException> | ||
| 622 | |||
| 623 | if an input or output exception occurred | ||
| 624 | |||
| 625 | =back | ||
| 626 | |||
| 627 | =cut | ||
| 628 | |||
| 629 | sub getWriter { | ||
| 630 | my $self = shift; | ||
| 631 | |||
| 632 | return $self->{writer} if $self->{writer}; | ||
| 633 | |||
| 634 | if ($self->{output}) { | ||
| 635 | my $msg = "getWriter: output handle already obtained"; | ||
| 636 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 637 | } | ||
| 638 | |||
| 639 | my $encoding = $self->getCharacterEncoding(); | ||
| 640 | unless ($encoding) { | ||
| 641 | my $msg = "getWriter: no character encoding specified"; | ||
| 642 | Servlet::Util::UnsupportedEncodingException->throw($msg); | ||
| 643 | } | ||
| 644 | # XXX: | ||
| 645 | unless (uc $encoding eq 'ISO-8859-1') { | ||
| 646 | my $msg = "getWriter: unsupported character encoding [$encoding]"; | ||
| 647 | Servlet::Util::UnsupportedEncodingException->throw($msg); | ||
| 648 | } | ||
| 649 | |||
| 650 | # XXX: create writer for $encoding | ||
| 651 | $self->{writer} = $self->createOutputHandle(); | ||
| 652 | |||
| 653 | return $self->{writer}; | ||
| 654 | } | ||
| 655 | |||
| 656 | =pod | ||
| 657 | |||
| 658 | =back | ||
| 659 | |||
| 660 | =head1 PUBLIC METHODS | ||
| 661 | |||
| 662 | =over | ||
| 663 | |||
| 664 | =item createOutputHandle() | ||
| 665 | |||
| 666 | Create and return a B<Servlet::ServletOutputHandle> to write the | ||
| 667 | response content. | ||
| 668 | |||
| 669 | B<Throws:> | ||
| 670 | |||
| 671 | =over | ||
| 672 | |||
| 673 | =item Servlet::Util::IOException | ||
| 674 | |||
| 675 | if an input or output error occurs | ||
| 676 | |||
| 677 | =back | ||
| 678 | |||
| 679 | =cut | ||
| 680 | |||
| 681 | sub createOutputHandle { | ||
| 682 | my $self = shift; | ||
| 683 | |||
| 684 | return Wombat::Connector::ResponseHandle->new($self); | ||
| 685 | } | ||
| 686 | |||
| 687 | =pod | ||
| 688 | |||
| 689 | =item finishResponse() | ||
| 690 | |||
| 691 | Perform whatever actions are required to flush and close the output | ||
| 692 | handle or writer. | ||
| 693 | |||
| 694 | B<Throws:> | ||
| 695 | |||
| 696 | =over | ||
| 697 | |||
| 698 | =item Servlet::Util::IOException | ||
| 699 | |||
| 700 | if an input or output error occurs | ||
| 701 | |||
| 702 | =back | ||
| 703 | |||
| 704 | =cut | ||
| 705 | |||
| 706 | sub finishResponse { | ||
| 707 | my $self = shift; | ||
| 708 | |||
| 709 | # Wombat::Globals::DEBUG && | ||
| 710 | # $self->debug("finishing response"); | ||
| 711 | |||
| 712 | if ($self->{writer}) { | ||
| 713 | $self->{writer}->flush(); | ||
| 714 | $self->{writer}->close(); | ||
| 715 | } | ||
| 716 | |||
| 717 | if ($self->{output}) { | ||
| 718 | $self->{output}->flush(); | ||
| 719 | $self->{output}->close(); | ||
| 720 | } | ||
| 721 | |||
| 722 | # the underlying handle is the connector's responsibility | ||
| 723 | |||
| 724 | return 1; | ||
| 725 | } | ||
| 726 | |||
| 727 | =pod | ||
| 728 | |||
| 729 | =item flushBuffer() | ||
| 730 | |||
| 731 | Force any content in the buffer to be written to the client. The | ||
| 732 | response is automatically committed on the first invocation of this | ||
| 733 | method. | ||
| 734 | |||
| 735 | B<Throws:> | ||
| 736 | |||
| 737 | =over | ||
| 738 | |||
| 739 | =item B<Servlet::Util::IOException> | ||
| 740 | |||
| 741 | =back | ||
| 742 | |||
| 743 | =cut | ||
| 744 | |||
| 745 | sub flushBuffer { | ||
| 746 | my $self = shift; | ||
| 747 | |||
| 748 | if ($self->{bufferCount}) { | ||
| 749 | eval { | ||
| 750 | $self->{handle}->write($self->{buffer}, $self->{bufferCount}); | ||
| 751 | }; | ||
| 752 | if ($@) { | ||
| 753 | my $msg = "flushBuffer: problem writing to socket"; | ||
| 754 | Servlet::Util::IOException->new($msg); | ||
| 755 | } | ||
| 756 | |||
| 757 | undef $self->{buffer}; | ||
| 758 | $self->{bufferCount} = 0; | ||
| 759 | |||
| 760 | unless ($self->{committed}) { | ||
| 761 | $self->{committed} = 1; | ||
| 762 | |||
| 763 | # Wombat::Globals::DEBUG && | ||
| 764 | # $self->debug("committed response"); | ||
| 765 | } | ||
| 766 | } | ||
| 767 | |||
| 768 | return 1; | ||
| 769 | } | ||
| 770 | |||
| 771 | =pod | ||
| 772 | |||
| 773 | =item reset() | ||
| 774 | |||
| 775 | Clear any data that exists in the content buffer and unset the | ||
| 776 | content length and content type. | ||
| 777 | |||
| 778 | B<Throws:> | ||
| 779 | |||
| 780 | =over | ||
| 781 | |||
| 782 | =item B<Servlet::Util::IllegalStateException> | ||
| 783 | |||
| 784 | if the response has already been committed | ||
| 785 | |||
| 786 | =back | ||
| 787 | |||
| 788 | =cut | ||
| 789 | |||
| 790 | sub reset { | ||
| 791 | my $self = shift; | ||
| 792 | |||
| 793 | return 1 if $self->isIncluded(); | ||
| 794 | |||
| 795 | $self->resetBuffer(); | ||
| 796 | |||
| 797 | $self->setContentLength(-1); | ||
| 798 | $self->setContentType(undef); | ||
| 799 | |||
| 800 | return 1; | ||
| 801 | } | ||
| 802 | |||
| 803 | =pod | ||
| 804 | |||
| 805 | =item resetBuffer() | ||
| 806 | |||
| 807 | Clear the content of the content buffer in the response without | ||
| 808 | modifying any other fields. | ||
| 809 | |||
| 810 | B<Throws:> | ||
| 811 | |||
| 812 | =over | ||
| 813 | |||
| 814 | =item B<Servlet::Util::IllegalStateException> | ||
| 815 | |||
| 816 | if the response has already been committed | ||
| 817 | |||
| 818 | =back | ||
| 819 | |||
| 820 | =cut | ||
| 821 | |||
| 822 | sub resetBuffer { | ||
| 823 | my $self = shift; | ||
| 824 | |||
| 825 | if ($self->isCommitted()) { | ||
| 826 | my $msg = "resetBuffer: response is committed"; | ||
| 827 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 828 | } | ||
| 829 | |||
| 830 | $self->{bufferCount} = 0; | ||
| 831 | undef $self->{buffer}; | ||
| 832 | |||
| 833 | return 1; | ||
| 834 | } | ||
| 835 | |||
| 836 | =pod | ||
| 837 | |||
| 838 | =back | ||
| 839 | |||
| 840 | =head1 PACKAGE METHODS | ||
| 841 | |||
| 842 | These methods should only ever be called by other classes in the | ||
| 843 | B<Wombat::Connector> namespace. Other packages should use the public | ||
| 844 | API only! | ||
| 845 | |||
| 846 | =over | ||
| 847 | |||
| 848 | =item write($string, [$length, [$offset]]) | ||
| 849 | |||
| 850 | Write the specified string to the content buffer and return the number | ||
| 851 | of bytes written. If the operation causes the buffer to be filled, the buffer | ||
| 852 | will be flushed (as many times as necessary). | ||
| 853 | |||
| 854 | =cut | ||
| 855 | |||
| 856 | sub write { | ||
| 857 | my $self = shift; | ||
| 858 | my $str = shift; | ||
| 859 | my $length = shift || length $str; | ||
| 860 | my $offset = shift || 0; | ||
| 861 | |||
| 862 | return 0 unless $length; | ||
| 863 | |||
| 864 | # if it all fits in the buffer, stick it in and we're done | ||
| 865 | |||
| 866 | if ($length <= $self->{bufferSize} - $self->{bufferCount}) { | ||
| 867 | $self->{buffer} .= substr($str, $offset, $length); | ||
| 868 | |||
| 869 | $self->{bufferCount} += $length; | ||
| 870 | $self->{contentCount} += $length; | ||
| 871 | |||
| 872 | return $length; | ||
| 873 | } | ||
| 874 | |||
| 875 | # if there's more than the buffer can hold, flush it and start | ||
| 876 | # writing buffer sized chunks | ||
| 877 | |||
| 878 | $self->flushBuffer(); | ||
| 879 | |||
| 880 | my $iterations = $length / $self->{bufferSize}; | ||
| 881 | my $leftoverStart = $iterations * $self->{bufferSize}; | ||
| 882 | my $leftoverLen = $length - $leftoverStart; | ||
| 883 | |||
| 884 | my $written = 0; | ||
| 885 | for (my $i = 0; $i < $iterations; $i++) { | ||
| 886 | $written += $self->write($str, $self->{bufferSize}, | ||
| 887 | $offset + ($i * $self->{bufferSize})); | ||
| 888 | } | ||
| 889 | |||
| 890 | # write the leftover bits, which are guaranteed to fit in the buffer | ||
| 891 | |||
| 892 | if ($leftoverLen) { | ||
| 893 | $written += $self->write($str, $leftoverLen, $offset + $leftoverStart); | ||
| 894 | } | ||
| 895 | |||
| 896 | return $written; | ||
| 897 | } | ||
| 898 | |||
| 899 | =pod | ||
| 900 | |||
| 901 | =item recycle() | ||
| 902 | |||
| 903 | Release all object references and initialize instances variables in | ||
| 904 | preparation for use or reuse of this object. | ||
| 905 | |||
| 906 | =cut | ||
| 907 | |||
| 908 | sub recycle { | ||
| 909 | my $self = shift; | ||
| 910 | my $facade = shift; | ||
| 911 | |||
| 912 | # Wombat::Response instance variables | ||
| 913 | $self->{application} = undef; | ||
| 914 | $self->{connector} = undef; | ||
| 915 | $self->{contentCount} = 0; | ||
| 916 | $self->{error} = undef; | ||
| 917 | $self->{facade} = $facade || Wombat::Connector::ResponseFacade->new($self); | ||
| 918 | $self->{handle} = undef; # the handle to which response data is written | ||
| 919 | $self->{included} = undef; | ||
| 920 | $self->{request} = undef; | ||
| 921 | |||
| 922 | # Servlet::ServletResponse instance variables | ||
| 923 | $self->{buffer} = undef; | ||
| 924 | $self->{bufferCount} = 0; | ||
| 925 | $self->{bufferSize} = 1024; | ||
| 926 | $self->{characterEncoding} = undef; | ||
| 927 | $self->{committed} = undef; | ||
| 928 | $self->{contentLength} = -1; | ||
| 929 | $self->{contentType} = undef; | ||
| 930 | $self->{locale} = undef; | ||
| 931 | $self->{output} = undef; # the Servlet::ServletOutputHandle wrapper | ||
| 932 | $self->{writer} = undef; # XXX: character handle | ||
| 933 | |||
| 934 | return 1; | ||
| 935 | } | ||
| 936 | |||
| 937 | =pod | ||
| 938 | |||
| 939 | =back | ||
| 940 | |||
| 941 | =cut | ||
| 942 | |||
| 943 | # private methods | ||
| 944 | |||
| 945 | sub log { | ||
| 946 | my $self = shift; | ||
| 947 | |||
| 948 | $self->{connector}->log(@_) if $self->{connector}; | ||
| 949 | |||
| 950 | return 1; | ||
| 951 | } | ||
| 952 | |||
| 953 | sub debug { | ||
| 954 | my $self = shift; | ||
| 955 | |||
| 956 | # extra check in case we forget to check DEBUG before | ||
| 957 | $self->log($_[0], undef, 'DEBUG') if Wombat::Globals::DEBUG; | ||
| 958 | |||
| 959 | return 1; | ||
| 960 | } | ||
| 961 | |||
| 962 | 1; | ||
| 963 | __END__ |