| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::RequestDispatcher; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Core::RequestDispatcher - request dispatcher implementation | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Implementation of B<Servlet::RequestDispatcher> that allows a request | ||
| 16 | to be forwardd to a different resource to create a response, or to | ||
| 17 | include the output of another resource in the response from this | ||
| 18 | resource. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use base qw(Servlet::RequestDispatcher); | ||
| 23 | use fields qw(application wrapper); | ||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 CONSTRUCTOR | ||
| 30 | |||
| 31 | =over | ||
| 32 | |||
| 33 | =item new($wrapper) | ||
| 34 | |||
| 35 | Construct and return an instance, initializing fields appropriately. | ||
| 36 | |||
| 37 | B<Parameters:> | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item $wrapper | ||
| 42 | |||
| 43 | the B<Wombat::Core::Wrapper> associated with the resource being | ||
| 44 | dispatched to | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =back | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $self = shift; | ||
| 54 | my $wrapper = shift; | ||
| 55 | |||
| 56 | $self = fields::new($self) unless ref $self; | ||
| 57 | |||
| 58 | $self->{wrapper} = $wrapper; | ||
| 59 | $self->{application} = $wrapper->getParent(); | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | =pod | ||
| 65 | |||
| 66 | =head1 PUBLIC METHODS | ||
| 67 | |||
| 68 | =over | ||
| 69 | |||
| 70 | =item forward($request, $response) | ||
| 71 | |||
| 72 | Forward the specified Request and Response to another resource for | ||
| 73 | processing. Any exception thrown by the called servlet will be | ||
| 74 | propagated to the caller. | ||
| 75 | |||
| 76 | B<Parameters:> | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item $request | ||
| 81 | |||
| 82 | the B<Servlet::ServletRequest> to be forwarded | ||
| 83 | |||
| 84 | =item $response | ||
| 85 | |||
| 86 | the B<Servlet::ServletResponse> to be forwarded | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | B<Throws:> | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item B<Servlet::ServletException> | ||
| 95 | |||
| 96 | if a servlet exception occurs | ||
| 97 | |||
| 98 | =item B<Servlet::Util::IllegalStateException> | ||
| 99 | |||
| 100 | if the response has already been committed | ||
| 101 | |||
| 102 | =item B<Servlet::Util::IOException> | ||
| 103 | |||
| 104 | if an input or output exception occurs | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub forward { | ||
| 111 | my $self = shift; | ||
| 112 | my $request = shift; | ||
| 113 | my $response = shift; | ||
| 114 | |||
| 115 | if ($response->isCommitted()) { | ||
| 116 | my $msg = 'forward: response already committed'; | ||
| 117 | Servlet::Util::IllegalStateException->throw($msg); | ||
| 118 | } | ||
| 119 | |||
| 120 | # reset any output that has been buffered, but keep headers and | ||
| 121 | # cookies | ||
| 122 | $response->resetBuffer(); | ||
| 123 | |||
| 124 | $self->invoke($request, $response); | ||
| 125 | |||
| 126 | # commit and close the response | ||
| 127 | $response->flushBuffer(); | ||
| 128 | |||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | =pod | ||
| 133 | |||
| 134 | =item include($request, $response) | ||
| 135 | |||
| 136 | Include the Response from another resource in the current | ||
| 137 | Response. Any exception thrown by the called servlet will be | ||
| 138 | propagated to the caller. | ||
| 139 | |||
| 140 | B<Parameters:> | ||
| 141 | |||
| 142 | =over | ||
| 143 | |||
| 144 | =item $request | ||
| 145 | |||
| 146 | the B<Servlet::ServletRequest> to be included | ||
| 147 | |||
| 148 | =item $response | ||
| 149 | |||
| 150 | the B<Servlet::ServletResponse> to be included | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | B<Throws:> | ||
| 155 | |||
| 156 | =over | ||
| 157 | |||
| 158 | =item B<Servlet::ServletException> | ||
| 159 | |||
| 160 | if a servlet exception occurs | ||
| 161 | |||
| 162 | =item B<Servlet::Util::IOException> | ||
| 163 | |||
| 164 | if an input or output exception occurs | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub include { | ||
| 171 | my $self = shift; | ||
| 172 | my $request = shift; | ||
| 173 | my $response = shift; | ||
| 174 | |||
| 175 | $self->invoke($request, $response, 1); | ||
| 176 | |||
| 177 | return 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub invoke { | ||
| 181 | my $self = shift; | ||
| 182 | my $request = shift; | ||
| 183 | my $response = shift; | ||
| 184 | my $included = shift; | ||
| 185 | |||
| 186 | my $wreq; | ||
| 187 | if ($request->isa('Wombat::HttpRequest')) { | ||
| 188 | $wreq = Wombat::Connector::HttpRequestBase->new($request); | ||
| 189 | } else { | ||
| 190 | $wreq = Wombat::Connector::RequestBase->new($request); | ||
| 191 | } | ||
| 192 | |||
| 193 | my $wres; | ||
| 194 | if ($response->isa('Wombat::HttpRequest')) { | ||
| 195 | $wres = Wombat::Connector::HttpResponseBase->new($response); | ||
| 196 | } else { | ||
| 197 | $wres = Wombat::Connector::ResponseBase->new($response); | ||
| 198 | } | ||
| 199 | |||
| 200 | $wreq->setApplication($self->{application}); | ||
| 201 | $wreq->setWrapper($self->{wrapper}); | ||
| 202 | $wreq->setResponse($wres); | ||
| 203 | |||
| 204 | $wres->setApplication($self->{application}); | ||
| 205 | $wres->setIncluded($included); | ||
| 206 | $wres->setRequest($wreq); | ||
| 207 | |||
| 208 | $self->{wrapper}->invoke($wreq, $wres); | ||
| 209 | |||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; | ||
| 214 | __END__ |