Cherry pick everything bar the use parent change from 25d49c2, fixing RT#46680
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize.pm
1 package Catalyst::Action::Serialize;
2
3 use strict;
4 use warnings;
5
6 use base 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
8 use MRO::Compat;
9
10 sub execute {
11     my $self = shift;
12     my ( $controller, $c ) = @_;
13
14     $self->maybe::next::method(@_);
15
16     return 1 if $c->req->method eq 'HEAD';
17     return 1 if length( $c->response->body );
18     return 1 if scalar @{ $c->error };
19     return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
20
21     my ( $sclass, $sarg, $content_type ) =
22       $self->_load_content_plugins( "Catalyst::Action::Serialize",
23         $controller, $c );
24     unless ( defined($sclass) ) {
25         if ( defined($content_type) ) {
26             $c->log->info("Could not find a serializer for $content_type");
27         } else {
28             $c->log->info(
29                 "Could not find a serializer for an empty content-type");
30         }
31         return 1;
32     }
33     $c->log->debug(
34         "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug;
35
36     my $rc;
37     if ( defined($sarg) ) {
38         $rc = $sclass->execute( $controller, $c, $sarg );
39     } else {
40         $rc = $sclass->execute( $controller, $c );
41     }
42     if ( $rc eq 0 ) {
43         return $self->_unsupported_media_type( $c, $content_type );
44     } elsif ( $rc ne 1 ) {
45         return $self->_serialize_bad_request( $c, $content_type, $rc );
46     }
47
48     return 1;
49 }
50
51 1;
52
53 =head1 NAME
54
55 Catalyst::Action::Serialize - Serialize Data in a Response
56
57 =head1 SYNOPSIS
58
59     package Foo::Controller::Bar;
60
61     __PACKAGE__->config(
62         'default'   => 'text/x-yaml',
63         'stash_key' => 'rest',
64         'map'       => {
65             'text/html'          => [ 'View', 'TT', ],
66             'text/x-yaml'        => 'YAML',
67             'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
68         }
69     );
70
71     sub end :ActionClass('Serialize') {}
72
73 =head1 DESCRIPTION
74
75 This action will serialize the body of an HTTP Response.  The serializer is
76 selected by introspecting the HTTP Requests content-type header.
77
78 It requires that your Catalyst controller is properly configured to set up the
79 mapping between Content Type's and Serialization classes.
80
81 The specifics of serializing each content-type is implemented as a plugin to
82 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 When you use this module, the request class will be changed to
92 L<Catalyst::Request::REST>.
93
94 =head1 CONFIGURATION
95
96 =head2 map
97
98 Takes a hashref, mapping Content-Types to a given serializer plugin.
99
100 =head2 default
101
102 This is the 'fall-back' Content-Type if none of the requested or acceptable
103 types is found in the L</map>. It must be an entry in the L</map>.
104
105 =head2 stash_key 
106
107 Specifies the key of the stash entry holding the data that is to be serialized.
108 So if the value is "rest", we will serialize the data under:
109
110   $c->stash->{'rest'}
111
112 =head2 content_type_stash_key
113
114 Specifies the key of the stash entry that optionally holds an overriding
115 Content-Type. If set, and if the specified stash entry has a valid value,
116 then it takes priority over the requested content types.
117
118 This can be useful if you want to dynamically force a particular content type,
119 perhaps for debugging.
120
121 =head1 HELPFUL PEOPLE
122
123 Daisuke Maki pointed out that early versions of this Action did not play
124 well with others, or generally behave in a way that was very consistent
125 with the rest of Catalyst. 
126
127 =head1 SEE ALSO
128
129 You likely want to look at L<Catalyst::Controller::REST>, which implements
130 a sensible set of defaults for doing a REST controller.
131
132 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
133
134 =head1 AUTHORS
135
136 See L<Catalyst::Action::REST> for authors.
137
138 =head1 LICENSE
139
140 You may distribute this code under the same terms as Perl itself.
141
142 =cut