why oh why does every class have VERSION???
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize.pm
1 package Catalyst::Action::Serialize;
2
3 use Moose;
4 use namespace::autoclean;
5
6 extends 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
8 use MRO::Compat;
9
10 our $VERSION = '1.13';
11 $VERSION = eval $VERSION;
12
13 has _encoders => (
14    is => 'ro',
15    isa => 'HashRef',
16    default => sub { {} },
17 );
18
19 sub execute {
20     my $self = shift;
21     my ( $controller, $c ) = @_;
22
23     $self->maybe::next::method(@_);
24
25     return 1 if $c->req->method eq 'HEAD';
26     return 1 if $c->response->has_body;
27     return 1 if scalar @{ $c->error };
28     return 1 if $c->response->status =~ /^(?:204)$/;
29     return 1 if defined $c->stash->{current_view};
30     return 1 if defined $c->stash->{current_view_instance};
31
32     my ( $sclass, $sarg, $content_type ) =
33       $self->_load_content_plugins( "Catalyst::Action::Serialize",
34         $controller, $c );
35     unless ( defined($sclass) ) {
36         if ( defined($content_type) ) {
37             $c->log->info("Could not find a serializer for $content_type");
38         } else {
39             $c->log->info(
40                 "Could not find a serializer for an empty content-type");
41         }
42         return 1;
43     }
44     $c->log->debug(
45         "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug;
46
47     $self->_encoders->{$sclass} ||= $sclass->new;
48     my $sobj = $self->_encoders->{$sclass};
49
50     my $rc;
51     eval {
52         if ( defined($sarg) ) {
53             $rc = $sobj->execute( $controller, $c, $sarg );
54         } else {
55             $rc = $sobj->execute( $controller, $c );
56         }
57     };
58     if ($@) {
59         return $self->serialize_bad_request( $c, $content_type, $@ );
60     } elsif (!$rc) {
61         return $self->unsupported_media_type( $c, $content_type );
62     }
63
64     return 1;
65 }
66
67 __PACKAGE__->meta->make_immutable;
68
69 1;
70
71 =head1 NAME
72
73 Catalyst::Action::Serialize - Serialize Data in a Response
74
75 =head1 SYNOPSIS
76
77     package Foo::Controller::Bar;
78
79     __PACKAGE__->config(
80         'default'   => 'text/x-yaml',
81         'stash_key' => 'rest',
82         'map'       => {
83             'text/html'          => [ 'View', 'TT', ],
84             'text/x-yaml'        => 'YAML',
85             'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
86         }
87     );
88
89     sub end :ActionClass('Serialize') {}
90
91 =head1 DESCRIPTION
92
93 This action will serialize the body of an HTTP Response.  The serializer is
94 selected by introspecting the HTTP Requests content-type header.
95
96 It requires that your Catalyst controller is properly configured to set up the
97 mapping between Content Type's and Serialization classes.
98
99 The specifics of serializing each content-type is implemented as a plugin to
100 L<Catalyst::Action::Serialize>.
101
102 Typically, you would use this ActionClass on your C<end> method.  However,
103 nothing is stopping you from choosing specific methods to Serialize:
104
105   sub foo :Local :ActionClass('Serialize') {
106      .. populate stash with data ..
107   }
108
109 When you use this module, the request class will be changed to
110 L<Catalyst::Request::REST>.
111
112 =head1 CONFIGURATION
113
114 =head2 map
115
116 Takes a hashref, mapping Content-Types to a given serializer plugin.
117
118 =head2 default
119
120 This is the 'fall-back' Content-Type if none of the requested or acceptable
121 types is found in the L</map>. It must be an entry in the L</map>.
122
123 =head2 stash_key
124
125 Specifies the key of the stash entry holding the data that is to be serialized.
126 So if the value is "rest", we will serialize the data under:
127
128   $c->stash->{'rest'}
129
130 =head2 content_type_stash_key
131
132 Specifies the key of the stash entry that optionally holds an overriding
133 Content-Type. If set, and if the specified stash entry has a valid value,
134 then it takes priority over the requested content types.
135
136 This can be useful if you want to dynamically force a particular content type,
137 perhaps for debugging.
138
139 =head1 HELPFUL PEOPLE
140
141 Daisuke Maki pointed out that early versions of this Action did not play
142 well with others, or generally behave in a way that was very consistent
143 with the rest of Catalyst.
144
145 =head1 CUSTOM ERRORS
146
147 For building custom error responses when serialization fails, you can create
148 an ActionRole (and use L<Catalyst::Controller::ActionRole> to apply it to the
149 C<end> action) which overrides C<unsupported_media_type> and/or C<_serialize_bad_request>
150 methods.
151
152 =head1 SEE ALSO
153
154 You likely want to look at L<Catalyst::Controller::REST>, which implements
155 a sensible set of defaults for doing a REST controller.
156
157 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
158
159 =head1 AUTHORS
160
161 See L<Catalyst::Action::REST> for authors.
162
163 =head1 LICENSE
164
165 You may distribute this code under the same terms as Perl itself.
166
167 =cut