Commit | Line | Data |
256c894f |
1 | package Catalyst::Controller::REST; |
930013e6 |
2 | use Moose; |
3 | use namespace::autoclean; |
256c894f |
4 | |
0e1381b4 |
5 | our $VERSION = '0.96'; |
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; |
5cb5f6bb |
15 | use Moose; |
16 | use namespace::autoclean; |
259c53c7 |
17 | |
5cb5f6bb |
18 | BEGIN { extends 'Catalyst::Controller::REST' } |
398c5a1b |
19 | |
20 | sub thing : Local : ActionClass('REST') { } |
21 | |
22 | # Answer GET requests to "thing" |
23 | sub thing_GET { |
24 | my ( $self, $c ) = @_; |
db8bb647 |
25 | |
398c5a1b |
26 | # Return a 200 OK, with the data in entity |
db8bb647 |
27 | # serialized in the body |
398c5a1b |
28 | $self->status_ok( |
db8bb647 |
29 | $c, |
398c5a1b |
30 | entity => { |
31 | some => 'data', |
32 | foo => 'is real bar-y', |
33 | }, |
34 | ); |
35 | } |
36 | |
37 | # Answer PUT requests to "thing" |
db8bb647 |
38 | sub thing_PUT { |
ace04991 |
39 | my ( $self, $c ) = @_; |
40 | |
fcf45ed9 |
41 | $radiohead = $c->req->data->{radiohead}; |
259c53c7 |
42 | |
10bcd217 |
43 | $self->status_created( |
44 | $c, |
259c53c7 |
45 | location => $c->req->uri, |
10bcd217 |
46 | entity => { |
47 | radiohead => $radiohead, |
48 | } |
49 | ); |
259c53c7 |
50 | } |
398c5a1b |
51 | |
52 | =head1 DESCRIPTION |
53 | |
54 | Catalyst::Controller::REST implements a mechanism for building |
55 | RESTful services in Catalyst. It does this by extending the |
db8bb647 |
56 | normal Catalyst dispatch mechanism to allow for different |
57 | subroutines to be called based on the HTTP Method requested, |
398c5a1b |
58 | while also transparently handling all the serialization/deserialization for |
59 | you. |
60 | |
61 | This is probably best served by an example. In the above |
62 | controller, we have declared a Local Catalyst action on |
db8bb647 |
63 | "sub thing", and have used the ActionClass('REST'). |
398c5a1b |
64 | |
65 | Below, we have declared "thing_GET" and "thing_PUT". Any |
db8bb647 |
66 | GET requests to thing will be dispatched to "thing_GET", |
67 | while any PUT requests will be dispatched to "thing_PUT". |
398c5a1b |
68 | |
e601adda |
69 | Any unimplemented HTTP methods will be met with a "405 Method Not Allowed" |
70 | response, automatically containing the proper list of available methods. You |
71 | can override this behavior through implementing a custom |
db8bb647 |
72 | C<thing_not_implemented> method. |
e601adda |
73 | |
74 | If you do not provide an OPTIONS handler, we will respond to any OPTIONS |
75 | requests with a "200 OK", populating the Allowed header automatically. |
76 | |
77 | Any data included in C<< $c->stash->{'rest'} >> will be serialized for you. |
78 | The serialization format will be selected based on the content-type |
79 | of the incoming request. It is probably easier to use the L<STATUS HELPERS>, |
80 | which are described below. |
398c5a1b |
81 | |
10bcd217 |
82 | "The HTTP POST, PUT, and OPTIONS methods will all automatically |
83 | L<deserialize|Catalyst::Action::Deserialize> the contents of |
259c53c7 |
84 | C<< $c->request->body >> into the C<< $c->request->data >> hashref", based on |
10bcd217 |
85 | the request's C<Content-type> header. A list of understood serialization |
86 | formats is L<below|/AVAILABLE SERIALIZERS>. |
398c5a1b |
87 | |
e601adda |
88 | If we do not have (or cannot run) a serializer for a given content-type, a 415 |
db8bb647 |
89 | "Unsupported Media Type" error is generated. |
398c5a1b |
90 | |
91 | To make your Controller RESTful, simply have it |
92 | |
5cb5f6bb |
93 | BEGIN { extends 'Catalyst::Controller::REST' } |
398c5a1b |
94 | |
9cd203c9 |
95 | =head1 CONFIGURATION |
96 | |
97 | See L<Catalyst::Action::Serialize/CONFIGURATION>. Note that the C<serialize> |
98 | key has been deprecated. |
99 | |
398c5a1b |
100 | =head1 SERIALIZATION |
101 | |
102 | Catalyst::Controller::REST will automatically serialize your |
e601adda |
103 | responses, and deserialize any POST, PUT or OPTIONS requests. It evaluates |
104 | which serializer to use by mapping a content-type to a Serialization module. |
db8bb647 |
105 | We select the content-type based on: |
e601adda |
106 | |
5cb5f6bb |
107 | =over |
e601adda |
108 | |
109 | =item B<The Content-Type Header> |
110 | |
111 | If the incoming HTTP Request had a Content-Type header set, we will use it. |
112 | |
113 | =item B<The content-type Query Parameter> |
114 | |
115 | If this is a GET request, you can supply a content-type query parameter. |
116 | |
117 | =item B<Evaluating the Accept Header> |
118 | |
119 | Finally, if the client provided an Accept header, we will evaluate |
db8bb647 |
120 | it and use the best-ranked choice. |
e601adda |
121 | |
122 | =back |
123 | |
124 | =head1 AVAILABLE SERIALIZERS |
125 | |
126 | A given serialization mechanism is only available if you have the underlying |
127 | modules installed. For example, you can't use XML::Simple if it's not already |
db8bb647 |
128 | installed. |
e601adda |
129 | |
95318468 |
130 | In addition, each serializer has its quirks in terms of what sorts of data |
e601adda |
131 | structures it will properly handle. L<Catalyst::Controller::REST> makes |
db8bb647 |
132 | no attempt to save you from yourself in this regard. :) |
e601adda |
133 | |
134 | =over 2 |
135 | |
95318468 |
136 | =item * C<text/x-yaml> => C<YAML::Syck> |
e601adda |
137 | |
138 | Returns YAML generated by L<YAML::Syck>. |
139 | |
95318468 |
140 | =item * C<text/html> => C<YAML::HTML> |
e601adda |
141 | |
142 | This uses L<YAML::Syck> and L<URI::Find> to generate YAML with all URLs turned |
26b59bcb |
143 | to hyperlinks. Only usable for Serialization. |
e601adda |
144 | |
95318468 |
145 | =item * C<application/json> => C<JSON> |
e601adda |
146 | |
db8bb647 |
147 | Uses L<JSON> to generate JSON output. It is strongly advised to also have |
e540a1fa |
148 | L<JSON::XS> installed. The C<text/x-json> content type is supported but is |
149 | deprecated and you will receive warnings in your log. |
e601adda |
150 | |
838f49dc |
151 | You can also add a hash in your controller config to pass options to the json object. |
152 | For instance, to relax permissions when deserializing input, add: |
153 | __PACKAGE__->config( |
154 | json_options => { relaxed => 1 } |
155 | ) |
156 | |
d0d292d4 |
157 | =item * C<text/javascript> => C<JSONP> |
158 | |
159 | If a callback=? parameter is passed, this returns javascript in the form of: $callback($serializedJSON); |
160 | |
92d78e8f |
161 | Note - this is disabled by default as it can be a security risk if you are unaware. |
162 | |
163 | The usual MIME types for this serialization format are: 'text/javascript', 'application/x-javascript', |
164 | 'application/javascript'. |
165 | |
95318468 |
166 | =item * C<text/x-data-dumper> => C<Data::Serializer> |
e601adda |
167 | |
168 | Uses the L<Data::Serializer> module to generate L<Data::Dumper> output. |
169 | |
95318468 |
170 | =item * C<text/x-data-denter> => C<Data::Serializer> |
e601adda |
171 | |
172 | Uses the L<Data::Serializer> module to generate L<Data::Denter> output. |
173 | |
95318468 |
174 | =item * C<text/x-data-taxi> => C<Data::Serializer> |
e601adda |
175 | |
176 | Uses the L<Data::Serializer> module to generate L<Data::Taxi> output. |
177 | |
95318468 |
178 | =item * C<application/x-storable> => C<Data::Serializer> |
e601adda |
179 | |
180 | Uses the L<Data::Serializer> module to generate L<Storable> output. |
181 | |
95318468 |
182 | =item * C<application/x-freezethaw> => C<Data::Serializer> |
e601adda |
183 | |
184 | Uses the L<Data::Serializer> module to generate L<FreezeThaw> output. |
185 | |
95318468 |
186 | =item * C<text/x-config-general> => C<Data::Serializer> |
e601adda |
187 | |
188 | Uses the L<Data::Serializer> module to generate L<Config::General> output. |
189 | |
95318468 |
190 | =item * C<text/x-php-serialization> => C<Data::Serializer> |
e601adda |
191 | |
192 | Uses the L<Data::Serializer> module to generate L<PHP::Serialization> output. |
193 | |
95318468 |
194 | =item * C<text/xml> => C<XML::Simple> |
e601adda |
195 | |
196 | Uses L<XML::Simple> to generate XML output. This is probably not suitable |
197 | for any real heavy XML work. Due to L<XML::Simple>s requirement that the data |
198 | you serialize be a HASHREF, we transform outgoing data to be in the form of: |
199 | |
200 | { data => $yourdata } |
201 | |
95318468 |
202 | =item * L<View> |
9a76221e |
203 | |
db8bb647 |
204 | Uses a regular Catalyst view. For example, if you wanted to have your |
3d8a0645 |
205 | C<text/html> and C<text/xml> views rendered by TT, set: |
206 | |
207 | __PACKAGE__->config( |
208 | map => { |
209 | 'text/html' => [ 'View', 'TT' ], |
210 | 'text/xml' => [ 'View', 'XML' ], |
211 | } |
5cb5f6bb |
212 | ); |
3d8a0645 |
213 | |
214 | Your views should have a C<process> method like this: |
215 | |
216 | sub process { |
217 | my ( $self, $c, $stash_key ) = @_; |
5cb5f6bb |
218 | |
3d8a0645 |
219 | my $output; |
220 | eval { |
221 | $output = $self->serialize( $c->stash->{$stash_key} ); |
222 | }; |
223 | return $@ if $@; |
5cb5f6bb |
224 | |
3d8a0645 |
225 | $c->response->body( $output ); |
226 | return 1; # important |
227 | } |
259c53c7 |
228 | |
3d8a0645 |
229 | sub serialize { |
230 | my ( $self, $data ) = @_; |
5cb5f6bb |
231 | |
3d8a0645 |
232 | my $serialized = ... process $data here ... |
5cb5f6bb |
233 | |
3d8a0645 |
234 | return $serialized; |
235 | } |
9a76221e |
236 | |
178f8470 |
237 | =item * Callback |
238 | |
239 | For infinite flexibility, you can provide a callback for the |
240 | deserialization/serialization steps. |
241 | |
242 | __PACKAGE__->config( |
243 | map => { |
244 | 'text/xml' => [ 'Callback', { deserialize => \&parse_xml, serialize => \&render_xml } ], |
245 | } |
246 | ); |
247 | |
248 | The C<deserialize> callback is passed a string that is the body of the |
249 | request and is expected to return a scalar value that results from |
250 | the deserialization. The C<serialize> callback is passed the data |
251 | structure that needs to be serialized and must return a string suitable |
252 | for returning in the HTTP response. In addition to receiving the scalar |
253 | to act on, both callbacks are passed the controller object and the context |
254 | (i.e. C<$c>) as the second and third arguments. |
255 | |
e601adda |
256 | =back |
257 | |
259c53c7 |
258 | By default, L<Catalyst::Controller::REST> will return a |
95318468 |
259 | C<415 Unsupported Media Type> response if an attempt to use an unsupported |
260 | content-type is made. You can ensure that something is always returned by |
261 | setting the C<default> config option: |
398c5a1b |
262 | |
5cb5f6bb |
263 | __PACKAGE__->config(default => 'text/x-yaml'); |
398c5a1b |
264 | |
95318468 |
265 | would make it always fall back to the serializer plugin defined for |
266 | C<text/x-yaml>. |
398c5a1b |
267 | |
e601adda |
268 | =head1 CUSTOM SERIALIZERS |
269 | |
95318468 |
270 | Implementing new Serialization formats is easy! Contributions |
259c53c7 |
271 | are most welcome! If you would like to implement a custom serializer, |
95318468 |
272 | you should create two new modules in the L<Catalyst::Action::Serialize> |
273 | and L<Catalyst::Action::Deserialize> namespace. Then assign your new |
274 | class to the content-type's you want, and you're done. |
275 | |
259c53c7 |
276 | See L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize> |
95318468 |
277 | for more information. |
e601adda |
278 | |
398c5a1b |
279 | =head1 STATUS HELPERS |
280 | |
e601adda |
281 | Since so much of REST is in using HTTP, we provide these Status Helpers. |
282 | Using them will ensure that you are responding with the proper codes, |
283 | headers, and entities. |
284 | |
398c5a1b |
285 | These helpers try and conform to the HTTP 1.1 Specification. You can |
db8bb647 |
286 | refer to it at: L<http://www.w3.org/Protocols/rfc2616/rfc2616.txt>. |
398c5a1b |
287 | These routines are all implemented as regular subroutines, and as |
288 | such require you pass the current context ($c) as the first argument. |
289 | |
5cb5f6bb |
290 | =over |
398c5a1b |
291 | |
292 | =cut |
293 | |
930013e6 |
294 | BEGIN { extends 'Catalyst::Controller' } |
d4611771 |
295 | use Params::Validate qw(SCALAR OBJECT); |
256c894f |
296 | |
297 | __PACKAGE__->mk_accessors(qw(serialize)); |
298 | |
299 | __PACKAGE__->config( |
e540a1fa |
300 | 'stash_key' => 'rest', |
301 | 'map' => { |
302 | 'text/html' => 'YAML::HTML', |
303 | 'text/xml' => 'XML::Simple', |
304 | 'text/x-yaml' => 'YAML', |
305 | 'application/json' => 'JSON', |
306 | 'text/x-json' => 'JSON', |
307 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
308 | 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], |
309 | 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], |
95318468 |
310 | 'application/x-storable' => [ 'Data::Serializer', 'Storable' ], |
311 | 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw' ], |
312 | 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], |
e540a1fa |
313 | 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], |
314 | }, |
256c894f |
315 | ); |
316 | |
e540a1fa |
317 | sub begin : ActionClass('Deserialize') { } |
5511d1ff |
318 | |
0ba73721 |
319 | sub end : ActionClass('Serialize') { } |
320 | |
398c5a1b |
321 | =item status_ok |
322 | |
323 | Returns a "200 OK" response. Takes an "entity" to serialize. |
324 | |
325 | Example: |
326 | |
327 | $self->status_ok( |
db8bb647 |
328 | $c, |
398c5a1b |
329 | entity => { |
330 | radiohead => "Is a good band!", |
331 | } |
332 | ); |
333 | |
334 | =cut |
335 | |
336 | sub status_ok { |
337 | my $self = shift; |
e601adda |
338 | my $c = shift; |
d4611771 |
339 | my %p = Params::Validate::validate( @_, { entity => 1, }, ); |
398c5a1b |
340 | |
341 | $c->response->status(200); |
e601adda |
342 | $self->_set_entity( $c, $p{'entity'} ); |
398c5a1b |
343 | return 1; |
344 | } |
345 | |
346 | =item status_created |
347 | |
348 | Returns a "201 CREATED" response. Takes an "entity" to serialize, |
349 | and a "location" where the created object can be found. |
350 | |
351 | Example: |
352 | |
353 | $self->status_created( |
db8bb647 |
354 | $c, |
259c53c7 |
355 | location => $c->req->uri, |
398c5a1b |
356 | entity => { |
357 | radiohead => "Is a good band!", |
358 | } |
359 | ); |
360 | |
361 | In the above example, we use the requested URI as our location. |
362 | This is probably what you want for most PUT requests. |
363 | |
364 | =cut |
bb4130f6 |
365 | |
5511d1ff |
366 | sub status_created { |
367 | my $self = shift; |
e601adda |
368 | my $c = shift; |
d4611771 |
369 | my %p = Params::Validate::validate( |
e601adda |
370 | @_, |
5511d1ff |
371 | { |
e601adda |
372 | location => { type => SCALAR | OBJECT }, |
373 | entity => { optional => 1 }, |
5511d1ff |
374 | }, |
375 | ); |
256c894f |
376 | |
5511d1ff |
377 | $c->response->status(201); |
259c53c7 |
378 | $c->response->header( 'Location' => $p{location} ); |
e601adda |
379 | $self->_set_entity( $c, $p{'entity'} ); |
bb4130f6 |
380 | return 1; |
381 | } |
382 | |
398c5a1b |
383 | =item status_accepted |
384 | |
385 | Returns a "202 ACCEPTED" response. Takes an "entity" to serialize. |
259c53c7 |
386 | Also takes optional "location" for queue type scenarios. |
398c5a1b |
387 | |
388 | Example: |
389 | |
390 | $self->status_accepted( |
db8bb647 |
391 | $c, |
259c53c7 |
392 | location => $c->req->uri, |
398c5a1b |
393 | entity => { |
394 | status => "queued", |
395 | } |
396 | ); |
397 | |
398 | =cut |
e601adda |
399 | |
398c5a1b |
400 | sub status_accepted { |
bb4130f6 |
401 | my $self = shift; |
e601adda |
402 | my $c = shift; |
259c53c7 |
403 | my %p = Params::Validate::validate( |
404 | @_, |
405 | { |
406 | location => { type => SCALAR | OBJECT, optional => 1 }, |
407 | entity => 1, |
408 | }, |
409 | ); |
bb4130f6 |
410 | |
398c5a1b |
411 | $c->response->status(202); |
259c53c7 |
412 | $c->response->header( 'Location' => $p{location} ) if exists $p{location}; |
e601adda |
413 | $self->_set_entity( $c, $p{'entity'} ); |
bb4130f6 |
414 | return 1; |
415 | } |
416 | |
bbf0feae |
417 | =item status_no_content |
418 | |
419 | Returns a "204 NO CONTENT" response. |
420 | |
421 | =cut |
422 | |
423 | sub status_no_content { |
424 | my $self = shift; |
425 | my $c = shift; |
426 | $c->response->status(204); |
427 | $self->_set_entity( $c, undef ); |
042656b6 |
428 | return 1; |
bbf0feae |
429 | } |
430 | |
bdff70a9 |
431 | =item status_multiple_choices |
432 | |
433 | Returns a "300 MULTIPLE CHOICES" response. Takes an "entity" to serialize, which should |
434 | provide list of possible locations. Also takes optional "location" for preferred choice. |
435 | |
436 | =cut |
437 | |
438 | sub status_multiple_choices { |
439 | my $self = shift; |
440 | my $c = shift; |
441 | my %p = Params::Validate::validate( |
442 | @_, |
443 | { |
444 | entity => 1, |
445 | location => { type => SCALAR | OBJECT, optional => 1 }, |
446 | }, |
447 | ); |
448 | |
bdff70a9 |
449 | $c->response->status(300); |
259c53c7 |
450 | $c->response->header( 'Location' => $p{location} ) if exists $p{'location'}; |
bdff70a9 |
451 | $self->_set_entity( $c, $p{'entity'} ); |
452 | return 1; |
453 | } |
454 | |
e52456a4 |
455 | =item status_found |
456 | |
457 | Returns a "302 FOUND" response. Takes an "entity" to serialize. |
259c53c7 |
458 | Also takes optional "location". |
e52456a4 |
459 | |
460 | =cut |
461 | |
462 | sub status_found { |
463 | my $self = shift; |
464 | my $c = shift; |
465 | my %p = Params::Validate::validate( |
466 | @_, |
467 | { |
468 | entity => 1, |
469 | location => { type => SCALAR | OBJECT, optional => 1 }, |
470 | }, |
471 | ); |
472 | |
e52456a4 |
473 | $c->response->status(302); |
259c53c7 |
474 | $c->response->header( 'Location' => $p{location} ) if exists $p{'location'}; |
e52456a4 |
475 | $self->_set_entity( $c, $p{'entity'} ); |
476 | return 1; |
477 | } |
478 | |
398c5a1b |
479 | =item status_bad_request |
480 | |
481 | Returns a "400 BAD REQUEST" response. Takes a "message" argument |
482 | as a scalar, which will become the value of "error" in the serialized |
483 | response. |
484 | |
485 | Example: |
486 | |
487 | $self->status_bad_request( |
db8bb647 |
488 | $c, |
33e5de96 |
489 | message => "Cannot do what you have asked!", |
398c5a1b |
490 | ); |
491 | |
492 | =cut |
e601adda |
493 | |
cc186a5b |
494 | sub status_bad_request { |
495 | my $self = shift; |
e601adda |
496 | my $c = shift; |
d4611771 |
497 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
cc186a5b |
498 | |
499 | $c->response->status(400); |
faf5c20b |
500 | $c->log->debug( "Status Bad Request: " . $p{'message'} ) if $c->debug; |
e601adda |
501 | $self->_set_entity( $c, { error => $p{'message'} } ); |
cc186a5b |
502 | return 1; |
503 | } |
504 | |
550807bc |
505 | =item status_forbidden |
506 | |
507 | Returns a "403 FORBIDDEN" response. Takes a "message" argument |
508 | as a scalar, which will become the value of "error" in the serialized |
509 | response. |
510 | |
511 | Example: |
512 | |
513 | $self->status_forbidden( |
514 | $c, |
515 | message => "access denied", |
516 | ); |
517 | |
518 | =cut |
519 | |
520 | sub status_forbidden { |
521 | my $self = shift; |
522 | my $c = shift; |
523 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
524 | |
525 | $c->response->status(403); |
526 | $c->log->debug( "Status Forbidden: " . $p{'message'} ) if $c->debug; |
527 | $self->_set_entity( $c, { error => $p{'message'} } ); |
528 | return 1; |
529 | } |
530 | |
398c5a1b |
531 | =item status_not_found |
532 | |
533 | Returns a "404 NOT FOUND" response. Takes a "message" argument |
534 | as a scalar, which will become the value of "error" in the serialized |
535 | response. |
536 | |
537 | Example: |
538 | |
539 | $self->status_not_found( |
db8bb647 |
540 | $c, |
33e5de96 |
541 | message => "Cannot find what you were looking for!", |
398c5a1b |
542 | ); |
543 | |
544 | =cut |
e601adda |
545 | |
bb4130f6 |
546 | sub status_not_found { |
547 | my $self = shift; |
e601adda |
548 | my $c = shift; |
d4611771 |
549 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
bb4130f6 |
550 | |
551 | $c->response->status(404); |
faf5c20b |
552 | $c->log->debug( "Status Not Found: " . $p{'message'} ) if $c->debug; |
e601adda |
553 | $self->_set_entity( $c, { error => $p{'message'} } ); |
bb4130f6 |
554 | return 1; |
555 | } |
556 | |
bbf0feae |
557 | =item gone |
558 | |
559 | Returns a "41O GONE" response. Takes a "message" argument as a scalar, |
560 | which will become the value of "error" in the serialized response. |
561 | |
562 | Example: |
563 | |
564 | $self->status_gone( |
565 | $c, |
566 | message => "The document have been deleted by foo", |
567 | ); |
568 | |
569 | =cut |
570 | |
571 | sub status_gone { |
572 | my $self = shift; |
573 | my $c = shift; |
574 | my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); |
575 | |
576 | $c->response->status(410); |
577 | $c->log->debug( "Status Gone " . $p{'message'} ) if $c->debug; |
578 | $self->_set_entity( $c, { error => $p{'message'} } ); |
579 | return 1; |
580 | } |
581 | |
bb4130f6 |
582 | sub _set_entity { |
e601adda |
583 | my $self = shift; |
584 | my $c = shift; |
bb4130f6 |
585 | my $entity = shift; |
e601adda |
586 | if ( defined($entity) ) { |
faf5c20b |
587 | $c->stash->{ $self->{'stash_key'} } = $entity; |
5511d1ff |
588 | } |
589 | return 1; |
eccb2137 |
590 | } |
256c894f |
591 | |
398c5a1b |
592 | =back |
593 | |
594 | =head1 MANUAL RESPONSES |
595 | |
596 | If you want to construct your responses yourself, all you need to |
597 | do is put the object you want serialized in $c->stash->{'rest'}. |
598 | |
e601adda |
599 | =head1 IMPLEMENTATION DETAILS |
600 | |
601 | This Controller ties together L<Catalyst::Action::REST>, |
602 | L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>. It should be suitable for most applications. You should be aware that it: |
603 | |
604 | =over 4 |
605 | |
606 | =item Configures the Serialization Actions |
607 | |
608 | This class provides a default configuration for Serialization. It is currently: |
609 | |
610 | __PACKAGE__->config( |
95318468 |
611 | 'stash_key' => 'rest', |
612 | 'map' => { |
613 | 'text/html' => 'YAML::HTML', |
614 | 'text/xml' => 'XML::Simple', |
615 | 'text/x-yaml' => 'YAML', |
616 | 'application/json' => 'JSON', |
617 | 'text/x-json' => 'JSON', |
618 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
619 | 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], |
620 | 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], |
621 | 'application/x-storable' => [ 'Data::Serializer', 'Storable' ], |
622 | 'application/x-freezethaw' => [ 'Data::Serializer', 'FreezeThaw' ], |
623 | 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], |
624 | 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], |
625 | }, |
e601adda |
626 | ); |
627 | |
628 | You can read the full set of options for this configuration block in |
629 | L<Catalyst::Action::Serialize>. |
630 | |
631 | =item Sets a C<begin> and C<end> method for you |
632 | |
633 | The C<begin> method uses L<Catalyst::Action::Deserialize>. The C<end> |
634 | method uses L<Catalyst::Action::Serialize>. If you want to override |
635 | either behavior, simply implement your own C<begin> and C<end> actions |
355d4385 |
636 | and forward to another action with the Serialize and/or Deserialize |
637 | action classes: |
e601adda |
638 | |
10bcd217 |
639 | package Foo::Controller::Monkey; |
640 | use Moose; |
641 | use namespace::autoclean; |
355d4385 |
642 | |
10bcd217 |
643 | BEGIN { extends 'Catalyst::Controller::REST' } |
e601adda |
644 | |
355d4385 |
645 | sub begin : Private { |
e601adda |
646 | my ($self, $c) = @_; |
db8bb647 |
647 | ... do things before Deserializing ... |
355d4385 |
648 | $c->forward('deserialize'); |
e601adda |
649 | ... do things after Deserializing ... |
db8bb647 |
650 | } |
e601adda |
651 | |
355d4385 |
652 | sub deserialize : ActionClass('Deserialize') {} |
653 | |
e601adda |
654 | sub end :Private { |
655 | my ($self, $c) = @_; |
db8bb647 |
656 | ... do things before Serializing ... |
355d4385 |
657 | $c->forward('serialize'); |
e601adda |
658 | ... do things after Serializing ... |
659 | } |
660 | |
355d4385 |
661 | sub serialize : ActionClass('Serialize') {} |
662 | |
8bf1f20e |
663 | If you need to deserialize multipart requests (i.e. REST data in |
664 | one part and file uploads in others) you can do so by using the |
665 | L<Catalyst::Action::DeserializeMultiPart> action class. |
666 | |
e540a1fa |
667 | =back |
668 | |
e601adda |
669 | =head1 A MILD WARNING |
670 | |
671 | I have code in production using L<Catalyst::Controller::REST>. That said, |
672 | it is still under development, and it's possible that things may change |
d6ece98c |
673 | between releases. I promise to not break things unnecessarily. :) |
e601adda |
674 | |
398c5a1b |
675 | =head1 SEE ALSO |
676 | |
677 | L<Catalyst::Action::REST>, L<Catalyst::Action::Serialize>, |
678 | L<Catalyst::Action::Deserialize> |
679 | |
680 | For help with REST in general: |
681 | |
682 | The HTTP 1.1 Spec is required reading. http://www.w3.org/Protocols/rfc2616/rfc2616.txt |
683 | |
684 | Wikipedia! http://en.wikipedia.org/wiki/Representational_State_Transfer |
685 | |
686 | The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage |
687 | |
5cb5f6bb |
688 | =head1 AUTHORS |
e540a1fa |
689 | |
5cb5f6bb |
690 | See L<Catalyst::Action::REST> for authors. |
e540a1fa |
691 | |
398c5a1b |
692 | =head1 LICENSE |
693 | |
694 | You may distribute this code under the same terms as Perl itself. |
695 | |
696 | =cut |
697 | |
24748286 |
698 | __PACKAGE__->meta->make_immutable; |
699 | |
256c894f |
700 | 1; |