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