Added some debug logging - From Dave Rolksy
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1#
2# Catlyst::Action::Deserialize
3# Created by: Adam Jacob, Marchex, <adam@marchex.com>
4#
5# $Id$
6
7package Catalyst::Action::Deserialize;
8
9use strict;
10use warnings;
11
e601adda 12use base 'Catalyst::Action::SerializeBase';
7ad87df9 13use Module::Pluggable::Object;
14use Catalyst::Request::REST;
15
16__PACKAGE__->mk_accessors(qw(plugins));
17
18sub 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
50Catalyst::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
71This action will deserialize HTTP POST, PUT, and OPTIONS requests.
72It assumes that the body of the HTTP Request is a serialized object.
73The serializer is selected by introspecting the requests content-type
74header.
75
76It requires that your Catalyst controller have a "serialize" entry
e601adda 77in it's configuration. See L<Catalyst::Action::Serialize> for the details.
398c5a1b 78
79The specifics of deserializing each content-type is implemented as
80a plugin to L<Catalyst::Action::Deserialize>. You can see a list
81of currently implemented plugins in L<Catalyst::Controller::REST>.
82
83The results of your Deserializing will wind up in $c->req->data.
84This is done through the magic of L<Catalyst::Request::REST>.
85
e601adda 86While it is common for this Action to be called globally as a
87C<begin> method, there is nothing stopping you from using it on a
88single routine:
398c5a1b 89
e601adda 90 sub foo :Local :Action('Deserialize') {}
398c5a1b 91
e601adda 92Will work just fine.
398c5a1b 93
94=head1 SEE ALSO
95
96You likely want to look at L<Catalyst::Controller::REST>, which implements
97a sensible set of defaults for a controller doing REST.
98
99L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
100
101=head1 AUTHOR
102
103Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
104
105Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
106
107=head1 LICENSE
108
109You may distribute this code under the same terms as Perl itself.
110
111=cut
112
7ad87df9 1131;