comparison Lib/IMPL/Web/Handler/ViewSelector.pm @ 229:47f77e6409f7

heavily reworked the resource model of the web application: *some ResourcesContraact functionality moved to Resource +Added CustomResource *Corrected action handlers
author sergey
date Sat, 29 Sep 2012 02:34:47 +0400
parents d6e2ea24af08
children 3cebcf6fdb9b
comparison
equal deleted inserted replaced
228:431db7034a88 229:47f77e6409f7
2 use strict; 2 use strict;
3 3
4 use IMPL::lang qw(:declare :constants); 4 use IMPL::lang qw(:declare :constants);
5 5
6 use IMPL::declare { 6 use IMPL::declare {
7 require => { 7 require => {
8 NotAcceptable => 'IMPL::Web::NotAcceptableException', 8 NotAcceptable => 'IMPL::Web::NotAcceptableException',
9 ViewResult => 'IMPL::Web::Application::ViewResult' 9 HttpResponse => 'IMPL::Web::HttpResponse'
10 }, 10 },
11 base => { 11 base => [
12 'IMPL::Object' => undef, 12 'IMPL::Object' => undef,
13 'IMPL::Object::Autofill' => '@_', 13 'IMPL::Object::Autofill' => '@_',
14 'IMPL::Object::Serializable' => undef 14 'IMPL::Object::Serializable' => undef
15 } 15 ],
16 props => [
17 views => PROP_ALL | PROP_LIST,
18 fallback => PROP_ALL,
19 types => PROP_ALL
20 ]
16 }; 21 };
17 22
18 BEGIN { 23 sub Invoke {
19 public property views => PROP_ALL | PROP_LIST; 24 my ( $this, $action, $next ) = @_;
20 public property fallback => PROP_ALL;
21 public property types => PROP_ALL;
22 }
23 25
24 sub Invoke { 26 my $result = $next ? $next->($action) : undef;
25 my ($this,$action,$next) = @_; 27
26 28 my $model;
27 my $result = $next ? $next->($action) : undef; 29
28 30 return $result if eval { $result->isa(HttpResponse) };
29 my $model; 31
30 32 my $handler;
31 if( eval { $result->isa(ViewResult) } ) { 33 my $path = $action->pathInfo;
32 34
33 my $handler; 35 if ( $this->types and $path =~ m/\.(\w+)$/ ) {
34 my $path = $action->query->path_info; 36 my $forced;
35 37 if ( $forced = $this->types->{$1} and $action->query->Accept($forced) )
36 if ($this->types and $path =~ m/\.(\w+)$/) { 38 {
37 my $forced; 39 ($handler) =
38 if ($forced = $this->types->{$1} and $action->query->Accept($forced) ) { 40 grep eval { $_->can('contentType') }
39 ($handler) = grep eval { $_->can('contentType') } && $_->contentType eq $forced, $this->views; 41 && $_->contentType eq $forced, $this->views;
40 } 42 }
41 } 43 }
42 44
43 if (not $handler) { 45 if ( not $handler ) {
44 46
45 my @handlers = 47 my @handlers =
46 sort { 48 sort { $b->{preference} <=> $a->{preference} } map {
47 $b->{preference} <=> $a->{preference} 49 {
48 } map { 50 handler => $_,
49 { 51 preference => eval { $_->can('contentType') }
50 handler => $_, 52 ? $action->query->Accept( $_->contentType )
51 preference => 53 : 0
52 eval { $_->can('contentType') } ? $action->query->Accept($_->contentType) : 0 54 }
53 } 55 } $this->views;
54 } $this->views; 56
55 57 my $info = shift @handlers;
56 my $info = shift @handlers; 58 $handler = $info ? $info->{handler} : undef;
57 $handler = $info ? $info->{handler} : undef; 59
58 60 }
59 } 61
60 62 die NotAcceptable->new(
61 die NotAcceptable->new(map { eval {$_->can('contentType') } ? $_->contentType : () } $this->views ) 63 map {
62 unless $handler; 64 eval { $_->can('contentType') } ? $_->contentType : ()
63 65 } $this->views
64 return $handler->Invoke($action,sub { $result }); 66 ) unless $handler;
65 } else { 67
66 return $result; 68 return $handler->Invoke( $action, sub { $result } );
67 }
68 } 69 }
69 70
70 1; 71 1;
71 72
72 __END__ 73 __END__