comparison Lib/IMPL/Web/Handler/ViewSelector.pm @ 206:c8fe3f84feba

+IMPL::Web::Handlers::ViewSelector +IMPL::Web::Handlers::ErrorHandler *IMPL::Web::Handlers::RestController moved types mappings to ViewSelector
author sergey
date Thu, 03 May 2012 16:48:39 +0400
parents
children 3d433a977e3b
comparison
equal deleted inserted replaced
205:891c04080658 206:c8fe3f84feba
1 package IMPL::Web::Handler::ViewSelector;
2 use strict;
3
4 use IMPL::lang qw(:declare :constants);
5
6 use IMPL::declare {
7 require => {
8 NotAcceptable => 'IMPL::Web::NotAcceptableException'
9 },
10 base => {
11 'IMPL::Object' => undef,
12 'IMPL::Object::Autofill' => '@_',
13 'IMPL::Object::Serializable' => undef
14 }
15 };
16
17 BEGIN {
18 public property views => PROP_ALL | PROP_LIST;
19 public property fallback => PROP_ALL;
20 public property types => PROP_ALL;
21 }
22
23 sub Invoke {
24 my ($this,$action,$next) = @_;
25
26 my $handler;
27 my $path = $action->query->path_info;
28
29 if ($this->types and $path =~ m/\.(\w+)$/) {
30 my $forced;
31 if ($forced = $this->types->{$1} and $action->query->Accept($forced) ) {
32 ($handler) = grep eval { $_->can('contentType') } && $_->contentType eq $forced, $this->views;
33 }
34 }
35
36 if (not $handler) {
37
38 my @handlers =
39 sort {
40 $b->{preference} <=> $a->{preference}
41 } map {
42 {
43 handler => $_,
44 preference =>
45 eval { $_->can('contentType') } ? $action->query->Accept($_->contentType) : 0
46 }
47 } $this->views;
48
49 my $info = shift @handlers;
50 $handler = $info ? $info->{handler} : undef;
51
52 }
53
54 die NotAcceptable->new(map { eval {$_->can('contentType') } ? $_->contentType : () } $this->views )
55 unless $handler;
56
57 return $handler->Invoke($action,$next);
58 }
59
60 1;