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