| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::HostMapper; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Core::HostMapper - core host mapper class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Implementation of B<Wombat::Mapper> for a B<Wombat::Core::Host>. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use base qw(Wombat::Core::MapperBase); | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use Servlet::Util::Exception (); | ||
| 24 | |||
| 25 | =pod | ||
| 26 | |||
| 27 | =head1 ACCESSOR METHODS | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item setContainer($container) | ||
| 32 | |||
| 33 | Set the Container with which the Mapper is associated. | ||
| 34 | |||
| 35 | B<Parameters:> | ||
| 36 | |||
| 37 | =over | ||
| 38 | |||
| 39 | =item $container | ||
| 40 | |||
| 41 | the B<Wombat::Container> used for processing Requests | ||
| 42 | |||
| 43 | =back | ||
| 44 | |||
| 45 | B<Throws:> | ||
| 46 | |||
| 47 | =over | ||
| 48 | |||
| 49 | =item B<Servlet::Util::IllegalArgumentException> | ||
| 50 | |||
| 51 | if the Container is not acceptable to this Mapper | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub setContainer { | ||
| 58 | my $self = shift; | ||
| 59 | my $container = shift; | ||
| 60 | |||
| 61 | unless ($container && $container->isa('Wombat::Core::Host')) { | ||
| 62 | my $msg = "setContainer: container is not Host\n"; | ||
| 63 | Servlet::Util::IllegalArgumentException->throw($container); | ||
| 64 | } | ||
| 65 | |||
| 66 | $self->SUPER::setContainer($container); | ||
| 67 | |||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | =pod | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =head1 PUBLIC METHODS | ||
| 76 | |||
| 77 | =over | ||
| 78 | |||
| 79 | =item map($request) | ||
| 80 | |||
| 81 | Return the child Container that should be used to process the | ||
| 82 | Request, or C<undef> f no such child Container can be identified. | ||
| 83 | |||
| 84 | B<Parameters:> | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item $request | ||
| 89 | |||
| 90 | the B<Wombat::Request> being processed | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub map { | ||
| 97 | my $self = shift; | ||
| 98 | my $request = shift; | ||
| 99 | |||
| 100 | my $application = $request->getApplication(); | ||
| 101 | return $application if $application; | ||
| 102 | |||
| 103 | my $freq = $request->getRequest(); | ||
| 104 | my $host = $self->getContainer(); | ||
| 105 | |||
| 106 | my $uri = $freq->getRequestURI(); | ||
| 107 | return undef unless $uri; | ||
| 108 | |||
| 109 | # match the longest possible context path prefix | ||
| 110 | while ($uri) { | ||
| 111 | $application = $host->getChild($uri); | ||
| 112 | last if $application; | ||
| 113 | $uri =~ s|/[^/]*$||; | ||
| 114 | } | ||
| 115 | |||
| 116 | $request->setApplication($application); | ||
| 117 | if ($application) { | ||
| 118 | $request->setContextPath($application->getPath()); | ||
| 119 | } else { | ||
| 120 | $request->setContextPath(undef); | ||
| 121 | } | ||
| 122 | |||
| 123 | return $application; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | __END__ |