r49@latte: adam | 2006-12-03 12:30:40 -0800
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize.pm
1 #
2 # Catlyst::Action::Serialize.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 #
5 # $Id$
6
7 package Catalyst::Action::Serialize;
8
9 use strict;
10 use warnings;
11
12 use base 'Catalyst::Action::SerializeBase';
13 use Module::Pluggable::Object;
14 use Data::Dump qw(dump);
15
16 sub execute {
17     my $self = shift;
18     my ( $controller, $c ) = @_;
19
20     $self->NEXT::execute( @_ );
21
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
27     my ($sclass, $sarg, $content_type) = $self->_load_content_plugins("Catalyst::Action::Serialize", $controller, $c);
28     return 1 unless defined $sclass;
29
30     my $rc;
31     if ( defined($sarg) ) {
32         $rc = $sclass->execute( $controller, $c, $sarg );
33     } else {
34         $rc = $sclass->execute( $controller, $c );
35     }
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     } 
41
42     return 1;
43 }
44
45 1;
46
47 =head1 NAME
48
49 Catalyst::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
66     sub end :ActionClass('Serialize') {}
67
68 =head1 DESCRIPTION
69
70 This action will serialize the body of an HTTP Response.  The serializer is
71 selected by introspecting the HTTP Requests content-type header.
72
73 It requires that your Catalyst controller have a "serialize" entry
74 in it's configuration, which sets up the mapping between Content Type's
75 and Serialization classes.
76
77 The specifics of serializing each content-type is implemented as
78 a plugin to L<Catalyst::Action::Serialize>.
79
80 Typically, you would use this ActionClass on your C<end> method.  However,
81 nothing is stopping you from choosing specific methods to Serialize:
82
83   sub foo :Local :ActionClass('Serialize') {
84      .. populate stash with data ..
85   }
86
87 =head1 CONFIGURATION
88
89 =over 4
90
91 =item default
92
93 The default Serialization format.  See the next section for
94 available options.  This is used if a requested content-type
95 is not recognized.
96
97 =item stash_key 
98
99 We will serialize the data that lives in this location in the stash.  So
100 if the value is "rest", we will serialize the data under:
101
102   $c->stash->{'rest'}
103
104 =item map
105
106 Takes a hashref, mapping Content-Types to a given plugin.
107
108 =back
109
110 =head1 HELPFUL PEOPLE
111
112 Daisuke Maki pointed out that early versions of this Action did not play
113 well with others, or generally behave in a way that was very consistent
114 with the rest of Catalyst. 
115
116 =head1 SEE ALSO
117
118 You likely want to look at L<Catalyst::Controller::REST>, which implements
119 a sensible set of defaults for doing a REST controller.
120
121 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
122
123 =head1 AUTHOR
124
125 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
126
127 Marchex, Inc. paid me while I developed this module.  (http://www.marchex.com)
128
129 =head1 LICENSE
130
131 You may distribute this code under the same terms as Perl itself.
132
133 =cut