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