Version 1.02
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1package Catalyst::Action::Deserialize;
2
930013e6 3use Moose;
4use namespace::autoclean;
7ad87df9 5
930013e6 6extends 'Catalyst::Action::SerializeBase';
7ad87df9 7use Module::Pluggable::Object;
def65dcc 8use MRO::Compat;
599755b6 9use Moose::Util::TypeConstraints;
7ad87df9 10
d0822465 11our $VERSION = '1.02';
f465980c 12$VERSION = eval $VERSION;
13
05009b91 14has plugins => ( is => 'rw' );
7ad87df9 15
588cbecc 16has deserialize_http_methods => (
599755b6 17 traits => ['Hash'],
18 isa => do {
19 my $tc = subtype as 'HashRef[Str]';
20 coerce $tc, from 'ArrayRef[Str]',
21 via { +{ map { ($_ => 1) } @$_ } };
22 $tc;
23 },
24 coerce => 1,
588cbecc 25 builder => '_build_deserialize_http_methods',
26 handles => {
599755b6 27 deserialize_http_methods => 'keys',
28 _deserialize_handles_http_method => 'exists',
588cbecc 29 },
30);
31
32sub _build_deserialize_http_methods { [qw(POST PUT OPTIONS DELETE)] }
33
7ad87df9 34sub execute {
35 my $self = shift;
e601adda 36 my ( $controller, $c ) = @_;
7ad87df9 37
5bf53db4 38 if ( !defined($c->req->data) && $self->_deserialize_handles_http_method($c->request->method) ) {
9a76221e 39 my ( $sclass, $sarg, $content_type ) =
40 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
41 $controller, $c );
42 return 1 unless defined($sclass);
e601adda 43 my $rc;
eccb2137 44 if ( defined($sarg) ) {
e601adda 45 $rc = $sclass->execute( $controller, $c, $sarg );
7ad87df9 46 } else {
e601adda 47 $rc = $sclass->execute( $controller, $c );
48 }
9a76221e 49 if ( $rc eq "0" ) {
b3996af8 50 return $self->unsupported_media_type( $c, $content_type );
9a76221e 51 } elsif ( $rc ne "1" ) {
b3996af8 52 return $self->serialize_bad_request( $c, $content_type, $rc );
7ad87df9 53 }
9a76221e 54 }
e601adda 55
def65dcc 56 $self->maybe::next::method(@_);
e601adda 57
58 return 1;
eccb2137 59}
7ad87df9 60
05009b91 61__PACKAGE__->meta->make_immutable;
62
398c5a1b 63=head1 NAME
64
65Catalyst::Action::Deserialize - Deserialize Data in a Request
66
67=head1 SYNOPSIS
68
69 package Foo::Controller::Bar;
70
71 __PACKAGE__->config(
faf5c20b 72 'default' => 'text/x-yaml',
73 'stash_key' => 'rest',
74 'map' => {
75 'text/x-yaml' => 'YAML',
76 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
77 },
398c5a1b 78 );
79
e601adda 80 sub begin :ActionClass('Deserialize') {}
398c5a1b 81
82=head1 DESCRIPTION
83
588cbecc 84This action will deserialize HTTP POST, PUT, OPTIONS and DELETE requests.
398c5a1b 85It assumes that the body of the HTTP Request is a serialized object.
86The serializer is selected by introspecting the requests content-type
87header.
88
588cbecc 89If you want deserialize any other HTTP method besides POST, PUT,
90OPTIONS and DELETE you can do this by setting the
91C<< deserialize_http_methods >> list via C<< action_args >>.
92Just modify the config in your controller and define a list of HTTP
93methods the deserialization should happen for:
94
95 __PACKAGE__->config(
96 action_args => {
97 '*' => {
98 deserialize_http_methods => [qw(POST PUT OPTIONS DELETE GET)]
99 }
100 }
101 );
102
103See also L<Catalyst::Controller/action_args>.
104
398c5a1b 105The specifics of deserializing each content-type is implemented as
106a plugin to L<Catalyst::Action::Deserialize>. You can see a list
107of currently implemented plugins in L<Catalyst::Controller::REST>.
108
109The results of your Deserializing will wind up in $c->req->data.
110This is done through the magic of L<Catalyst::Request::REST>.
111
e601adda 112While it is common for this Action to be called globally as a
113C<begin> method, there is nothing stopping you from using it on a
114single routine:
398c5a1b 115
e601adda 116 sub foo :Local :Action('Deserialize') {}
398c5a1b 117
e601adda 118Will work just fine.
398c5a1b 119
9a76221e 120When you use this module, the request class will be changed to
121L<Catalyst::Request::REST>.
122
b3996af8 123=head1 CUSTOM ERRORS
124
125For building custom error responses when de-serialization fails, you can create
126an ActionRole (and use L<Catalyst::Controller::ActionRole> to apply it to the
127C<begin> action) which overrides C<unsupported_media_type> and/or C<_serialize_bad_request>
128methods.
129
398c5a1b 130=head1 SEE ALSO
131
132You likely want to look at L<Catalyst::Controller::REST>, which implements
133a sensible set of defaults for a controller doing REST.
134
135L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
136
5cb5f6bb 137=head1 AUTHORS
398c5a1b 138
5cb5f6bb 139See L<Catalyst::Action::REST> for authors.
398c5a1b 140
141=head1 LICENSE
142
143You may distribute this code under the same terms as Perl itself.
144
145=cut