Commit | Line | Data |
256c894f |
1 | package Catalyst::Controller::REST; |
a66af307 |
2 | use strict; |
3 | use warnings; |
256c894f |
4 | |
eaa7cec1 |
5 | our $VERSION = '0.78'; |
a66af307 |
6 | $VERSION = eval $VERSION; |
832e768d |
7 | |
398c5a1b |
8 | =head1 NAME |
9 | |
db8bb647 |
10 | Catalyst::Controller::REST - A RESTful controller |
398c5a1b |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | package Foo::Controller::Bar; |
15 | |
16 | use base 'Catalyst::Controller::REST'; |
17 | |
18 | sub thing : Local : ActionClass('REST') { } |
19 | |
20 | # Answer GET requests to "thing" |
21 | sub thing_GET { |
22 | my ( $self, $c ) = @_; |
db8bb647 |
23 | |
398c5a1b |
24 | # Return a 200 OK, with the data in entity |
db8bb647 |
25 | # serialized in the body |
398c5a1b |
26 | $self->status_ok( |
db8bb647 |
27 | $c, |
398c5a1b |
28 | entity => { |
29 | some => 'data', |
30 | foo => 'is real bar-y', |
31 | }, |
32 | ); |
33 | } |
34 | |
35 | # Answer PUT requests to "thing" |
db8bb647 |
36 | sub thing_PUT { |
95318468 |
37 | ... some action ... |
398c5a1b |
38 | } |
39 | |
40 | =head1 DESCRIPTION |
41 | |
42 | Catalyst::Controller::REST implements a mechanism for building |
43 | RESTful services in Catalyst. It does this by extending the |
db8bb647 |
44 | normal Catalyst dispatch mechanism to allow for different |
45 | subroutines to be called based on the HTTP Method requested, |
398c5a1b |
46 | while also transparently handling all the serialization/deserialization for |
47 | you. |
48 | |
49 | This is probably best served by an example. In the above |
50 | controller, we have declared a Local Catalyst action on |
db8bb647 |
51 | "sub thing", and have used the ActionClass('REST'). |
398c5a1b |
52 | |
53 | Below, we have declared "thing_GET" and "thing_PUT". Any |
db8bb647 |
54 | GET requests to thing will be dispatched to "thing_GET", |
55 | while any PUT requests will be dispatched to "thing_PUT". |
398c5a1b |
56 | |
e601adda |
57 | Any unimplemented HTTP methods will be met with a "405 Method Not Allowed" |
58 | response, automatically containing the proper list of available methods. You |
59 | can override this behavior through implementing a custom |
db8bb647 |
60 | C<thing_not_implemented> method. |
e601adda |
61 | |
62 | If you do not provide an OPTIONS handler, we will respond to any OPTIONS |
63 | requests with a "200 OK", populating the Allowed header automatically. |
64 | |
65 | Any data included in C<< $c->stash->{'rest'} >> will be serialized for you. |
66 | The serialization format will be selected based on the content-type |
67 | of the incoming request. It is probably easier to use the L<STATUS HELPERS>, |
68 | which are described below. |
398c5a1b |
69 | |
70 | The HTTP POST, PUT, and OPTIONS methods will all automatically deserialize the |
71 | contents of $c->request->body based on the requests content-type header. |
72 | A list of understood serialization formats is below. |
73 | |
e601adda |
74 | If we do not have (or cannot run) a serializer for a given content-type, a 415 |
db8bb647 |
75 | "Unsupported Media Type" error is generated. |
398c5a1b |
76 | |
77 | To make your Controller RESTful, simply have it |
78 | |
95318468 |
79 | BEGIN {extends 'Catalyst::Controller::REST'; } |
80 | |
81 | Or if you use pre-Moose Catalyst versions, |
82 | |
83 | use parent 'Catalyst::Controller::REST'; |
84 | |
398c5a1b |
85 | |
86 | =head1 SERIALIZATION |
87 | |
88 | Catalyst::Controller::REST will automatically serialize your |
e601adda |
89 | responses, and deserialize any POST, PUT or OPTIONS requests. It evaluates |
90 | which serializer to use by mapping a content-type to a Serialization module. |
db8bb647 |
91 | We select the content-type based on: |
e601adda |
92 | |
93 | =over 2 |
94 | |
95 | =item B<The Content-Type Header> |
96 | |
97 | If the incoming HTTP Request had a Content-Type header set, we will use it. |
98 | |
99 | =item B<The content-type Query Parameter> |
100 | |
101 | If this is a GET request, you can supply a content-type query parameter. |
102 | |
103 | =item B<Evaluating the Accept Header> |
104 | |
105 | Finally, if the client provided an Accept header, we will evaluate |
db8bb647 |
106 | it and use the best-ranked choice. |
e601adda |
107 | |
108 | =back |
109 | |
95318468 |
110 | |
e601adda |
111 | =head1 AVAILABLE SERIALIZERS |
112 | |
113 | A given serialization mechanism is only available if you have the underlying |
114 | modules installed. For example, you can't use XML::Simple if it's not already |
db8bb647 |
115 | installed. |
e601adda |
116 | |
95318468 |
117 | In addition, each serializer has its quirks in terms of what sorts of data |
e601adda |
118 | structures it will properly handle. L<Catalyst::Controller::REST> makes |
db8bb647 |
119 | no attempt to save you from yourself in this regard. :) |
e601adda |
120 | |
121 | =over 2 |
122 | |
95318468 |
123 | =item * C<text/x-yaml> => C<YAML::Syck> |
e601adda |
124 | |
125 | Returns YAML generated by L<YAML::Syck>. |
126 | |
95318468 |
127 | =item * C<text/html> => C<YAML::HTML> |
e601adda |
128 | |
129 | This uses L<YAML::Syck> and L<URI::Find> to generate YAML with all URLs turned |
130 | to hyperlinks. Only useable for Serialization. |
131 | |
95318468 |
132 | =item * C<application/json> => C<JSON> |
e601adda |
133 | |
db8bb647 |
134 | Uses L<JSON> to generate JSON output. It is strongly advised to also have |
e540a1fa |
135 | L<JSON::XS> installed. The C<text/x-json> content type is supported but is |
136 | deprecated and you will receive warnings in your log. |
e601adda |
137 | |
95318468 |
138 | =item * C<text/x-data-dumper> => C<Data::Serializer> |
e601adda |
139 | |
140 | Uses the L<Data::Serializer> module to generate L<Data::Dumper> output. |
141 | |
95318468 |
142 | =item * C<text/x-data-denter> => C<Data::Serializer> |
e601adda |
143 | |
144 | Uses the L<Data::Serializer> module to generate L<Data::Denter> output. |
145 | |
95318468 |
146 | =item * C<text/x-data-taxi> => C<Data::Serializer> |
e601adda |
147 | |
148 | Uses the L<Data::Serializer> module to generate L<Data::Taxi> output. |
149 | |
95318468 |
150 | =item * C<application/x-storable> => C<Data::Serializer> |
e601adda |
151 | |
152 | Uses the L<Data::Serializer> module to generate L<Storable> output. |
153 | |
95318468 |
154 | =item * C<application/x-freezethaw> => C<Data::Serializer> |
e601adda |
155 | |
156 | Uses the L<Data::Serializer> module to generate L<FreezeThaw> output. |
157 | |
95318468 |
158 | =item * C<text/x-config-general> => C<Data::Serializer> |
e601adda |
159 | |
160 | Uses the L<Data::Serializer> module to generate L<Config::General> output. |
161 | |
95318468 |
162 | =item * C<text/x-php-serialization> => C<Data::Serializer> |
e601adda |
163 | |
164 | Uses the L<Data::Serializer> module to generate L<PHP::Serialization> output. |
165 | |
95318468 |
166 | =item * C<text/xml> => C<XML::Simple> |
e601adda |
167 | |
168 | Uses L<XML::Simple> to generate XML output. This is probably not suitable |
169 | for any real heavy XML work. Due to L<XML::Simple>s requirement that the data |
170 | you serialize be a HASHREF, we transform outgoing data to be in the form of: |
171 | |
172 | { data => $yourdata } |
173 | |
95318468 |
174 | =item * L<View> |
9a76221e |
175 | |
db8bb647 |
176 | Uses a regular Catalyst view. For example, if you wanted to have your |
3d8a0645 |
177 | C<text/html> and C<text/xml> views rendered by TT, set: |
178 | |
179 | __PACKAGE__->config( |
180 | map => { |
181 | 'text/html' => [ 'View', 'TT' ], |
182 | 'text/xml' => [ 'View', 'XML' ], |
183 | } |
184 | ); |
185 | |
186 | Your views should have a C<process> method like this: |
187 | |
188 | sub process { |
189 | my ( $self, $c, $stash_key ) = @_; |
190 | |
191 | my $output; |
192 | eval { |
193 | $output = $self->serialize( $c->stash->{$stash_key} ); |
194 | }; |
195 | return $@ if $@; |
196 | |
197 | $c->response->body( $output ); |
198 | return 1; # important |
199 | } |
200 | |
201 | sub serialize { |
202 | my ( $self, $data ) = @_; |
203 | |
204 | my $serialized = ... process $data here ... |
205 | |
206 | return $serialized; |
207 | } |
208 | |
9a76221e |
209 | |
e601adda |
210 | =back |
211 | |
95318468 |
212 | By default, L<Catalyst::Controller::REST> will return a |
213 | C<415 Unsupported Media Type> response if an attempt to use an unsupported |
214 | content-type is made. You can ensure that something is always returned by |
215 | setting the C<default> config option: |
398c5a1b |
216 | |
21d3f6ae |
217 | __PACKAGE__->config->{'default'} = 'text/x-yaml'; |
398c5a1b |
218 | |
95318468 |
219 | would make it always fall back to the serializer plugin defined for |
220 | C<text/x-yaml>. |
398c5a1b |
221 | |
e601adda |
222 | =head1 CUSTOM SERIALIZERS |
223 | |
95318468 |
224 | Implementing new Serialization formats is easy! Contributions |
225 | are most welcome! If you would like to implement a custom serializer, |
226 | you should create two new modules in the L<Catalyst::Action::Serialize> |
227 | and L<Catalyst::Action::Deserialize> namespace. Then assign your new |
228 | class to the content-type's you want, and you're done. |
229 | |
230 | See L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize> |
231 | for more information. |
e601adda |
232 | |
398c5a1b |
233 | =head1 STATUS HELPERS |
234 | |
e601adda |
235 | Since so much of REST is in using HTTP, we provide these Status Helpers. |
236 | Using them will ensure that you are responding with the proper codes, |
237 | headers, and entities. |
238 | |
398c5a1b |
239 | These helpers try and conform to the HTTP 1.1 Specification. You can |
db8bb647 |
240 | refer to it at: L<http://www.w3.org/Protocols/rfc2616/rfc2616.txt>. |
398c5a1b |
241 | These routines are all implemented as regular subroutines, and as |
242 | such require you pass the current context ($c) as the first argument. |
243 | |
244 | =over 4 |
245 | |
246 | =cut |
247 | |
256c894f |
248 | use base 'Catalyst::Controller'; |
d4611771 |
249 | use Params::Validate qw(SCALAR OBJECT); |
256c894f |
250 | |
251 | __PACKAGE__->mk_accessors(qw(serialize)); |
252 | |
253 | __PACKAGE__->config( |
e540a1fa |
254 | 'stash_key' => 'rest', |
255 | 'map' => { |
256 | 'text/html' => 'YAML::HTML', |
257 | 'text/xml' => 'XML::Simple', |
258 | 'text/x-yaml' => 'YAML', |
259 | 'application/json' => 'JSON', |
260 | 'text/x-json' => 'JSON', |
261 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
262 | 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], |
263 | 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], |
95318468 |
264 | 'application/x-storable' => [ 'Data::Serializer', 'Storable' ], |
265 | 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw' ], |
266 | 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], |
e540a1fa |
267 | 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], |
268 | }, |
256c894f |
269 | ); |
270 | |
e540a1fa |
271 | sub begin : ActionClass('Deserialize') { } |
5511d1ff |
272 | |
0ba73721 |
273 | sub end : ActionClass('Serialize') { } |
274 | |
398c5a1b |
275 | =item status_ok |
276 | |
277 | Returns a "200 OK" response. Takes an "entity" to serialize. |
278 | |
279 | Example: |
280 | |
281 | $self->status_ok( |
db8bb647 |
282 | $c, |
398c5a1b |
283 | entity => { |
284 | radiohead => "Is a good band!", |
285 | } |
286 | ); |
287 | |
288 | =cut |
289 | |
290 | sub status_ok { |
291 | my $self = shift; |
e601adda |
292 | my $c = shift; |
d4611771 |
293 | my %p = Params::Validate::validate( @_, { entity => 1, }, ); |
398c5a1b |
294 | |
295 | $c->response->status(200); |
e601adda |
296 | $self->_set_entity( $c, $p{'entity'} ); |
398c5a1b |
297 | return 1; |
298 | } |
299 | |
300 | =item status_created |
301 | |
302 | Returns a "201 CREATED" response. Takes an "entity" to serialize, |
303 | and a "location" where the created object can be found. |
304 | |
305 | Example: |
306 | |
307 | $self->status_created( |
db8bb647 |
308 | $c, |
398c5a1b |
309 | location => $c->req->uri->as_string, |
310 | entity => { |
311 | radiohead => "Is a good band!", |
312 | } |
313 | ); |
314 | |
315 | In the above example, we use the requested URI as our location. |
316 | This is probably what you want for most PUT requests. |
317 | |
318 | =cut |
bb4130f6 |
319 | |
5511d1ff |
320 | sub status_created { |
321 | my $self = shift; |
e601adda |
322 | my $c = shift; |
d4611771 |
323 | my %p = Params::Validate::validate( |
e601adda |
324 | @_, |
5511d1ff |
325 | { |
e601adda |
326 | location => { type => SCALAR | OBJECT }, |
327 | entity => { optional => 1 }, |
5511d1ff |
328 | }, |
329 | ); |
256c894f |
330 | |
5511d1ff |
331 | my $location; |
e601adda |
332 | if ( ref( $p{'location'} ) ) { |
5511d1ff |
333 | $location = $p{'location'}->as_string; |
33e5de96 |
334 | } else { |
335 | $location = $p{'location'}; |
5511d1ff |
336 | } |
337 | $c->response->status(201); |
e601adda |
338 | $c->response->header( 'Location' => $location ); |
339 | $self->_set_entity( $c, $p{'entity'} ); |
bb4130f6 |
340 | return 1; |
341 | } |
342 | |
398c5a1b |
343 | =item status_accepted |
344 | |
345 | Returns a "202 ACCEPTED" response. Takes an "entity" to serialize. |
346 | |
347 | Example: |
348 | |
349 | $self->status_accepted( |
db8bb647 |
350 | $c, |
398c5a1b |
351 | entity => { |
352 | status => "queued", |
353 | } |
354 | ); |
355 | |
356 | =cut |
e601adda |
357 | |
398c5a1b |
358 | sub status_accepted { |
bb4130f6 |
359 | my $self = shift; |
e601adda |
360 | my $c = shift; |
d4611771 |
361 | my %p = Params::Validate::validate( @_, { entity => 1, }, ); |
bb4130f6 |
362 | |
398c5a1b |
363 | $c->response->status(202); |
e601adda |
364 | $self->_set_entity( $c, $p{'entity'} ); |
bb4130f6 |
365 | return 1; |
366 | } |
367 | |
bbf0feae |
368 | =item status_no_content |
369 | |
370 | Returns a "204 NO CONTENT" response. |
371 | |
372 | =cut |
373 | |
374 | sub status_no_content { |
375 | my $self = shift; |
376 | my $c = shift; |
377 | $c->response->status(204); |
378 | $self->_set_entity( $c, undef ); |
379 | return 1.; |
380 | } |
381 | |
398c5a1b |
382 | =item status_bad_request |
383 | |
384 | Returns a "400 BAD REQUEST" response. Takes a "message" argument |
385 | as a scalar, which will become the value of "error" in the serialized |
386 | response. |
387 | |
388 | Example: |
389 | |
390 | $self->status_bad_request( |
db8bb647 |
391 | $c, |
33e5de96 |
392 | message => "Cannot do what you have asked!", |
398c5a1b |
393 | ); |
394 | |
395 | =cut |
e601adda |
396 | |
cc186a5b |
397 | sub status_bad_request { |
398 | my $self = shift; |
e601adda |
399 | my $c = shift; |
d4611771 |
400 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
cc186a5b |
401 | |
402 | $c->response->status(400); |
faf5c20b |
403 | $c->log->debug( "Status Bad Request: " . $p{'message'} ) if $c->debug; |
e601adda |
404 | $self->_set_entity( $c, { error => $p{'message'} } ); |
cc186a5b |
405 | return 1; |
406 | } |
407 | |
398c5a1b |
408 | =item status_not_found |
409 | |
410 | Returns a "404 NOT FOUND" response. Takes a "message" argument |
411 | as a scalar, which will become the value of "error" in the serialized |
412 | response. |
413 | |
414 | Example: |
415 | |
416 | $self->status_not_found( |
db8bb647 |
417 | $c, |
33e5de96 |
418 | message => "Cannot find what you were looking for!", |
398c5a1b |
419 | ); |
420 | |
421 | =cut |
e601adda |
422 | |
bb4130f6 |
423 | sub status_not_found { |
424 | my $self = shift; |
e601adda |
425 | my $c = shift; |
d4611771 |
426 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
bb4130f6 |
427 | |
428 | $c->response->status(404); |
faf5c20b |
429 | $c->log->debug( "Status Not Found: " . $p{'message'} ) if $c->debug; |
e601adda |
430 | $self->_set_entity( $c, { error => $p{'message'} } ); |
bb4130f6 |
431 | return 1; |
432 | } |
433 | |
bbf0feae |
434 | =item gone |
435 | |
436 | Returns a "41O GONE" response. Takes a "message" argument as a scalar, |
437 | which will become the value of "error" in the serialized response. |
438 | |
439 | Example: |
440 | |
441 | $self->status_gone( |
442 | $c, |
443 | message => "The document have been deleted by foo", |
444 | ); |
445 | |
446 | =cut |
447 | |
448 | sub status_gone { |
449 | my $self = shift; |
450 | my $c = shift; |
451 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
452 | |
453 | $c->response->status(410); |
454 | $c->log->debug( "Status Gone " . $p{'message'} ) if $c->debug; |
455 | $self->_set_entity( $c, { error => $p{'message'} } ); |
456 | return 1; |
457 | } |
458 | |
bb4130f6 |
459 | sub _set_entity { |
e601adda |
460 | my $self = shift; |
461 | my $c = shift; |
bb4130f6 |
462 | my $entity = shift; |
e601adda |
463 | if ( defined($entity) ) { |
faf5c20b |
464 | $c->stash->{ $self->{'stash_key'} } = $entity; |
5511d1ff |
465 | } |
466 | return 1; |
eccb2137 |
467 | } |
256c894f |
468 | |
398c5a1b |
469 | =back |
470 | |
471 | =head1 MANUAL RESPONSES |
472 | |
473 | If you want to construct your responses yourself, all you need to |
474 | do is put the object you want serialized in $c->stash->{'rest'}. |
475 | |
e601adda |
476 | =head1 IMPLEMENTATION DETAILS |
477 | |
478 | This Controller ties together L<Catalyst::Action::REST>, |
479 | L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>. It should be suitable for most applications. You should be aware that it: |
480 | |
481 | =over 4 |
482 | |
483 | =item Configures the Serialization Actions |
484 | |
485 | This class provides a default configuration for Serialization. It is currently: |
486 | |
487 | __PACKAGE__->config( |
95318468 |
488 | 'stash_key' => 'rest', |
489 | 'map' => { |
490 | 'text/html' => 'YAML::HTML', |
491 | 'text/xml' => 'XML::Simple', |
492 | 'text/x-yaml' => 'YAML', |
493 | 'application/json' => 'JSON', |
494 | 'text/x-json' => 'JSON', |
495 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
496 | 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], |
497 | 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], |
498 | 'application/x-storable' => [ 'Data::Serializer', 'Storable' ], |
499 | 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw' ], |
500 | 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], |
501 | 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], |
502 | }, |
e601adda |
503 | ); |
504 | |
505 | You can read the full set of options for this configuration block in |
506 | L<Catalyst::Action::Serialize>. |
507 | |
508 | =item Sets a C<begin> and C<end> method for you |
509 | |
510 | The C<begin> method uses L<Catalyst::Action::Deserialize>. The C<end> |
511 | method uses L<Catalyst::Action::Serialize>. If you want to override |
512 | either behavior, simply implement your own C<begin> and C<end> actions |
def65dcc |
513 | and use MRO::Compat: |
e601adda |
514 | |
515 | my Foo::Controller::Monkey; |
516 | use base qw(Catalyst::Controller::REST); |
517 | |
518 | sub begin :Private { |
519 | my ($self, $c) = @_; |
db8bb647 |
520 | ... do things before Deserializing ... |
521 | $self->maybe::next::method($c); |
e601adda |
522 | ... do things after Deserializing ... |
db8bb647 |
523 | } |
e601adda |
524 | |
525 | sub end :Private { |
526 | my ($self, $c) = @_; |
db8bb647 |
527 | ... do things before Serializing ... |
def65dcc |
528 | $self->maybe::next::method($c); |
e601adda |
529 | ... do things after Serializing ... |
530 | } |
531 | |
e540a1fa |
532 | =back |
533 | |
e601adda |
534 | =head1 A MILD WARNING |
535 | |
536 | I have code in production using L<Catalyst::Controller::REST>. That said, |
537 | it is still under development, and it's possible that things may change |
538 | between releases. I promise to not break things unneccesarily. :) |
539 | |
398c5a1b |
540 | =head1 SEE ALSO |
541 | |
542 | L<Catalyst::Action::REST>, L<Catalyst::Action::Serialize>, |
543 | L<Catalyst::Action::Deserialize> |
544 | |
545 | For help with REST in general: |
546 | |
547 | The HTTP 1.1 Spec is required reading. http://www.w3.org/Protocols/rfc2616/rfc2616.txt |
548 | |
549 | Wikipedia! http://en.wikipedia.org/wiki/Representational_State_Transfer |
550 | |
551 | The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage |
552 | |
553 | =head1 AUTHOR |
554 | |
555 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
556 | |
557 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
558 | |
e540a1fa |
559 | =head1 MAINTAINER |
560 | |
561 | J. Shirley <jshirley@cpan.org> |
562 | |
398c5a1b |
563 | =head1 LICENSE |
564 | |
565 | You may distribute this code under the same terms as Perl itself. |
566 | |
567 | =cut |
568 | |
256c894f |
569 | 1; |