Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application.pm @ 130:06a34c197b05
Added support for utf-8 and old versions of CGI module
author | wizard |
---|---|
date | Wed, 16 Jun 2010 01:50:56 +0400 |
parents | e4f15cbc3f1a |
children | c5bc900eefd3 |
rev | line source |
---|---|
49 | 1 package IMPL::Web::Application; |
2 use strict; | |
3 use warnings; | |
4 | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
5 use base qw(IMPL::Config IMPL::Object::Singleton); |
58 | 6 |
7 require IMPL::Web::Application::Action; | |
8 require IMPL::Web::Application::Response; | |
9 | |
49 | 10 use IMPL::Class::Property; |
57 | 11 use CGI; |
49 | 12 |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
13 __PACKAGE__->PassThroughArgs; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
14 |
49 | 15 BEGIN { |
52 | 16 public property handlerError => prop_all; |
67 | 17 public property actionFactory => prop_all; |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
58
diff
changeset
|
18 public property handlersQuery => prop_all | prop_list; |
65 | 19 public property responseCharset => prop_all; |
73 | 20 public property security => prop_all; |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
21 public property options => prop_all; |
97 | 22 public property fetchRequestMethod => prop_all; |
49 | 23 } |
24 | |
62 | 25 sub CTOR { |
26 my ($this) = @_; | |
27 | |
67 | 28 $this->actionFactory('IMPL::Web::Application::Action') unless $this->actionFactory; |
65 | 29 $this->responseCharset('utf-8') unless $this->responseCharset; |
97 | 30 $this->fetchRequestMethod(\&defaultFetchRequest) unless $this->fetchRequestMethod; |
31 $this->handlerError(\&defaultHandlerError) unless $this->handlerError; | |
62 | 32 } |
33 | |
49 | 34 sub Run { |
35 my ($this) = @_; | |
36 | |
58 | 37 while (my $query = $this->FetchRequest()) { |
38 | |
67 | 39 my $action = $this->actionFactory->new( |
62 | 40 query => $query, |
58 | 41 application => $this, |
65 | 42 ); |
43 | |
97 | 44 eval { |
45 $action->response->charset($this->responseCharset); | |
46 | |
47 $action->ChainHandler($_) foreach $this->handlersQuery; | |
48 | |
49 $action->Invoke(); | |
50 | |
51 $action->response->Complete; | |
52 }; | |
53 if ($@) { | |
99
6dd659f6f66c
Minor changes, DOM schema is in development (in the aspect of a forms)
wizard
parents:
97
diff
changeset
|
54 my $e = $@; |
129 | 55 # we are expecting this method to be safe otherwise we can trust nothing in this wolrd |
99
6dd659f6f66c
Minor changes, DOM schema is in development (in the aspect of a forms)
wizard
parents:
97
diff
changeset
|
56 $this->handlerError()->($this,$action,$e); |
97 | 57 } |
49 | 58 } |
59 } | |
60 | |
97 | 61 sub FetchRequest { |
62 my ($this) = @_; | |
63 | |
64 if( ref $this->fetchRequestMethod eq 'CODE' ) { | |
65 return $this->fetchRequestMethod->($this); | |
66 } else { | |
67 die new IMPL::Exception("Unknown fetchRequestMethod type",ref $this->fetchRequestMethod); | |
68 } | |
69 } | |
70 | |
57 | 71 { |
72 my $hasFetched = 0; | |
73 | |
97 | 74 sub defaultFetchRequest { |
129 | 75 my ($this) = @_; |
57 | 76 return undef if $hasFetched; |
77 $hasFetched = 1; | |
130
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
78 my $query = CGIWrapper->new(); |
129 | 79 $query->charset($this->responseCharset); |
80 return $query; | |
57 | 81 } |
82 } | |
83 | |
97 | 84 sub defaultHandlerError { |
85 my ($this,$action,$e) = @_; | |
86 warn $e; | |
87 if ( eval { $action->ReinitResponse(); 1; } ) { | |
88 $action->response->contentType('text/plain'); | |
89 $action->response->charset($this->responseCharset); | |
90 $action->response->status(500); | |
91 my $hout = $action->response->streamBody; | |
92 print $hout $e; | |
93 $action->response->Complete(); | |
94 } | |
95 } | |
96 | |
130
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
97 package CGIWrapper; |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
98 use base qw(CGI); |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
99 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
100 use Encode; |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
101 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
102 sub param { |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
103 my $this = shift; |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
104 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
105 if (wantarray) { |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
106 my @result = $this->SUPER::param(@_); |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
107 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
108 return map Encode::is_utf8($_) ? $_ : Encode::decode($this->charset,$_,Encode::LEAVE_SRC), @result; |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
109 } else { |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
110 my $result = $this->SUPER::param(@_); |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
111 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
112 return Encode::is_utf8($result) ? $result : Encode::decode($this->charset,$result,Encode::LEAVE_SRC); |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
113 } |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
114 |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
115 } |
06a34c197b05
Added support for utf-8 and old versions of CGI module
wizard
parents:
129
diff
changeset
|
116 |
49 | 117 1; |
118 | |
52 | 119 __END__ |
120 | |
49 | 121 =pod |
122 | |
123 =head1 SYNOPSIS | |
124 | |
67 | 125 =begin code |
126 | |
49 | 127 require MyApp; |
67 | 128 |
129 my $instance = spawn MyApp('app.config'); | |
130 | |
131 $instance->Run(); | |
132 | |
133 =end code | |
49 | 134 |
135 =head1 DESCRIPTION | |
136 | |
73 | 137 C< use base qw( IMPL::Config IMPL::Object::Singleton )> |
138 | |
49 | 139 Зкземпляр приложения содержит в себе глобальные настройки, реализует контроллер запросов, |
52 | 140 в качестве источника запросов используется CGI или иной совместимый модуль. |
49 | 141 |
52 | 142 Процесс обработки запроса состоит из следующих частей |
49 | 143 |
67 | 144 =over |
145 | |
146 =item 1 | |
147 | |
148 Получение cgi запроса | |
149 | |
150 =item 2 | |
52 | 151 |
67 | 152 Создание объекта C<IMPL::Web::Application::Action> |
52 | 153 |
67 | 154 =item 3 |
52 | 155 |
67 | 156 Формирование цепочки вызовов при помощи C<< IMPL::Web::Application::Action->ChainHandler >> |
157 | |
158 =item 4 | |
159 | |
160 Выполнение запроса C<< IMPL::Web::Application::Action->Invoke >> | |
49 | 161 |
162 =cut | |
67 | 163 |
164 Также приложение поддерживает отложенное создание объектов, которые по первому обращению | |
165 к свойствам. Это реализовано в базовом классе C< IMPL::Configuration >. Для настройки | |
166 активаторов можно использовать свойство C<options>, в которое должен быть помещен хеш | |
167 со ссылками на активаторы, см. пример ниже C<CONFIGURATION>. | |
168 | |
169 =head2 CONFIGURATION | |
170 | |
171 Ниже приведен пример конфигурации приложения | |
172 | |
173 =begin code xml | |
174 | |
175 <?xml version="1.0" encoding="UTF-8"?> | |
176 <Application id='app' type="Test::Web::Application::Instance"> | |
177 | |
178 <!-- Begin custom properties --> | |
179 <name>Sample application</name> | |
180 <dataSource type='IMPL::Config::Activator' id='ds'> | |
181 <factory>IMPL::Object</factory> | |
182 <parameters type='HASH'> | |
183 <db>data</db> | |
184 <user>nobody</user> | |
185 </parameters> | |
186 </dataSource> | |
187 <securityMod type='IMPL::Config::Activator'> | |
188 <factory>IMPL::Object</factory> | |
189 <parameters type='HASH'> | |
190 <ds refid='ds'/> | |
191 </parameters> | |
192 </securityMod> | |
193 <!-- End custom properties --> | |
194 | |
195 <!-- direct access to the activators --> | |
196 <options type="HASH"> | |
197 <dataSource refid='ds'/> | |
198 </options> | |
199 | |
200 <!-- Set default output encoding, can be changed due query handling --> | |
201 <responseCharset>utf-8</responseCharset> | |
202 | |
203 <!-- Actions creation configuration --> | |
204 <actionFactory type="IMPL::Object::Factory"> | |
205 | |
206 <!-- Construct actions --> | |
207 <factory>IMPL::Web::Application::Action</factory> | |
208 <parameters type='HASH'> | |
209 | |
210 <!-- with special responseFactory --> | |
211 <responseFactory type='IMPL::Object::Factory'> | |
212 | |
213 <!-- Where resopnses have a special streamOut --> | |
214 <factory>IMPL::Web::Application::Response</factory> | |
215 <parameters type='HASH'> | |
216 | |
217 <!-- in memory dummy output instead of STDOUT --> | |
218 <streamOut>memory</streamOut> | |
219 | |
220 </parameters> | |
221 </responseFactory> | |
222 </parameters> | |
223 </actionFactory> | |
224 | |
225 <!-- Query processing chain --> | |
226 <handlersQuery type="IMPL::Object::List"> | |
227 <item type="IMPL::Web::QueryHandler::PageFormat"> | |
228 <templatesCharset>cp1251</templatesCharset> | |
229 </item> | |
230 </handlersQuery> | |
231 </Application> | |
232 | |
233 =end code xml | |
234 | |
73 | 235 =head1 MEMBERS |
236 | |
237 =over | |
238 | |
239 =item C<[get,set] handlerError> | |
240 | |
241 Обработчик который будет вызван в случае возникновения необработанной ошибки | |
242 в процессе работы приложения. После чего приложение корректно завершается. | |
243 | |
244 =item C<[get,set] actionFactory> | |
245 | |
246 Фабрика объектов, которая используется приложением, для создания объектов | |
247 типа C<IMPL::Web::Application::Action> при обработки C<CGI> запросов. | |
248 | |
249 =begin code | |
250 | |
251 my $action = $this->actionFactory->new( | |
252 query => $query, | |
253 application => $this, | |
254 ); | |
255 | |
256 =end code | |
257 | |
97 | 258 =item C< [get,set] fetchRequestMethod > |
259 | |
260 Метод получения CGI запроса. Возвращает C<CGI> объект следующего запроса, если | |
261 запросов больше нет, то возвращает C<undef>. По-умолчанию использует C<defaultFetchRequest>. | |
262 | |
263 Может быть как ссылкой на функцию, так и объектом типа C<IMPL::Web::Application::RequestFetcher>. | |
264 | |
73 | 265 =item C< [get,set,list] handlersQuery > |
266 | |
267 Список обработчиков запросов, которые будут переданы созданному объекту-действию. | |
268 | |
269 =item C< [get,set] responseCharset> | |
270 | |
271 Кодировка ответа клиенту. | |
272 | |
273 =item C< [get,set] security > | |
274 | |
275 Объект C<IMPL::Web::Security>, для работы с инфраструктурой безопасности. | |
276 | |
277 =item C< [get,set] options > | |
278 | |
279 Обычно ссылка на хеш с настраиваемыми объектами, используется для возможности | |
280 програмной настройки активаторов, т.к. напрямую через свойства приложения получить | |
281 к ним доступ не получится. | |
282 | |
283 =back | |
284 | |
285 =cut |