Cherry pick everything bar the use parent change from 25d49c2, fixing RT#46680
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1package Catalyst::Action::Deserialize;
2
3use strict;
4use warnings;
5
e601adda 6use base 'Catalyst::Action::SerializeBase';
7ad87df9 7use Module::Pluggable::Object;
def65dcc 8use MRO::Compat;
7ad87df9 9
10__PACKAGE__->mk_accessors(qw(plugins));
11
12sub execute {
13 my $self = shift;
e601adda 14 my ( $controller, $c ) = @_;
7ad87df9 15
7ad87df9 16 my @demethods = qw(POST PUT OPTIONS);
eccb2137 17 my $method = $c->request->method;
18 if ( grep /^$method$/, @demethods ) {
9a76221e 19 my ( $sclass, $sarg, $content_type ) =
20 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
21 $controller, $c );
22 return 1 unless defined($sclass);
e601adda 23 my $rc;
eccb2137 24 if ( defined($sarg) ) {
e601adda 25 $rc = $sclass->execute( $controller, $c, $sarg );
7ad87df9 26 } else {
e601adda 27 $rc = $sclass->execute( $controller, $c );
28 }
9a76221e 29 if ( $rc eq "0" ) {
30 return $self->_unsupported_media_type( $c, $content_type );
31 } elsif ( $rc ne "1" ) {
32 return $self->_serialize_bad_request( $c, $content_type, $rc );
7ad87df9 33 }
9a76221e 34 }
e601adda 35
def65dcc 36 $self->maybe::next::method(@_);
e601adda 37
38 return 1;
eccb2137 39}
7ad87df9 40
398c5a1b 41=head1 NAME
42
43Catalyst::Action::Deserialize - Deserialize Data in a Request
44
45=head1 SYNOPSIS
46
47 package Foo::Controller::Bar;
48
49 __PACKAGE__->config(
faf5c20b 50 'default' => 'text/x-yaml',
51 'stash_key' => 'rest',
52 'map' => {
53 'text/x-yaml' => 'YAML',
54 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
55 },
398c5a1b 56 );
57
e601adda 58 sub begin :ActionClass('Deserialize') {}
398c5a1b 59
60=head1 DESCRIPTION
61
62This action will deserialize HTTP POST, PUT, and OPTIONS requests.
63It assumes that the body of the HTTP Request is a serialized object.
64The serializer is selected by introspecting the requests content-type
65header.
66
398c5a1b 67The specifics of deserializing each content-type is implemented as
68a plugin to L<Catalyst::Action::Deserialize>. You can see a list
69of currently implemented plugins in L<Catalyst::Controller::REST>.
70
71The results of your Deserializing will wind up in $c->req->data.
72This is done through the magic of L<Catalyst::Request::REST>.
73
e601adda 74While it is common for this Action to be called globally as a
75C<begin> method, there is nothing stopping you from using it on a
76single routine:
398c5a1b 77
e601adda 78 sub foo :Local :Action('Deserialize') {}
398c5a1b 79
e601adda 80Will work just fine.
398c5a1b 81
9a76221e 82When you use this module, the request class will be changed to
83L<Catalyst::Request::REST>.
84
398c5a1b 85=head1 SEE ALSO
86
87You likely want to look at L<Catalyst::Controller::REST>, which implements
88a sensible set of defaults for a controller doing REST.
89
90L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
91
5cb5f6bb 92=head1 AUTHORS
398c5a1b 93
5cb5f6bb 94See L<Catalyst::Action::REST> for authors.
398c5a1b 95
96=head1 LICENSE
97
98You may distribute this code under the same terms as Perl itself.
99
100=cut
101
7ad87df9 1021;