| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Servlet::InvokerServlet; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Servlet::InvokerServlet - servlet that invokes other servlets | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | The default servlet-invoking servlet for most web applications, used | ||
| 14 | to serve requests to servlets that have not been registered in the web | ||
| 15 | application deployment descriptor. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use base qw(Wombat::ContainerServlet Servlet::Http::HttpServlet); | ||
| 20 | use fields qw(application wrapper); | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Servlet::UnavailableException (); | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =head1 CONSTRUCTOR | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item new() | ||
| 33 | |||
| 34 | Create and return an instance, initializing fields to default values. | ||
| 35 | |||
| 36 | =back | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | $self = fields::new($self) unless ref $self; | ||
| 44 | $self->SUPER::new(); | ||
| 45 | |||
| 46 | $self->{application} = undef; | ||
| 47 | $self->{wrapper} = undef; | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =pod | ||
| 53 | |||
| 54 | =head1 ACCESSOR METHODS | ||
| 55 | |||
| 56 | =over | ||
| 57 | |||
| 58 | =item getWrapper() | ||
| 59 | |||
| 60 | Return the Wrapper that wraps this Servlet. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub getWrapper { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | return $self->{wrapper}; | ||
| 68 | } | ||
| 69 | |||
| 70 | =pod | ||
| 71 | |||
| 72 | =item setWrapper($wrapper) | ||
| 73 | |||
| 74 | Set the Wrapper that wraps this Servlet. | ||
| 75 | |||
| 76 | B<Parameters:> | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item $wrapper | ||
| 81 | |||
| 82 | the B<Wombat::Core::Wrapper> that wraps this Servlet | ||
| 83 | |||
| 84 | =back | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub setWrapper { | ||
| 89 | my $self = shift; | ||
| 90 | my $wrapper = shift; | ||
| 91 | |||
| 92 | $self->{wrapper} = $wrapper; | ||
| 93 | $self->{application} = $wrapper->getParent() if $wrapper; | ||
| 94 | |||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | =head1 PUBLIC METHODS | ||
| 103 | |||
| 104 | =over | ||
| 105 | |||
| 106 | =item doDelete($request, $response) | ||
| 107 | |||
| 108 | Process a DELETE request for the specified resource. | ||
| 109 | |||
| 110 | B<Parameters:> | ||
| 111 | |||
| 112 | =over | ||
| 113 | |||
| 114 | =item I<$request> | ||
| 115 | |||
| 116 | the B<Servlet::Http::HttpServletRequest> object that contains the | ||
| 117 | client request | ||
| 118 | |||
| 119 | =item I<$response> | ||
| 120 | |||
| 121 | the B<Servlet::Http::HttpServletResponse> object that contains the | ||
| 122 | servlet response | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | B<Throws:> | ||
| 127 | |||
| 128 | =over | ||
| 129 | |||
| 130 | =item B<Servlet::ServletException> | ||
| 131 | |||
| 132 | if the request cannot be handled | ||
| 133 | |||
| 134 | =item B<Servlet::Util::IOException> | ||
| 135 | |||
| 136 | if an input or output error occurs | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub doDelete { | ||
| 143 | my $self = shift; | ||
| 144 | my $request = shift; | ||
| 145 | my $response = shift; | ||
| 146 | |||
| 147 | return $self->serveRequest($request, $response); | ||
| 148 | } | ||
| 149 | |||
| 150 | =pod | ||
| 151 | |||
| 152 | =item doGet($request, $response) | ||
| 153 | |||
| 154 | Process a GET request for the specified resource. | ||
| 155 | |||
| 156 | B<Parameters:> | ||
| 157 | |||
| 158 | =over | ||
| 159 | |||
| 160 | =item I<$request> | ||
| 161 | |||
| 162 | the B<Servlet::Http::HttpServletRequest> object that contains the | ||
| 163 | client request | ||
| 164 | |||
| 165 | =item I<$response> | ||
| 166 | |||
| 167 | the B<Servlet::Http::HttpServletResponse> object that contains the | ||
| 168 | servlet response | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | B<Throws:> | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item B<Servlet::ServletException> | ||
| 177 | |||
| 178 | if the request cannot be handled | ||
| 179 | |||
| 180 | =item B<Servlet::Util::IOException> | ||
| 181 | |||
| 182 | if an input or output error occurs | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub doGet { | ||
| 189 | my $self = shift; | ||
| 190 | my $request = shift; | ||
| 191 | my $response = shift; | ||
| 192 | |||
| 193 | return $self->serveRequest($request, $response); | ||
| 194 | } | ||
| 195 | |||
| 196 | =pod | ||
| 197 | |||
| 198 | =item doHead($request, $response) | ||
| 199 | |||
| 200 | Process a HEAD request for the specified resource. | ||
| 201 | |||
| 202 | B<Parameters:> | ||
| 203 | |||
| 204 | =over | ||
| 205 | |||
| 206 | =item I<$request> | ||
| 207 | |||
| 208 | the B<Servlet::Http::HttpServletRequest> object that contains the | ||
| 209 | client request | ||
| 210 | |||
| 211 | =item I<$response> | ||
| 212 | |||
| 213 | the B<Servlet::Http::HttpServletResponse> object that contains the | ||
| 214 | servlet response | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | B<Throws:> | ||
| 219 | |||
| 220 | =over | ||
| 221 | |||
| 222 | =item B<Servlet::ServletException> | ||
| 223 | |||
| 224 | if the request cannot be handled | ||
| 225 | |||
| 226 | =item B<Servlet::Util::IOException> | ||
| 227 | |||
| 228 | if an input or output error occurs | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub doHead { | ||
| 235 | my $self = shift; | ||
| 236 | my $request = shift; | ||
| 237 | my $response = shift; | ||
| 238 | |||
| 239 | return $self->serveRequest($request, $response); | ||
| 240 | } | ||
| 241 | |||
| 242 | =pod | ||
| 243 | |||
| 244 | =item doPost($request, $response) | ||
| 245 | |||
| 246 | Process a POST request for the specified resoruce. | ||
| 247 | |||
| 248 | B<Parameters:> | ||
| 249 | |||
| 250 | =over | ||
| 251 | |||
| 252 | =item I<$request> | ||
| 253 | |||
| 254 | the B<Servlet::Http::HttpServletRequest> object that contains the | ||
| 255 | client request | ||
| 256 | |||
| 257 | =item I<$response> | ||
| 258 | |||
| 259 | the B<Servlet::Http::HttpServletResponse> object that contains the | ||
| 260 | servlet response | ||
| 261 | |||
| 262 | =back | ||
| 263 | |||
| 264 | B<Throws:> | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =item B<Servlet::ServletException> | ||
| 269 | |||
| 270 | if the request cannot be handled | ||
| 271 | |||
| 272 | =item B<Servlet::Util::IOException> | ||
| 273 | |||
| 274 | if an input or output error occurs | ||
| 275 | |||
| 276 | =back | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub doPost { | ||
| 281 | my $self = shift; | ||
| 282 | my $request = shift; | ||
| 283 | my $response = shift; | ||
| 284 | |||
| 285 | return $self->serveRequest($request, $response); | ||
| 286 | } | ||
| 287 | |||
| 288 | =pod | ||
| 289 | |||
| 290 | =item doPut($request, $response) | ||
| 291 | |||
| 292 | Process a PUT request for the specified resource. | ||
| 293 | |||
| 294 | B<Parameters:> | ||
| 295 | |||
| 296 | =over | ||
| 297 | |||
| 298 | =item I<$request> | ||
| 299 | |||
| 300 | the B<Servlet::Http::HttpServletRequest> object that contains the | ||
| 301 | client request | ||
| 302 | |||
| 303 | =item I<$response> | ||
| 304 | |||
| 305 | the B<Servlet::Http::HttpServletResponse> object that contains the | ||
| 306 | servlet response | ||
| 307 | |||
| 308 | =back | ||
| 309 | |||
| 310 | B<Throws:> | ||
| 311 | |||
| 312 | =over | ||
| 313 | |||
| 314 | =item B<Servlet::ServletException> | ||
| 315 | |||
| 316 | if the request cannot be handled | ||
| 317 | |||
| 318 | =item B<Servlet::Util::IOException> | ||
| 319 | |||
| 320 | if an input or output error occurs | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub doPut { | ||
| 327 | my $self = shift; | ||
| 328 | my $request = shift; | ||
| 329 | my $response = shift; | ||
| 330 | |||
| 331 | return $self->serveRequest($request, $response); | ||
| 332 | } | ||
| 333 | |||
| 334 | =pod | ||
| 335 | |||
| 336 | =item init([$config]) | ||
| 337 | |||
| 338 | Called by the servlet container to indicate to a servlet that the | ||
| 339 | servlet is being placed into service. | ||
| 340 | |||
| 341 | This implementation stores the config object it receives from the | ||
| 342 | servlet container for later use. When overriding this method, make | ||
| 343 | sure to call | ||
| 344 | |||
| 345 | $self->SUPER::init($config) | ||
| 346 | |||
| 347 | B<Parameters:> | ||
| 348 | |||
| 349 | =over | ||
| 350 | |||
| 351 | =item I<$config> | ||
| 352 | |||
| 353 | the B<Servlet::ServletConfig> object that contains configuration | ||
| 354 | information for this servlet | ||
| 355 | |||
| 356 | =back | ||
| 357 | |||
| 358 | B<Throws:> | ||
| 359 | |||
| 360 | =over | ||
| 361 | |||
| 362 | =item B<Servlet::ServletException> | ||
| 363 | |||
| 364 | if an exception occurs that interrupts the servlet's normal operation | ||
| 365 | |||
| 366 | =back | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub init { | ||
| 371 | my $self = shift; | ||
| 372 | my $servletConfig = shift; | ||
| 373 | |||
| 374 | unless ($self->{wrapper} && $self->{application} ) { | ||
| 375 | Servlet::UnavailableException->throw("init: no wrapper"); | ||
| 376 | } | ||
| 377 | |||
| 378 | $self->SUPER::init($servletConfig); | ||
| 379 | |||
| 380 | return 1; | ||
| 381 | } | ||
| 382 | |||
| 383 | # private methods | ||
| 384 | |||
| 385 | sub serveRequest { | ||
| 386 | my $self = shift; | ||
| 387 | my $request = shift; | ||
| 388 | my $response = shift; | ||
| 389 | |||
| 390 | # XXX: account for includes | ||
| 391 | |||
| 392 | # servlet name or class is in the path info | ||
| 393 | my $pathInfo = $request->getPathInfo(); | ||
| 394 | unless ($pathInfo) { | ||
| 395 | # Wombat::Globals::DEBUG && | ||
| 396 | # $self->debug(sprintf("no path info for %s", | ||
| 397 | # $request->getRequestURI())); | ||
| 398 | |||
| 399 | my $status = Servlet::Http::HttpServletResponse::SC_NOT_FOUND; | ||
| 400 | $response->sendError($status); | ||
| 401 | |||
| 402 | return 1; | ||
| 403 | } | ||
| 404 | |||
| 405 | # pathInfo is of the form '/SnoopServlet' or | ||
| 406 | # '/Wombat::Servlet::SnoopServlet' | ||
| 407 | my $class; | ||
| 408 | (undef, $class, $pathInfo) = split /\//, $pathInfo, 3; | ||
| 409 | $pathInfo ||= ''; | ||
| 410 | |||
| 411 | my $name = "INVOKED::$class"; | ||
| 412 | my $pattern = join '/', $request->getServletPath(), $class, '*'; | ||
| 413 | |||
| 414 | # does the named servlet already exist? | ||
| 415 | my $wrapper = $self->{application}->getChild($class); | ||
| 416 | if ($wrapper) { | ||
| 417 | $name = $wrapper->getName() || $class; | ||
| 418 | $self->{application}->addServletMapping($pattern, $name); | ||
| 419 | |||
| 420 | # Wombat::Globals::DEBUG && | ||
| 421 | # $self->debug("added servlet mapping for $pattern to name $name"); | ||
| 422 | } else { | ||
| 423 | # create and install a new wrapper | ||
| 424 | eval { | ||
| 425 | $wrapper = $self->{application}->createWrapper(); | ||
| 426 | $wrapper->setName($name); | ||
| 427 | $wrapper->setLoadOnStartup(1); | ||
| 428 | $wrapper->setServletClass($class); | ||
| 429 | |||
| 430 | $self->{application}->addChild($wrapper); | ||
| 431 | $self->{application}->addServletMapping($pattern, $name); | ||
| 432 | |||
| 433 | # Wombat::Globals::DEBUG && | ||
| 434 | # $self->debug("added servlet mapping for $pattern to class $name"); | ||
| 435 | }; | ||
| 436 | if ($@) { | ||
| 437 | $self->log("failed to create wrapper for $name", $@, 'ERROR'); | ||
| 438 | |||
| 439 | my $status = Servlet::Http::HttpServletResponse::SC_NOT_FOUND; | ||
| 440 | $response->sendError($status); | ||
| 441 | |||
| 442 | return 1; | ||
| 443 | } | ||
| 444 | |||
| 445 | # ensure the instance can be allocated and deallocated | ||
| 446 | eval { | ||
| 447 | my $instance = $wrapper->allocate(); | ||
| 448 | $wrapper->deallocate($instance); | ||
| 449 | }; | ||
| 450 | if ($@) { | ||
| 451 | my $e = $@; | ||
| 452 | |||
| 453 | $self->{application}->removeServletMapping($pattern); | ||
| 454 | $self->{application}->removeChild($wrapper); | ||
| 455 | |||
| 456 | # Wombat::Globals::DEBUG && | ||
| 457 | # $self->debug("removed servlet mapping for $pattern"); | ||
| 458 | |||
| 459 | my $status; | ||
| 460 | if ($e->isa('Servlet::ServletException')) { | ||
| 461 | my $root = $e->getRootCause(); | ||
| 462 | if ($root && $root =~ /^Can't locate/) { | ||
| 463 | $status = | ||
| 464 | Servlet::Http::HttpServletResponse::SC_NOT_FOUND; | ||
| 465 | $response->sendError($status); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | unless ($status) { | ||
| 470 | # rethrow the exception so that the container will | ||
| 471 | # serve an error page containing an exception message | ||
| 472 | $e->rethrow() if ref $e; | ||
| 473 | Servlet::ServletException->throw($@); | ||
| 474 | } | ||
| 475 | |||
| 476 | return 1; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | my $path = join '/', $request->getServletPath(), $class, $pathInfo; | ||
| 481 | my $dispatcher = $self->getServletContext()->getRequestDispatcher($path); | ||
| 482 | return $dispatcher->forward($request, $response); | ||
| 483 | } | ||
| 484 | |||
| 485 | sub debug { | ||
| 486 | my $self = shift; | ||
| 487 | my $msg = shift; | ||
| 488 | |||
| 489 | return $self->getWrapper()->log($msg, undef, 'DEBUG'); | ||
| 490 | } | ||
| 491 | |||
| 492 | 1; | ||
| 493 | __END__ |