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