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