3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
7 package Catalyst::Action::REST;
10 use Moose::Util qw(does_role);
11 use Catalyst::RequestRole::REST;
12 use Catalyst::Controller::REST;
13 use namespace::clean -except => 'meta';
15 extends 'Catalyst::Action';
17 BEGIN { require 5.008001; }
19 our $VERSION = '0.76';
20 $VERSION = eval $VERSION;
24 Catalyst::Action::REST - Automated REST Method Dispatching
28 sub foo :Local :ActionClass('REST') {
29 ... do setup for HTTP method specific handlers ...
33 ... do something for GET requests ...
37 ... do somethign for PUT requests ...
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.
46 For example, in the synopsis above, calling GET on "/foo" would result in
47 the foo_GET method being dispatched.
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:
54 sub foo_not_implemented {
55 ... handle not implemented methods ...
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.
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
66 When you use this module, the request class will be changed to
67 L<Catalyst::Request::REST>.
75 This method overrides the default dispatch mechanism to the re-dispatching
76 mechanism described above.
84 Catalyst::RequestRole::REST->meta->apply($c->request)
85 unless does_role($c->request, 'Catalyst::RequestRole::REST');
87 my $controller = $c->component( $self->class );
88 my $rest_method = $self->name . "_" . uc( $c->request->method );
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 } );
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, @_) };
105 # Otherwise, not implemented.
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, @_) };
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;
118 $c->execute( $self->class, $self, @{ $c->req->args } );
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);
126 foreach my $method ( @{$methods} ) {
127 if ( $method =~ /^$name\_(.+)$/ ) {
128 push( @allowed, $1 );
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 );
142 sub _return_not_implemented {
143 my ( $self, $method_name, $controller, $c ) = @_;
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 ) );
161 You likely want to look at L<Catalyst::Controller::REST>, which implements
162 a sensible set of defaults for a controller doing REST.
164 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
166 =head1 TROUBLESHOOTING
170 =item Q: I'm getting a "415 Unsupported Media Type" error. What gives?!
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). >>
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.
191 Daisuke Maki <daisuke@endeworks.jp>
193 J. Shirley <jshirley@gmail.com>
197 Tomas Doran (t0m) <bobtfish@bobtfish.net>
201 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
203 Marchex, Inc. paid me while I developed this module. (L<http://www.marchex.com>)
207 You may distribute this code under the same terms as Perl itself.