Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/Resource.pm @ 259:b92f19630d33
Merge with 91bae9f41a9cf2d52f5cff5a5c3e7c8683c7d47e
author | sergey |
---|---|
date | Fri, 21 Dec 2012 00:09:11 +0400 |
parents | 32aceba4ee6d |
children | 4abda21186cd |
rev | line source |
---|---|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
1 package IMPL::Web::Application::Resource; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
2 use strict; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
3 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
255
diff
changeset
|
4 use URI; |
230 | 5 use IMPL::Const qw(:prop); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
6 use IMPL::declare { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
7 require => { |
244 | 8 ViewResult => 'IMPL::Web::ViewResult', |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
9 Exception => 'IMPL::Exception', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
10 ArgumentException => '-IMPL::InvalidArgumentException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
11 OperationException => '-IMPL::InvalidOperationException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
12 NotAllowedException => 'IMPL::Web::NotAllowedException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
13 NotFoundException => 'IMPL::Web::NotFoundException' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
14 }, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
15 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
16 'IMPL::Object' => undef, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
17 'IMPL::Web::Application::ResourceInterface' => undef |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
18 ], |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
19 props => [ |
255 | 20 parent => PROP_RO, |
21 model => PROP_RO, | |
22 id => PROP_RO, | |
23 contract => PROP_RO, | |
24 location => PROP_RO, | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
25 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
26 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
27 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
28 sub CTOR { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
29 my ( $this, %args ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
30 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
31 die ArgumentException->new( id => 'A resource identifier is required' ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
32 unless $args{id}; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
33 die ArgumentException->new( contract => 'A contract is required' ) |
255 | 34 unless $args{contract}; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
35 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
36 $this->parent( $args{parent} ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
37 $this->model( $args{model} ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
38 $this->id( $args{id} ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
39 $this->contract( $args{contract} ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
40 |
230 | 41 # если расположение явно не указано, то оно вычисляется автоматически, |
42 # либо остается не заданным | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
255
diff
changeset
|
43 $this->location( $args{location} || eval { $this->parent->location->Child( $this->id ) } ); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
44 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
45 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
46 sub InvokeHttpVerb { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
47 my ( $this, $verb, $action ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
48 |
230 | 49 my $operation = $this->contract->verbs->{ lc($verb) }; |
50 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
51 die NotAllowedException->new( |
230 | 52 allow => join( ',', map( uc, keys %{ $this->contract->verbs } ) ) |
53 ) | |
54 unless $operation; | |
245 | 55 |
56 # в случае, когда один ресурс вызывает HTTP метод другого ресурса, нужно | |
57 # сохранить оригинальный resourceLocation | |
58 $action->context->{resourceLocation} ||= $this->location; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
59 |
230 | 60 return $operation->Invoke( $this, $action ); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
61 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
62 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
63 # это реализация по умолчанию, базируется информации о ресурсах, содержащийся |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
64 # в контракте. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
65 sub FetchChildResource { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
66 my ( $this, $childId ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
67 |
230 | 68 my ($info,$childIdParts) = $this->contract->FindChildResourceInfo($childId); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
69 |
230 | 70 die NotFoundException->new($this->location->url,$childId) unless $info; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
71 |
230 | 72 my $binding = $info->{binding}; |
248 | 73 my $contract = $info->{contract}; |
74 | |
75 if (ref $contract eq 'CODE') { | |
76 $contract = $contract->(); | |
77 $info->{contract} = $contract; | |
78 } | |
79 | |
80 die OperationException->new("Can't fetch a contract for the resource", $childId) | |
81 unless $contract; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
82 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
83 my %args = ( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
84 parent => $this, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
85 id => $childId |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
86 ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
87 |
230 | 88 $args{model} = _InvokeDelegate($binding,$this,@$childIdParts); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
89 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
90 return $contract->CreateResource(%args); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
91 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
92 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
93 sub _InvokeDelegate { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
94 my $delegate = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
95 |
230 | 96 return $delegate->(@_) if ref $delegate eq 'CODE'; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
97 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')}; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
98 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
99 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
100 1; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
101 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
102 __END__ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
103 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
104 =pod |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
105 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
106 =head1 NAME |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
107 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
108 C<IMPL::Web::Application::Resource> - Web-ресурс. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
109 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
110 =head1 SYNOPSIS |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
111 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
112 Класс для внутреннего использования. Объединяет в себе контракт и модель данных. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
113 Основная задача - обработать поступающий от контроллера запрос на вызов C<HTTP> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
114 метода. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
115 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
116 Экземпляры данного класса передаются в качестве параметров делегатам |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
117 осуществляющим привязку к модели в C<IMPL::Web::Application::ResourceContract> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
118 и C<IMPL::Web::Application::OperationContract>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
119 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
120 =head1 DESCRIPTION |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
121 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
122 Весь функционал ресурса, поддерживаемые им C<HTTP> методы определяются |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
123 контрактом. Однако можно реализовывать ресурсы, которые не имеют контракта |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
124 или он отличается от того, что предоставляется стандартно |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
125 C<IMPL::Web::Application::ResourceContract>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
126 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
127 Каждый ресурс является контейнером, тоесть позволяет получить дочерний ресурс |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
128 по идентифифкатору, если таковой имеется, тоесть ресурс, у которого нет дочерних |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
129 ресурсов на самом деле рассматривается как пустой контейнер. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
130 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
131 С ресурсом непосредственно взаимодействует котроллер запросов |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
132 C<IMPL::Web::Handler::RestController>, вызывая два метода. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
133 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
134 =over |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
135 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
136 =item * C<FetchChildResource($childId)> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
137 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
138 Данный метод возвращает дочерний ресурс, соответствующий C<$childId>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
139 Текущая реализация использует метод C<FindChildResourceInfo> контракта текущего |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
140 ресурса, после чего создает дочерний ресурс. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
141 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
142 Если дочерний ресурс не найден, вызывается исключение |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
143 C<IMPL::Web::NotFoundException>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
144 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
145 =item * C<InvokeHttpVerb($verb,$action)> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
146 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
147 Обрабатывает запрос к ресурсу. Для этого используется контракт ресурса, в |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
148 нем выбирается соответсвующий C<IMPL::Web::Application::OperationContract>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
149 Затем найденный контракт для указанной операции используется для обработки |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
150 запроса. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
151 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
152 =back |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
153 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
154 Если объект реализует два вышеуказанных метода, он является веб-ресурсом, а |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
155 детали его реализации, котнракт и прочее уже не важно, поэтому можно реализовать |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
156 собственный класс ресурса, например унаследованный от |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
157 C<IMPL::Web::Application::CustomResource>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
158 |
230 | 159 =head1 MEMBERS |
160 | |
161 =head2 C<[get]contract> | |
162 | |
248 | 163 Обязательное свойство для ресурса, ссылается, на контракт, соответствующий |
230 | 164 данному ресурсу, используется для выполнения C<HTTP> методов и получения |
165 дочерних ресурсов. | |
166 | |
167 =head2 C<[get]id> | |
168 | |
169 Обязательное свойство ресурса, идентифицирует его в родительском контейнере, | |
170 для корневого ресурса может иметь произвольное значение. | |
171 | |
172 =head2 C<[get]parent> | |
173 | |
174 Ссылка на родительский ресурс, для корневого ресурса не определена. | |
175 | |
176 =head2 C<[get]model> | |
177 | |
178 Ссылка на объект предметной области, представляемый данным ресурсом. Данное | |
179 свойство не является обязательным и может быть не задано. | |
180 | |
181 =head2 C<[get]location> | |
182 | |
183 Объект типа C<IMPL::Web::AutoLocator> или аналогичный описывающий адрес текущего | |
184 ресурса, может быть как явно передан при создании ресурса, так и вычислен | |
185 автоматически (только для ресурсов имеющих родителя). Следует заметить, что | |
186 адрес ресурса не содержит параметров запроса, а только путь. | |
187 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
188 =cut |