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