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