r1144@mbp: claco | 2008-01-03 19:43:42 -0500
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST.pm
1 #
2 # REST.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
5 #
6 # $Id$
7
8 package Catalyst::Action::REST;
9
10 use strict;
11 use warnings;
12
13 use base 'Catalyst::Action';
14 use Class::Inspector;
15 use Catalyst;
16 use Catalyst::Request::REST;
17 use 5.8.1;
18
19 our
20    $VERSION = '0.60';
21
22 # This is wrong in several ways. First, there's no guarantee that
23 # Catalyst.pm has not been subclassed. Two, there's no guarantee that
24 # the user isn't already using their request subclass.
25 Catalyst->request_class('Catalyst::Request::REST')
26   unless Catalyst->request_class->isa('Catalyst::Request::REST');
27
28 =head1 NAME
29
30 Catalyst::Action::REST - Automated REST Method Dispatching
31
32 =head1 SYNOPSIS
33
34     sub foo :Local :ActionClass('REST') {
35       ... do setup for HTTP method specific handlers ...
36     }
37
38     sub foo_GET { 
39       ... do something for GET requests ...
40     }
41
42     sub foo_PUT { 
43       ... do somethign for PUT requests ...
44     }
45
46 =head1 DESCRIPTION
47
48 This Action handles doing automatic method dispatching for REST requests.  It
49 takes a normal Catalyst action, and changes the dispatch to append an
50 underscore and method name. 
51
52 For example, in the synopsis above, calling GET on "/foo" would result in
53 the foo_GET method being dispatched.
54
55 If a method is requested that is not implemented, this action will 
56 return a status 405 (Method Not Found).  It will populate the "Allow" header 
57 with the list of implemented request methods.  You can override this behavior
58 by implementing a custom 405 handler like so:
59
60    sub foo_not_implemented {
61       ... handle not implemented methods ...
62    }
63
64 If you do not provide an _OPTIONS subroutine, we will automatically respond
65 with a 200 OK.  The "Allow" header will be populated with the list of
66 implemented request methods.
67
68 It is likely that you really want to look at L<Catalyst::Controller::REST>,
69 which brings this class together with automatic Serialization of requests
70 and responses.
71
72 When you use this module, the request class will be changed to
73 L<Catalyst::Request::REST>.
74
75 =head1 METHODS
76
77 =over 4
78
79 =item dispatch
80
81 This method overrides the default dispatch mechanism to the re-dispatching
82 mechanism described above.
83
84 =cut
85
86 sub dispatch {
87     my $self = shift;
88     my $c    = shift;
89
90     my $controller = $c->component( $self->class );
91     my $method     = $self->name . "_" . uc( $c->request->method );
92     if ( $controller->can($method) ) {
93         $c->execute( $self->class, $self, @{ $c->req->args } );
94         return $controller->$method( $c, @{ $c->req->args } );
95     } else {
96         if ( $c->request->method eq "OPTIONS" ) {
97             return $self->_return_options($c);
98         } else {
99             my $handle_ni = $self->name . "_not_implemented";
100             if ( $controller->can($handle_ni) ) {
101                 return $controller->$handle_ni( $c, @{ $c->req->args } );
102             } else {
103                 return $self->_return_not_implemented($c);
104             }
105         }
106     }
107 }
108
109 sub _return_options {
110     my ( $self, $c ) = @_;
111
112     my @allowed = $self->_get_allowed_methods($c);
113     $c->response->content_type('text/plain');
114     $c->response->status(200);
115     $c->response->header( 'Allow' => \@allowed );
116 }
117
118 sub _get_allowed_methods {
119     my ( $self, $c ) = @_;
120
121     my $controller = $self->class;
122     my $methods    = Class::Inspector->methods($controller);
123     my @allowed;
124     foreach my $method ( @{$methods} ) {
125         my $name = $self->name;
126         if ( $method =~ /^$name\_(.+)$/ ) {
127             push( @allowed, $1 );
128         }
129     }
130     return @allowed;
131 }
132
133 sub _return_not_implemented {
134     my ( $self, $c ) = @_;
135
136     my @allowed = $self->_get_allowed_methods($c);
137     $c->response->content_type('text/plain');
138     $c->response->status(405);
139     $c->response->header( 'Allow' => \@allowed );
140     $c->response->body( "Method "
141           . $c->request->method
142           . " not implemented for "
143           . $c->uri_for( $self->reverse ) );
144 }
145
146 1;
147
148 =back
149
150 =head1 SEE ALSO
151
152 You likely want to look at L<Catalyst::Controller::REST>, which implements
153 a sensible set of defaults for a controller doing REST.
154
155 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
156
157 =head1 AUTHOR
158
159 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
160
161 Marchex, Inc. paid me while I developed this module.  (http://www.marchex.com)
162
163 =head1 CONTRIBUTERS
164
165 Daisuke Maki <daisuke@endeworks.jp>
166
167 =head1 LICENSE
168
169 You may distribute this code under the same terms as Perl itself.
170
171 =cut
172