annotate Lib/IMPL/Web/Handler/ViewSelector.pm @ 295:9b25e1f50144

sync
author cin
date Fri, 01 Mar 2013 03:15:30 +0400
parents 3cebcf6fdb9b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
1 package IMPL::Web::Handler::ViewSelector;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
2 use strict;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
3
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
4 use IMPL::Const qw(:prop);
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
5
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
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
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
17 views => PROP_RW | PROP_LIST,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
18 fallback => PROP_RW,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 229
diff changeset
19 types => PROP_RW
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
20 ]
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
21 };
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
22
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
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
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
69 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents:
diff changeset
70
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
71 1;
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
72
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
73 __END__
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
74
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
75 =pod
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
76
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
77 =head1 NAME
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
78
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
79 C<IMPL::Web::Handler::ViewSelector> - Выбор нужного представления на основе заголовка C<Accept>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
80
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
81 =head1 DESCRIPTION
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
82
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
83 Использует заголовок запроса C<Accept> для выбора подходящего представления, если задано свойство C<types>,
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
84 пытается в первую очередь по расширению определить, какое представление подходит.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
85
213
sergey
parents: 208
diff changeset
86 В случаях, когда не требуется строить представление для данных (например, при перенаправлении к другому
sergey
parents: 208
diff changeset
87 ресурсу или если нет данных), нужно, чтобы данному обработчику был возвращен
sergey
parents: 208
diff changeset
88 L<IMPL::Web::Application::ActionResult>, который будет просто передан далее.
sergey
parents: 208
diff changeset
89
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
90 =head1 MEMBERS
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
91
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
92 =head2 C<[get,set,list]views>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
93
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
94 Список представлений, которые могут быть возвращены.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
95
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
96 =head2 C<[get,set]types>
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
97
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
98 Хеш с соотвествием между расширением и типом содержимого, для подсказки при выборе представления.
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
99
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
100 =cut