annotate lib/IMPL/Web/Handler/RestController.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::Handler::RestController;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 Locator => 'IMPL::Web::AutoLocator',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 ResourceInterface => 'IMPL::Web::Application::ResourceInterface',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 ArgumentExecption => '-IMPL::InvalidArgumentException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 NotFoundException => 'IMPL::Web::NotFoundException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 Loader => 'IMPL::Code::Loader'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 'IMPL::Object::Autofill' => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 'IMPL::Object::Serializable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 resourceFactory => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 trailingSlash => PROP_RO
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 die ArgumentException->new(resourceFactory => "A web-resource is required")
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 unless $this->resourceFactory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 #unless eval { $this->resourceFacotry->isa(ResourceInterface) };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 sub GetResourcePath {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my ($this,$action) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my $pathInfo = $action->pathInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 my @segments;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 if (length $pathInfo) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 @segments = split(/\//, $pathInfo, $this->trailingSlash ? -1 : 0);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 # remove first segment if it is empty
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 shift @segments if @segments && length($segments[0]) == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 return @segments;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 sub Invoke {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 my ($this,$request) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 my $method = $request->requestMethod;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my @segments = $this->GetResourcePath($request);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 my $factory = $this->resourceFactory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 $factory = Loader->default->Require($factory)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 unless ref($factory) || eval { $factory->can('new') };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my $res = $factory->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 id => 'root',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 request => $request,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 location => Locator->new(base => $request->application->baseUrl),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 while(@segments) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my $id = shift @segments;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 $res = $res->FetchChildResource($id);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 $res = $res->InvokeHttpVerb($method);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 C<IMPL::Web::Handler::RestController> - Обрабатывает C<HTTP> запрос передавая
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 его соответствующему ресурсу.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 Используется в конфигурации приложения как элемент цепочки обработчиков.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 Как правило располагается на самом верхнем уровне.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 =begin code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 <handlers type="ARRAY">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 <item type="IMPL::Web::Handler::RestController">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 <resourceFactory>My::App::Web::RootResource</resourceFactory>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 </item>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 <item type="IMPL::Web::Handler::JSONView" />
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 <item type="IMPL::Web::Handler::SecureCookie" />
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 <item type="IMPL::Web::Handler::ErrorHandler" />
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 </handlers>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 =end code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 Использует C<PATH_INFO> для определения нужного ресурса, затем предает
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 найденному ресурсу управление для обработки запроса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 Если ресурс не найден, то возникает исключение C<IMPL::Web::NotFoundException>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 Для определения нужного ресурса контроллер разбивает C<PATH_INFO> на фрагменты
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 и использует каждый фрагмент для получения дочернего ресурса начиная с корневого.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 Для чего используется метод
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 C<< IMPL::Web::Application::ResourceInterface->FetchChildResource($childId) >>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 Дерево ресурсов сущестувет независимо от обрабатываемого запроса, однако оно
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 может полностью или частично загружаться в начале обработки запроса и
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 освобождаться по окончании обработки запроса. Поэтому при получении дочерних
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 ресурсов не участвует C<HTTP> запрос, он адресуется только последнему ресурсу.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 =begin text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 /music/audio.mp3 -> ['music','audio.mp3']
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =end text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =head1 MEMEBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =head2 C<[get]resourceFactory>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 Фабрика для создания корневого ресурса приложения, полученный ресурс должен
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 реализовывать интерфейс C<IMPL::Web::Application::ResourceInterface>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 Фабрика может сохранять ссылку на корневой ресурс и каждый раз не создавать
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 его, а возвращать уже существующий. Это вполне оправдано, если хранение
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 дерева ресурсов требует меньше ресурсов, чем его создание и при этом приложение
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 остается в памяти между C<HTTP> запросами.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 =head2 C<[get]trailingSlash>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 Если данная переменная имеет значение C<true>, то слеш в конце пути к ресурсу
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 будет интерпретироваться, как дочерний ресурс с пустым идентификатором.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 =cut