Commit | Line | Data |
7ad87df9 |
1 | # |
2 | # Catlyst::Action::Serialize.pm |
3 | # Created by: Adam Jacob, Marchex, <adam@marchex.com> |
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; |
e601adda |
14 | use Data::Dump qw(dump); |
7ad87df9 |
15 | |
16 | sub execute { |
17 | my $self = shift; |
18 | my ( $controller, $c ) = @_; |
19 | |
e601adda |
20 | $self->NEXT::execute( @_ ); |
21 | |
7ad87df9 |
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)$/; |
26 | |
e601adda |
27 | my ($sclass, $sarg, $content_type) = $self->_load_content_plugins("Catalyst::Action::Serialize", $controller, $c); |
28 | return 1 unless defined $sclass; |
7ad87df9 |
29 | |
e601adda |
30 | my $rc; |
eccb2137 |
31 | if ( defined($sarg) ) { |
e601adda |
32 | $rc = $sclass->execute( $controller, $c, $sarg ); |
7ad87df9 |
33 | } else { |
e601adda |
34 | $rc = $sclass->execute( $controller, $c ); |
7ad87df9 |
35 | } |
e601adda |
36 | if ($rc eq 0) { |
37 | return $self->_unsupported_media_type($c, $content_type); |
38 | } elsif ($rc ne 1) { |
39 | return $self->_serialize_bad_request($c, $content_type, $rc); |
40 | } |
7ad87df9 |
41 | |
42 | return 1; |
eccb2137 |
43 | } |
7ad87df9 |
44 | |
45 | 1; |
398c5a1b |
46 | |
47 | =head1 NAME |
48 | |
49 | Catalyst::Action::Serialize - Serialize Data in a Response |
50 | |
51 | =head1 SYNOPSIS |
52 | |
53 | package Foo::Controller::Bar; |
54 | |
55 | __PACKAGE__->config( |
56 | serialize => { |
57 | 'default' => 'YAML', |
58 | 'stash_key' => 'rest', |
59 | 'map' => { |
60 | 'text/x-yaml' => 'YAML', |
61 | 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], |
62 | }, |
63 | } |
64 | ); |
65 | |
e601adda |
66 | sub end :ActionClass('Serialize') {} |
398c5a1b |
67 | |
68 | =head1 DESCRIPTION |
69 | |
70 | This action will serialize the body of an HTTP Response. The serializer is |
e601adda |
71 | selected by introspecting the HTTP Requests content-type header. |
398c5a1b |
72 | |
73 | It requires that your Catalyst controller have a "serialize" entry |
e601adda |
74 | in it's configuration, which sets up the mapping between Content Type's |
75 | and Serialization classes. |
398c5a1b |
76 | |
77 | The specifics of serializing each content-type is implemented as |
78 | a plugin to L<Catalyst::Action::Serialize>. |
79 | |
e601adda |
80 | Typically, you would use this ActionClass on your C<end> method. However, |
81 | nothing is stopping you from choosing specific methods to Serialize: |
82 | |
83 | sub foo :Local :ActionClass('Serialize') { |
84 | .. populate stash with data .. |
85 | } |
86 | |
398c5a1b |
87 | =head1 CONFIGURATION |
88 | |
89 | =over 4 |
90 | |
91 | =item default |
92 | |
93 | The default Serialization format. See the next section for |
94 | available options. This is used if a requested content-type |
95 | is not recognized. |
96 | |
97 | =item stash_key |
98 | |
e601adda |
99 | We will serialize the data that lives in this location in the stash. So |
100 | if the value is "rest", we will serialize the data under: |
101 | |
102 | $c->stash->{'rest'} |
398c5a1b |
103 | |
104 | =item map |
105 | |
106 | Takes a hashref, mapping Content-Types to a given plugin. |
107 | |
108 | =back |
109 | |
e601adda |
110 | =head1 HELPFUL PEOPLE |
111 | |
112 | Daisuke Maki pointed out that early versions of this Action did not play |
113 | well with others, or generally behave in a way that was very consistent |
114 | with the rest of Catalyst. |
115 | |
398c5a1b |
116 | =head1 SEE ALSO |
117 | |
118 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
e601adda |
119 | a sensible set of defaults for doing a REST controller. |
398c5a1b |
120 | |
121 | L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST> |
122 | |
123 | =head1 AUTHOR |
124 | |
125 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
126 | |
127 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
128 | |
129 | =head1 LICENSE |
130 | |
131 | You may distribute this code under the same terms as Perl itself. |
132 | |
133 | =cut |