r48@latte: adam | 2006-12-03 11:32:40 -0800
[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
12use base 'Catalyst::Action';
13use Module::Pluggable::Object;
14
15__PACKAGE__->mk_accessors(qw(plugins));
16
17sub execute {
18 my $self = shift;
19 my ( $controller, $c ) = @_;
20
21 return 1 if $c->req->method eq 'HEAD';
22 return 1 if length( $c->response->body );
23 return 1 if scalar @{ $c->error };
24 return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
25
26 # Load the Serialize Classes
eccb2137 27 unless ( defined( $self->plugins ) ) {
7ad87df9 28 my $mpo = Module::Pluggable::Object->new(
eccb2137 29 'require' => 1,
30 'search_path' => ['Catalyst::Action::Serialize'],
7ad87df9 31 );
32 my @plugins = $mpo->plugins;
eccb2137 33 $self->plugins( \@plugins );
7ad87df9 34 }
35
36 # Look up what serializer to use from content_type map
eccb2137 37 #
7ad87df9 38 # If we don't find one, we use the default
39 my $content_type = $c->request->content_type;
eccb2137 40 my $sclass = 'Catalyst::Action::Serialize::';
7ad87df9 41 my $sarg;
42 my $map = $controller->serialize->{'map'};
eccb2137 43 if ( exists( $map->{$content_type} ) ) {
7ad87df9 44 my $mc;
eccb2137 45 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
46 $mc = $map->{$content_type}->[0];
7ad87df9 47 $sarg = $map->{$content_type}->[1];
48 } else {
49 $mc = $map->{$content_type};
50 }
51 $sclass .= $mc;
eccb2137 52 if ( !grep( /^$sclass$/, @{ $self->plugins } ) ) {
7ad87df9 53 die "Cannot find plugin $sclass for $content_type!";
54 }
55 } else {
eccb2137 56 if ( exists( $controller->serialize->{'default'} ) ) {
7ad87df9 57 $sclass .= $controller->serialize->{'default'};
58 } else {
59 die "I cannot find a default serializer!";
60 }
61 }
62
63 # Go ahead and serialize ourselves
eccb2137 64 if ( defined($sarg) ) {
65 $sclass->execute( $controller, $c, $sarg );
7ad87df9 66 } else {
eccb2137 67 $sclass->execute( $controller, $c );
7ad87df9 68 }
69
eccb2137 70 if ( !$c->response->content_type ) {
71 $c->response->content_type( $c->request->content_type );
7ad87df9 72 }
73
74 return 1;
eccb2137 75}
7ad87df9 76
771;
398c5a1b 78
79=head1 NAME
80
81Catalyst::Action::Serialize - Serialize Data in a Response
82
83=head1 SYNOPSIS
84
85 package Foo::Controller::Bar;
86
87 __PACKAGE__->config(
88 serialize => {
89 'default' => 'YAML',
90 'stash_key' => 'rest',
91 'map' => {
92 'text/x-yaml' => 'YAML',
93 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
94 },
95 }
96 );
97
98 sub end : ActionClass('Serialize') {}
99
100=head1 DESCRIPTION
101
102This action will serialize the body of an HTTP Response. The serializer is
103selected by introspecting the requests content-type header.
104
105It requires that your Catalyst controller have a "serialize" entry
106in it's configuration.
107
108The specifics of serializing each content-type is implemented as
109a plugin to L<Catalyst::Action::Serialize>.
110
111=head1 CONFIGURATION
112
113=over 4
114
115=item default
116
117The default Serialization format. See the next section for
118available options. This is used if a requested content-type
119is not recognized.
120
121=item stash_key
122
123Where in the stash the data you want serialized lives.
124
125=item map
126
127Takes a hashref, mapping Content-Types to a given plugin.
128
129=back
130
131=head1 SEE ALSO
132
133You likely want to look at L<Catalyst::Controller::REST>, which implements
134a sensible set of defaults for a controller doing REST.
135
136L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
137
138=head1 AUTHOR
139
140Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
141
142Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
143
144=head1 LICENSE
145
146You may distribute this code under the same terms as Perl itself.
147
148=cut