comparison Lib/IMPL/Web/Application/Action.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents d1676be8afcc
children c8fe3f84feba
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
6 __PACKAGE__->PassThroughArgs; 6 __PACKAGE__->PassThroughArgs;
7 7
8 use IMPL::Class::Property; 8 use IMPL::Class::Property;
9 9
10 BEGIN { 10 BEGIN {
11 public property application => prop_get | owner_set; 11 public property application => prop_get | owner_set;
12 public property query => prop_get | owner_set; 12 public property query => prop_get | owner_set;
13 public property response => prop_get | owner_set; 13 public property response => prop_get | owner_set;
14 public property responseFactory => prop_get | owner_set; 14 public property responseFactory => prop_get | owner_set;
15 public property context => prop_get | owner_set; 15 public property context => prop_get | owner_set;
16 private property _entryPoint => prop_all; 16 private property _entryPoint => prop_all;
17 } 17 }
18 18
19 sub CTOR { 19 sub CTOR {
20 my ($this) = @_; 20 my ($this) = @_;
21 21
22 $this->responseFactory('IMPL::Web::Application::Response') unless $this->responseFactory; 22 $this->responseFactory('IMPL::Web::Application::Response') unless $this->responseFactory;
23 $this->response( $this->responseFactory->new(query => $this->query) ); 23 $this->response( $this->responseFactory->new(query => $this->query) );
24 $this->context({}); 24 $this->context({});
25 } 25 }
26 26
27 sub Invoke { 27 sub Invoke {
28 my ($this) = @_; 28 my ($this) = @_;
29 29
30 if ($this->_entryPoint) { 30 if ($this->_entryPoint) {
31 $this->_entryPoint->(); 31 $this->_entryPoint->();
32 } else { 32 } else {
33 die new IMPL::InvalidOperationException("At least one handler is required"); 33 die new IMPL::InvalidOperationException("At least one handler is required");
34 } 34 }
35 } 35 }
36 36
37 sub ReinitResponse { 37 sub ReinitResponse {
38 my ($this) = @_; 38 my ($this) = @_;
39 39
40 die new IMPL::InvalidOperationException("Response already sent") if $this->response->isHeaderPrinted; 40 die new IMPL::InvalidOperationException("Response already sent") if $this->response->isHeaderPrinted;
41 41
42 $this->response->Discard; 42 $this->response->Discard;
43 $this->response($this->responseFactory->new(query => $this->query)); 43 $this->response($this->responseFactory->new(query => $this->query));
44 } 44 }
45 45
46 sub ChainHandler { 46 sub ChainHandler {
47 my ($this,$handler) = @_; 47 my ($this,$handler) = @_;
48 48
49 my $delegateNext = $this->_entryPoint(); 49 my $delegateNext = $this->_entryPoint();
50 50
51 if (ref $handler eq 'CODE') { 51 if (ref $handler eq 'CODE') {
52 $this->_entryPoint( sub { 52 $this->_entryPoint( sub {
53 $handler->($this,$delegateNext); 53 $handler->($this,$delegateNext);
54 } ); 54 } );
55 } elsif (ref $handler and UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) { 55 } elsif (ref $handler and UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) {
56 $this->_entryPoint( sub { 56 $this->_entryPoint( sub {
57 $handler->Invoke($this,$delegateNext); 57 $handler->Invoke($this,$delegateNext);
58 } ); 58 } );
59 } elsif ($handler and not ref $handler) { 59 } elsif ($handler and not ref $handler) {
60 60
61 if (my $method = $this->can($handler) ) { 61 if (my $method = $this->can($handler) ) {
62 $this->_entryPoint( sub { 62 $this->_entryPoint( sub {
63 $method->($this,$delegateNext); 63 $method->($this,$delegateNext);
64 } ); 64 } );
65 } else { 65 } else {
66 { 66 {
67 no strict 'refs'; 67 no strict 'refs';
68 eval "require $handler; 1;" or die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler,"Failed to load module") unless keys %{"${handler}::"}; 68 eval "require $handler; 1;" or die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler,"Failed to load module") unless keys %{"${handler}::"};
69 } 69 }
70 70
71 if (UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) { 71 if (UNIVERSAL::isa($handler,'IMPL::Web::QueryHandler')) {
72 $this->_entryPoint( sub { 72 $this->_entryPoint( sub {
73 $handler->Invoke($this,$delegateNext); 73 $handler->Invoke($this,$delegateNext);
74 } ); 74 } );
75 } else { 75 } else {
76 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler); 76 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler);
77 } 77 }
78 } 78 }
79 } else { 79 } else {
80 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler); 80 die new IMPL::InvalidArgumentException("An invalid handler supplied",$handler);
81 } 81 }
82 82
83 } 83 }
84 84
85 sub cookie { 85 sub cookie {
86 my ($this,$name,$rx) = @_; 86 my ($this,$name,$rx) = @_;
87 87
88 $this->_launder(scalar( $this->query->cookie($name) ), $rx ); 88 $this->_launder(scalar( $this->query->cookie($name) ), $rx );
89 } 89 }
90 90
91 sub param { 91 sub param {
92 my ($this,$name,$rx) = @_; 92 my ($this,$name,$rx) = @_;
93 93
94 $this->_launder(scalar( $this->query->param($name) ), $rx ); 94 $this->_launder(scalar( $this->query->param($name) ), $rx );
95 } 95 }
96 96
97 sub _launder { 97 sub _launder {
98 my ($this,$value,$rx) = @_; 98 my ($this,$value,$rx) = @_;
99 99
100 if ( $value ) { 100 if ( $value ) {
101 if ($rx) { 101 if ($rx) {
102 if ( my @result = ($value =~ m/$rx/) ) { 102 if ( my @result = ($value =~ m/$rx/) ) {
103 return @result > 1 ? \@result : $result[0]; 103 return @result > 1 ? \@result : $result[0];
104 } else { 104 } else {
105 return undef; 105 return undef;
106 } 106 }
107 } else { 107 } else {
108 return $value; 108 return $value;
109 } 109 }
110 } else { 110 } else {
111 return undef; 111 return undef;
112 } 112 }
113 } 113 }
114 114
115 1; 115 1;
116 116
117 __END__ 117 __END__
145 =begin code 145 =begin code
146 146
147 # the application creates a new Action object 147 # the application creates a new Action object
148 148
149 my $action = $application->actionFactory->new( 149 my $action = $application->actionFactory->new(
150 action => $application, # the application passes self 150 action => $application, # the application passes self
151 query => $query # current CGI query 151 query => $query # current CGI query
152 ); 152 );
153 153
154 # forms query handlers stack 154 # forms query handlers stack
155 155
156 $action->ChainHandler($_) foreach qw ( 156 $action->ChainHandler($_) foreach qw (
157 IMPL::Web::QueryHandler::SecCallToMethod 157 IMPL::Web::QueryHandler::SecCallToMethod
158 IMPL::Web::QueryHandler::AuthenticateCookie 158 IMPL::Web::QueryHandler::AuthenticateCookie
159 IMPL::Web::QueryHandler::PageFormat 159 IMPL::Web::QueryHandler::PageFormat
160 ); 160 );
161 161
162 # and finally invokes the action 162 # and finally invokes the action
163 163
164 $action->Invoke() { 164 $action->Invoke() {
165 165
166 # some internals 166 # some internals
167 167
168 IMPL::Web::QueryHandler::PageFormat->Invoke($action,$nextHandlerIsAuthHandler) { 168 IMPL::Web::QueryHandler::PageFormat->Invoke($action,$nextHandlerIsAuthHandler) {
169 169
170 #some internals 170 #some internals
171 171
172 my $result = $nextHandlerIsAuthHandler() { 172 my $result = $nextHandlerIsAuthHandler() {
173 173
174 # some internals 174 # some internals
175 175
176 IMPL::Web::QueryHandler::AuthenticateCookie->Invoke($action,$nextHandlerIsSecCall) { 176 IMPL::Web::QueryHandler::AuthenticateCookie->Invoke($action,$nextHandlerIsSecCall) {
177 177
178 # some internals 178 # some internals
179 # do auth and generate security $context 179 # do auth and generate security $context
180 180
181 # impersonate $context and call the next handler 181 # impersonate $context and call the next handler
182 return $context->Impersonate($nextHandlerIsSecCall) { 182 return $context->Impersonate($nextHandlerIsSecCall) {
183 183
184 # some internals 184 # some internals
185 185
186 IMPL::Web::QueryHandler::SecCallToMethod->Invoke($action,undef) { 186 IMPL::Web::QueryHandler::SecCallToMethod->Invoke($action,undef) {
187 187
188 # next handler isn't present as it is the last hanler 188 # next handler isn't present as it is the last hanler
189 189
190 # some internals 190 # some internals
191 # calculate the $method and the $target from CGI request 191 # calculate the $method and the $target from CGI request
192 192
193 IMPL::Security->AccessCheck($target,$method); 193 IMPL::Security->AccessCheck($target,$method);
194 return $target->$method(); 194 return $target->$method();
195 195
196 } 196 }
197 197
198 } 198 }
199 199
200 } 200 }
201 } 201 }
202 202
203 # some intenals 203 # some intenals
204 # formatted output to $action->response->streamBody 204 # formatted output to $action->response->streamBody
205 } 205 }
206 } 206 }
207 207
208 =end code 208 =end code
209 209
210 или как альтернатива может быть еще 210 или как альтернатива может быть еще