perltidy
[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
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) ) {
32 $c->log->debug("Could not find a serializer for $content_type");
33 } else {
34 $c->log->debug(
35 "Could not find a serializer for an empty content type");
36 }
51cc8fe9 37 return 1;
38 }
9a76221e 39 $c->log->debug(
40 "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) );
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(
68 serialize => {
69 'default' => 'YAML',
70 'stash_key' => 'rest',
71 'map' => {
9a76221e 72 'text/html' => [ 'View', 'TT', ],
398c5a1b 73 'text/x-yaml' => 'YAML',
74 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
75 },
76 }
77 );
78
e601adda 79 sub end :ActionClass('Serialize') {}
398c5a1b 80
81=head1 DESCRIPTION
82
83This action will serialize the body of an HTTP Response. The serializer is
e601adda 84selected by introspecting the HTTP Requests content-type header.
398c5a1b 85
86It requires that your Catalyst controller have a "serialize" entry
e601adda 87in it's configuration, which sets up the mapping between Content Type's
88and Serialization classes.
398c5a1b 89
90The specifics of serializing each content-type is implemented as
91a plugin to L<Catalyst::Action::Serialize>.
92
e601adda 93Typically, you would use this ActionClass on your C<end> method. However,
94nothing is stopping you from choosing specific methods to Serialize:
95
96 sub foo :Local :ActionClass('Serialize') {
97 .. populate stash with data ..
98 }
99
9a76221e 100When you use this module, the request class will be changed to
101L<Catalyst::Request::REST>.
102
398c5a1b 103=head1 CONFIGURATION
104
105=over 4
106
107=item default
108
109The default Serialization format. See the next section for
110available options. This is used if a requested content-type
111is not recognized.
112
113=item stash_key
114
e601adda 115We will serialize the data that lives in this location in the stash. So
116if the value is "rest", we will serialize the data under:
117
118 $c->stash->{'rest'}
398c5a1b 119
120=item map
121
122Takes a hashref, mapping Content-Types to a given plugin.
123
124=back
125
e601adda 126=head1 HELPFUL PEOPLE
127
128Daisuke Maki pointed out that early versions of this Action did not play
129well with others, or generally behave in a way that was very consistent
130with the rest of Catalyst.
131
398c5a1b 132=head1 SEE ALSO
133
134You likely want to look at L<Catalyst::Controller::REST>, which implements
e601adda 135a sensible set of defaults for doing a REST controller.
398c5a1b 136
137L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
138
139=head1 AUTHOR
140
141Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
142
143Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
144
145=head1 LICENSE
146
147You may distribute this code under the same terms as Perl itself.
148
149=cut
9a76221e 150