Commit | Line | Data |
7ad87df9 |
1 | # |
2 | # Catlyst::Action::Deserialize |
be3c588a |
3 | # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com> |
7ad87df9 |
4 | # |
5 | # $Id$ |
6 | |
7 | package Catalyst::Action::Deserialize; |
8 | |
9 | use strict; |
10 | use warnings; |
11 | |
e601adda |
12 | use base 'Catalyst::Action::SerializeBase'; |
7ad87df9 |
13 | use Module::Pluggable::Object; |
def65dcc |
14 | use MRO::Compat; |
7ad87df9 |
15 | |
16 | __PACKAGE__->mk_accessors(qw(plugins)); |
17 | |
18 | sub execute { |
19 | my $self = shift; |
e601adda |
20 | my ( $controller, $c ) = @_; |
7ad87df9 |
21 | |
7ad87df9 |
22 | my @demethods = qw(POST PUT OPTIONS); |
eccb2137 |
23 | my $method = $c->request->method; |
24 | if ( grep /^$method$/, @demethods ) { |
9a76221e |
25 | my ( $sclass, $sarg, $content_type ) = |
26 | $self->_load_content_plugins( 'Catalyst::Action::Deserialize', |
27 | $controller, $c ); |
28 | return 1 unless defined($sclass); |
e601adda |
29 | my $rc; |
eccb2137 |
30 | if ( defined($sarg) ) { |
e601adda |
31 | $rc = $sclass->execute( $controller, $c, $sarg ); |
7ad87df9 |
32 | } else { |
e601adda |
33 | $rc = $sclass->execute( $controller, $c ); |
34 | } |
9a76221e |
35 | if ( $rc eq "0" ) { |
36 | return $self->_unsupported_media_type( $c, $content_type ); |
37 | } elsif ( $rc ne "1" ) { |
38 | return $self->_serialize_bad_request( $c, $content_type, $rc ); |
7ad87df9 |
39 | } |
9a76221e |
40 | } |
e601adda |
41 | |
def65dcc |
42 | $self->maybe::next::method(@_); |
e601adda |
43 | |
44 | return 1; |
eccb2137 |
45 | } |
7ad87df9 |
46 | |
398c5a1b |
47 | =head1 NAME |
48 | |
49 | Catalyst::Action::Deserialize - Deserialize Data in a Request |
50 | |
51 | =head1 SYNOPSIS |
52 | |
53 | package Foo::Controller::Bar; |
54 | |
55 | __PACKAGE__->config( |
faf5c20b |
56 | 'default' => 'text/x-yaml', |
57 | 'stash_key' => 'rest', |
58 | 'map' => { |
59 | 'text/x-yaml' => 'YAML', |
60 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
61 | }, |
398c5a1b |
62 | ); |
63 | |
e601adda |
64 | sub begin :ActionClass('Deserialize') {} |
398c5a1b |
65 | |
66 | =head1 DESCRIPTION |
67 | |
68 | This action will deserialize HTTP POST, PUT, and OPTIONS requests. |
69 | It assumes that the body of the HTTP Request is a serialized object. |
70 | The serializer is selected by introspecting the requests content-type |
71 | header. |
72 | |
398c5a1b |
73 | The specifics of deserializing each content-type is implemented as |
74 | a plugin to L<Catalyst::Action::Deserialize>. You can see a list |
75 | of currently implemented plugins in L<Catalyst::Controller::REST>. |
76 | |
77 | The results of your Deserializing will wind up in $c->req->data. |
78 | This is done through the magic of L<Catalyst::Request::REST>. |
79 | |
e601adda |
80 | While it is common for this Action to be called globally as a |
81 | C<begin> method, there is nothing stopping you from using it on a |
82 | single routine: |
398c5a1b |
83 | |
e601adda |
84 | sub foo :Local :Action('Deserialize') {} |
398c5a1b |
85 | |
e601adda |
86 | Will work just fine. |
398c5a1b |
87 | |
9a76221e |
88 | When you use this module, the request class will be changed to |
89 | L<Catalyst::Request::REST>. |
90 | |
398c5a1b |
91 | =head1 SEE ALSO |
92 | |
93 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
94 | a sensible set of defaults for a controller doing REST. |
95 | |
96 | L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST> |
97 | |
98 | =head1 AUTHOR |
99 | |
100 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
101 | |
102 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
103 | |
104 | =head1 LICENSE |
105 | |
106 | You may distribute this code under the same terms as Perl itself. |
107 | |
108 | =cut |
109 | |
7ad87df9 |
110 | 1; |