annotate Lib/IMPL/Web/Application/RestBaseResource.pm @ 200:a9dbe534d236

sync
author sergey
date Tue, 24 Apr 2012 02:34:49 +0400
parents
children 0c018a247c8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
200
sergey
parents:
diff changeset
1 package IMPL::Web::Application::RestBaseResource;
sergey
parents:
diff changeset
2 use strict;
sergey
parents:
diff changeset
3
sergey
parents:
diff changeset
4 use IMPL::lang qw(:declare :constants);
sergey
parents:
diff changeset
5 use IMPL::declare {
sergey
parents:
diff changeset
6 require => {
sergey
parents:
diff changeset
7 Exception => 'IMPL::Exception',
sergey
parents:
diff changeset
8 ArgumentException => '-IMPL::InvalidArgumentException',
sergey
parents:
diff changeset
9 NotImplException => '-IMPL::NotImplementedException',
sergey
parents:
diff changeset
10 ForbiddenException => 'IMPL::Web::ForbiddenException',
sergey
parents:
diff changeset
11 TTransform => '-IMPL::Transform',
sergey
parents:
diff changeset
12 TResolve => '-IMPL::Config::Resolve'
sergey
parents:
diff changeset
13 },
sergey
parents:
diff changeset
14 base => {
sergey
parents:
diff changeset
15 'IMPL::Object' => undef,
sergey
parents:
diff changeset
16 'IMPL::Object::Autofill' => '@_'
sergey
parents:
diff changeset
17 }
sergey
parents:
diff changeset
18 };
sergey
parents:
diff changeset
19
sergey
parents:
diff changeset
20
sergey
parents:
diff changeset
21 BEGIN {
sergey
parents:
diff changeset
22 public property id => PROP_GET | PROP_OWNERSET;
sergey
parents:
diff changeset
23 public property parent => PROP_GET | PROP_OWNERSET;
sergey
parents:
diff changeset
24 public property contract => PROP_GET | PROP_OWNERSET;
sergey
parents:
diff changeset
25 }
sergey
parents:
diff changeset
26
sergey
parents:
diff changeset
27 sub target {
sergey
parents:
diff changeset
28 shift;
sergey
parents:
diff changeset
29 }
sergey
parents:
diff changeset
30
sergey
parents:
diff changeset
31 sub CTOR {
sergey
parents:
diff changeset
32 my ($this) = @_;
sergey
parents:
diff changeset
33
sergey
parents:
diff changeset
34 die ArgumentException->new("id","Identifier is required for non-root resources") if $this->id and not length $this->id;
sergey
parents:
diff changeset
35 die ArgumentException->new("A contract is required") unless $this->contract;
sergey
parents:
diff changeset
36 }
sergey
parents:
diff changeset
37
sergey
parents:
diff changeset
38 sub GetHttpImpl {
sergey
parents:
diff changeset
39 my($this,$method) = @_;
sergey
parents:
diff changeset
40
sergey
parents:
diff changeset
41 my %map = (
sergey
parents:
diff changeset
42 GET => 'GetImpl',
sergey
parents:
diff changeset
43 PUT => 'PutImpl',
sergey
parents:
diff changeset
44 POST => 'PostImpl',
sergey
parents:
diff changeset
45 DELETE => 'DeleteImpl'
sergey
parents:
diff changeset
46 );
sergey
parents:
diff changeset
47
sergey
parents:
diff changeset
48 return $map{$method};
sergey
parents:
diff changeset
49 }
sergey
parents:
diff changeset
50
sergey
parents:
diff changeset
51 sub InvokeHttpMethod {
sergey
parents:
diff changeset
52 my ($this,$method,$childId,$action) = @_;
sergey
parents:
diff changeset
53
sergey
parents:
diff changeset
54 my $impl = $this->GetHttpImpl($method) || 'HttpFallbackImpl';
sergey
parents:
diff changeset
55
sergey
parents:
diff changeset
56 return $this->$impl($childId,$action);
sergey
parents:
diff changeset
57 }
sergey
parents:
diff changeset
58
sergey
parents:
diff changeset
59 sub GetImpl {
sergey
parents:
diff changeset
60 die NotImplException->new();
sergey
parents:
diff changeset
61 }
sergey
parents:
diff changeset
62
sergey
parents:
diff changeset
63 sub PutImpl {
sergey
parents:
diff changeset
64 die NotImplException->new();
sergey
parents:
diff changeset
65 }
sergey
parents:
diff changeset
66
sergey
parents:
diff changeset
67 sub PostImpl {
sergey
parents:
diff changeset
68 die NotImplException->new();
sergey
parents:
diff changeset
69 }
sergey
parents:
diff changeset
70
sergey
parents:
diff changeset
71 sub DeleteImpl {
sergey
parents:
diff changeset
72 die NotImplException->new();
sergey
parents:
diff changeset
73 }
sergey
parents:
diff changeset
74
sergey
parents:
diff changeset
75 sub HttpFallbackImpl {
sergey
parents:
diff changeset
76 die ForbiddenException->new();
sergey
parents:
diff changeset
77 }
sergey
parents:
diff changeset
78
sergey
parents:
diff changeset
79 sub InvokeMember {
sergey
parents:
diff changeset
80 my ($this,$method,$action) = @_;
sergey
parents:
diff changeset
81
sergey
parents:
diff changeset
82 die ArgumentException->new("method","No method information provided") unless $method;
sergey
parents:
diff changeset
83
sergey
parents:
diff changeset
84 #normalize method info
sergey
parents:
diff changeset
85 if (not ref $method) {
sergey
parents:
diff changeset
86 $method = {
sergey
parents:
diff changeset
87 method => $method
sergey
parents:
diff changeset
88 };
sergey
parents:
diff changeset
89 }
sergey
parents:
diff changeset
90
sergey
parents:
diff changeset
91 if (ref $method eq 'HASH') {
sergey
parents:
diff changeset
92 my $member = $method->{method} or die InvalidOpException->new("A member name isn't specified");
sergey
parents:
diff changeset
93 my @args;
sergey
parents:
diff changeset
94
sergey
parents:
diff changeset
95 if (my $params = $method->{parameters}) {
sergey
parents:
diff changeset
96 if (ref $params eq 'HASH') {
sergey
parents:
diff changeset
97 @args = map {
sergey
parents:
diff changeset
98 $_,
sergey
parents:
diff changeset
99 $this->MakeParameter($params->{$_},$action)
sergey
parents:
diff changeset
100 } keys %$params;
sergey
parents:
diff changeset
101 } elsif (ref $params eq 'ARRAY') {
sergey
parents:
diff changeset
102 @args = map $this->MakeParameter($_,$action), @$params;
sergey
parents:
diff changeset
103 } else {
sergey
parents:
diff changeset
104 @args = ($this->MakeParameter($params,$action));
sergey
parents:
diff changeset
105 }
sergey
parents:
diff changeset
106 }
sergey
parents:
diff changeset
107 return $this->target->$member(@args);
sergey
parents:
diff changeset
108 } elsif (ref $method eq TResolve) {
sergey
parents:
diff changeset
109 return $method->Invoke($this->target);
sergey
parents:
diff changeset
110 } elsif (ref $method eq 'CODE') {
sergey
parents:
diff changeset
111 return $method->($this->target,$action);
sergey
parents:
diff changeset
112 } else {
sergey
parents:
diff changeset
113 die InvalidOpException->new("Unsupported type of the method information", ref $method);
sergey
parents:
diff changeset
114 }
sergey
parents:
diff changeset
115 }
sergey
parents:
diff changeset
116
sergey
parents:
diff changeset
117 sub MakeParameter {
sergey
parents:
diff changeset
118 my ($this,$param,$action) = @_;
sergey
parents:
diff changeset
119
sergey
parents:
diff changeset
120 if ($param) {
sergey
parents:
diff changeset
121 if (is $param, TTransform ) {
sergey
parents:
diff changeset
122 return $param->Transform($this,$action->query);
sergey
parents:
diff changeset
123 } elsif ($param and not ref $param) {
sergey
parents:
diff changeset
124 my %std = (
sergey
parents:
diff changeset
125 id => $this->id,
sergey
parents:
diff changeset
126 action => $action,
sergey
parents:
diff changeset
127 query => $action->query
sergey
parents:
diff changeset
128 );
sergey
parents:
diff changeset
129
sergey
parents:
diff changeset
130 return $std{$param} || $action->query->param($param);
sergey
parents:
diff changeset
131 }
sergey
parents:
diff changeset
132 } else {
sergey
parents:
diff changeset
133 return undef;
sergey
parents:
diff changeset
134 }
sergey
parents:
diff changeset
135 }
sergey
parents:
diff changeset
136
sergey
parents:
diff changeset
137
sergey
parents:
diff changeset
138 1;