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