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