| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Connector::HttpRequestBase; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Connector::HttpRequestBase - internal http request base class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | package My::Connector::HttpRequest; | ||
| 14 | |||
| 15 | use base qw(Wombat::Connector::HttpRequestBase); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Convenience base implementation of B<Wombat::HttpRequest> and | ||
| 20 | B<Servlet::Http::HttpServletRequest> which can be used for most | ||
| 21 | connectors that implement the HTTP protocol. Only connector-specific | ||
| 22 | methods need to be implemented. Extends | ||
| 23 | B<Wombat::Connector::RequestBase>. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use base qw(Wombat::HttpRequest Wombat::Connector::RequestBase); | ||
| 28 | use fields qw(authType contextPath cookies headers method pathInfo parsed); | ||
| 29 | use fields qw(queryString requestedSessionCookie requestedSessionId); | ||
| 30 | use fields qw(requestedSessionURL requestURI servletPath session); | ||
| 31 | use fields qw(userPrincipal); | ||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | |||
| 35 | use HTTP::Headers (); | ||
| 36 | use Wombat::Connector::HttpRequestFacade (); | ||
| 37 | use Wombat::Globals (); | ||
| 38 | use Wombat::Util::RequestUtil (); | ||
| 39 | |||
| 40 | =pod | ||
| 41 | |||
| 42 | =head1 CONSTRUCTOR | ||
| 43 | |||
| 44 | =over | ||
| 45 | |||
| 46 | =item new() | ||
| 47 | |||
| 48 | Construct and return a B<Wombat::Connector::Http::RequestBase> | ||
| 49 | instance, initializing fields appropriately. If subclasses override the | ||
| 50 | constructor, they must be sure to call | ||
| 51 | |||
| 52 | $self->SUPER::new(); | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my $self = shift; | ||
| 60 | my $facade = shift; | ||
| 61 | |||
| 62 | $self = fields::new($self) unless ref $self; | ||
| 63 | $self->SUPER::new($facade); | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | =pod | ||
| 69 | |||
| 70 | =head1 PUBLIC METHODS | ||
| 71 | |||
| 72 | =over | ||
| 73 | |||
| 74 | =item getAuthType() | ||
| 75 | |||
| 76 | Return the authentication type used for this Request. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub getAuthType { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | return $self->{authType}; | ||
| 84 | } | ||
| 85 | |||
| 86 | =pod | ||
| 87 | |||
| 88 | =item setAuthType($type) | ||
| 89 | |||
| 90 | Set the authentication type used for this request. | ||
| 91 | |||
| 92 | B<Parameters:> | ||
| 93 | |||
| 94 | =over | ||
| 95 | |||
| 96 | =item $type | ||
| 97 | |||
| 98 | the authentication type, as defined in | ||
| 99 | C<Servlet::Http::HttpServletRequest> | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub setAuthType { | ||
| 106 | my $self = shift; | ||
| 107 | my $type = shift; | ||
| 108 | |||
| 109 | $self->{authType} = $type; | ||
| 110 | |||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =item getContextPath() | ||
| 117 | |||
| 118 | Return the portion of the request URI used to select the Application | ||
| 119 | for the Request. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub getContextPath { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | return $self->{contextPath}; | ||
| 127 | } | ||
| 128 | |||
| 129 | =pod | ||
| 130 | |||
| 131 | =item setContextPath($path) | ||
| 132 | |||
| 133 | Set the context path for this Request. This will normally be called | ||
| 134 | when the associated Application is mapping the Request to a particular | ||
| 135 | Wrapper. | ||
| 136 | |||
| 137 | B<Parameters:> | ||
| 138 | |||
| 139 | =over | ||
| 140 | |||
| 141 | =item $path | ||
| 142 | |||
| 143 | the context path | ||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub setContextPath { | ||
| 150 | my $self = shift; | ||
| 151 | my $path = shift; | ||
| 152 | |||
| 153 | $self->{contextPath} = $path; | ||
| 154 | } | ||
| 155 | |||
| 156 | =pod | ||
| 157 | |||
| 158 | =item addCookie($cookie) | ||
| 159 | |||
| 160 | Add a Cookie to the set of Cookies for this Request. This will | ||
| 161 | normally be called when the Connector is parsing the request headers. | ||
| 162 | |||
| 163 | B<Parameters:> | ||
| 164 | |||
| 165 | =over | ||
| 166 | |||
| 167 | =item $cookie | ||
| 168 | |||
| 169 | the B<Servlet::Http::Cookie> | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub addCookie { | ||
| 176 | my $self = shift; | ||
| 177 | my $cookie = shift; | ||
| 178 | |||
| 179 | push @{ $self->{cookies} }, $cookie; | ||
| 180 | |||
| 181 | return 1; | ||
| 182 | } | ||
| 183 | |||
| 184 | =pod | ||
| 185 | |||
| 186 | =item getCookies() | ||
| 187 | |||
| 188 | Return the set of Cookies received with this Request. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub getCookies { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | my @cookies = @{ $self->{cookies} }; | ||
| 196 | |||
| 197 | return wantarray ? @cookies : \@cookies; | ||
| 198 | } | ||
| 199 | |||
| 200 | =pod | ||
| 201 | |||
| 202 | =item clearCookies() | ||
| 203 | |||
| 204 | Remove all Cookies for this Request. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub clearCookies { | ||
| 209 | my $self = shift; | ||
| 210 | |||
| 211 | $self->{cookies} = []; | ||
| 212 | |||
| 213 | return 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | =pod | ||
| 217 | |||
| 218 | =item getDateHeader($name) | ||
| 219 | |||
| 220 | Return the value of the named date header, or -1. | ||
| 221 | |||
| 222 | B<Parameters:> | ||
| 223 | |||
| 224 | =over | ||
| 225 | |||
| 226 | =item $name | ||
| 227 | |||
| 228 | the header name | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub getDateHeader { | ||
| 235 | my $self = shift; | ||
| 236 | my $name = shift; | ||
| 237 | |||
| 238 | my $val = -1; | ||
| 239 | if ($name) { | ||
| 240 | my $match = lc $name; | ||
| 241 | if ($match eq 'date') { | ||
| 242 | $val = $self->{headers}->date(); | ||
| 243 | } elsif ($match eq 'expires') { | ||
| 244 | $val = $self->{headers}->expires(); | ||
| 245 | } elsif ($match eq 'if_modified_since') { | ||
| 246 | $val = $self->{headers}->if_modified_since(); | ||
| 247 | } elsif ($match eq 'if_unmodified_since') { | ||
| 248 | $val = $self->{headers}->if_unmodified_since(); | ||
| 249 | } elsif ($match eq 'last_modified') { | ||
| 250 | $val = $self->{headers}->last_modified(); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | return $val; | ||
| 255 | } | ||
| 256 | |||
| 257 | =pod | ||
| 258 | |||
| 259 | =item addHeader($name, $value) | ||
| 260 | |||
| 261 | Add a value for the named request header. | ||
| 262 | |||
| 263 | B<Parameters:> | ||
| 264 | |||
| 265 | =over | ||
| 266 | |||
| 267 | =item $name | ||
| 268 | |||
| 269 | the parameter name | ||
| 270 | |||
| 271 | =item $value | ||
| 272 | |||
| 273 | the parameter value, scalar | ||
| 274 | |||
| 275 | =back | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | sub addHeader | ||
| 280 | { | ||
| 281 | my $self = shift; | ||
| 282 | my $name = shift; | ||
| 283 | my $value = shift; | ||
| 284 | |||
| 285 | $self->{headers}->push_header($name => $value); | ||
| 286 | |||
| 287 | return 1; | ||
| 288 | } | ||
| 289 | |||
| 290 | =pod | ||
| 291 | |||
| 292 | =item getHeader($name) | ||
| 293 | |||
| 294 | Return the first value for the named request header. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | sub getHeader { | ||
| 299 | my $self = shift; | ||
| 300 | my $name = shift; | ||
| 301 | |||
| 302 | return $self->getHeaders($name)->[0]; | ||
| 303 | } | ||
| 304 | |||
| 305 | =pod | ||
| 306 | |||
| 307 | =item getHeaderNames() | ||
| 308 | |||
| 309 | Return the names of all headers received for this Request. | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | sub getHeaderNames { | ||
| 314 | my $self = shift; | ||
| 315 | |||
| 316 | my %names; | ||
| 317 | $self->{headers}->scan(sub { $names{$_[0]} = 1 }); | ||
| 318 | |||
| 319 | my @names = keys %names; | ||
| 320 | |||
| 321 | return wantarray ? @names : \@names; | ||
| 322 | } | ||
| 323 | |||
| 324 | =pod | ||
| 325 | |||
| 326 | =item getHeaders($name) | ||
| 327 | |||
| 328 | Return the list of values for the named request header. | ||
| 329 | |||
| 330 | B<Parameters:> | ||
| 331 | |||
| 332 | =over | ||
| 333 | |||
| 334 | =item $name | ||
| 335 | |||
| 336 | the header name | ||
| 337 | |||
| 338 | =back | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | sub getHeaders { | ||
| 343 | my $self = shift; | ||
| 344 | my $name = shift; | ||
| 345 | |||
| 346 | my @vals = $self->{headers}->header($name); | ||
| 347 | |||
| 348 | return wantarray ? @vals : \@vals; | ||
| 349 | } | ||
| 350 | |||
| 351 | =pod | ||
| 352 | |||
| 353 | =item clearHeaders() | ||
| 354 | |||
| 355 | Unset all headers for this Request. | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | sub clearHeaders { | ||
| 360 | my $self = shift; | ||
| 361 | |||
| 362 | $self->{headers}->scan(sub { $self->{headers}->remove_header($_[0]) }); | ||
| 363 | |||
| 364 | return 1; | ||
| 365 | } | ||
| 366 | |||
| 367 | =pod | ||
| 368 | |||
| 369 | =item getMethod() | ||
| 370 | |||
| 371 | Return the HTTP request method used for this Request. | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub getMethod { | ||
| 376 | my $self = shift; | ||
| 377 | |||
| 378 | return $self->{method}; | ||
| 379 | } | ||
| 380 | |||
| 381 | =pod | ||
| 382 | |||
| 383 | =item setMethod($method) | ||
| 384 | |||
| 385 | Set the HTTP request method used for this Request. | ||
| 386 | |||
| 387 | B<Parameters:> | ||
| 388 | |||
| 389 | =over | ||
| 390 | |||
| 391 | =item $method | ||
| 392 | |||
| 393 | the request method | ||
| 394 | |||
| 395 | =back | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | sub setMethod { | ||
| 400 | my $self = shift; | ||
| 401 | my $method = shift; | ||
| 402 | |||
| 403 | $self->{method} = $method; | ||
| 404 | } | ||
| 405 | |||
| 406 | =pod | ||
| 407 | |||
| 408 | =item getParameter($name) | ||
| 409 | |||
| 410 | Return the value of the named request parameter. If more than one | ||
| 411 | value is defined, return only the first one. Extract parameters from | ||
| 412 | the request if not already done. | ||
| 413 | |||
| 414 | B<Parameters:> | ||
| 415 | |||
| 416 | =over | ||
| 417 | |||
| 418 | =item $name | ||
| 419 | |||
| 420 | the name of the parameter | ||
| 421 | |||
| 422 | =back | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub getParameter { | ||
| 427 | my $self = shift; | ||
| 428 | my $name = shift; | ||
| 429 | |||
| 430 | $self->parseParameters(); | ||
| 431 | |||
| 432 | my $param = $self->{parameters}->{$name}; | ||
| 433 | |||
| 434 | return defined $param ? $param->[0] : ''; | ||
| 435 | } | ||
| 436 | |||
| 437 | =pod | ||
| 438 | |||
| 439 | =item getParameterMap() | ||
| 440 | |||
| 441 | Return a hash of the parameters of this Request. The keys of the hash | ||
| 442 | are the parameter names, and the values of the hash are arrays of | ||
| 443 | parameter values. Extract parameters from the request if not already | ||
| 444 | done. | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub getParameterMap { | ||
| 449 | my $self = shift; | ||
| 450 | |||
| 451 | my %params; | ||
| 452 | for my $name ($self->getParameterNames()) { | ||
| 453 | $params{$name} = $self->getParameterValues($name); | ||
| 454 | } | ||
| 455 | |||
| 456 | return wantarray ? %params : \%params; | ||
| 457 | } | ||
| 458 | |||
| 459 | =pod | ||
| 460 | |||
| 461 | =item getParameterNames() | ||
| 462 | |||
| 463 | Return an array containing the names of the parameters contained in | ||
| 464 | this Request. Extract parameters from the request if not already done. | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub getParameterNames { | ||
| 469 | my $self = shift; | ||
| 470 | |||
| 471 | $self->parseParameters(); | ||
| 472 | |||
| 473 | my @names = keys %{ $self->{parameters} }; | ||
| 474 | |||
| 475 | return wantarray ? @names : \@names; | ||
| 476 | } | ||
| 477 | |||
| 478 | =pod | ||
| 479 | |||
| 480 | =item getParameterValues($name) | ||
| 481 | |||
| 482 | Return an array containing all of the values of the named request | ||
| 483 | parameter. Extract parameters from the request if not already done. | ||
| 484 | |||
| 485 | B<Parameters:> | ||
| 486 | |||
| 487 | =over | ||
| 488 | |||
| 489 | =item $name | ||
| 490 | |||
| 491 | the name of the parameter | ||
| 492 | |||
| 493 | =back | ||
| 494 | |||
| 495 | =cut | ||
| 496 | |||
| 497 | sub getParameterValues { | ||
| 498 | my $self = shift; | ||
| 499 | my $name = shift; | ||
| 500 | |||
| 501 | $self->parseParameters(); | ||
| 502 | |||
| 503 | my @vals; | ||
| 504 | my $param = $self->{parameters}->{$name}; | ||
| 505 | push @vals, @$param if defined $param; | ||
| 506 | |||
| 507 | return wantarray ? @vals : \@vals; | ||
| 508 | } | ||
| 509 | |||
| 510 | sub parseParameters { | ||
| 511 | my $self = shift; | ||
| 512 | |||
| 513 | return 1 if $self->{parsed}; | ||
| 514 | |||
| 515 | my @uriwords = | ||
| 516 | Wombat::Util::RequestUtil->parseParameters($self->getQueryString()); | ||
| 517 | for my $uriword (@uriwords) { | ||
| 518 | $self->addParameter(@$uriword); | ||
| 519 | } | ||
| 520 | |||
| 521 | if ($self->getMethod() eq Servlet::Http::HttpServlet::METHOD_POST) { | ||
| 522 | my $length = $self->getContentLength(); | ||
| 523 | my $type = $self->getContentType(); | ||
| 524 | $type = Wombat::Util::RequestUtil->parseContentType($type); | ||
| 525 | |||
| 526 | if ($length && $type eq Wombat::Globals::FORM_TYPE_URLENCODED) { | ||
| 527 | eval { | ||
| 528 | my $handle = $self->getInputHandle(); | ||
| 529 | my $buf; | ||
| 530 | my $read = $handle->read($buf, $length); | ||
| 531 | |||
| 532 | my @uriwords = | ||
| 533 | Wombat::Util::RequestUtil->parseParameters($buf); | ||
| 534 | for my $uriword (@uriwords) { | ||
| 535 | $self->addParameter(@$uriword); | ||
| 536 | } | ||
| 537 | }; | ||
| 538 | if ($@) { | ||
| 539 | $self->log("problem parsing parameters", $@, 'ERROR'); | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | $self->{parsed} = 1; | ||
| 545 | |||
| 546 | return 1; | ||
| 547 | } | ||
| 548 | |||
| 549 | =pod | ||
| 550 | |||
| 551 | =item getPathInfo() | ||
| 552 | |||
| 553 | Return the path information for this Request. | ||
| 554 | |||
| 555 | =cut | ||
| 556 | |||
| 557 | sub getPathInfo { | ||
| 558 | my $self = shift; | ||
| 559 | |||
| 560 | return $self->{pathInfo}; | ||
| 561 | } | ||
| 562 | |||
| 563 | =pod | ||
| 564 | |||
| 565 | =item setPathInfo($path) | ||
| 566 | |||
| 567 | Set the path information for this Request. This will normally be | ||
| 568 | called when the associated Application is mapping the Request to a | ||
| 569 | particular Wrapper. | ||
| 570 | |||
| 571 | B<Parameters:> | ||
| 572 | |||
| 573 | =over | ||
| 574 | |||
| 575 | =item $path | ||
| 576 | |||
| 577 | the path information | ||
| 578 | |||
| 579 | =back | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | sub setPathInfo { | ||
| 584 | my $self = shift; | ||
| 585 | my $path = shift; | ||
| 586 | |||
| 587 | $self->{pathInfo} = $path; | ||
| 588 | } | ||
| 589 | |||
| 590 | =pod | ||
| 591 | |||
| 592 | =item getPathTranslated() | ||
| 593 | |||
| 594 | turn extra path information for this request, translated to a real | ||
| 595 | path. | ||
| 596 | |||
| 597 | =cut | ||
| 598 | |||
| 599 | sub getPathTranslated { | ||
| 600 | my $self = shift; | ||
| 601 | |||
| 602 | my $tr = undef; | ||
| 603 | if ($self->{application} && defined $self->{pathInfo}) { | ||
| 604 | my $context = $self->{application}->getServletContext(); | ||
| 605 | if ($context) { | ||
| 606 | $tr = $context->getRealPath($self->{pathInfo}); | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | return $tr; | ||
| 611 | } | ||
| 612 | |||
| 613 | =pod | ||
| 614 | |||
| 615 | =item getQueryString() | ||
| 616 | |||
| 617 | Return the query string for this Request. | ||
| 618 | |||
| 619 | =cut | ||
| 620 | |||
| 621 | sub getQueryString { | ||
| 622 | my $self = shift; | ||
| 623 | |||
| 624 | return $self->{queryString}; | ||
| 625 | } | ||
| 626 | |||
| 627 | =pod | ||
| 628 | |||
| 629 | =item setQueryString($query) | ||
| 630 | |||
| 631 | Set the query string for this Request. This is normally called by the | ||
| 632 | Connector when it parses the request headers. | ||
| 633 | |||
| 634 | B<Parameters:> | ||
| 635 | |||
| 636 | =over | ||
| 637 | |||
| 638 | =item $query | ||
| 639 | |||
| 640 | the query string | ||
| 641 | |||
| 642 | =back | ||
| 643 | |||
| 644 | =cut | ||
| 645 | |||
| 646 | sub setQueryString { | ||
| 647 | my $self = shift; | ||
| 648 | my $query = shift; | ||
| 649 | |||
| 650 | $self->{queryString} = $query; | ||
| 651 | } | ||
| 652 | |||
| 653 | =pod | ||
| 654 | |||
| 655 | =item getRemoteUser() | ||
| 656 | |||
| 657 | Return the name of the remote user that has been authenticated for | ||
| 658 | this Request. | ||
| 659 | |||
| 660 | =cut | ||
| 661 | |||
| 662 | sub getRemoteUser { | ||
| 663 | my $self = shift; | ||
| 664 | |||
| 665 | return $self->{userPrincipal} ? $self->{userPrincipal}->getName() : undef; | ||
| 666 | } | ||
| 667 | |||
| 668 | =pod | ||
| 669 | |||
| 670 | =item isRequestedSessionIdFromCookie() | ||
| 671 | |||
| 672 | Return a flag indicating whether or not the requested session ID for | ||
| 673 | this Request came in through a cookie. | ||
| 674 | |||
| 675 | =cut | ||
| 676 | |||
| 677 | sub isRequestedSessionIdFromCookie { | ||
| 678 | my $self = shift; | ||
| 679 | |||
| 680 | return defined $self->{requestedSessionId} && | ||
| 681 | $self->{requestedSessionCookie}; | ||
| 682 | } | ||
| 683 | |||
| 684 | =pod | ||
| 685 | |||
| 686 | =item setRequestedSessionCookie($flag) | ||
| 687 | |||
| 688 | Set a flag indicating whether or not the requested session ID for this | ||
| 689 | Request came in through a cookie. This is normally called by the | ||
| 690 | Connector when it parses the request headers. | ||
| 691 | |||
| 692 | B<Parameters:> | ||
| 693 | |||
| 694 | =over | ||
| 695 | |||
| 696 | =item $flag | ||
| 697 | |||
| 698 | a boolean value | ||
| 699 | |||
| 700 | =back | ||
| 701 | |||
| 702 | =cut | ||
| 703 | |||
| 704 | sub setRequestedSessionCookie { | ||
| 705 | my $self = shift; | ||
| 706 | my $flag = shift; | ||
| 707 | |||
| 708 | $self->{requestedSessionCookie} = $flag; | ||
| 709 | } | ||
| 710 | |||
| 711 | =pod | ||
| 712 | |||
| 713 | =pod | ||
| 714 | |||
| 715 | =item getRequestedSessionId() | ||
| 716 | |||
| 717 | Return the session ID included in this Request, if any. | ||
| 718 | |||
| 719 | =cut | ||
| 720 | |||
| 721 | sub getRequestedSessionId { | ||
| 722 | my $self = shift; | ||
| 723 | |||
| 724 | return $self->{requestedSessionId}; | ||
| 725 | } | ||
| 726 | |||
| 727 | =item setRequestedSessionId($id) | ||
| 728 | |||
| 729 | Set the requested session ID for this Request. This is normally called | ||
| 730 | by the Connector when it parses the request headers. | ||
| 731 | |||
| 732 | B<Parameters:> | ||
| 733 | |||
| 734 | =over | ||
| 735 | |||
| 736 | =item $id | ||
| 737 | |||
| 738 | the session id | ||
| 739 | |||
| 740 | =back | ||
| 741 | |||
| 742 | =cut | ||
| 743 | |||
| 744 | sub setRequestedSessionId { | ||
| 745 | my $self = shift; | ||
| 746 | my $id = shift; | ||
| 747 | |||
| 748 | $self->{requestedSessionId} = $id; | ||
| 749 | } | ||
| 750 | |||
| 751 | =pod | ||
| 752 | |||
| 753 | =item isRequestedSessionIdFromURL() | ||
| 754 | |||
| 755 | Return a flag indicating whether or not the requested session ID for | ||
| 756 | this Request came in through the request URL. | ||
| 757 | |||
| 758 | =cut | ||
| 759 | |||
| 760 | sub isRequestedSessionIdFromURL { | ||
| 761 | my $self = shift; | ||
| 762 | |||
| 763 | return defined $self->{requestedSessionId} && | ||
| 764 | $self->{requestedSessionURL}; | ||
| 765 | } | ||
| 766 | |||
| 767 | =pod | ||
| 768 | |||
| 769 | =item setRequestedSessionURL($flag) | ||
| 770 | |||
| 771 | Set a flag indicating whether or not the requested session ID for this | ||
| 772 | Request came in through the request URL. This is normally called by | ||
| 773 | the Connector when it parses the request headers. | ||
| 774 | |||
| 775 | B<Parameters:> | ||
| 776 | |||
| 777 | =over | ||
| 778 | |||
| 779 | =item $flag | ||
| 780 | |||
| 781 | a boolean value | ||
| 782 | |||
| 783 | =back | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub setRequestedSessionURL { | ||
| 788 | my $self = shift; | ||
| 789 | my $flag = shift; | ||
| 790 | |||
| 791 | $self->{requestedSessionURL} = $flag; | ||
| 792 | } | ||
| 793 | |||
| 794 | =pod | ||
| 795 | |||
| 796 | =item getRequestURI() | ||
| 797 | |||
| 798 | Return the request URI for this Request. | ||
| 799 | |||
| 800 | =cut | ||
| 801 | |||
| 802 | sub getRequestURI { | ||
| 803 | my $self = shift; | ||
| 804 | |||
| 805 | return $self->{requestURI}; | ||
| 806 | } | ||
| 807 | |||
| 808 | =pod | ||
| 809 | |||
| 810 | =item setRequestURI($uri) | ||
| 811 | |||
| 812 | Set the unparsed request URI for this Request. This is normally called | ||
| 813 | by the Connector when it parses the request headers. | ||
| 814 | |||
| 815 | B<Parameters:> | ||
| 816 | |||
| 817 | =over | ||
| 818 | |||
| 819 | =item $uri | ||
| 820 | |||
| 821 | the request URI | ||
| 822 | |||
| 823 | =back | ||
| 824 | |||
| 825 | =cut | ||
| 826 | |||
| 827 | sub setRequestURI { | ||
| 828 | my $self = shift; | ||
| 829 | my $uri = shift; | ||
| 830 | |||
| 831 | $self->{requestURI} = $uri; | ||
| 832 | } | ||
| 833 | |||
| 834 | =pod | ||
| 835 | |||
| 836 | =item getRequestURL() | ||
| 837 | |||
| 838 | Reconstructs the URL the client used to make the request. The returned | ||
| 839 | URL contains a protocol, server name, port number, and server path, | ||
| 840 | but it does no tinclude query string parameters. | ||
| 841 | |||
| 842 | This method is useful for creating redirect messages and for reporting | ||
| 843 | errors. | ||
| 844 | |||
| 845 | =cut | ||
| 846 | |||
| 847 | sub getRequestURL { | ||
| 848 | my $self = shift; | ||
| 849 | |||
| 850 | my $scheme = $self->getScheme(); | ||
| 851 | |||
| 852 | my $authority = $self->getServerName(); | ||
| 853 | my $port = $self->getServerPort() || 80; | ||
| 854 | $authority = join ':', $authority, $port unless $port == 80; | ||
| 855 | |||
| 856 | my $url = sprintf("%s://%s%s", $self->getScheme, $authority, | ||
| 857 | $self->getRequestURI()); | ||
| 858 | |||
| 859 | return $url; | ||
| 860 | } | ||
| 861 | |||
| 862 | =pod | ||
| 863 | |||
| 864 | =item getServletPath() | ||
| 865 | |||
| 866 | Return the portion of the request URI used to select the servlet that | ||
| 867 | will process this Request. | ||
| 868 | |||
| 869 | =cut | ||
| 870 | |||
| 871 | sub getServletPath { | ||
| 872 | my $self = shift; | ||
| 873 | |||
| 874 | return $self->{servletPath}; | ||
| 875 | } | ||
| 876 | |||
| 877 | =pod | ||
| 878 | |||
| 879 | =item setServletPath($path) | ||
| 880 | |||
| 881 | Set the servlet path for this Request. This is normally called when | ||
| 882 | the associated Application is mapping the Request to a particular | ||
| 883 | Wrapper. | ||
| 884 | |||
| 885 | B<Parameters:> | ||
| 886 | |||
| 887 | =over | ||
| 888 | |||
| 889 | =item $path | ||
| 890 | |||
| 891 | the servlet path | ||
| 892 | |||
| 893 | =back | ||
| 894 | |||
| 895 | =cut | ||
| 896 | |||
| 897 | sub setServletPath { | ||
| 898 | my $self = shift; | ||
| 899 | my $path = shift; | ||
| 900 | |||
| 901 | $self->{servletPath} = $path; | ||
| 902 | } | ||
| 903 | |||
| 904 | =pod | ||
| 905 | |||
| 906 | =item getSession($boolean) | ||
| 907 | |||
| 908 | Return the Session associated with this Request, creating one if | ||
| 909 | necessary and requested. | ||
| 910 | |||
| 911 | B<Parameters:> | ||
| 912 | |||
| 913 | =over | ||
| 914 | |||
| 915 | =item $create | ||
| 916 | |||
| 917 | a flag indicating whether or not a session is to be created if one is | ||
| 918 | needed and does not exist | ||
| 919 | |||
| 920 | =back | ||
| 921 | |||
| 922 | =cut | ||
| 923 | |||
| 924 | sub getSession { | ||
| 925 | my $self = shift; | ||
| 926 | my $create = shift; | ||
| 927 | |||
| 928 | # returns the session's facade, not the session itself | ||
| 929 | |||
| 930 | return $self->{session}->getSession() if $self->{session}; | ||
| 931 | |||
| 932 | # can't have a session if no application has been assigned | ||
| 933 | return undef unless $self->{application}; | ||
| 934 | return undef unless $self->{response}; | ||
| 935 | |||
| 936 | my $manager = $self->{application}->getSessionManager(); | ||
| 937 | |||
| 938 | # sessions are not supported | ||
| 939 | return undef unless $manager; | ||
| 940 | |||
| 941 | # get the requested session, if necessary | ||
| 942 | if (defined $self->{requestedSessionId}) { | ||
| 943 | eval { | ||
| 944 | $self->{session} = | ||
| 945 | $manager->getSession($self->{requestedSessionId}); | ||
| 946 | }; | ||
| 947 | if ($@) { | ||
| 948 | my $msg = sprintf("problem getting session [id %s]", | ||
| 949 | $self->{requestedSessionId}); | ||
| 950 | $self->log($msg, $@, 'ERROR'); | ||
| 951 | } | ||
| 952 | |||
| 953 | if ($self->{session}) { | ||
| 954 | # Wombat::Globals::DEBUG && | ||
| 955 | # $self->log("got session " . $self->{session}->getId(), | ||
| 956 | # undef, 'DEBUG'); | ||
| 957 | return $self->{session}->getSession(); | ||
| 958 | } | ||
| 959 | } | ||
| 960 | |||
| 961 | # requested session didn't exist, was invalid, couldn't be gotten | ||
| 962 | # for some other reason; or there was no requested session. | ||
| 963 | |||
| 964 | return undef unless $create; | ||
| 965 | |||
| 966 | # can't create a session if the response is already | ||
| 967 | # committed. timing is important! | ||
| 968 | return undef if $self->{application}->isSessionCookie() && | ||
| 969 | $self->{response}->isCommitted(); | ||
| 970 | |||
| 971 | eval { | ||
| 972 | $self->{session} = $manager->createSession(); | ||
| 973 | }; | ||
| 974 | if ($@) { | ||
| 975 | $self->log("problem creating session", $@, 'ERROR'); | ||
| 976 | } | ||
| 977 | |||
| 978 | if ($self->{session}) { | ||
| 979 | # Wombat::Globals::DEBUG && | ||
| 980 | # $self->log("created session " . $self->{session}->getId(), | ||
| 981 | # undef, 'DEBUG'); | ||
| 982 | return $self->{session}->getSession(); | ||
| 983 | } | ||
| 984 | |||
| 985 | return undef; | ||
| 986 | } | ||
| 987 | |||
| 988 | =pod | ||
| 989 | |||
| 990 | =item setSession($session) | ||
| 991 | |||
| 992 | Set the internal Session associated with this Request. | ||
| 993 | |||
| 994 | B<Parameters:> | ||
| 995 | |||
| 996 | =over | ||
| 997 | |||
| 998 | =item $session | ||
| 999 | |||
| 1000 | The B<Wombat::Core::Session> to set | ||
| 1001 | |||
| 1002 | =back | ||
| 1003 | |||
| 1004 | =cut | ||
| 1005 | |||
| 1006 | sub setSession { | ||
| 1007 | my $self = shift; | ||
| 1008 | my $session = shift; | ||
| 1009 | |||
| 1010 | $self->{session} = $session; | ||
| 1011 | |||
| 1012 | return 1; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | =pod | ||
| 1016 | |||
| 1017 | =item getUserPrincipal() | ||
| 1018 | |||
| 1019 | Return the name of the current authenticated user. | ||
| 1020 | |||
| 1021 | =cut | ||
| 1022 | |||
| 1023 | sub getUserPrincipal { | ||
| 1024 | my $self = shift; | ||
| 1025 | |||
| 1026 | return $self->{userPrincipal}; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | =pod | ||
| 1030 | |||
| 1031 | =item setUserPrincipal($principal) | ||
| 1032 | |||
| 1033 | Set the principal that has been authenticated for this Request. This | ||
| 1034 | value is also used to calculate the value to be returned by | ||
| 1035 | C<getRemoteUser()>. | ||
| 1036 | |||
| 1037 | B<Parameters:> | ||
| 1038 | |||
| 1039 | =over | ||
| 1040 | |||
| 1041 | =item $principal | ||
| 1042 | |||
| 1043 | the B<Servlet::Util::Principal> to set | ||
| 1044 | |||
| 1045 | =back | ||
| 1046 | |||
| 1047 | =cut | ||
| 1048 | |||
| 1049 | sub setUserPrincipal { | ||
| 1050 | my $self = shift; | ||
| 1051 | my $principal = shift; | ||
| 1052 | |||
| 1053 | $self->{userPrincipal} = $principal; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | =pod | ||
| 1057 | |||
| 1058 | =back | ||
| 1059 | |||
| 1060 | =head1 PUBLIC METHODS | ||
| 1061 | |||
| 1062 | =over | ||
| 1063 | |||
| 1064 | =item isRequestedSessionIdValid() | ||
| 1065 | |||
| 1066 | Return a boolean value indicating whether or not the requested session | ||
| 1067 | ID is still valid. | ||
| 1068 | |||
| 1069 | =cut | ||
| 1070 | |||
| 1071 | sub isRequestedSessionIdValid { | ||
| 1072 | my $self = shift; | ||
| 1073 | |||
| 1074 | return undef unless defined $self->{requestedSessionId}; | ||
| 1075 | return undef unless $self->{context}; | ||
| 1076 | |||
| 1077 | my $manager = $self->{context}->getSessionManager; | ||
| 1078 | return undef unless $manager; | ||
| 1079 | |||
| 1080 | my $session; | ||
| 1081 | eval { | ||
| 1082 | $session = $manager->getSession($self->{requestedSessionId}); | ||
| 1083 | }; | ||
| 1084 | if ($@) { | ||
| 1085 | my $msg = sprintf("problem getting session [id %s]", | ||
| 1086 | $self->{requestedSessionId}); | ||
| 1087 | $self->log($msg, $@, 'ERROR'); | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | return $session && $session->isValid(); | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | =pod | ||
| 1094 | |||
| 1095 | =item isUserInRole($role) | ||
| 1096 | |||
| 1097 | Return a boolean value indicating whether or not the authenticated | ||
| 1098 | user principal is associated with the named role. | ||
| 1099 | |||
| 1100 | B<Parameters:> | ||
| 1101 | |||
| 1102 | =over | ||
| 1103 | |||
| 1104 | =item $role | ||
| 1105 | |||
| 1106 | the name of the role | ||
| 1107 | |||
| 1108 | =back | ||
| 1109 | |||
| 1110 | =cut | ||
| 1111 | |||
| 1112 | sub isUserInRole { | ||
| 1113 | my $self = shift; | ||
| 1114 | my $role = shift; | ||
| 1115 | |||
| 1116 | return undef unless $self->{userPrincipal}; | ||
| 1117 | return undef unless $self->{application}; | ||
| 1118 | |||
| 1119 | my $realm = $self->{application}->getRealm(); | ||
| 1120 | return undef unless $realm; | ||
| 1121 | |||
| 1122 | # first check for a role alias defined in a <security-role-ref> | ||
| 1123 | # element, then for a role defined directly as a <security-role> | ||
| 1124 | |||
| 1125 | my @check = ($role); | ||
| 1126 | if ($self->{wrapper}) { | ||
| 1127 | my $realRole = $self->{wrapper}->getSecurityReference($role); | ||
| 1128 | unshift @check, $realRole if $realRole; | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | for my $check (@check) { | ||
| 1132 | return 1 if $realm->hasRole($self->{userPrincipal}, $check); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | return undef; | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | =pod | ||
| 1139 | |||
| 1140 | =item recycle() | ||
| 1141 | |||
| 1142 | Release all object references and initialize instances variables in | ||
| 1143 | preparation for use or reuse of this object. | ||
| 1144 | |||
| 1145 | =cut | ||
| 1146 | |||
| 1147 | sub recycle { | ||
| 1148 | my $self = shift; | ||
| 1149 | my $facade = shift; | ||
| 1150 | |||
| 1151 | $self->SUPER::recycle($facade); | ||
| 1152 | |||
| 1153 | $self->{authType} = undef; | ||
| 1154 | $self->{contextPath} = ''; | ||
| 1155 | $self->{cookies} = []; | ||
| 1156 | $self->{headers} = HTTP::Headers->new(); | ||
| 1157 | $self->{method} = undef; | ||
| 1158 | $self->{parsed} = undef; | ||
| 1159 | $self->{pathInfo} = undef; | ||
| 1160 | $self->{queryString} = undef; | ||
| 1161 | $self->{requestedSessionCookie} = undef; | ||
| 1162 | $self->{requestedSessionId} = undef; | ||
| 1163 | $self->{requestedSessionURL} = undef; | ||
| 1164 | $self->{requestURI} = undef; | ||
| 1165 | $self->{servletPath} = undef; | ||
| 1166 | $self->{session} = undef; | ||
| 1167 | $self->{userPrincipal} = undef; | ||
| 1168 | |||
| 1169 | $self->{facade} = $facade || | ||
| 1170 | Wombat::Connector::HttpRequestFacade->new($self); | ||
| 1171 | |||
| 1172 | return 1; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | =pod | ||
| 1176 | |||
| 1177 | =back | ||
| 1178 | |||
| 1179 | =cut | ||
| 1180 | |||
| 1181 | 1; | ||
| 1182 | __END__ |