Added some debug logging - From Dave Rolksy
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize.pm
CommitLineData
7ad87df9 1#
2# Catlyst::Action::Serialize.pm
3# Created by: Adam Jacob, Marchex, <adam@marchex.com>
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
e601adda 20 $self->NEXT::execute( @_ );
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
e601adda 27 my ($sclass, $sarg, $content_type) = $self->_load_content_plugins("Catalyst::Action::Serialize", $controller, $c);
51cc8fe9 28 unless ( defined($sclass) ) {
29 $c->log->debug("Could not find a serializer for $content_type");
30 return 1;
31 }
32 $c->log->debug("Serializing with $sclass" . ($sarg ? " [$sarg]" : ''));
7ad87df9 33
e601adda 34 my $rc;
eccb2137 35 if ( defined($sarg) ) {
e601adda 36 $rc = $sclass->execute( $controller, $c, $sarg );
7ad87df9 37 } else {
e601adda 38 $rc = $sclass->execute( $controller, $c );
7ad87df9 39 }
e601adda 40 if ($rc eq 0) {
41 return $self->_unsupported_media_type($c, $content_type);
42 } elsif ($rc ne 1) {
43 return $self->_serialize_bad_request($c, $content_type, $rc);
44 }
7ad87df9 45
46 return 1;
eccb2137 47}
7ad87df9 48
491;
398c5a1b 50
51=head1 NAME
52
53Catalyst::Action::Serialize - Serialize Data in a Response
54
55=head1 SYNOPSIS
56
57 package Foo::Controller::Bar;
58
59 __PACKAGE__->config(
60 serialize => {
61 'default' => 'YAML',
62 'stash_key' => 'rest',
63 'map' => {
64 'text/x-yaml' => 'YAML',
65 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
66 },
67 }
68 );
69
e601adda 70 sub end :ActionClass('Serialize') {}
398c5a1b 71
72=head1 DESCRIPTION
73
74This action will serialize the body of an HTTP Response. The serializer is
e601adda 75selected by introspecting the HTTP Requests content-type header.
398c5a1b 76
77It requires that your Catalyst controller have a "serialize" entry
e601adda 78in it's configuration, which sets up the mapping between Content Type's
79and Serialization classes.
398c5a1b 80
81The specifics of serializing each content-type is implemented as
82a plugin to L<Catalyst::Action::Serialize>.
83
e601adda 84Typically, you would use this ActionClass on your C<end> method. However,
85nothing is stopping you from choosing specific methods to Serialize:
86
87 sub foo :Local :ActionClass('Serialize') {
88 .. populate stash with data ..
89 }
90
398c5a1b 91=head1 CONFIGURATION
92
93=over 4
94
95=item default
96
97The default Serialization format. See the next section for
98available options. This is used if a requested content-type
99is not recognized.
100
101=item stash_key
102
e601adda 103We will serialize the data that lives in this location in the stash. So
104if the value is "rest", we will serialize the data under:
105
106 $c->stash->{'rest'}
398c5a1b 107
108=item map
109
110Takes a hashref, mapping Content-Types to a given plugin.
111
112=back
113
e601adda 114=head1 HELPFUL PEOPLE
115
116Daisuke Maki pointed out that early versions of this Action did not play
117well with others, or generally behave in a way that was very consistent
118with the rest of Catalyst.
119
398c5a1b 120=head1 SEE ALSO
121
122You likely want to look at L<Catalyst::Controller::REST>, which implements
e601adda 123a sensible set of defaults for doing a REST controller.
398c5a1b 124
125L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
126
127=head1 AUTHOR
128
129Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
130
131Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
132
133=head1 LICENSE
134
135You may distribute this code under the same terms as Perl itself.
136
137=cut