annotate Lib/IMPL/Web/Application.pm @ 335:e8be9062ecf2

improved resource classes, contracts are deprecated
author cin
date Thu, 13 Jun 2013 20:13:24 +0400
parents 63709a4e6da0
children 97628101b765
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
1 package IMPL::Web::Application;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
4
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
5 use CGI;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
6 use Carp qw(carp);
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 230
diff changeset
7 use IMPL::Const qw(:prop);
58
a35b60b16a99 Configuration, late activation
wizard
parents: 57
diff changeset
8
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
9 use IMPL::declare {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
10 require => {
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
11 Locator => 'IMPL::Web::AutoLocator',
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
12 TAction => 'IMPL::Web::Application::Action',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
13 HttpResponse => 'IMPL::Web::HttpResponse',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
14 TFactory => '-IMPL::Object::Factory',
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
15 Exception => 'IMPL::Exception',
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
16 ArgException => '-IMPL::InvalidArgumentException',
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
17 InvalidOperationException => '-IMPL::InvalidOperationException',
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
18 Loader => 'IMPL::Code::Loader'
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
19 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
20 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
21 'IMPL::Config' => '@_',
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
22 'IMPL::Object::Singleton' => undef
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
23 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
24 props => [
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
25 baseUrl => PROP_RW,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 230
diff changeset
26 actionFactory => PROP_RW,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 230
diff changeset
27 handlers => PROP_RW | PROP_LIST,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 230
diff changeset
28 security => PROP_RW,
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
29 output => PROP_RW,
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
30 location => PROP_RO,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
31 _handler => PROP_RW
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
32 ]
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
33 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
34
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
35 sub CTOR {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
36 my ($this) = @_;
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
37
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
38 die IMPL::InvalidArgumentException->new( "handlers",
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
39 "At least one handler should be supplied" )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
40 unless $this->handlers->Count;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
41
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
42 $this->baseUrl('/') unless $this->baseUrl;
a02b110da931 refactoring
sergey
parents: 242
diff changeset
43
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
44 $this->actionFactory(TAction) unless $this->actionFactory;
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
45 $this->location(Locator->new(base => $this->baseUrl));
62
c64bd1bf727d Web application
wizard
parents: 60
diff changeset
46 }
c64bd1bf727d Web application
wizard
parents: 60
diff changeset
47
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
48 sub ProcessRequest {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
49 my ($this,$q) = @_;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
50
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
51 die ArgException->new(q => 'A query is required')
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
52 unless $q;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
53
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
54 my $handler = $this->_handler;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
55 unless ($handler) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
56 $handler = _ChainHandler( $_, $handler ) foreach $this->handlers;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
57 $this->_handler($handler);
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
58 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
59
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
60 my $action = $this->actionFactory->new(
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
61 query => $q,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
62 application => $this,
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
63 );
328
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
64
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
65 $this->BeforeProcessRequest($action);
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
66
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
67 eval {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
68 my $result = $handler->($action);
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
69
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
70 die InvalidOperationException->new("Invalid handlers result. A reference to IMPL::Web::HttpResponse is expexted.")
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
71 unless eval { $result->isa(HttpResponse) };
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
72
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
73 $result->PrintResponse( $this->output );
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
74 };
328
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
75
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
76 $this->AfterProcessRequest($action);
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
77 if ($@) {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
78 my $e = $@;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
79
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
80 HttpResponse->InternalError(
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
81 type => 'text/plain',
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
82 charset => 'utf-8',
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
83 body => $e
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
84 )->PrintResponse( $this->output );
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
85
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
86 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
87 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
88
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
89 sub _ChainHandler {
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
90 my ( $handler, $next ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
91
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
92 if ( ref $handler eq 'CODE' ) {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
93 return sub {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
94 my ($action) = @_;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
95 return $handler->( $action, $next );
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
96 };
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
97 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
98 elsif ( eval { $handler->can('Invoke') } ) {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
99 return sub {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
100 my ($action) = @_;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
101 return $handler->Invoke( $action, $next );
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
102 };
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
103 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
104 elsif ( eval { $handler->isa(TFactory) } ) {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
105 return sub {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
106 my ($action) = @_;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
107 my $inst = $handler->new();
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
108 return $inst->Invoke( $action, $next );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
109 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
110 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
111 elsif ( $handler
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
112 and not ref $handler
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
113 and $handler =~ m/^(-)?(\w+(?:::\w+)*)$/ )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
114 {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
115 my $class = $2;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
116 if ( not $1 ) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
117 Loader->safe->Require($class);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
118 die IMPL::InvalidArgumentException->(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
119 "An invalid handler supplied", $handler
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
120 ) unless $class->can('Invoke');
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
121 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
122
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
123 return sub {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
124 my ($action) = @_;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
125 my $inst = $class->new();
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
126 return $inst->Invoke( $action, $next );
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
127 };
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
128 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
129 else {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
130 die new IMPL::InvalidArgumentException( "An invalid handler supplied",
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
131 $handler );
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
132 }
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
133 }
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 194
diff changeset
134
328
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
135 sub BeforeProcessRequest {
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
136
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
137 }
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
138
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
139 sub AfterProcessRequest {
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
140
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
141 }
63709a4e6da0 Security framework refactoring
cin
parents: 285
diff changeset
142
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
143 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
144
52
15d720913562 security in work
wizard@linux-odin.local
parents: 49
diff changeset
145 __END__
15d720913562 security in work
wizard@linux-odin.local
parents: 49
diff changeset
146
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
147 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
148
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
149 =head1 NAME
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 213
diff changeset
150
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 244
diff changeset
151 C<IMPL::Web::Application> Базовай класс для веб-приложения
244
a02b110da931 refactoring
sergey
parents: 242
diff changeset
152
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 47
diff changeset
153 =cut