| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Core::ApplicationValve; | ||
| 4 | |||
| 5 | use base qw(Wombat::Valve::ValveBase); | ||
| 6 | use fields qw(); | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Servlet::Util::Exception (); | ||
| 11 | use Wombat::Globals (); | ||
| 12 | |||
| 13 | # public methods | ||
| 14 | |||
| 15 | sub getName { return 'ApplicationValve' } | ||
| 16 | |||
| 17 | sub invoke { | ||
| 18 | my $self = shift; | ||
| 19 | my $request = shift; | ||
| 20 | my $response = shift; | ||
| 21 | |||
| 22 | my $freq = $request->getRequest(); | ||
| 23 | |||
| 24 | my $application = $self->getContainer(); | ||
| 25 | |||
| 26 | # disallow direct access to resources under WEB-INF or META-INF | ||
| 27 | my $contextPath = $freq->getContextPath(); | ||
| 28 | my $requestURI = $freq->getRequestURI(); | ||
| 29 | |||
| 30 | my $relativeURI = uc $requestURI; | ||
| 31 | $relativeURI =~ s|^$contextPath||; | ||
| 32 | if ($relativeURI =~ m|^/META-INF| || | ||
| 33 | $relativeURI =~ m|^/WEB-INF|) { | ||
| 34 | my $status = Servlet::Http::HttpServletResponse::SC_NOT_FOUND; | ||
| 35 | $response->getResponse()->sendError($status); | ||
| 36 | |||
| 37 | $application->handleError($request, $response); | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | # set up the session for the request | ||
| 42 | my $session; | ||
| 43 | my $manager = $application->getSessionManager(); | ||
| 44 | if ($manager) { | ||
| 45 | # sessions are supported | ||
| 46 | my $id = $request->getRequestedSessionId(); | ||
| 47 | if ($id) { | ||
| 48 | # request contained a session id | ||
| 49 | eval { | ||
| 50 | $session = $manager->getSession($id); | ||
| 51 | }; | ||
| 52 | if ($@) { | ||
| 53 | my $msg = "invoke: problem getting session [id $id]"; | ||
| 54 | $self->log($msg, $@, 'ERROR'); | ||
| 55 | } | ||
| 56 | |||
| 57 | if ($session) { | ||
| 58 | # the session has already been created; check validity. | ||
| 59 | my $interval = $session->getMaxInactiveInterval(); | ||
| 60 | my $lastAccessed = $session->getLastAccessedTime(); | ||
| 61 | if (time - $lastAccessed >= $interval) { | ||
| 62 | $session->expire(); | ||
| 63 | $session->recycle(); | ||
| 64 | undef $session; | ||
| 65 | $self->log("expired session [id $id]", $@, 'DEBUG'); | ||
| 66 | } else { | ||
| 67 | # update the session's last accessed time | ||
| 68 | $session->access(); | ||
| 69 | |||
| 70 | # Wombat::Globals::DEBUG && | ||
| 71 | # $self->log("got session " . $session->getId(), | ||
| 72 | # undef, 'DEBUG'); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | unless ($session) { | ||
| 78 | # either no session was requested, or the session doesn't | ||
| 79 | # exist or is invalid; create a new one. | ||
| 80 | eval { | ||
| 81 | $session = $manager->createSession(); | ||
| 82 | }; | ||
| 83 | if ($@) { | ||
| 84 | my $msg = "invoke: problem creating session"; | ||
| 85 | $self->log($msg, $@, 'ERROR'); | ||
| 86 | } | ||
| 87 | |||
| 88 | # Wombat::Globals::DEBUG && | ||
| 89 | # $self->log("created session " . $session->getId(), | ||
| 90 | # undef, 'DEBUG'); | ||
| 91 | } | ||
| 92 | |||
| 93 | # set the session in the request so that it doesn't have to be | ||
| 94 | # fetched from the session cache again in this request | ||
| 95 | $request->setSession($session); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $wrapper = $application->map($request); | ||
| 99 | unless ($wrapper) { | ||
| 100 | my $status = Servlet::Http::HttpServletResponse::SC_NOT_FOUND; | ||
| 101 | $response->getResponse()->sendError($status); | ||
| 102 | |||
| 103 | $application->handleError($request, $response); | ||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | $response->setApplication($application); | ||
| 108 | |||
| 109 | $wrapper->invoke($request, $response); | ||
| 110 | |||
| 111 | if ($session) { | ||
| 112 | if ($session->isNew()) { | ||
| 113 | # allow $session->access() to work next time around | ||
| 114 | $response->flushBuffer() unless $response->isCommitted(); | ||
| 115 | $session->setNew(undef) if $session->isNew(); | ||
| 116 | } | ||
| 117 | |||
| 118 | # write the updated session state, if any, to the session | ||
| 119 | # cache | ||
| 120 | $manager->save($session) if $manager; | ||
| 121 | } | ||
| 122 | |||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | __END__ |