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