Mercurial > pub > Impl
annotate Lib/IMPL/Web/QueryHandler/PageFormat.pm @ 144:b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
minor speed improvements to the object CTOR caching
Added support for a secure processing (and 'laundering' ) a CGI parameters
Many minor fixes
author | wizard |
---|---|
date | Tue, 13 Jul 2010 02:05:38 +0400 |
parents | fb896377389f |
children | 60fd224f3e3c |
rev | line source |
---|---|
62 | 1 package IMPL::Web::QueryHandler::PageFormat; |
64 | 2 use base qw(IMPL::Web::QueryHandler IMPL::Object::Autofill); |
136 | 3 use strict; |
62 | 4 |
5 __PACKAGE__->PassThroughArgs; | |
6 | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
7 use JSON; |
62 | 8 use IMPL::Class::Property; |
77 | 9 use IMPL::Web::TT::Document; |
136 | 10 use Template::Plugin::URL; |
97 | 11 use IMPL::Security::Context; |
65 | 12 use File::Spec; |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
13 use Error qw(:try); |
62 | 14 |
136 | 15 $Template::Plugin::URL::JOINT = '&'; |
16 | |
64 | 17 BEGIN { |
65 | 18 public property templatesCharset => prop_all; |
19 public property templatesBase => prop_all; | |
97 | 20 public property defaultTarget => prop_all; |
116 | 21 public property pathinfoPrefix => prop_all; |
134 | 22 public property cache => prop_all; |
64 | 23 } |
24 | |
25 sub CTOR { | |
26 my ($this) = @_; | |
27 | |
65 | 28 $this->templatesCharset('utf-8') unless $this->templatesCharset; |
134 | 29 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; |
30 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; | |
64 | 31 } |
32 | |
62 | 33 sub Process { |
34 my ($this,$action,$nextHandler) = @_; | |
35 | |
134 | 36 my $doc = new IMPL::Web::TT::Document(cache => $this->cache); |
62 | 37 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
38 try { |
97 | 39 |
40 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; | |
41 | |
136 | 42 my ($requestUri) = split /\?/, $ENV{REQUEST_URI}; |
43 | |
44 my $pathInfo; | |
45 | |
46 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { | |
47 # CGI with path info | |
48 $pathInfo = $ENV{PATH_INFO}; | |
49 } else { | |
137 | 50 die "REQUEST_URI: $requestUri\nPATH_INFO: $ENV{PATH_INFO}" unless $requestUri eq $ENV{PATH_INFO}; |
136 | 51 } |
52 | |
53 my @root = (''); | |
54 my @base; | |
55 | |
116 | 56 if (my $rx = $this->pathinfoPrefix) { |
136 | 57 $requestUri =~ s/^($rx)//; |
58 push @root, grep $_, split /\//, $1 if $1; | |
116 | 59 } |
136 | 60 |
61 $pathInfo = $requestUri unless defined $pathInfo; | |
62 | |
63 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); | |
64 | |
116 | 65 local $ENV{PATH_INFO} = $pathInfo || $this->defaultTarget; |
97 | 66 |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
67 my @path = grep $_, split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" ); |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
68 |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
69 my @pathContainer = @path; |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
70 pop @pathContainer; |
65 | 71 |
121 | 72 $doc->LoadFile ( File::Spec->catfile($this->templatesBase,@path), $this->templatesCharset, $this->templatesBase ); |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
73 |
97 | 74 $doc->AddVar( result => $nextHandler->() ); |
144
b56ebc31bf18
Empty nodes no more created while transforming a post request to the DOM document
wizard
parents:
140
diff
changeset
|
75 $doc->AddVar( action => $action ); |
129 | 76 $doc->AddVar( app => $action->application ); |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
77 |
136 | 78 $doc->AddVar( absoluteUrl => sub { join '/', @root, $_[0] } ); |
79 $doc->AddVar( baseUrl => sub { join '/', @root, @base, $_[0] } ); | |
80 $doc->AddVar( relativeUrl => sub { join '/', @root, @base, @pathContainer,$_[0] } ); | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
81 |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
82 $doc->AddVar( user => IMPL::Security::Context->current->principal ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
83 $doc->AddVar( session => IMPL::Security::Context->current ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
84 |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
85 $doc->AddVar( to_json => \&to_json ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
86 $doc->AddVar( escape_string => sub { $_[0] =~ s/"/"/g; $_[0] } ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
87 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
88 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
89 $action->response->contentType('text/html'); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
90 my $hOut = $action->response->streamBody; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
91 print $hOut $doc->Render(); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
92 } finally { |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
93 $doc->Dispose; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
94 }; |
62 | 95 } |
96 | |
65 | 97 1; |
98 | |
99 __END__ | |
100 | |
101 =pod | |
102 | |
103 =head1 NAME | |
104 | |
105 C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона. | |
106 | |
107 =head1 SYNOPSIS | |
108 | |
109 В файле конфигурации приложения | |
110 | |
111 =begin code xml | |
112 | |
113 <handlersQuery type="IMPL::Object::List"> | |
114 <item type="IMPL::Web::QueryHandler::PageFormat"> | |
115 <charsetTemplates>utf-8</charsetTemplates> | |
116 </item> | |
117 </handlersQuery> | |
118 | |
119 =end code xml | |
120 | |
121 Программно | |
122 | |
123 =begin code | |
124 | |
125 my $app = new IMPL::Web::Application(); | |
126 $app->handlersQuery->Add( | |
127 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); | |
128 ); | |
129 | |
130 =end | |
131 | |
132 =head1 DESCRIPTION | |
133 | |
134 Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется | |
135 из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>. | |
136 | |
137 Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала | |
138 обработчика запроса и переопределяет метод C<Process>. | |
139 | |
140 C<Serializable> | |
141 | |
142 =head1 MEMBERS | |
143 | |
144 =over | |
145 | |
146 =item C<CTOR(%props)> | |
147 | |
148 Создает новый экземпляр и заполняет свойства. | |
149 | |
150 =item C<[get,set] templatesCharset> | |
151 | |
152 Кодировка шаблонов. По умолчанию utf-8. | |
153 | |
154 =item C<[get,set] templatesBase> | |
155 | |
156 Каталог относительно которого ищется шаблон. | |
157 | |
158 =item C<[override] Process($action,$nextHandler)> | |
159 | |
160 Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление | |
161 для выполнения действий. | |
162 | |
163 =back | |
164 | |
165 =cut |