r49@latte: adam | 2006-12-03 12:30:40 -0800
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
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::SerializeBase';
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 ) = @_;
21
22     my $nreq = bless( $c->request, 'Catalyst::Request::REST' );
23     $c->request($nreq);
24
25     my @demethods = qw(POST PUT OPTIONS);
26     my $method    = $c->request->method;
27     if ( grep /^$method$/, @demethods ) {
28         my ($sclass, $sarg, $content_type) = $self->_load_content_plugins('Catalyst::Action::Deserialize', $controller, $c);        
29         return 1 unless defined ($sclass);
30         my $rc;
31         if ( defined($sarg) ) {
32             $rc = $sclass->execute( $controller, $c, $sarg );
33         } else {
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);
40         }
41     } 
42
43     $self->NEXT::execute( @_ );
44
45     return 1;
46 }
47
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
67     sub begin :ActionClass('Deserialize') {}
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
77 in it's configuration.  See L<Catalyst::Action::Serialize> for the details.
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
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:
89
90    sub foo :Local :Action('Deserialize') {}
91
92 Will work just fine.
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
113 1;