Wombat/Core/Session.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 342 CC 19 SCAR 20.6
CC
19
Cov
0%
setAttribute
30.0
removeAttribute
20.0
getAttributeNames
12.0
CRAP 7.9
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Core::Session;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::Core::Session - internal session class
10  
11 =head1 SYNOPSIS
12  
13 =head1 DESCRIPTION
14  
15 Core implementation of B<Wombat::Session> and
16 B<Servlet::Http::HttpSession>.
17  
18 =cut
19  
20 use base qw(Wombat::Session);
21 use fields qw(authType expiring facade sessionManager principal valid);
22 use fields qw(thisAccessedTime attributes creationTime id lastAccessedTime);
23 use fields qw(maxInactiveInterval new);
24 use strict;
25 use warnings;
26  
27 use Servlet::Http::HttpSessionBindingEvent ();
28 use Servlet::Util::Exception ();
29 use Wombat::Core::SessionFacade ();
30 use Wombat::Globals ();
31  
32 =pod
33  
34 =head1 CONSTRUCTOR
35  
36 =over
37  
38 =item new()
39  
40 Construct and return a B<Wombat::Core::Session> instance, initializing
41 fields appropriately.
42  
43 $self->SUPER::new();
44  
45 =back
46  
47 =cut
48  
49 sub new {
50 my $self = shift;
51  
52 $self = fields::new($self) unless ref $self;
53  
54 $self->recycle();
55  
56 return $self;
57 }
58  
59 =pod
60  
61 =head1 ACCESSOR METHODS
62  
63 =over
64  
65 =item getAttribute($name)
66  
67 Returns the object bound with the specified name in this session, or
68 I<undef> if no object is bound under the name.
69  
70 B<Parameters:>
71  
72 =over
73  
74 =item $name
75  
76 the name of the object
77  
78 =back
79  
80 B<Throws:>
81  
82 =over
83  
84 =item B<Servlet::Util::IllegalStateException>
85  
86 if this method is called on an invalidated session
87  
88 =back
89  
90 =cut
91  
92 sub getAttribute {
93 my $self = shift;
94 my $name = shift;
95  
96 if (! $self->{expiring} && ! $self->{valid}) {
97 my $msg = "getAttribute: invalid session";
98 Servlet::Util::IllegalStateException->throw($msg);
99 }
100  
101 return $self->{attributes}->{$name};
102 }
103  
104 =pod
105  
106 =item getAttributeNames()
107  
108 Returns an array containing the names of all the objects bound to this
109 session, or an empty array if there are no bound objects.
110  
111 B<Throws:>
112  
113 =over
114  
115 =item B<Servlet::Util::IllegalStateException>
116  
117 if this method is called on an invalidated session
118  
119 =back
120  
121 =cut
122  
123 sub getAttributeNames {
124 my $self = shift;
125 my $name = shift;
126  
127 if (! $self->{expiring} && ! $self->{valid}) {
128 my $msg = "getAttribute: invalid session";
129 Servlet::Util::IllegalStateException->throw($msg);
130 }
131  
132 my @attributes = keys %{ $self->{attributes} };
133  
134 return wantarray ? @attributes : \@attributes;
135 }
136  
137 =pod
138  
139 =item removeAttribute($name)
140  
141 Removes the object bound with the specified name from this session. If
142 the session does not have an object bound with the specified name,
143 this method does nothing.
144  
145 After this method executes, and if the object implements
146 B<Servlet::Http::HttpSessionBindingListener>, the container calls
147 C<valueUnbound()> on the object.
148  
149 B<Parameters:>
150  
151 =over
152  
153 =item $name
154  
155 the name of the object
156  
157 =back
158  
159 B<Throws:>
160  
161 =over
162  
163 =item B<Servlet::Util::IllegalStateException>
164  
165 if this method is called on an invalidated session
166  
167 =back
168  
169 =cut
170  
171 sub removeAttribute {
172 my $self = shift;
173 my $name = shift;
174  
175 if (!$self->{expiring} && !$self->{valid}) {
176 my $msg = "getAttribute: invalid session";
177 Servlet::Util::IllegalStateException->throw($msg);
178 }
179  
180 my $value = $self->{attributes}->{$name};
181 if (defined $value) {
182 delete $self->{attributes}->{$name};
183 } else {
184 return 1;
185 }
186  
187 # notify unbound value if necessary
188 if ($value && ref $value &&
189 $value->isa('Servlet::Http::HttpSessionBindingListener')) {
190 my $event = Servlet::Http::HttpSessionBindingEvent->new($self, $name,
191 $value);
192 $value->valueUnbound($event);
193 }
194  
195 # XXX: notify application event listeners
196  
197 return 1;
198 }
199  
200 =pod
201  
202 =item setAttribute($name, $value)
203  
204 Binds an object to this session using the specified name. If an object
205 of the same name is already bound to the session, the object is
206 replaced.
207  
208 After this method executes, and if the new object implements
209 B<Servlet::Http::HttpSessionBindingListener>, the container calls
210 C<valueBound()> on the object.
211  
212 If a previously bound object was replaced, and it implements
213 B<Servlet::Http::HttpSessionBindingListener>, the container calls
214 C<valueUnbound()> on it.
215  
216 B<Parameters:>
217  
218 =over
219  
220 =item $name
221  
222 the name to which the object is bound
223  
224 =item $value
225  
226 the object to be bound
227  
228 =back
229  
230 B<Throws:>
231  
232 =over
233  
234 =item B<Servlet::Util::IllegalStateException>
235  
236 if this method is called on an invalidated session
237  
238 =back
239  
240 =cut
241  
242 sub setAttribute {
243 my $self = shift;
244 my $name = shift;
245 my $value = shift;
246  
247 return $self->removeAttribute($name) unless defined $value;
248  
249 if (! $self->{expiring} && ! $self->{valid}) {
250 my $msg = "getAttribute: invalid session";
251 Servlet::Util::IllegalStateException->throw($msg);
252 }
253  
254 my $unbound = $self->{attributes}->{$name};
255 $self->{attributes}->{$name} = $value;
256  
257 # notify unbound value if necessary
258 if ($unbound && ref $unbound &&
259 $unbound->isa('Servlet::Http::HttpSessionBindingListener')) {
260 my $event = Servlet::Http::HttpSessionBindingEvent->new($self, $name);
261 $unbound->valueUnbound($event);
262 }
263  
264 # notify bound value if necessary
265 if (ref $value &&
266 $value->isa('Servlet::Http::HttpSessionBindingListener')) {
267 my $event = Servlet::Http::HttpSessionBindingEvent->new($self, $name,
268 $unbound);
269 $value->valueBound($event);
270 }
271  
272 # XXX: notify application event listeners
273  
274 return 1;
275 }
276  
277 =pod
278  
279 =item getAuthType()
280  
281 Return the authentication type used to authenticate the cached
282 principal, if any.
283  
284 =cut
285  
286 sub getAuthType {
287 my $self = shift;
288  
289 return $self->{authType};
290 }
291  
292 =pod
293  
294 =item setAuthType($authType)
295  
296 Set the authentication type used to authenticate the cached principal,
297 if any.
298  
299 B<Parameters:>
300  
301 =over
302  
303 =item $authType
304  
305 the authentication type
306  
307 =back
308  
309 =cut
310  
311 sub setAuthType {
312 my $self = shift;
313 my $type = shift;
314  
315 $self->{authType} = $type;
316  
317 return 1;
318 }
319  
320 =pod
321  
322 =item getCreationTime()
323  
324 Return the creation time for this Session, in seconds since the epoch.
325  
326 B<Throws:>
327  
328 =over
329  
330 =item B<Servlet::Util::IllegalStateException>
331  
332 if this method is called on an invalidated session
333  
334 =back
335  
336 =cut
337  
338 sub getCreationTime {
339 my $self = shift;
340  
341 if (! $self->{expiring} && ! $self->{valid}) {
342 my $msg = "getAttribute: invalid session";
343 Servlet::Util::IllegalStateException->throw($msg);
344 }
345  
346 return $self->{creationTime};
347 }
348  
349 =pod
350  
351 =item setCreationTime(time)
352  
353 Set the creation time for this Session. This method is called by the
354 SessionManager when a Session instance is created or an existing
355 Session instance is reused.
356  
357 B<Parameters:>
358  
359 =over
360  
361 =item $time
362  
363 the creation time, in seconds since the epoch
364  
365 =back
366  
367 =cut
368  
369 sub setCreationTime {
370 my $self = shift;
371 my $time = shift;
372  
373 $self->{creationTime} = $time;
374 $self->{lastAccessedTime} = $time;
375 $self->{thisAccessedTime} = $time;
376  
377 return 1;
378 }
379  
380 =pod
381  
382 =item getId()
383  
384 Return the session identifier for this Session.
385  
386 =cut
387  
388 sub getId {
389 my $self = shift;
390  
391 return $self->{id};
392 }
393  
394 =pod
395  
396 =item setId($id)
397  
398 Set the session identifier for this Session.
399  
400 B<Parameters:>
401  
402 =over
403  
404 =item $id
405  
406 the session identifier
407  
408 =back
409  
410 =cut
411  
412 sub setId {
413 my $self = shift;
414 my $id = shift;
415  
416 $self->{sessionManager}->remove($self) if
417 defined $self->getId() && $self->{sessionManager};
418  
419 $self->{id} = $id;
420  
421 $self->{sessionManager}->add($self) if $self->{sessionManager};
422  
423 # XXX: notify application event listeners
424  
425 return 1;
426 }
427  
428 =pod
429  
430 =item getLastAccessedTime()
431  
432 Return the last time the client sent a request associated with this
433 Session, as the number of seconds since the epoch. Actions taken by
434 servlet applications do not affect the last accessed time.
435  
436 =cut
437  
438 sub getLastAccessedTime {
439 my $self = shift;
440  
441 return $self->{lastAccessedTime};
442 }
443  
444 =pod
445  
446 =item getMaxInactiveInterval()
447  
448 Return the maximum inactive interval, in seconds, between client
449 requests before the servlet container will invalidate this Session. A
450 negative itnerval indicates that the session should never be
451 invalidated.
452  
453 =cut
454  
455 sub getMaxInactiveInterval {
456 my $self = shift;
457  
458 return $self->{maxInactiveInterval};
459 }
460  
461 =pod
462  
463 =item setMaxInactiveInterval($interval)
464  
465 Set the maximum inactive interval for the Session.
466  
467 B<Parameters:>
468  
469 =over
470  
471 =item $interval
472  
473 the new interval, in seconds
474  
475 =back
476  
477 =cut
478  
479 sub setMaxInactiveInterval {
480 my $self = shift;
481 my $interval = shift;
482  
483 return $self->{maxInactiveInterval} = $interval;
484 }
485  
486 =pod
487  
488 =item isNew()
489  
490 Returns true if the client does not yet know about the session or if
491 the client chooses not to join the session. For example, if the server
492 used only cookie-based sessions, and the client had disabled the use
493 of cookies, then a session would be new on each request.
494  
495 B<Throws:>
496  
497 =over
498  
499 =item B<Servlet::Util::IllegalStateException>
500  
501 if this method is called on an invalidated session
502  
503 =back
504  
505 =cut
506  
507 sub isNew {
508 my $self = shift;
509  
510 if (! $self->{expiring} && ! $self->{valid}) {
511 my $msg = "getAttribute: invalid session";
512 Servlet::Util::IllegalStateException->throw($msg);
513 }
514  
515 return $self->{new};
516 }
517  
518 =pod
519  
520 =item setNew($flag)
521  
522 Set a flag specifying whether or not the session is newly created.
523  
524 B<Parameters:>
525  
526 =over
527  
528 =item $flag
529  
530 the boolean value to set
531  
532 =back
533  
534 =cut
535  
536 sub setNew {
537 my $self = shift;
538 my $flag = shift;
539  
540 $self->{new} = $flag;
541  
542 return 1;
543 }
544  
545 =pod
546  
547 =item getPrincipal()
548  
549 Return the authenticated principal for this Session, or C<undef> if
550 there is none.
551  
552 =cut
553  
554 sub getPrincipal {
555 my $self = shift;
556  
557 return $self->{principal};
558 }
559  
560 =pod
561  
562 =item setPrincipal($principal)
563  
564 Set the authenticated principal for this Session.
565  
566 B<Parameters:>
567  
568 =over
569  
570 =item $principal
571  
572 the new principal
573  
574 =back
575  
576 =cut
577  
578 sub setPrincipal {
579 my $self = shift;
580 my $principal = shift;
581  
582 $self->{principal} = $principal;
583  
584 return 1;
585 }
586  
587 =pod
588  
589 =item getSession()
590  
591 Return the HttpSession which acts as a facade for this Session to
592 servlet applications.
593  
594 =cut
595  
596 sub getSession {
597 my $self = shift;
598  
599 return $self->{facade};
600 }
601  
602 =pod
603  
604 =item getSessionManager()
605  
606 Return the SessionManager which manages this Session.
607  
608 =cut
609  
610 sub getSessionManager {
611 my $self = shift;
612  
613 return $self->{sessionManager};
614 }
615  
616 =pod
617  
618 =item setSessionManager($manager)
619  
620 Set the SessionManager which manages this Session.
621  
622 B<Parameters:>
623  
624 =over
625  
626 =item $manager
627  
628 the B<Wombat::SessionManager>
629  
630 =back
631  
632 =cut
633  
634 sub setSessionManager {
635 my $self = shift;
636 my $manager = shift;
637  
638 $self->{sessionManager} = $manager;
639  
640 return 1;
641 }
642  
643 =pod
644  
645 =item isValid()
646  
647 Return a flag specifying whether or not the session is valid.
648  
649 =cut
650  
651 sub isValid {
652 my $self = shift;
653  
654 return $self->{valid};
655 }
656  
657 =pod
658  
659 =item setValid($flag)
660  
661 Set a flag specifying whether or not the session is valid.
662  
663 B<Parameters:>
664  
665 =over
666  
667 =item $flag
668  
669 the boolean value to set
670  
671 =back
672  
673 =cut
674  
675 sub setValid {
676 my $self = shift;
677 my $flag = shift;
678  
679 $self->{valid} = $flag;
680  
681 return 1;
682 }
683  
684 =pod
685  
686 =back
687  
688 =head1 PUBLIC METHODS
689  
690 =over
691  
692 =item access()
693  
694 Update the accessed time information for this Session. This method
695 should be called by the Application when processing a Request, even if
696 the Application does not reference it.
697  
698 =cut
699  
700 sub access {
701 my $self = shift;
702  
703 $self->{new} = undef;
704 $self->{lastAccessedTime} = $self->{thisAccessedTime};
705 $self->{thisAccessedTime} = time;
706  
707 return 1;
708 }
709  
710 =pod
711  
712 =item expire()
713  
714 Perform the internal processing required to invalidate this session,
715 without triggering an exception if the session has already expired.
716  
717 =cut
718  
719 sub expire {
720 my $self = shift;
721  
722 $self->{expiring} = 1;
723  
724 undef $self->{valid};
725 $self->{sessionManager}->remove($self) if $self->{sessionManager};
726  
727 # unbind attributes
728 for my $name ($self->getAttributeNames()) {
729 $self->removeAttribute($name);
730 }
731  
732 # XXX: notify application event listeners
733  
734 undef $self->{expiring};
735  
736 $self->recycle();
737  
738 return 1;
739 }
740  
741 =pod
742  
743 =item invalidate()
744  
745 Invalidates this session, then unbinds any objects bound to it.
746  
747 B<Throws:>
748  
749 =over
750  
751 =item B<Servlet::Util::IllegalStateException>
752  
753 if this method is called on an invalidated session
754  
755 =back
756  
757 =cut
758  
759 sub invalidate {
760 my $self = shift;
761  
762 if (! $self->{expiring} && ! $self->{valid}) {
763 my $msg = "getAttribute: invalid session";
764 Servlet::Util::IllegalStateException->throw($msg);
765 }
766  
767 $self->expire();
768  
769 return 1;
770 }
771  
772 =pod
773  
774 =item recycle()
775  
776 Release all object references and initialize instances variables in
777 preparation for use or reuse of this object.
778  
779 =cut
780  
781 sub recycle {
782 my $self = shift;
783  
784 # Wombat::Session fields
785 $self->{authType} = undef;
786 $self->{expiring} = undef;
787 $self->{facade} = Wombat::Core::SessionFacade->new($self);
788 $self->{sessionManager} = undef;
789 $self->{principal} = undef;
790 $self->{thisAccessedTime} = 0;
791 $self->{valid} = undef;
792  
793 # Servlet::Http::HttpSession fields
794 $self->{attributes} = {};
795 $self->{creationTime} = 0;
796 $self->{id} = undef;
797 $self->{lastAccessedTime} = 0;
798 $self->{maxInactiveInterval} = -1;
799 $self->{new} = undef;
800  
801 return 1;
802 }
803  
804 =pod
805  
806 =back
807  
808 =cut
809  
810 # private methods
811  
812 sub log {
813 my $self = shift;
814  
815 $self->{container}->log(@_) if $self->{container};
816  
817 return 1;
818 }
819  
820 1;
821 __END__