annotate Lib/IMPL/Web/Application/Resource.pm @ 406:f23fcb19d3c1 ref20150831

implemented ServicesBag
author cin
date Mon, 31 Aug 2015 20:22:16 +0300
parents ef37ec102dab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
1 package IMPL::Web::Application::Resource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
2 use strict;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
3
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
4 use constant {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
5 ResourceClass => __PACKAGE__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
6 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
7 use Scalar::Util qw(blessed);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
8
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
9 use IMPL::lang qw(:hash :base);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
10 use IMPL::Const qw(:prop);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
11 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
12 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
13 Exception => 'IMPL::Exception',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
14 OpException => '-IMPL::InvalidOperationException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
15 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
16 ResourceInterface => '-IMPL::Web::Application',
400
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
17 HttpResponse => 'IMPL::Web::HttpResponse',
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
18 HttpResponseResource => 'IMPL::Web::Application::HttpResponseResource',
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
19 Loader => 'IMPL::Code::Loader'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
20 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
21 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
22 'IMPL::Web::Application::ResourceBase' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
23 ],
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
24 props => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
25 access => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
26 verbs => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
27 children => PROP_RW
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
28 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
29 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
30
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
31 __PACKAGE__->static_accessor(verbNames => [qw(get post put delete options head)]);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
32 __PACKAGE__->static_accessor(httpMethodPrefix => 'Http');
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
33
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
34 sub CTOR {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
35 my ($this, %args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
36
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
37 my %verbs;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
38 my $httpPrefix = $this->httpMethodPrefix;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
39
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
40 foreach my $verb (@{$this->verbNames}) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
41 my $method = exists $args{$verb} ? $args{$verb} : $this->can($httpPrefix . ucfirst($verb));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
42 $verbs{$verb} = $method
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
43 if $method;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
44 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
45
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
46 hashApply(\%verbs,$args{verbs})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
47 if ref($args{verbs}) eq 'HASH' ;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
48
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
49 $this->children($args{children} || $this->GetChildResources());
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
50
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
51 $this->access($args{access})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
52 if $args{access};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
53
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
54 $this->verbs(\%verbs);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
55 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
56
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
57 sub _isInvokable {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
58 my ($this,$method) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
59
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
60 return
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
61 (blessed($method) and $method->can('Invoke')) ||
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
62 ref($method) eq 'CODE'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
63 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
64
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
65 sub _invoke {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
66 my ($this,$method,@args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
67
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
68 if(blessed($method) and $method->can('Invoke')) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
69 return $method->Invoke($this,@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
70 } elsif(ref($method) eq 'CODE' || (not(ref($method)) and $this->can($method))) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
71 return $this->$method(@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
72 } else {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
73 die OpException->new("Can't invoke the specified method: $method");
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
74 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
75 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
76
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
77 sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
78 shift->model;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
79 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
80
392
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
81 sub AccessCheck {
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
82 my ($this,$verb) = @_;
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
83
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
84 $this->_invoke($this->access,$verb)
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
85 if $this->access;
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
86 }
54b1dc9e808a fixed web-resources access check
sergey
parents: 391
diff changeset
87
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
88 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
89 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
90
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
91 my $children = $this->children
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
92 or die NotFoundException->new( $this->location->url, $childId );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
93
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
94 if (ref($children) eq 'HASH') {
375
cin
parents: 374
diff changeset
95 if(my $child = $children->{$childId}) {
cin
parents: 374
diff changeset
96 return $this->_isInvokable($child) ? $this->_invoke($child, $childId) : $child;
cin
parents: 374
diff changeset
97 } else {
cin
parents: 374
diff changeset
98 die NotFoundException->new( $this->location->url, $childId );
cin
parents: 374
diff changeset
99 }
374
cin
parents: 373
diff changeset
100 } elsif($this->_isInvokable($children)) {
cin
parents: 373
diff changeset
101 return $this->_invoke($children,$childId);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
102 } else {
374
cin
parents: 373
diff changeset
103 die OpException->new("Invalid resource description", $childId, $children);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
104 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
105 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
106
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
107 sub FetchChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
108 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
109
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
110 my $info = $this->Fetch($childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
111
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
112 return $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
113 if (is($info,ResourceInterface));
400
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
114
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
115 $info = {
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
116 response => $info,
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
117 class => HttpResponseResource
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
118 }
ef37ec102dab redirection support while fetching child resources
cin
parents: 392
diff changeset
119 if is($info,HttpResponse);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
120
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
121 return $this->CreateChildResource($info, $childId)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
122 if ref($info) eq 'HASH';
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
123
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
124 die OpException->new("Invalid resource description", $childId, $info);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
125 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
126
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
127 sub CreateChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
128 my ($this,$info, $childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
129
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
130 my $params = hashApply(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
131 {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
132 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
133 id => $childId,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
134 request => $this->request,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
135 class => ResourceClass
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
136 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
137 $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
138 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
139
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
140 $params->{model} = $this->_invoke($params->{model})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
141 if $this->_isInvokable($params->{model});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
142
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
143 my $factory = Loader->default->Require($params->{class});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
144
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
145 return $factory->new(%$params);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
146 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
147
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
148 sub GetChildResources {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
149 return {};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
150 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
151
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
152 1;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
153
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
154 __END__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
155
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
156 =pod
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
157
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
158 =head1 NAME
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
159
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
160 C<IMPL::Web::Application::Resource> - Ресурс C<REST> веб приложения
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
161
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
162 =head1 SYNOPSIS
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
163
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
164 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
165
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
166 use IMPL::require {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
167 Resource => 'IMPL::Web::Application::Resource',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
168 Security => 'IMPL::Security',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
169 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
170 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
171 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
172
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
173 my $model = Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
174 get => sub { },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
175 verbs => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
176 # non-standart verbs placed here
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
177 myverb => sub { }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
178 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
179 #child resources can be a hash
391
2287c72f303a code cleanup
cin
parents: 375
diff changeset
180 children => {
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
181 user => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
182 # a resource class may be specified optionally
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
183 # class => Resource,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
184 model => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
185 return Security->principal
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
186 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
187 # the default get implementation is implied
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
188 # get => sub { shift->model },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
189 access => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
190 my ($this,$verb) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
191 die ForbiddenException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
192 if Security->principal->isNobody
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
193 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
194 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
195 catalog => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
196 get => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
197 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
198
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
199 return $ctx->products->find_rs({ in_stock => 1 });
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
200 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
201 # chid resource may be created dynamically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
202 children => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
203 # binds model against the parent reource and id
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
204 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
205
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
206 ($id) = ($id =~ /^(\w+)$/)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
207 or die NotFoundException->new($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
208
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
209 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
210
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
211 my $item = $ctx->products->fetch($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
212
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
213 die NotFoundException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
214 unless $item;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
215
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
216 # return parameters for the new resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
217 return {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
218 model => $item,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
219 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
220 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
221 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
222 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
223 # dynamically binds whole child resource. The result of binding is
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
224 # the new resource or a hash with arguments to create one
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
225 posts => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
226 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
227
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
228 # this approach can be used to create a dynamic resource relaying
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
229 # on the type of the model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
230
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
231 return Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
232 id => $id,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
233 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
234 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
235 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
236
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
237 # ditto
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
238 # parent and id will be mixed in automagically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
239 # return { get => sub { shift->model} }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
240 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
241 post_only => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
242 get => undef, # remove GET verb implicitly
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
243 post => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
244 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
245 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
246 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
247 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
248 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
249
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
250 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
251
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
252 Альтернативный вариант для создания класса ресурса.
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
253
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
254 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
255
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
256 package MyResource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
257
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
258 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
259 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
260 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
261 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
262 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
263 'IMPL::Web::Application::Resource' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
264 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
265 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
266
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
267 sub ds {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
268 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
269
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
270 $this->context->{ds} ||= $this->application->ConnectDb();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
271 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
272
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
273 sub InvokeHttpVerb {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
274 my $this = shift;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
275
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
276 $this->ds->Begin();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
277
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
278 my $result = $this->next::method(@_);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
279
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
280 # in case of error the data context will be disposed and the transaction
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
281 # will be reverted
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
282 $this->ds->Commit();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
283
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
284 return $result;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
285 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
286
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
287 # this method is inherited by default
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
288 # sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
289 # shift->model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
290 #
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
291 # }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
292
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
293 sub HttpPost {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
294 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
295
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
296 my %data = map {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
297 $_,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
298 $this->request->param($_)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
299 } qw(name description value);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
300
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
301 die ForbiddenException->new("The item with the scpecified name can't be created'")
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
302 if(not $data{name} or $this->ds->items->find({ name => $data{name}))
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
303
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
304 $this->ds->items->insert(\%data);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
305
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
306 return $this->NoContent();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
307 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
308
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
309 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
310 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
311
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
312 my $item = $this->ds->items->find({name => $childId})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
313 or die NotFoundException->new();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
314
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
315 # return parameters for the child resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
316 return { model => $item, role => "item food" };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
317 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
318
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
319 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
320
374
cin
parents: 373
diff changeset
321 =head1 MEMBERS
cin
parents: 373
diff changeset
322
375
cin
parents: 374
diff changeset
323 =head2 C<[get,set]verbs>
cin
parents: 374
diff changeset
324
cin
parents: 374
diff changeset
325 Хеш с C<HTTP> методами. При попытке вызова C<HTTP> метода, которого нет в этом
cin
parents: 374
diff changeset
326 хеше приводит к исключению C<IMPL::Web::NotAllowedException>.
cin
parents: 374
diff changeset
327
cin
parents: 374
diff changeset
328 =head2 C<[get,set]access>
cin
parents: 374
diff changeset
329
cin
parents: 374
diff changeset
330 Метод для проверки прав доступа. Если не задан, то доспуп возможен для всех.
cin
parents: 374
diff changeset
331
cin
parents: 374
diff changeset
332 =head2 C<[get,set]children>
cin
parents: 374
diff changeset
333
cin
parents: 374
diff changeset
334 Дочерние ресурсы. Дочерние ресурсы могут быть описаны либо в виде хеша, либо
cin
parents: 374
diff changeset
335 в виде метода.
cin
parents: 374
diff changeset
336
cin
parents: 374
diff changeset
337 =head3 C<HASH>
cin
parents: 374
diff changeset
338
cin
parents: 374
diff changeset
339 Данный хещ содержит в себе таблицу идентификаторов дочерних ресурсов и их
cin
parents: 374
diff changeset
340 описаний.
cin
parents: 374
diff changeset
341
cin
parents: 374
diff changeset
342 Описание каждого ресурса представляет собой либо функцию, либо параметры для
cin
parents: 374
diff changeset
343 создания ресурса C<CraeteChildResource>. Если описание в виде функции, то она
cin
parents: 374
diff changeset
344 должна возвращать либо объект типа ресурс либо параметры для его создания.
cin
parents: 374
diff changeset
345
cin
parents: 374
diff changeset
346 =head3 C<CODE>
cin
parents: 374
diff changeset
347
cin
parents: 374
diff changeset
348 Если дочерние ресурсы описаны в виде функции (возможно использовать имя метода
cin
parents: 374
diff changeset
349 класса текущего ресурса), то для получения дочернего ресурса будет вызвана
cin
parents: 374
diff changeset
350 функция с параметрами C<($this,$childId)>, где C<$this> - текущий ресурс,
cin
parents: 374
diff changeset
351 C<$childId> - идентификатор дочернего ресурса, который нужно вернуть.
cin
parents: 374
diff changeset
352
cin
parents: 374
diff changeset
353 Данная функция должна возвратить либо объект типа ресурс, либо ссылку на хеш с
cin
parents: 374
diff changeset
354 параметрами для создания оного при помощи метода
cin
parents: 374
diff changeset
355 C<CreateChildResource($params,$childId)>.
cin
parents: 374
diff changeset
356
374
cin
parents: 373
diff changeset
357 =head2 C<[virtual]Fetch($childId)>
cin
parents: 373
diff changeset
358
cin
parents: 373
diff changeset
359 Метод для получения дочернего ресурса.
cin
parents: 373
diff changeset
360
cin
parents: 373
diff changeset
361 Возвращает параметры для создания дочернего ресурса, либо уже созданный ресурс.
cin
parents: 373
diff changeset
362 Создание дочернего ресурса происходит при помощи метода C<CreateChildResource()>
cin
parents: 373
diff changeset
363 который добавляет недостающие параметры к возвращенным в данным методом и
cin
parents: 373
diff changeset
364 создает новый ресурс
cin
parents: 373
diff changeset
365
375
cin
parents: 374
diff changeset
366 =head2 C<CreateChildResource($params,$id)>
cin
parents: 374
diff changeset
367
cin
parents: 374
diff changeset
368 Создает новый дочерний ресурс с указанным идентификатором и параметрами.
cin
parents: 374
diff changeset
369 Автоматически заполняет параметры
cin
parents: 374
diff changeset
370
cin
parents: 374
diff changeset
371 =over
cin
parents: 374
diff changeset
372
cin
parents: 374
diff changeset
373 =item * C<parent>
cin
parents: 374
diff changeset
374
cin
parents: 374
diff changeset
375 =item * C<id>
cin
parents: 374
diff changeset
376
cin
parents: 374
diff changeset
377 =item * C<request>
cin
parents: 374
diff changeset
378
cin
parents: 374
diff changeset
379 =back
cin
parents: 374
diff changeset
380
cin
parents: 374
diff changeset
381 Тип создаваемого ресурса C<IMPL::Web::Application::Resource>, либо указывается
cin
parents: 374
diff changeset
382 в параметре C<class>.
cin
parents: 374
diff changeset
383
cin
parents: 374
diff changeset
384 =head2 C<[virtual]HttpGet()>
cin
parents: 374
diff changeset
385
cin
parents: 374
diff changeset
386 Реализует C<HTTP> метод C<GET>. По-умолчанию возвращает модель.
cin
parents: 374
diff changeset
387
cin
parents: 374
diff changeset
388 Данный метод нужен для того, чтобы ресурс по-умолчанию поддерживал метод C<GET>,
cin
parents: 374
diff changeset
389 что является самым частым случаем, если нужно изменить данное поведение, нужно:
cin
parents: 374
diff changeset
390
cin
parents: 374
diff changeset
391 =over
cin
parents: 374
diff changeset
392
cin
parents: 374
diff changeset
393 =item * Передать в параметр конструктора C<get> значение undef
cin
parents: 374
diff changeset
394
cin
parents: 374
diff changeset
395 =item * Переопределить метод C<HttpGet>
cin
parents: 374
diff changeset
396
cin
parents: 374
diff changeset
397 =item * При проверке прав доступа выдать исключение
cin
parents: 374
diff changeset
398
cin
parents: 374
diff changeset
399 =back
374
cin
parents: 373
diff changeset
400
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
401 =cut
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
402