Mercurial > pub > Impl
comparison Lib/IMPL/Web/Application/CustomResourceContract.pm @ 229:47f77e6409f7
heavily reworked the resource model of the web application:
*some ResourcesContraact functionality moved to Resource
+Added CustomResource
*Corrected action handlers
| author | sergey |
|---|---|
| date | Sat, 29 Sep 2012 02:34:47 +0400 |
| parents | |
| children | 6d8092d8ce1b |
comparison
equal
deleted
inserted
replaced
| 228:431db7034a88 | 229:47f77e6409f7 |
|---|---|
| 1 package IMPL::Web::Application::CustomResourceContract; | |
| 2 use strict; | |
| 3 | |
| 4 use IMPL::lang qw(:constants); | |
| 5 use IMPL::declare { | |
| 6 require => { | |
| 7 NotAllowedException => 'IMPL::Web::NotAllowedException', | |
| 8 OperationContract => 'IMPL::Web::Application::OperationContract' | |
| 9 }, | |
| 10 base => [ | |
| 11 'IMPL::Web::Application::ResourceContract' => '@_' | |
| 12 ] | |
| 13 }; | |
| 14 | |
| 15 our %RESOURCE_BINDINGS = ( | |
| 16 GET => 'HttpGet', | |
| 17 POST => 'HttpPost', | |
| 18 PUT => 'HttpPut' | |
| 19 DELETE => 'HttpDelete', | |
| 20 HEAD => 'HttpHead' | |
| 21 ); | |
| 22 | |
| 23 sub CTOR { | |
| 24 my ($this) = @_; | |
| 25 | |
| 26 $this->verbs->{options} = OperationContract->new( binding => \&_HttpOptionsBinding ); | |
| 27 | |
| 28 while(my ($verb,$methodName) = each %RESOURCE_BINDINGS) { | |
| 29 $this->verbs->{lc($verb)} = OperationContract->new ( | |
| 30 binding => sub { | |
| 31 my ($resource,$action) = @_; | |
| 32 | |
| 33 if ($resource->can($methodName)) { | |
| 34 return $resource->$methodName($action); | |
| 35 } else { | |
| 36 die NotAllowedException->new(allow => join(',', _GetAllowedHttpMethods($resource))); | |
| 37 } | |
| 38 | |
| 39 } | |
| 40 ); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 sub _HttpOptionsBinding { | |
| 45 my ($resource) = @_; | |
| 46 | |
| 47 my @allow = _GetAllowedHttpMethods; | |
| 48 retrun HttpResponse->new( | |
| 49 status => '200 OK', | |
| 50 headers => { | |
| 51 allow => join ( ',', @allow ) | |
| 52 } | |
| 53 ); | |
| 54 } | |
| 55 | |
| 56 sub _GetAllowedHttpMethods { | |
| 57 my ($resource) = @_; | |
| 58 return grep $resource->can($RESOURCE_BINDINGS{$_}), values %RESOURCE_BINDINGS; | |
| 59 } | |
| 60 | |
| 61 1; | |
| 62 | |
| 63 __END__ | |
| 64 | |
| 65 =pod | |
| 66 | |
| 67 =head1 NAME | |
| 68 | |
| 69 C<IMPL::Web::Application::CustomResourceContract> - контракт для веб-ресурсов, | |
| 70 реальзуемых в коде см. C<IMPL::Web::Application::CustomResource}>. | |
| 71 | |
| 72 =head1 DESCRIPTION | |
| 73 | |
| 74 Данный класс не используется напрямую. | |
| 75 | |
| 76 =cut |
