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