Commit | Line | Data |
256c894f |
1 | # |
2 | # REST.pm |
be3c588a |
3 | # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com> |
256c894f |
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'; |
7ad87df9 |
14 | use Class::Inspector; |
d4611771 |
15 | use Catalyst; |
9a76221e |
16 | use Catalyst::Request::REST; |
ebba5325 |
17 | use Catalyst::Controller::REST; |
18 | |
9c5c9bd1 |
19 | BEGIN { require 5.008001; } |
256c894f |
20 | |
fec6d454 |
21 | our $VERSION = '0.67'; |
9a76221e |
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') |
2f91bf68 |
27 | unless Catalyst->request_class->isa('Catalyst::Request::REST'); |
7328f0ab |
28 | |
29 | =head1 NAME |
30 | |
31 | Catalyst::Action::REST - Automated REST Method Dispatching |
32 | |
33 | =head1 SYNOPSIS |
34 | |
5e87ec47 |
35 | sub foo :Local :ActionClass('REST') { |
36 | ... do setup for HTTP method specific handlers ... |
37 | } |
7328f0ab |
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 |
5e87ec47 |
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. |
7328f0ab |
68 | |
5e87ec47 |
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. |
398c5a1b |
72 | |
9a76221e |
73 | When you use this module, the request class will be changed to |
74 | L<Catalyst::Request::REST>. |
75 | |
7328f0ab |
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 |
d34c067a |
86 | |
256c894f |
87 | sub dispatch { |
bb4130f6 |
88 | my $self = shift; |
d34c067a |
89 | my $c = shift; |
256c894f |
90 | |
2f91bf68 |
91 | my $controller = $c->component( $self->class ); |
eccb2137 |
92 | my $method = $self->name . "_" . uc( $c->request->method ); |
93 | if ( $controller->can($method) ) { |
d34c067a |
94 | $c->execute( $self->class, $self, @{ $c->req->args } ); |
95 | return $controller->$method( $c, @{ $c->req->args } ); |
256c894f |
96 | } else { |
d34c067a |
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 | } |
256c894f |
107 | } |
108 | } |
109 | |
d34c067a |
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 { |
7ad87df9 |
120 | my ( $self, $c ) = @_; |
121 | |
122 | my $controller = $self->class; |
eccb2137 |
123 | my $methods = Class::Inspector->methods($controller); |
7ad87df9 |
124 | my @allowed; |
eccb2137 |
125 | foreach my $method ( @{$methods} ) { |
7ad87df9 |
126 | my $name = $self->name; |
eccb2137 |
127 | if ( $method =~ /^$name\_(.+)$/ ) { |
128 | push( @allowed, $1 ); |
7ad87df9 |
129 | } |
130 | } |
d34c067a |
131 | return @allowed; |
132 | } |
133 | |
134 | sub _return_not_implemented { |
135 | my ( $self, $c ) = @_; |
136 | |
137 | my @allowed = $self->_get_allowed_methods($c); |
7ad87df9 |
138 | $c->response->content_type('text/plain'); |
139 | $c->response->status(405); |
eccb2137 |
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 ) ); |
7ad87df9 |
145 | } |
146 | |
256c894f |
147 | 1; |
7328f0ab |
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 | |
5d7480da |
158 | =head1 TROUBLESHOOTING |
159 | |
160 | =over 4 |
161 | |
162 | =item Q: I'm getting a "415 Unsupported Media Type" error. What gives?! |
163 | |
164 | A: Most likely, you haven't set Content-type equal to "application/json", or one of the |
165 | accepted return formats. You can do this by setting it in your query string thusly: |
6df7d0ae |
166 | ?content-type=application%2Fjson (where %2F == / uri escaped). |
167 | |
168 | **NOTE** Apache will refuse %2F unless configured otherise. |
169 | Make sure AllowEncodedSlashes On is in your httpd.conf file in order for this to run smoothly. |
5d7480da |
170 | |
171 | =cut |
172 | |
173 | =cut |
174 | |
175 | |
176 | |
177 | |
2f7533ed |
178 | =head1 MAINTAINER |
7328f0ab |
179 | |
2f7533ed |
180 | J. Shirley <jshirley@gmail.com> |
7328f0ab |
181 | |
2f7533ed |
182 | =head1 CONTRIBUTORS |
398c5a1b |
183 | |
2f7533ed |
184 | Christopher Laco |
185 | |
186 | Luke Saunders |
187 | |
188 | John Goulah |
33e5de96 |
189 | |
190 | Daisuke Maki <daisuke@endeworks.jp> |
191 | |
2f7533ed |
192 | =head1 AUTHOR |
193 | |
194 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
195 | |
196 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
197 | |
7328f0ab |
198 | =head1 LICENSE |
199 | |
200 | You may distribute this code under the same terms as Perl itself. |
201 | |
202 | =cut |
d34c067a |
203 | |