| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::Application; | ||
| 4 | |||
| 5 | use base qw(Servlet::ServletContext Wombat::Core::ContainerBase); | ||
| 6 | use fields qw(available docBase basePath constraints displayName facade); | ||
| 7 | use fields qw(loginConfig servletMappings sessionTimeout); | ||
| 8 | use fields qw(attributes initParameters sessionCookie); | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use File::Spec (); | ||
| 13 | use Servlet::Util::Exception (); | ||
| 14 | use URI (); | ||
| 15 | use URI::Escape (); | ||
| 16 | use Wombat::Core::ApplicationFacade (); | ||
| 17 | use Wombat::Core::ApplicationValve (); | ||
| 18 | use Wombat::Core::RequestDispatcher (); | ||
| 19 | use Wombat::Core::Wrapper (); | ||
| 20 | use Wombat::Deploy::LoginConfig (); | ||
| 21 | use Wombat::Exception (); | ||
| 22 | use Wombat::Util::XmlMapper (); | ||
| 23 | |||
| 24 | use constant DESCRIPTOR => '/WEB-INF/web.xml'; | ||
| 25 | use constant AUTHENTICATORS => { | ||
| 26 | 'BASIC' => | ||
| 27 | 'Wombat::Authenticator::BasicAuthenticator', | ||
| 28 | }; | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $class = shift; | ||
| 32 | |||
| 33 | my $self = fields::new($class); | ||
| 34 | $self->SUPER::new(); | ||
| 35 | |||
| 36 | # internal application fields | ||
| 37 | $self->{available} = undef; | ||
| 38 | $self->{constraints} = []; | ||
| 39 | $self->{displayName} = undef; | ||
| 40 | $self->{facade} = Wombat::Core::ApplicationFacade->new($self); | ||
| 41 | $self->{loginConfig} = undef; | ||
| 42 | $self->{servletMappings} = {}; | ||
| 43 | $self->{sessionTimeout} = 30; # minutes | ||
| 44 | |||
| 45 | # Servlet::ServletContext fields | ||
| 46 | $self->{attributes} = {}; | ||
| 47 | $self->{initParameters} = {}; | ||
| 48 | $self->{sessionCookie} = 1; | ||
| 49 | |||
| 50 | $self->{mapperClass} = 'Wombat::Core::ApplicationMapper'; | ||
| 51 | $self->{pipeline}->setBasic(Wombat::Core::ApplicationValve->new()); | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | # accessors | ||
| 57 | |||
| 58 | sub getAvailable { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | return $self->{available}; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub setAvailable { | ||
| 65 | my $self = shift; | ||
| 66 | my $flag = shift; | ||
| 67 | |||
| 68 | $self->{available} = $flag; | ||
| 69 | |||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub getAttribute { | ||
| 74 | my $self = shift; | ||
| 75 | my $name = shift; | ||
| 76 | |||
| 77 | return $self->{attributes}->{$name}; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub getAttributeNames { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | my @attributes = keys %{ $self->{attributes} }; | ||
| 84 | |||
| 85 | return wantarray ? @attributes : \@attributes; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub removeAttribute { | ||
| 89 | my $self = shift; | ||
| 90 | my $name = shift; | ||
| 91 | |||
| 92 | delete $self->{attributes}->{$name}; | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub setAttribute { | ||
| 98 | my $self = shift; | ||
| 99 | my $name = shift; | ||
| 100 | my $value = shift; | ||
| 101 | |||
| 102 | $self->{attributes}->{$name} = $value; | ||
| 103 | |||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub addConstraint { | ||
| 108 | my $self = shift; | ||
| 109 | my $constraint = shift; | ||
| 110 | |||
| 111 | # validate the proposed constraint | ||
| 112 | for my $collection ($constraint->getCollections()) { | ||
| 113 | for my $pattern ($collection->getPatterns()) { | ||
| 114 | unless ($self->validateURLPattern($pattern)) { | ||
| 115 | my $msg = "addConstraint: invalid pattern [$pattern]"; | ||
| 116 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | push @{ $self->{constraints} }, $constraint; | ||
| 122 | |||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub getConstraints { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | my @constraints = @{ $self->{constraints} }; | ||
| 130 | |||
| 131 | return wantarray ? @constraints : \@constraints; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub removeConstraint { | ||
| 135 | my $self = shift; | ||
| 136 | my $constraint = shift; | ||
| 137 | |||
| 138 | my $n; | ||
| 139 | for (my $i=0; $i< @{ $self->{constraints} }; $i++) { | ||
| 140 | if (ref $constraint eq ref $self->{constraints}->[$i]) { | ||
| 141 | $n = $i; | ||
| 142 | last; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | splice @{ $self->{constraints} }, $n, 1 if defined $n; | ||
| 147 | |||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub getDisplayName { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | return $self->{displayName}; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub setDisplayName { | ||
| 158 | my $self = shift; | ||
| 159 | my $displayName = shift; | ||
| 160 | |||
| 161 | $self->{displayName} = $displayName; | ||
| 162 | |||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub getDocBase { | ||
| 167 | my $self = shift; | ||
| 168 | |||
| 169 | return $self->{docBase}; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub setDocBase { | ||
| 173 | my $self = shift; | ||
| 174 | my $docBase = shift; | ||
| 175 | |||
| 176 | $self->{docBase} = $docBase; | ||
| 177 | |||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub addInitParameter { | ||
| 182 | my $self = shift; | ||
| 183 | my $name = shift; | ||
| 184 | my $value = shift; | ||
| 185 | |||
| 186 | $self->{initParameters}->{$name} = $value; | ||
| 187 | |||
| 188 | return 1; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub getInitParameter { | ||
| 192 | my $self = shift; | ||
| 193 | my $name = shift; | ||
| 194 | |||
| 195 | return $self->{initParameters}->{$name}; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub getInitParameterNames { | ||
| 199 | my $self = shift; | ||
| 200 | |||
| 201 | my @initParameters = keys %{ $self->{initParameters} }; | ||
| 202 | |||
| 203 | return wantarray ? @initParameters : \@initParameters; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub removeInitParameter { | ||
| 207 | my $self = shift; | ||
| 208 | my $name = shift; | ||
| 209 | |||
| 210 | delete $self->{initParameters}->{$name}; | ||
| 211 | |||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub getLoginConfig { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | return $self->{loginConfig}; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub setLoginConfig { | ||
| 222 | my $self = shift; | ||
| 223 | my $config = shift; | ||
| 224 | |||
| 225 | unless ($config) { | ||
| 226 | my $msg = "setLoginConfig: login config required"; | ||
| 227 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 228 | } | ||
| 229 | |||
| 230 | my $loginPage = $config->getLoginPage(); | ||
| 231 | if ($loginPage && $loginPage !~ m|^/|) { | ||
| 232 | my $msg = "setLoginConfig: login page must be relative to context"; | ||
| 233 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 234 | } | ||
| 235 | |||
| 236 | my $errorPage = $config->getErrorPage(); | ||
| 237 | if ($errorPage && $errorPage !~ m|^/|) { | ||
| 238 | my $msg = "setLoginConfig: error page must be relative to context"; | ||
| 239 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 240 | } | ||
| 241 | |||
| 242 | $self->{loginConfig} = $config; | ||
| 243 | |||
| 244 | return 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub getPath { | ||
| 248 | my $self = shift; | ||
| 249 | |||
| 250 | return $self->getName(); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub setPath { | ||
| 254 | my $self = shift; | ||
| 255 | my $path = shift; | ||
| 256 | |||
| 257 | $self->setName(URI::Escape::uri_unescape($path)); | ||
| 258 | |||
| 259 | return 1; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub addServletMapping { | ||
| 263 | my $self = shift; | ||
| 264 | my $pattern = shift; | ||
| 265 | my $name = shift; | ||
| 266 | |||
| 267 | unless ($self->getChild($name)) { | ||
| 268 | my $msg = "servletMapping: servlet not defined [$name]"; | ||
| 269 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 270 | } | ||
| 271 | |||
| 272 | $pattern = URI::Escape::uri_unescape($pattern); | ||
| 273 | unless ($self->validateURLPattern($pattern)) { | ||
| 274 | my $msg = "servletMapping: invalid pattern [$pattern]"; | ||
| 275 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 276 | } | ||
| 277 | |||
| 278 | $self->{servletMappings}->{$pattern} = $name; | ||
| 279 | |||
| 280 | return 1; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub getServletMapping { | ||
| 284 | my $self = shift; | ||
| 285 | my $pattern = shift; | ||
| 286 | |||
| 287 | return $self->{servletMappings}->{$pattern}; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub getServletMappingNames { | ||
| 291 | my $self = shift; | ||
| 292 | |||
| 293 | my @servletMappings = keys %{ $self->{servletMappings} }; | ||
| 294 | |||
| 295 | return wantarray ? @servletMappings : \@servletMappings; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub removeServletMapping { | ||
| 299 | my $self = shift; | ||
| 300 | my $pattern = shift; | ||
| 301 | |||
| 302 | delete $self->{servletMappings}->{$pattern}; | ||
| 303 | |||
| 304 | return 1; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub isSessionCookie { | ||
| 308 | my $self = shift; | ||
| 309 | |||
| 310 | return $self->{sessionCookie}; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub setSessionCookie { | ||
| 314 | my $self = shift; | ||
| 315 | my $flag = shift; | ||
| 316 | |||
| 317 | $self->{sessionCookie} = $flag; | ||
| 318 | |||
| 319 | return 1; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub getSessionTimeout { | ||
| 323 | my $self = shift; | ||
| 324 | |||
| 325 | return $self->{sessionTimeout}; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub setSessionTimeout { | ||
| 329 | my $self = shift; | ||
| 330 | my $timeout = shift; | ||
| 331 | |||
| 332 | $self->{sessionTimeout} = $timeout; | ||
| 333 | |||
| 334 | return 1; | ||
| 335 | } | ||
| 336 | |||
| 337 | # public methods | ||
| 338 | |||
| 339 | sub addChild { | ||
| 340 | my $self = shift; | ||
| 341 | my $child = shift; | ||
| 342 | |||
| 343 | unless ($child->isa('Wombat::Core::Wrapper')) { | ||
| 344 | my $msg = "addChild: child container must be Wrapper"; | ||
| 345 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 346 | } | ||
| 347 | |||
| 348 | $self->SUPER::addChild($child); | ||
| 349 | |||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub createWrapper { | ||
| 354 | my $self = shift; | ||
| 355 | |||
| 356 | my $wrapper = Wombat::Core::Wrapper->new(); | ||
| 357 | |||
| 358 | return $wrapper; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub getServletContext { | ||
| 362 | my $self = shift; | ||
| 363 | |||
| 364 | return $self->{facade}; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub toString { | ||
| 368 | my $self = shift; | ||
| 369 | |||
| 370 | my $parent = $self->getParent(); | ||
| 371 | my $str = sprintf "Application[%s]", $self->getName(); | ||
| 372 | $str = sprintf "%s.%s", $parent->toString(), $str if $parent; | ||
| 373 | |||
| 374 | return $str; | ||
| 375 | } | ||
| 376 | |||
| 377 | # Servlet::ServletContext methods | ||
| 378 | |||
| 379 | sub getRealPath { | ||
| 380 | my $self = shift; | ||
| 381 | my $path = shift; | ||
| 382 | |||
| 383 | return undef unless $path; | ||
| 384 | |||
| 385 | return File::Spec->canonpath(File::Spec->catfile($self->getBasePath(), | ||
| 386 | $path)); | ||
| 387 | } | ||
| 388 | |||
| 389 | sub getRequestDispatcher { | ||
| 390 | my $self = shift; | ||
| 391 | my $path = shift; | ||
| 392 | |||
| 393 | return undef unless $path; | ||
| 394 | |||
| 395 | unless ($path =~ m|^/|) { | ||
| 396 | my $msg = "getRequestDispatcher: path must be context-relative"; | ||
| 397 | Servlet::Util::IllegalArgumentException->throw($msg); | ||
| 398 | } | ||
| 399 | |||
| 400 | my $contextPath = $self->getPath() || ''; | ||
| 401 | my ($relativeURI, $queryString) = split /\?/, $path, 2; | ||
| 402 | |||
| 403 | # make a fake request in order to map the wrapper | ||
| 404 | my $request = Wombat::Connector::HttpRequestBase->new(); | ||
| 405 | $request->setApplication($self); | ||
| 406 | $request->setContextPath($contextPath || undef); | ||
| 407 | $request->setRequestURI(join '', $contextPath, $relativeURI); | ||
| 408 | $request->setQueryString($queryString); | ||
| 409 | |||
| 410 | my $wrapper = $self->map($request); | ||
| 411 | return undef unless $wrapper; | ||
| 412 | |||
| 413 | return Wombat::Core::RequestDispatcher->new($wrapper); | ||
| 414 | } | ||
| 415 | |||
| 416 | sub getResourceAsHandle { | ||
| 417 | my $self = shift; | ||
| 418 | my $path = shift; | ||
| 419 | |||
| 420 | # XXX: in java, resources are bound to a JNDI naming context. we | ||
| 421 | # don't want to be that sophisticated/complex for now. so assume | ||
| 422 | # that all resources refer to files in the local filesystem. | ||
| 423 | |||
| 424 | my $loc = $self->getRealPath($path); | ||
| 425 | return undef unless $loc; | ||
| 426 | |||
| 427 | my $fh = IO::File->new($loc); | ||
| 428 | |||
| 429 | Wombat::Globals::DEBUG && !$fh && | ||
| 430 | $self->debug("can't find resource for $path [$loc]: $!"); | ||
| 431 | |||
| 432 | return $fh; | ||
| 433 | } | ||
| 434 | |||
| 435 | # lifecycle methods | ||
| 436 | |||
| 437 | sub start { | ||
| 438 | my $self = shift; | ||
| 439 | |||
| 440 | $self->setAvailable(undef); | ||
| 441 | |||
| 442 | $self->SUPER::start(); | ||
| 443 | undef $self->{started}; | ||
| 444 | |||
| 445 | # configure application from deployment descriptor | ||
| 446 | $self->applicationConfig(); | ||
| 447 | |||
| 448 | # configure an authenticator if necessary | ||
| 449 | $self->authenticatorConfig(); | ||
| 450 | |||
| 451 | # apply session timeout override | ||
| 452 | my $manager = $self->getSessionManager(); | ||
| 453 | $manager->setMaxInactiveInterval($self->{sessionTimeout} * 60) if $manager; | ||
| 454 | |||
| 455 | # XXX: set up welcome files | ||
| 456 | |||
| 457 | # add /WEB-INF/lib to @INC so application-specific classes can be found | ||
| 458 | # XXX: application-specific classloaders sure would be nice! | ||
| 459 | my $libdir = File::Spec->catdir($self->getBasePath(), 'WEB-INF', 'lib'); | ||
| 460 | unshift @INC, $libdir; | ||
| 461 | |||
| 462 | # XXX: load servlets as specified by "loadOnStartup" | ||
| 463 | |||
| 464 | $self->{started} = 1; | ||
| 465 | $self->setAvailable(1); | ||
| 466 | |||
| 467 | return 1; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub stop { | ||
| 471 | my $self = shift; | ||
| 472 | |||
| 473 | $self->setAvailable(undef); | ||
| 474 | $self->SUPER::stop(); | ||
| 475 | |||
| 476 | return 1; | ||
| 477 | } | ||
| 478 | |||
| 479 | # private methods | ||
| 480 | |||
| 481 | sub applicationConfig { | ||
| 482 | my $self = shift; | ||
| 483 | |||
| 484 | my $fh = $self->getResourceAsHandle(DESCRIPTOR); | ||
| 485 | unless ($fh) { | ||
| 486 | my $msg = sprintf("start: can't find deployment descriptor for %s", | ||
| 487 | $self->getName()); | ||
| 488 | Wombat::LifecycleException->throw($msg); | ||
| 489 | } | ||
| 490 | |||
| 491 | my $mapper = Wombat::Util::XmlMapper->new(); | ||
| 492 | $self->createRules($mapper); | ||
| 493 | $mapper->readXml($fh, $self); | ||
| 494 | $fh->close(); | ||
| 495 | |||
| 496 | return 1; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub getBasePath { | ||
| 500 | my $self = shift; | ||
| 501 | |||
| 502 | return $self->{basePath} if $self->{basePath}; | ||
| 503 | |||
| 504 | if (File::Spec->file_name_is_absolute($self->{docBase})) { | ||
| 505 | $self->{basePath} = $self->{docBase}; | ||
| 506 | return $self->{basePath}; | ||
| 507 | } | ||
| 508 | |||
| 509 | # find host parent app base | ||
| 510 | my $appBase; | ||
| 511 | my $container = $self; | ||
| 512 | while ($container = $container->getParent()) { | ||
| 513 | if ($container->isa('Wombat::Core::Host')) { | ||
| 514 | $appBase = $container->getAppBase(); | ||
| 515 | last; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | # XXX: if there's no host parent, use the server home | ||
| 520 | $appBase ||= $ENV{WOMBAT_HOME}; | ||
| 521 | |||
| 522 | $self->{basePath} = File::Spec->rel2abs($self->{docBase}, $appBase); | ||
| 523 | return $self->{basePath}; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub validateURLPattern { | ||
| 527 | my $self = shift; | ||
| 528 | my $pattern = shift; | ||
| 529 | |||
| 530 | return undef unless defined $pattern; | ||
| 531 | |||
| 532 | # if the pattern starts with "*." it may not contain "/" | ||
| 533 | return $pattern !~ m|/| if $pattern =~ m|^\*\.|; | ||
| 534 | |||
| 535 | # the pattern must start with "/" | ||
| 536 | return $pattern =~ m|^/|; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub createRules { | ||
| 540 | my $self = shift; | ||
| 541 | my $mapper = shift; | ||
| 542 | |||
| 543 | # init parameters | ||
| 544 | $mapper->addRule("web-app/context-param", | ||
| 545 | $mapper->methodSetter('addInitParameter', 2)); | ||
| 546 | $mapper->addRule("web-app/context-param/param-name", | ||
| 547 | $mapper->methodParam(0)); | ||
| 548 | $mapper->addRule("web-app/context-param/param-value", | ||
| 549 | $mapper->methodParam(1)); | ||
| 550 | |||
| 551 | # display name | ||
| 552 | $mapper->addRule("web-app/display-name", | ||
| 553 | $mapper->methodSetter('setDisplayName', 0)); | ||
| 554 | |||
| 555 | # login config | ||
| 556 | $mapper->addRule("web-app/login-config", | ||
| 557 | $mapper->objectCreate("Wombat::Deploy::LoginConfig")); | ||
| 558 | $mapper->addRule("web-app/login-config", | ||
| 559 | $mapper->addChild("setLoginConfig")); | ||
| 560 | $mapper->addRule("web-app/login-config/auth-method", | ||
| 561 | $mapper->methodSetter('setAuthMethod', 0)); | ||
| 562 | $mapper->addRule("web-app/login-config/realm-name", | ||
| 563 | $mapper->methodSetter('setRealmName', 0)); | ||
| 564 | $mapper->addRule("web-app/login-config/form-login-config/form-login-page", | ||
| 565 | $mapper->methodSetter('setLoginPage', 0)); | ||
| 566 | $mapper->addRule("web-app/login-config/form-login-config/form-error-page", | ||
| 567 | $mapper->methodSetter('setErrorPage', 0)); | ||
| 568 | |||
| 569 | # security constraints | ||
| 570 | $mapper->addRule("web-app/security-constraint", | ||
| 571 | $mapper->objectCreate("Wombat::Deploy::SecurityConstraint")); | ||
| 572 | $mapper->addRule("web-app/security-constraint", | ||
| 573 | $mapper->addChild("addConstraint")); | ||
| 574 | $mapper->addRule("web-app/security-constraint/auth-constraint", | ||
| 575 | Wombat::Util::XmlMapper::SetAuthConstraintAction->new()); | ||
| 576 | $mapper->addRule("web-app/security-constraint/auth-constraint/role-name", | ||
| 577 | $mapper->methodSetter('addAuthRole', 0)); | ||
| 578 | $mapper->addRule("web-app/security-constraint/display-name", | ||
| 579 | $mapper->methodSetter('setDisplayName', 0)); | ||
| 580 | $mapper->addRule("web-app/security-constraint/user-data-constraint/transport-guarantee", | ||
| 581 | $mapper->methodSetter('setUserConstraint', 0)); | ||
| 582 | $mapper->addRule("web-app/security-constraint/web-resource-collection", | ||
| 583 | $mapper->objectCreate("Wombat::Deploy::SecurityCollection")); | ||
| 584 | $mapper->addRule("web-app/security-constraint/web-resource-collection", | ||
| 585 | $mapper->addChild("addCollection")); | ||
| 586 | $mapper->addRule("web-app/security-constraint/web-resource-collection/http-method", | ||
| 587 | $mapper->methodSetter('addMethod', 0)); | ||
| 588 | $mapper->addRule("web-app/security-constraint/web-resource-collection/url-pattern", | ||
| 589 | $mapper->methodSetter('addPattern', 0)); | ||
| 590 | $mapper->addRule("web-app/security-constraint/web-resource-collection/web-resource-name", | ||
| 591 | $mapper->methodSetter('setName', 0)); | ||
| 592 | |||
| 593 | # servlets | ||
| 594 | $mapper->addRule("web-app/servlet", | ||
| 595 | Wombat::Util::XmlMapper::WrapperCreateAction->new("Wombat::Core::Wrapper")); | ||
| 596 | $mapper->addRule("web-app/servlet", | ||
| 597 | $mapper->addChild("addChild")); | ||
| 598 | $mapper->addRule("web-app/servlet/init-param", | ||
| 599 | $mapper->methodSetter('addInitParameter', 2)); | ||
| 600 | $mapper->addRule("web-app/servlet/init-param/param-name", | ||
| 601 | $mapper->methodParam(0)); | ||
| 602 | $mapper->addRule("web-app/servlet/init-param/param-value", | ||
| 603 | $mapper->methodParam(1)); | ||
| 604 | $mapper->addRule("web-app/servlet/load-on-startup", | ||
| 605 | $mapper->methodSetter('setLoadOnStartup', 0)); | ||
| 606 | $mapper->addRule("web-app/servlet/run-as/role-name", | ||
| 607 | $mapper->methodSetter('setRunAs', 0)); | ||
| 608 | $mapper->addRule("web-app/servlet/security-role-ref", | ||
| 609 | $mapper->methodSetter('addSecurityReference', 2)); | ||
| 610 | $mapper->addRule("web-app/servlet/security-role-ref/role-name", | ||
| 611 | $mapper->methodParam(0)); | ||
| 612 | $mapper->addRule("web-app/servlet/security-role-ref/role-link", | ||
| 613 | $mapper->methodParam(1)); | ||
| 614 | $mapper->addRule("web-app/servlet/servlet-class", | ||
| 615 | $mapper->methodSetter('setServletClass', 0)); | ||
| 616 | $mapper->addRule("web-app/servlet/servlet-name", | ||
| 617 | $mapper->methodSetter('setName', 0)); | ||
| 618 | |||
| 619 | # servlet mappings | ||
| 620 | $mapper->addRule("web-app/servlet-mapping", | ||
| 621 | $mapper->methodSetter('addServletMapping', 2)); | ||
| 622 | $mapper->addRule("web-app/servlet-mapping/url-pattern", | ||
| 623 | $mapper->methodParam(0)); | ||
| 624 | $mapper->addRule("web-app/servlet-mapping/servlet-name", | ||
| 625 | $mapper->methodParam(1)); | ||
| 626 | |||
| 627 | # session config | ||
| 628 | $mapper->addRule("web-app/session-config", | ||
| 629 | $mapper->methodSetter('setSessionTimeout', 1)); | ||
| 630 | $mapper->addRule("web-app/session-config/session-timeout", | ||
| 631 | $mapper->methodParam(0)); | ||
| 632 | |||
| 633 | return 1; | ||
| 634 | } | ||
| 635 | |||
| 636 | sub authenticatorConfig { | ||
| 637 | my $self = shift; | ||
| 638 | |||
| 639 | # make sure the application requires an authenticator | ||
| 640 | my $constraints = $self->getConstraints(); | ||
| 641 | return 1 unless @$constraints; | ||
| 642 | |||
| 643 | my $loginConfig = $self->getLoginConfig(); | ||
| 644 | unless ($loginConfig) { | ||
| 645 | $self->setLoginConfig(Wombat::Deploy::LoginConfig->new('NONE')); | ||
| 646 | } | ||
| 647 | |||
| 648 | # make sure one hasn't already been configured | ||
| 649 | if ($self->{pipeline}) { | ||
| 650 | my $basic = $self->{pipeline}->getBasic(); | ||
| 651 | return 1 if $basic && $basic->isa('Wombat::Authenticator'); | ||
| 652 | |||
| 653 | for my $valve ($self->{pipeline}->getValves()) { | ||
| 654 | return 1 if $valve->isa('Wombat::Authenticator'); | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | # make sure a realm has been configured | ||
| 659 | unless ($self->getRealm()) { | ||
| 660 | $self->log("security constraints configured without realm", | ||
| 661 | undef, 'ERROR'); | ||
| 662 | return 1; | ||
| 663 | } | ||
| 664 | |||
| 665 | my $authMethod = $loginConfig->getAuthMethod(); | ||
| 666 | my $class = AUTHENTICATORS->{$authMethod}; | ||
| 667 | unless ($class) { | ||
| 668 | $self->log("no authenticator for auth method [$authMethod]", | ||
| 669 | undef, 'ERROR'); | ||
| 670 | return 1; | ||
| 671 | } | ||
| 672 | |||
| 673 | eval "require $class"; | ||
| 674 | if ($@) { | ||
| 675 | $self->log("could not load authenticator $class", $@, 'ERROR'); | ||
| 676 | return 1; | ||
| 677 | } | ||
| 678 | |||
| 679 | my $authenticator = eval { $class->new() }; | ||
| 680 | if ($@) { | ||
| 681 | $self->log("could not instantiate authenticator $class", $@, 'ERROR'); | ||
| 682 | return 1; | ||
| 683 | } | ||
| 684 | |||
| 685 | $self->{pipeline}->addValve($authenticator); | ||
| 686 | |||
| 687 | return 1; | ||
| 688 | } | ||
| 689 | |||
| 690 | package Wombat::Util::XmlMapper::SetAuthConstraintAction; | ||
| 691 | |||
| 692 | use base qw(Wombat::Util::XmlAction); | ||
| 693 | use fields qw(); | ||
| 694 | |||
| 695 | sub start { | ||
| 696 | my $self = shift; | ||
| 697 | my $mapper = shift; | ||
| 698 | |||
| 699 | my $top = @{ $mapper->{objects} } - 1; | ||
| 700 | my $obj = $mapper->{objects}->[$top]; | ||
| 701 | return 1 unless $obj; | ||
| 702 | |||
| 703 | $obj->setAuthConstraint(1); | ||
| 704 | |||
| 705 | return 1; | ||
| 706 | } | ||
| 707 | |||
| 708 | package Wombat::Util::XmlMapper::WrapperCreateAction; | ||
| 709 | |||
| 710 | use base qw(Wombat::Util::XmlMapper::ObjectCreateAction); | ||
| 711 | use fields qw(); | ||
| 712 | |||
| 713 | sub start { | ||
| 714 | my $self = shift; | ||
| 715 | my $mapper = shift; | ||
| 716 | |||
| 717 | $self->SUPER::start($mapper, @_); | ||
| 718 | |||
| 719 | my $top = @{ $mapper->{objects} } - 1; | ||
| 720 | my $wrapper = $mapper->{objects}->[$top]; | ||
| 721 | return 1 unless $wrapper; | ||
| 722 | |||
| 723 | $wrapper->start(); | ||
| 724 | |||
| 725 | return 1; | ||
| 726 | } | ||
| 727 | |||
| 728 | 1; | ||
| 729 | __END__ |