Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/Resource.pm @ 255:827cf96faa1c
refactoring (incomplete)
| author | sergey |
|---|---|
| date | Fri, 07 Dec 2012 16:58:19 +0400 |
| parents | 814d755e5d12 |
| children | 32aceba4ee6d |
| 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 |
| 230 | 4 use IMPL::Const qw(:prop); |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
5 use IMPL::declare { |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
6 require => { |
| 244 | 7 ViewResult => 'IMPL::Web::ViewResult', |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
8 Exception => 'IMPL::Exception', |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
9 ArgumentException => '-IMPL::InvalidArgumentException', |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
10 OperationException => '-IMPL::InvalidOperationException', |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
11 NotAllowedException => 'IMPL::Web::NotAllowedException', |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
12 NotFoundException => 'IMPL::Web::NotFoundException' |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
13 }, |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
14 base => [ |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
15 'IMPL::Object' => undef, |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
16 'IMPL::Web::Application::ResourceInterface' => undef |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
17 ], |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
18 props => [ |
| 255 | 19 parent => PROP_RO, |
| 20 model => PROP_RO, | |
| 21 id => PROP_RO, | |
| 22 name => 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}; |
| 35 die ArgumentException->new( name => 'A name is required' ) | |
| 36 if $args{parent} && not(length $args{name}); | |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
37 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
38 $this->parent( $args{parent} ); |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
39 $this->model( $args{model} ); |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
40 $this->id( $args{id} ); |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
41 $this->contract( $args{contract} ); |
| 255 | 42 $this->name($args{name}); |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
43 |
| 230 | 44 # если расположение явно не указано, то оно вычисляется автоматически, |
| 45 # либо остается не заданным | |
| 255 | 46 $this->location( $args{location} || eval { $this->parent->location->Child( $this->name ) } ); |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
47 } |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
48 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
49 sub InvokeHttpVerb { |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
50 my ( $this, $verb, $action ) = @_; |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
51 |
| 230 | 52 my $operation = $this->contract->verbs->{ lc($verb) }; |
| 53 | |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
54 die NotAllowedException->new( |
| 230 | 55 allow => join( ',', map( uc, keys %{ $this->contract->verbs } ) ) |
| 56 ) | |
| 57 unless $operation; | |
| 245 | 58 |
| 59 # в случае, когда один ресурс вызывает HTTP метод другого ресурса, нужно | |
| 60 # сохранить оригинальный resourceLocation | |
| 61 $action->context->{resourceLocation} ||= $this->location; | |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
62 |
| 230 | 63 return $operation->Invoke( $this, $action ); |
|
229
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 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
66 # это реализация по умолчанию, базируется информации о ресурсах, содержащийся |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
67 # в контракте. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
68 sub FetchChildResource { |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
69 my ( $this, $childId ) = @_; |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
70 |
| 230 | 71 my ($info,$childIdParts) = $this->contract->FindChildResourceInfo($childId); |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
72 |
| 230 | 73 die NotFoundException->new($this->location->url,$childId) unless $info; |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
74 |
| 230 | 75 my $binding = $info->{binding}; |
| 248 | 76 my $contract = $info->{contract}; |
| 77 | |
| 78 if (ref $contract eq 'CODE') { | |
| 79 $contract = $contract->(); | |
| 80 $info->{contract} = $contract; | |
| 81 } | |
| 82 | |
| 83 die OperationException->new("Can't fetch a contract for the resource", $childId) | |
| 84 unless $contract; | |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
85 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
86 my %args = ( |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
87 parent => $this, |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
88 id => $childId |
|
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 |
| 230 | 91 $args{model} = _InvokeDelegate($binding,$this,@$childIdParts); |
|
229
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 return $contract->CreateResource(%args); |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
94 } |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
95 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
96 sub _InvokeDelegate { |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
97 my $delegate = shift; |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
98 |
| 230 | 99 return $delegate->(@_) if ref $delegate eq 'CODE'; |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
100 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')}; |
|
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 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
103 1; |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
104 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
105 __END__ |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
106 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
107 =pod |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
108 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
109 =head1 NAME |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
110 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
111 C<IMPL::Web::Application::Resource> - Web-ресурс. |
|
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 =head1 SYNOPSIS |
|
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 Основная задача - обработать поступающий от контроллера запрос на вызов C<HTTP> |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
117 метода. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
118 |
|
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 осуществляющим привязку к модели в C<IMPL::Web::Application::ResourceContract> |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
121 и C<IMPL::Web::Application::OperationContract>. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
122 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
123 =head1 DESCRIPTION |
|
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<HTTP> методы определяются |
|
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 C<IMPL::Web::Application::ResourceContract>. |
|
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 ресурсов на самом деле рассматривается как пустой контейнер. |
|
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 С ресурсом непосредственно взаимодействует котроллер запросов |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
135 C<IMPL::Web::Handler::RestController>, вызывая два метода. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
136 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
137 =over |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
138 |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
139 =item * C<FetchChildResource($childId)> |
|
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 Данный метод возвращает дочерний ресурс, соответствующий C<$childId>. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
142 Текущая реализация использует метод C<FindChildResourceInfo> контракта текущего |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
143 ресурса, после чего создает дочерний ресурс. |
|
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 Если дочерний ресурс не найден, вызывается исключение |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
146 C<IMPL::Web::NotFoundException>. |
|
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 =item * C<InvokeHttpVerb($verb,$action)> |
|
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 нем выбирается соответсвующий C<IMPL::Web::Application::OperationContract>. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
152 Затем найденный контракт для указанной операции используется для обработки |
|
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 =back |
|
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 Если объект реализует два вышеуказанных метода, он является веб-ресурсом, а |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
158 детали его реализации, котнракт и прочее уже не важно, поэтому можно реализовать |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
159 собственный класс ресурса, например унаследованный от |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
160 C<IMPL::Web::Application::CustomResource>. |
|
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
161 |
| 230 | 162 =head1 MEMBERS |
| 163 | |
| 164 =head2 C<[get]contract> | |
| 165 | |
| 248 | 166 Обязательное свойство для ресурса, ссылается, на контракт, соответствующий |
| 230 | 167 данному ресурсу, используется для выполнения C<HTTP> методов и получения |
| 168 дочерних ресурсов. | |
| 169 | |
| 170 =head2 C<[get]id> | |
| 171 | |
| 172 Обязательное свойство ресурса, идентифицирует его в родительском контейнере, | |
| 173 для корневого ресурса может иметь произвольное значение. | |
| 174 | |
| 255 | 175 =head2 C<[get]name> |
| 176 | |
| 177 Имя ресурса в адресной строке. При разборе адреса идентификаторы ресурсов могут | |
| 178 не всегда совпадать с именами, например C<http://audio/sonar.mp3> может иметь | |
| 179 идентификатор C<sonar>, а его имя будет C<sonar.mp3>. Однако за частую имя | |
| 180 совпадает с идентификатором. | |
| 181 | |
| 230 | 182 =head2 C<[get]parent> |
| 183 | |
| 184 Ссылка на родительский ресурс, для корневого ресурса не определена. | |
| 185 | |
| 186 =head2 C<[get]model> | |
| 187 | |
| 188 Ссылка на объект предметной области, представляемый данным ресурсом. Данное | |
| 189 свойство не является обязательным и может быть не задано. | |
| 190 | |
| 191 =head2 C<[get]location> | |
| 192 | |
| 193 Объект типа C<IMPL::Web::AutoLocator> или аналогичный описывающий адрес текущего | |
| 194 ресурса, может быть как явно передан при создании ресурса, так и вычислен | |
| 195 автоматически (только для ресурсов имеющих родителя). Следует заметить, что | |
| 196 адрес ресурса не содержит параметров запроса, а только путь. | |
| 197 | |
|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
198 =cut |
