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 | |
12 | use base 'Catalyst::Action'; |
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; |
20 | my ( $controller, $c, $test ) = @_; |
21 | |
eccb2137 |
22 | my $nreq = bless( $c->request, 'Catalyst::Request::REST' ); |
7ad87df9 |
23 | $c->request($nreq); |
24 | |
eccb2137 |
25 | unless ( defined( $self->plugins ) ) { |
7ad87df9 |
26 | my $mpo = Module::Pluggable::Object->new( |
eccb2137 |
27 | 'require' => 1, |
28 | 'search_path' => ['Catalyst::Action::Deserialize'], |
7ad87df9 |
29 | ); |
30 | my @plugins = $mpo->plugins; |
eccb2137 |
31 | $self->plugins( \@plugins ); |
7ad87df9 |
32 | } |
33 | my $content_type = $c->request->content_type; |
eccb2137 |
34 | my $sclass = 'Catalyst::Action::Deserialize::'; |
7ad87df9 |
35 | my $sarg; |
36 | my $map = $controller->serialize->{'map'}; |
eccb2137 |
37 | if ( exists( $map->{$content_type} ) ) { |
7ad87df9 |
38 | my $mc; |
eccb2137 |
39 | if ( ref( $map->{$content_type} ) eq "ARRAY" ) { |
40 | $mc = $map->{$content_type}->[0]; |
7ad87df9 |
41 | $sarg = $map->{$content_type}->[1]; |
42 | } else { |
43 | $mc = $map->{$content_type}; |
44 | } |
45 | $sclass .= $mc; |
eccb2137 |
46 | if ( !grep( /^$sclass$/, @{ $self->plugins } ) ) { |
7ad87df9 |
47 | die "Cannot find plugin $sclass for $content_type!"; |
48 | } |
49 | } else { |
eccb2137 |
50 | if ( exists( $controller->serialize->{'default'} ) ) { |
7ad87df9 |
51 | $sclass .= $controller->serialize->{'default'}; |
52 | } else { |
53 | die "I cannot find a default serializer!"; |
54 | } |
55 | } |
56 | |
57 | my @demethods = qw(POST PUT OPTIONS); |
eccb2137 |
58 | my $method = $c->request->method; |
59 | if ( grep /^$method$/, @demethods ) { |
60 | if ( defined($sarg) ) { |
61 | $sclass->execute( $controller, $c, $sarg ); |
7ad87df9 |
62 | } else { |
eccb2137 |
63 | $sclass->execute( $controller, $c ); |
7ad87df9 |
64 | } |
65 | $self->NEXT::execute( @_, ); |
66 | } else { |
eccb2137 |
67 | $self->NEXT::execute(@_); |
7ad87df9 |
68 | } |
eccb2137 |
69 | } |
7ad87df9 |
70 | |
398c5a1b |
71 | =head1 NAME |
72 | |
73 | Catalyst::Action::Deserialize - Deserialize Data in a Request |
74 | |
75 | =head1 SYNOPSIS |
76 | |
77 | package Foo::Controller::Bar; |
78 | |
79 | __PACKAGE__->config( |
80 | serialize => { |
81 | 'default' => 'YAML', |
82 | 'stash_key' => 'rest', |
83 | 'map' => { |
84 | 'text/x-yaml' => 'YAML', |
85 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
86 | }, |
87 | } |
88 | ); |
89 | |
90 | sub begin : ActionClass('Deserialize') {} |
91 | |
92 | =head1 DESCRIPTION |
93 | |
94 | This action will deserialize HTTP POST, PUT, and OPTIONS requests. |
95 | It assumes that the body of the HTTP Request is a serialized object. |
96 | The serializer is selected by introspecting the requests content-type |
97 | header. |
98 | |
99 | It requires that your Catalyst controller have a "serialize" entry |
100 | in it's configuration. |
101 | |
102 | The specifics of deserializing each content-type is implemented as |
103 | a plugin to L<Catalyst::Action::Deserialize>. You can see a list |
104 | of currently implemented plugins in L<Catalyst::Controller::REST>. |
105 | |
106 | The results of your Deserializing will wind up in $c->req->data. |
107 | This is done through the magic of L<Catalyst::Request::REST>. |
108 | |
109 | =head1 CONFIGURATION |
110 | |
111 | =over 4 |
112 | |
113 | =item default |
114 | |
115 | The default Serialization format. See the next section for |
116 | available options. |
117 | |
118 | =item map |
119 | |
120 | Takes a hashref, mapping Content-Types to a given plugin. |
121 | |
122 | =back |
123 | |
124 | =head1 SEE ALSO |
125 | |
126 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
127 | a sensible set of defaults for a controller doing REST. |
128 | |
129 | L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST> |
130 | |
131 | =head1 AUTHOR |
132 | |
133 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
134 | |
135 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
136 | |
137 | =head1 LICENSE |
138 | |
139 | You may distribute this code under the same terms as Perl itself. |
140 | |
141 | =cut |
142 | |
7ad87df9 |
143 | 1; |