| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::ApplicationMapper; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Core::ApplicationMapper - core application mapper class | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Implementation of B<Wombat::Mapper> for a B<Wombat::Core::Application>. | ||
| 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::Application')) { | ||
| 62 | my $msg = "setContainer: container is not Application\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 $wrapper = $request->getWrapper(); | ||
| 101 | return $wrapper if $wrapper; | ||
| 102 | |||
| 103 | my $freq = $request->getRequest(); | ||
| 104 | my $application = $self->getContainer(); | ||
| 105 | |||
| 106 | # identify the context-relative uri to be mapped | ||
| 107 | my $contextPath = $freq->getContextPath(); | ||
| 108 | my $requestURI = $freq->getRequestURI(); | ||
| 109 | |||
| 110 | (my $relativeURI = $requestURI) =~ s|^$contextPath||; | ||
| 111 | $relativeURI = URI::Escape::uri_unescape($relativeURI); | ||
| 112 | |||
| 113 | my $servletPath; | ||
| 114 | my $pathInfo; | ||
| 115 | my $name; | ||
| 116 | |||
| 117 | # check for exact match | ||
| 118 | unless ($wrapper) { | ||
| 119 | $name = $application->getServletMapping($relativeURI); | ||
| 120 | if ($name) { | ||
| 121 | $wrapper = $application->getChild($name); | ||
| 122 | if ($wrapper) { | ||
| 123 | $servletPath = $relativeURI; | ||
| 124 | $pathInfo = undef; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | # check for path prefix matching | ||
| 130 | unless ($wrapper) { | ||
| 131 | $servletPath = $relativeURI; | ||
| 132 | while ($servletPath) { | ||
| 133 | $name = $application->getServletMapping("$servletPath/*"); | ||
| 134 | if ($name) { | ||
| 135 | $wrapper = $application->getChild($name); | ||
| 136 | if ($wrapper) { | ||
| 137 | ($pathInfo = $relativeURI) =~ s|^$servletPath||; | ||
| 138 | $pathInfo ||= undef; | ||
| 139 | last; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | $servletPath =~ s|/[^/]*$||; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # check for suffix matching | ||
| 147 | unless ($wrapper) { | ||
| 148 | my ($ext) = ($relativeURI =~ m|\.([^.]+)$|); | ||
| 149 | if ($ext) { | ||
| 150 | my $pattern = "*.$ext"; | ||
| 151 | $name = $application->getServletMapping($pattern); | ||
| 152 | if ($name) { | ||
| 153 | $wrapper = $application->getChild($name); | ||
| 154 | if ($wrapper) { | ||
| 155 | $servletPath = $relativeURI; | ||
| 156 | $pathInfo = undef; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | # check for default matching | ||
| 163 | unless ($wrapper) { | ||
| 164 | $name = $application->getServletMapping('/'); | ||
| 165 | if ($name) { | ||
| 166 | $wrapper = $application->getChild($name); | ||
| 167 | if ($wrapper) { | ||
| 168 | $servletPath = $relativeURI; | ||
| 169 | $pathInfo = undef; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | $request->setWrapper($wrapper); | ||
| 175 | $request->setServletPath($servletPath); | ||
| 176 | $request->setPathInfo($pathInfo); | ||
| 177 | |||
| 178 | return $wrapper; | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ |