Commit | Line | Data |
7ad87df9 |
1 | package Catalyst::Action::Deserialize; |
2 | |
930013e6 |
3 | use Moose; |
4 | use namespace::autoclean; |
7ad87df9 |
5 | |
930013e6 |
6 | extends 'Catalyst::Action::SerializeBase'; |
7ad87df9 |
7 | use Module::Pluggable::Object; |
def65dcc |
8 | use MRO::Compat; |
599755b6 |
9 | use Moose::Util::TypeConstraints; |
7ad87df9 |
10 | |
be099c6c |
11 | our $VERSION = '1.03'; |
f465980c |
12 | $VERSION = eval $VERSION; |
13 | |
05009b91 |
14 | has plugins => ( is => 'rw' ); |
7ad87df9 |
15 | |
588cbecc |
16 | has 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 | |
32 | sub _build_deserialize_http_methods { [qw(POST PUT OPTIONS DELETE)] } |
33 | |
7ad87df9 |
34 | sub 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 | |
65 | Catalyst::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 |
84 | This action will deserialize HTTP POST, PUT, OPTIONS and DELETE requests. |
398c5a1b |
85 | It assumes that the body of the HTTP Request is a serialized object. |
86 | The serializer is selected by introspecting the requests content-type |
87 | header. |
88 | |
588cbecc |
89 | If you want deserialize any other HTTP method besides POST, PUT, |
90 | OPTIONS and DELETE you can do this by setting the |
91 | C<< deserialize_http_methods >> list via C<< action_args >>. |
92 | Just modify the config in your controller and define a list of HTTP |
93 | methods 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 | |
103 | See also L<Catalyst::Controller/action_args>. |
104 | |
398c5a1b |
105 | The specifics of deserializing each content-type is implemented as |
106 | a plugin to L<Catalyst::Action::Deserialize>. You can see a list |
107 | of currently implemented plugins in L<Catalyst::Controller::REST>. |
108 | |
109 | The results of your Deserializing will wind up in $c->req->data. |
110 | This is done through the magic of L<Catalyst::Request::REST>. |
111 | |
e601adda |
112 | While it is common for this Action to be called globally as a |
113 | C<begin> method, there is nothing stopping you from using it on a |
114 | single routine: |
398c5a1b |
115 | |
e601adda |
116 | sub foo :Local :Action('Deserialize') {} |
398c5a1b |
117 | |
e601adda |
118 | Will work just fine. |
398c5a1b |
119 | |
9a76221e |
120 | When you use this module, the request class will be changed to |
121 | L<Catalyst::Request::REST>. |
122 | |
b3996af8 |
123 | =head1 CUSTOM ERRORS |
124 | |
125 | For building custom error responses when de-serialization fails, you can create |
126 | an ActionRole (and use L<Catalyst::Controller::ActionRole> to apply it to the |
127 | C<begin> action) which overrides C<unsupported_media_type> and/or C<_serialize_bad_request> |
128 | methods. |
129 | |
398c5a1b |
130 | =head1 SEE ALSO |
131 | |
132 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
133 | a sensible set of defaults for a controller doing REST. |
134 | |
135 | L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST> |
136 | |
5cb5f6bb |
137 | =head1 AUTHORS |
398c5a1b |
138 | |
5cb5f6bb |
139 | See L<Catalyst::Action::REST> for authors. |
398c5a1b |
140 | |
141 | =head1 LICENSE |
142 | |
143 | You may distribute this code under the same terms as Perl itself. |
144 | |
145 | =cut |