annotate Lib/IMPL/Web/Application/Resource.pm @ 229:47f77e6409f7

heavily reworked the resource model of the web application: *some ResourcesContraact functionality moved to Resource +Added CustomResource *Corrected action handlers
author sergey
date Sat, 29 Sep 2012 02:34:47 +0400
parents
children 6d8092d8ce1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
4 use IMPL::lang qw(:constants);
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 => {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
7 Exception => 'IMPL::Exception',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
8 ArgumentException => '-IMPL::InvalidArgumentException',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
9 OperationException => '-IMPL::InvalidOperationException',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
10 NotAllowedException => 'IMPL::Web::NotAllowedException',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
11 NotFoundException => 'IMPL::Web::NotFoundException'
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
12 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
13 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
14 'IMPL::Object' => undef,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
15 'IMPL::Web::Application::ResourceInterface' => undef
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
16 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
17 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
18 parent => PROP_GET | PROP_OWNERSET,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
19 model => PROP_GET | PROP_OWNERSET,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
20 id => PROP_GET | PROP_OWNERSET,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
21 contract => PROP_GET | PROP_OWNERSET,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
22 location => PROP_GET | PROP_OWNERSET
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
23 ]
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
24 };
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 sub CTOR {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
27 my ( $this, %args ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
28
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
29 die ArgumentException->new( id => 'A resource identifier is required' )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
30 unless $args{id};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
31 die ArgumentException->new( contract => 'A contract 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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
34 $this->parent( $args{parent} );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
35 $this->model( $args{model} );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
36 $this->id( $args{id} );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
37 $this->contract( $args{contract} );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
38
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
39 # если расположение явно не указано, что обычно делается для корневого
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
40 # ресурса, то оно вычисляется автоматически, либо остается не заданным
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
41 $this->location( $args{location}
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
42 || eval { $this->parent->location->Child( $this->id ) } );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
43 )
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
47 sub InvokeHttpVerb {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
48 my ( $this, $verb, $action ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
49
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
50 my $verb = $this->contract->verbs->{ lc($verb) };
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
51
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
52 die NotAllowedException->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
53 allow => join( ',' map( uc, keys %{ $this->contract->verbs } ) ) )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
54 unless $verb;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
55
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
56 return $verb->Invoke( $this, $action );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
57 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
58
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
59 # это реализация по умолчанию, базируется информации о ресурсах, содержащийся
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
60 # в контракте.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
61 sub FetchChildResource {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
62 my ( $this, $childId ) = @_;
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 my $info = $this->contract->FindChildResourceInfo($childId);
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 die NotFoundException->new() unless $info;
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 my $binding = $this->{binding};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
69 my $contract = $this->{contract}
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
70 or die OperationException->new("Can't fetch a contract for the resource", $childId);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
71
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
72 my %args = (
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
73 parent => $this,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
74 id => $childId
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
75 );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
76
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
77 $args{model} = _InvokeDelegate($binding,$this);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
78
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
79 return $contract->CreateResource(%args);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
80 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
81
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
82 sub _InvokeDelegate {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
83 my $delegate = shift;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
84
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
85 return $delegete->(@_) if ref $delegate eq 'CODE';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
86 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
87 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
88
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
89 1;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
90
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
91 __END__
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 =pod
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 =head1 NAME
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
96
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
97 C<IMPL::Web::Application::Resource> - Web-ресурс.
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 =head1 SYNOPSIS
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
100
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 Основная задача - обработать поступающий от контроллера запрос на вызов C<HTTP>
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
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 осуществляющим привязку к модели в C<IMPL::Web::Application::ResourceContract>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
107 и C<IMPL::Web::Application::OperationContract>.
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 DESCRIPTION
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<HTTP> методы определяются
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 или он отличается от того, что предоставляется стандартно
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
114 C<IMPL::Web::Application::ResourceContract>.
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 по идентифифкатору, если таковой имеется, тоесть ресурс, у которого нет дочерних
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 С ресурсом непосредственно взаимодействует котроллер запросов
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
121 C<IMPL::Web::Handler::RestController>, вызывая два метода.
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 =over
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 =item * C<FetchChildResource($childId)>
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 Данный метод возвращает дочерний ресурс, соответствующий C<$childId>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
128 Текущая реализация использует метод C<FindChildResourceInfo> контракта текущего
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::NotFoundException>.
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 =item * C<InvokeHttpVerb($verb,$action)>
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 Обрабатывает запрос к ресурсу. Для этого используется контракт ресурса, в
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents:
diff changeset
137 нем выбирается соответсвующий C<IMPL::Web::Application::OperationContract>.
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 запроса.
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 =back
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 Если объект реализует два вышеуказанных метода, он является веб-ресурсом, а
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::Application::CustomResource>.
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 =cut