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