Commit | Line | Data |
7ad87df9 |
1 | package Catalyst::Action::Serialize; |
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; |
7ad87df9 |
9 | |
f465980c |
10 | our $VERSION = '0.81'; |
11 | $VERSION = eval $VERSION; |
12 | |
7ad87df9 |
13 | sub execute { |
14 | my $self = shift; |
15 | my ( $controller, $c ) = @_; |
16 | |
def65dcc |
17 | $self->maybe::next::method(@_); |
e601adda |
18 | |
7ad87df9 |
19 | return 1 if $c->req->method eq 'HEAD'; |
20 | return 1 if length( $c->response->body ); |
21 | return 1 if scalar @{ $c->error }; |
22 | return 1 if $c->response->status =~ /^(?:204|3\d\d)$/; |
23 | |
9a76221e |
24 | my ( $sclass, $sarg, $content_type ) = |
25 | $self->_load_content_plugins( "Catalyst::Action::Serialize", |
26 | $controller, $c ); |
51cc8fe9 |
27 | unless ( defined($sclass) ) { |
9a76221e |
28 | if ( defined($content_type) ) { |
faf5c20b |
29 | $c->log->info("Could not find a serializer for $content_type"); |
9a76221e |
30 | } else { |
faf5c20b |
31 | $c->log->info( |
9cd203c9 |
32 | "Could not find a serializer for an empty content-type"); |
9a76221e |
33 | } |
51cc8fe9 |
34 | return 1; |
35 | } |
9a76221e |
36 | $c->log->debug( |
faf5c20b |
37 | "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug; |
7ad87df9 |
38 | |
e601adda |
39 | my $rc; |
878b2b54 |
40 | eval { |
41 | if ( defined($sarg) ) { |
42 | $rc = $sclass->execute( $controller, $c, $sarg ); |
43 | } else { |
44 | $rc = $sclass->execute( $controller, $c ); |
45 | } |
46 | }; |
47 | if ($@) { |
48 | return $self->_serialize_bad_request( $c, $content_type, $@ ); |
49 | } elsif (!$rc) { |
9a76221e |
50 | return $self->_unsupported_media_type( $c, $content_type ); |
9a76221e |
51 | } |
7ad87df9 |
52 | |
53 | return 1; |
eccb2137 |
54 | } |
7ad87df9 |
55 | |
05009b91 |
56 | __PACKAGE__->meta->make_immutable; |
398c5a1b |
57 | |
58 | =head1 NAME |
59 | |
60 | Catalyst::Action::Serialize - Serialize Data in a Response |
61 | |
62 | =head1 SYNOPSIS |
63 | |
64 | package Foo::Controller::Bar; |
65 | |
66 | __PACKAGE__->config( |
faf5c20b |
67 | 'default' => 'text/x-yaml', |
68 | 'stash_key' => 'rest', |
69 | 'map' => { |
70 | 'text/html' => [ 'View', 'TT', ], |
71 | 'text/x-yaml' => 'YAML', |
72 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
398c5a1b |
73 | } |
74 | ); |
75 | |
e601adda |
76 | sub end :ActionClass('Serialize') {} |
398c5a1b |
77 | |
78 | =head1 DESCRIPTION |
79 | |
80 | This action will serialize the body of an HTTP Response. The serializer is |
e601adda |
81 | selected by introspecting the HTTP Requests content-type header. |
398c5a1b |
82 | |
faf5c20b |
83 | It requires that your Catalyst controller is properly configured to set up the |
84 | mapping between Content Type's and Serialization classes. |
398c5a1b |
85 | |
faf5c20b |
86 | The specifics of serializing each content-type is implemented as a plugin to |
87 | L<Catalyst::Action::Serialize>. |
398c5a1b |
88 | |
e601adda |
89 | Typically, you would use this ActionClass on your C<end> method. However, |
90 | nothing is stopping you from choosing specific methods to Serialize: |
91 | |
92 | sub foo :Local :ActionClass('Serialize') { |
93 | .. populate stash with data .. |
94 | } |
95 | |
9a76221e |
96 | When you use this module, the request class will be changed to |
97 | L<Catalyst::Request::REST>. |
98 | |
398c5a1b |
99 | =head1 CONFIGURATION |
100 | |
a51e7bbd |
101 | =head2 map |
398c5a1b |
102 | |
a51e7bbd |
103 | Takes a hashref, mapping Content-Types to a given serializer plugin. |
398c5a1b |
104 | |
a51e7bbd |
105 | =head2 default |
367b3ff4 |
106 | |
a51e7bbd |
107 | This is the 'fall-back' Content-Type if none of the requested or acceptable |
108 | types is found in the L</map>. It must be an entry in the L</map>. |
398c5a1b |
109 | |
a51e7bbd |
110 | =head2 stash_key |
398c5a1b |
111 | |
a51e7bbd |
112 | Specifies the key of the stash entry holding the data that is to be serialized. |
113 | So if the value is "rest", we will serialize the data under: |
e601adda |
114 | |
115 | $c->stash->{'rest'} |
398c5a1b |
116 | |
a51e7bbd |
117 | =head2 content_type_stash_key |
398c5a1b |
118 | |
a51e7bbd |
119 | Specifies the key of the stash entry that optionally holds an overriding |
120 | Content-Type. If set, and if the specified stash entry has a valid value, |
121 | then it takes priority over the requested content types. |
398c5a1b |
122 | |
a51e7bbd |
123 | This can be useful if you want to dynamically force a particular content type, |
124 | perhaps for debugging. |
398c5a1b |
125 | |
e601adda |
126 | =head1 HELPFUL PEOPLE |
127 | |
128 | Daisuke Maki pointed out that early versions of this Action did not play |
129 | well with others, or generally behave in a way that was very consistent |
130 | with the rest of Catalyst. |
131 | |
398c5a1b |
132 | =head1 SEE ALSO |
133 | |
134 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
e601adda |
135 | a sensible set of defaults for doing a REST controller. |
398c5a1b |
136 | |
137 | L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST> |
138 | |
5cb5f6bb |
139 | =head1 AUTHORS |
398c5a1b |
140 | |
5cb5f6bb |
141 | See L<Catalyst::Action::REST> for authors. |
398c5a1b |
142 | |
143 | =head1 LICENSE |
144 | |
145 | You may distribute this code under the same terms as Perl itself. |
146 | |
147 | =cut |