Merge branch 'master' into roles-saner
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST.pm
1 #
2 # REST.pm
3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
5 #
6
7 package Catalyst::Action::REST;
8 use Moose;
9 use Class::Inspector;
10 use Moose::Util qw(does_role);
11 use Catalyst::RequestRole::REST;
12 use Catalyst::Controller::REST;
13 use namespace::clean -except => 'meta';
14
15 extends 'Catalyst::Action';
16
17 BEGIN { require 5.008001; }
18
19 our $VERSION = '0.74';
20
21 =head1 NAME
22
23 Catalyst::Action::REST - Automated REST Method Dispatching
24
25 =head1 SYNOPSIS
26
27     sub foo :Local :ActionClass('REST') {
28       ... do setup for HTTP method specific handlers ...
29     }
30
31     sub foo_GET {
32       ... do something for GET requests ...
33     }
34
35     sub foo_PUT {
36       ... do somethign for PUT requests ...
37     }
38
39 =head1 DESCRIPTION
40
41 This Action handles doing automatic method dispatching for REST requests.  It
42 takes a normal Catalyst action, and changes the dispatch to append an
43 underscore and method name.
44
45 For example, in the synopsis above, calling GET on "/foo" would result in
46 the foo_GET method being dispatched.
47
48 If a method is requested that is not implemented, this action will
49 return a status 405 (Method Not Found).  It will populate the "Allow" header
50 with the list of implemented request methods.  You can override this behavior
51 by implementing a custom 405 handler like so:
52
53    sub foo_not_implemented {
54       ... handle not implemented methods ...
55    }
56
57 If you do not provide an _OPTIONS subroutine, we will automatically respond
58 with a 200 OK.  The "Allow" header will be populated with the list of
59 implemented request methods.
60
61 It is likely that you really want to look at L<Catalyst::Controller::REST>,
62 which brings this class together with automatic Serialization of requests
63 and responses.
64
65 When you use this module, the request class will be changed to
66 L<Catalyst::Request::REST>.
67
68 =head1 METHODS
69
70 =over 4
71
72 =item dispatch
73
74 This method overrides the default dispatch mechanism to the re-dispatching
75 mechanism described above.
76
77 =cut
78
79 sub dispatch {
80     my $self = shift;
81     my $c    = shift;
82
83     Catalyst::RequestRole::REST->meta->apply($c->request)
84         unless does_role($c->request, 'Catalyst::RequestRole::REST');
85
86     my $controller = $c->component( $self->class );
87     my $rest_method = $self->name . "_" . uc( $c->request->method );
88
89     my ($code, $name);
90
91     # Common case, for foo_GET etc
92     if ($code = $controller->can($rest_method)) {
93         # Exceute normal action
94         $c->execute( $self->class, $self, @{ $c->req->args } );
95         $name = $rest_method;
96     }
97
98     # Generic handling for foo_OPTIONS
99     if (!$code && $c->request->method eq "OPTIONS") {
100         $name = $rest_method;
101         $code = sub { $self->_return_options($self->name, @_) };
102     }
103
104     # Otherwise, not implemented.
105     if (!$code) {
106         $name = $self->name . "_not_implemented";
107         $code = $controller->can($name) # User method
108             # Generic not implemented
109             || sub { $self->_return_not_implemented($self->name, @_) };
110     }
111
112     # localise stuff so we can dispatch the action 'as normal, but get
113     # different stats shown, and different code run.
114     local $self->{code} = $code;
115     local $self->{reverse} = $name;
116
117     $c->execute( $self->class, $self, @{ $c->req->args } );
118 }
119
120 sub _get_allowed_methods {
121     my ( $self, $controller, $c, $name ) = @_;
122     my $class = ref($controller) ? ref($controller) : $controller;
123     my $methods    = Class::Inspector->methods($class);
124     my @allowed;
125     foreach my $method ( @{$methods} ) {
126         if ( $method =~ /^$name\_(.+)$/ ) {
127             push( @allowed, $1 );
128         }
129     }
130     return @allowed;
131 };
132
133 sub _return_options {
134     my ( $self, $method_name, $controller, $c) = @_;
135     my @allowed = $self->_get_allowed_methods($controller, $c, $method_name);
136     $c->response->content_type('text/plain');
137     $c->response->status(200);
138     $c->response->header( 'Allow' => \@allowed );
139 }
140
141 sub _return_not_implemented {
142     my ( $self, $method_name, $controller, $c ) = @_;
143
144     my @allowed = $self->_get_allowed_methods($controller, $c, $method_name);
145     $c->response->content_type('text/plain');
146     $c->response->status(405);
147     $c->response->header( 'Allow' => \@allowed );
148     $c->response->body( "Method "
149           . $c->request->method
150           . " not implemented for "
151           . $c->uri_for( $method_name ) );
152 }
153
154 1;
155
156 =back
157
158 =head1 SEE ALSO
159
160 You likely want to look at L<Catalyst::Controller::REST>, which implements
161 a sensible set of defaults for a controller doing REST.
162
163 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
164
165 =head1 TROUBLESHOOTING
166
167 =over 4
168
169 =item Q: I'm getting a "415 Unsupported Media Type" error. What gives?!
170
171 A:  Most likely, you haven't set Content-type equal to "application/json", or
172 one of the accepted return formats.  You can do this by setting it in your query
173 accepted return formats.  You can do this by setting it in your query string
174 thusly: C<< ?content-type=application%2Fjson (where %2F == / uri escaped). >>
175
176 B<NOTE> Apache will refuse %2F unless configured otherise.
177 Make sure C<< AllowEncodedSlashes On >> is in your httpd.conf file in orde
178 for this to run smoothly.
179
180 =back
181
182 =head1 CONTRIBUTORS
183
184 Christopher Laco
185
186 Luke Saunders
187
188 John Goulah
189
190 Daisuke Maki <daisuke@endeworks.jp>
191
192 J. Shirley <jshirley@gmail.com>
193
194 Hans Dieter Pearcey
195
196 Tomas Doran (t0m) <bobtfish@bobtfish.net>
197
198 =head1 AUTHOR
199
200 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
201
202 Marchex, Inc. paid me while I developed this module. (L<http://www.marchex.com>)
203
204 =head1 LICENSE
205
206 You may distribute this code under the same terms as Perl itself.
207
208 =cut
209