annotate lib/IMPL/Web/Application/Action.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::Application::Action;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use URI;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use JSON;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::lang;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use IMPL::Web::CGIWrapper();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 Disposable => '-IMPL::Object::Disposable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 HttpResponse => 'IMPL::Web::HttpResponse'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 'IMPL::Object::Autofill' => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 'IMPL::Object::Disposable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 application => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 security => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 query => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 context => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 _jsonData => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $this->context({});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 $this->security($this->application->CreateSecurity());
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 sub cookie {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 my ($this,$name,$rx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 $this->_launder(scalar( $this->query->cookie($name) ), $rx );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 sub header {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my ($this,$header) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 $this->query->https ? $this->query->https($header) : $this->query->http($header);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 sub isSecure {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 shift->query->https ? 1 : 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 sub isJson {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 return shift->contentType =~ m{^application/json} ? 1 : 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 sub param {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my ($this,$name,$rx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 if (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 $this->requestMethod eq 'GET'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 or
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $this->contentType eq 'multipart/form-data'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 or
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 $this->contentType eq 'application/x-www-form-urlencoded'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $value = scalar( $this->query->param($name) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $value = scalar( $this->query->url_param($name) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 $this->_launder($value, $rx );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 sub urlParam {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 my ($this,$name,$rx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 $this->_launder(scalar( $this->query->url_param($name) ), $rx);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 sub urlParams {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 shift->query->url_param();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 sub rawData {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my ($this, $decode) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 local $IMPL::Web::CGIWrapper::NO_DECODE = $decode ? 0 : 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 if ($this->requestMethod eq 'POST') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 return $this->query->param('POSTDATA');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 } elsif($this->requestMethod eq 'PUT') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 return $this->query->param('PUTDATA');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 sub jsonData {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 if ($this->isJson ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 my $data = $this->_jsonData;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 unless($data) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 $data = JSON->new()->decode($this->rawData('decode encoding'));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 $this->_jsonData($data);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 return $data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 sub requestMethod {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 return $this->query->request_method;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 sub contentType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 return shift->query->content_type();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 sub pathInfo {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 return $this->query->path_info;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 sub baseUrl {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 return $this->query->url(-base => 1);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 sub applicationUrl {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 shift->application->baseUrl;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 sub applicationFullUrl {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 return URI->new_abs($this->application->baseUrl, $this->query->url(-base => 1));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 # creates an url that contains server, schema and path parts
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 sub CreateFullUrl {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 my ($this,$path) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 return $path ? URI->new_abs($path,$this->applicationFullUrl) : $this->applicationFullUrl;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 # creates an url that contains only a path part
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 sub CreateAbsoluteUrl {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 my ($this,$path) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 return $path ? URI->new_abs($path,$this->applicationUrl) : $this->applicationUrl;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 sub Redirect {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 my ($this,$path) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 return HttpResponse->Redirect(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 location => $this->CreateFullUrl($path)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 sub _launder {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 my ($this,$value,$rx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 if ( $value ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 if ($rx) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 if ( my @result = ($value =~ m/$rx/) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 return @result > 1 ? \@result : $result[0];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 return $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 sub Dispose {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 $this->security->Dispose()
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 if $this->security and $this->security->can('Dispose');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 $_->Dispose() foreach grep is($_,Disposable), values %{$this->context};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 $this->next::method();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 C<IMPL::Web::Application::Action> - Обертка вокруг C<CGI> запроса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 C<[Infrastructure]>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 Свзяывет CGI запрос, приложение, орабатывающее его и ответ, который будет отправлен клиенту.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 =head2 C<CTOR(%args)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 Инициализирует новый экземпляр. Именованными параметрами передаются значения
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214 свойств.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 =head2 C< [get]application>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 Экземпляр приложения создавшего текущий объект
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220 =item C< [get] query >
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 Экземпляр C<CGI> запроса
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227 =cut