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