2 # Catlyst::Action::Serialize.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
7 package Catalyst::Action::Serialize;
12 use base 'Catalyst::Action::SerializeBase';
13 use Module::Pluggable::Object;
14 use Data::Dump qw(dump);
18 my ( $controller, $c ) = @_;
20 $self->NEXT::execute(@_);
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)$/;
27 my ( $sclass, $sarg, $content_type ) =
28 $self->_load_content_plugins( "Catalyst::Action::Serialize",
30 unless ( defined($sclass) ) {
31 if ( defined($content_type) ) {
32 $c->log->info("Could not find a serializer for $content_type");
35 "Could not find a serializer for an empty content type");
40 "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug;
43 if ( defined($sarg) ) {
44 $rc = $sclass->execute( $controller, $c, $sarg );
46 $rc = $sclass->execute( $controller, $c );
49 return $self->_unsupported_media_type( $c, $content_type );
50 } elsif ( $rc ne 1 ) {
51 return $self->_serialize_bad_request( $c, $content_type, $rc );
61 Catalyst::Action::Serialize - Serialize Data in a Response
65 package Foo::Controller::Bar;
68 'default' => 'text/x-yaml',
69 'stash_key' => 'rest',
71 'text/html' => [ 'View', 'TT', ],
72 'text/x-yaml' => 'YAML',
73 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
77 sub end :ActionClass('Serialize') {}
81 This action will serialize the body of an HTTP Response. The serializer is
82 selected by introspecting the HTTP Requests content-type header.
84 It requires that your Catalyst controller is properly configured to set up the
85 mapping between Content Type's and Serialization classes.
87 The specifics of serializing each content-type is implemented as a plugin to
88 L<Catalyst::Action::Serialize>.
90 Typically, you would use this ActionClass on your C<end> method. However,
91 nothing is stopping you from choosing specific methods to Serialize:
93 sub foo :Local :ActionClass('Serialize') {
94 .. populate stash with data ..
97 When you use this module, the request class will be changed to
98 L<Catalyst::Request::REST>.
106 The Content-Type of the default Serialization format. This must be a
107 Content-Type associated with a plugin in the "map" section below.
109 This is used if a requested content-type is not recognized.
113 We will serialize the data that lives in this location in the stash. So
114 if the value is "rest", we will serialize the data under:
120 Takes a hashref, mapping Content-Types to a given plugin.
124 =head1 HELPFUL PEOPLE
126 Daisuke Maki pointed out that early versions of this Action did not play
127 well with others, or generally behave in a way that was very consistent
128 with the rest of Catalyst.
132 You likely want to look at L<Catalyst::Controller::REST>, which implements
133 a sensible set of defaults for doing a REST controller.
135 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
139 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
141 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
145 You may distribute this code under the same terms as Perl itself.