Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/ViewSelector.pm @ 397:73f81f4e9570
sync
author | cin |
---|---|
date | Mon, 05 May 2014 18:17:03 +0400 |
parents | 3cebcf6fdb9b |
children |
rev | line source |
---|---|
206 | 1 package IMPL::Web::Handler::ViewSelector; |
2 use strict; | |
3 | |
233 | 4 use IMPL::Const qw(:prop); |
206 | 5 |
6 use IMPL::declare { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
7 require => { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
8 NotAcceptable => 'IMPL::Web::NotAcceptableException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
9 HttpResponse => 'IMPL::Web::HttpResponse' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
10 }, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
11 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
12 'IMPL::Object' => undef, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
13 'IMPL::Object::Autofill' => '@_', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
14 'IMPL::Object::Serializable' => undef |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
15 ], |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
16 props => [ |
233 | 17 views => PROP_RW | PROP_LIST, |
18 fallback => PROP_RW, | |
19 types => PROP_RW | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
20 ] |
206 | 21 }; |
22 | |
23 sub Invoke { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
24 my ( $this, $action, $next ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
25 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
26 my $result = $next ? $next->($action) : undef; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
27 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
28 my $model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
29 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
30 return $result if eval { $result->isa(HttpResponse) }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
31 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
32 my $handler; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
33 my $path = $action->pathInfo; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
34 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
35 if ( $this->types and $path =~ m/\.(\w+)$/ ) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
36 my $forced; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
37 if ( $forced = $this->types->{$1} and $action->query->Accept($forced) ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
38 { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
39 ($handler) = |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
40 grep eval { $_->can('contentType') } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
41 && $_->contentType eq $forced, $this->views; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
42 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
43 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
44 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
45 if ( not $handler ) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
46 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
47 my @handlers = |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
48 sort { $b->{preference} <=> $a->{preference} } map { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
49 { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
50 handler => $_, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
51 preference => eval { $_->can('contentType') } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
52 ? $action->query->Accept( $_->contentType ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
53 : 0 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
54 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
55 } $this->views; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
56 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
57 my $info = shift @handlers; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
58 $handler = $info ? $info->{handler} : undef; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
59 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
60 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
61 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
62 die NotAcceptable->new( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
63 map { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
64 eval { $_->can('contentType') } ? $_->contentType : () |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
65 } $this->views |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
66 ) unless $handler; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
67 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
68 return $handler->Invoke( $action, sub { $result } ); |
206 | 69 } |
70 | |
208 | 71 1; |
72 | |
73 __END__ | |
74 | |
75 =pod | |
76 | |
77 =head1 NAME | |
78 | |
79 C<IMPL::Web::Handler::ViewSelector> - Выбор нужного представления на основе заголовка C<Accept> | |
80 | |
81 =head1 DESCRIPTION | |
82 | |
83 Использует заголовок запроса C<Accept> для выбора подходящего представления, если задано свойство C<types>, | |
84 пытается в первую очередь по расширению определить, какое представление подходит. | |
85 | |
213 | 86 В случаях, когда не требуется строить представление для данных (например, при перенаправлении к другому |
87 ресурсу или если нет данных), нужно, чтобы данному обработчику был возвращен | |
88 L<IMPL::Web::Application::ActionResult>, который будет просто передан далее. | |
89 | |
208 | 90 =head1 MEMBERS |
91 | |
92 =head2 C<[get,set,list]views> | |
93 | |
94 Список представлений, которые могут быть возвращены. | |
95 | |
96 =head2 C<[get,set]types> | |
97 | |
98 Хеш с соотвествием между расширением и типом содержимого, для подсказки при выборе представления. | |
99 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
100 =cut |