Additional pod cleanups
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / SerializeBase.pm
CommitLineData
e601adda 1#
2# Catlyst::Action::SerializeBase.pm
be3c588a 3# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
e601adda 4#
5# $Id$
6
7package Catalyst::Action::SerializeBase;
8
9use strict;
10use warnings;
11
12use base 'Catalyst::Action';
13use Module::Pluggable::Object;
9a76221e 14use Catalyst::Request::REST;
5132f5e4 15use Catalyst::Utils ();
9a76221e 16
5132f5e4 17sub new {
18 my $class = shift;
19 my $config = shift;
37694e6c 20 Catalyst::Request::REST->_insert_self_into( $config->{class} );
5132f5e4 21 return $class->SUPER::new($config, @_);
22}
e601adda 23
24__PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
25
26sub _load_content_plugins {
27 my $self = shift;
28 my ( $search_path, $controller, $c ) = @_;
29
30 unless ( defined( $self->_loaded_plugins ) ) {
31 $self->_loaded_plugins( {} );
32 }
33
34 # Load the Serialize Classes
35 unless ( defined( $self->_serialize_plugins ) ) {
36 my @plugins;
37 my $mpo =
38 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
39 @plugins = $mpo->plugins;
40 $self->_serialize_plugins( \@plugins );
41 }
42
e601adda 43 # Finally, we load the class. If you have a default serializer,
44 # and we still don't have a content-type that exists in the map,
45 # we'll use it.
46 my $sclass = $search_path . "::";
47 my $sarg;
faf5c20b 48 my $map;
367b3ff4 49
faf5c20b 50 my $config;
51
07682cbc 52 if ( exists $controller->{'serialize'} ) {
9cd203c9 53 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
54 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
07682cbc 55 $config = $controller->{'serialize'};
7b8c5be2 56 # if they're using the deprecated config, they may be expecting a
57 # default mapping too.
58 $config->{map} ||= $controller->{map};
faf5c20b 59 } else {
07682cbc 60 $config = $controller;
faf5c20b 61 }
62 $map = $config->{'map'};
c0aef9cd 63
a51e7bbd 64 # pick preferred content type
65 my @accepted_types; # priority order, best first
66 # give top priority to content type specified by stash, if any
67 my $content_type_stash_key = $config->{content_type_stash_key};
68 if ($content_type_stash_key
69 and my $stashed = $c->stash->{$content_type_stash_key}
70 ) {
71 # convert to array if not already a ref
72 $stashed = [ $stashed ] if not ref $stashed;
73 push @accepted_types, @$stashed;
367b3ff4 74 }
a51e7bbd 75 # then content types requested by caller
76 push @accepted_types, @{ $c->request->accepted_content_types };
77 # then the default
78 push @accepted_types, $config->{'default'} if $config->{'default'};
79 # pick the best match that we have a serializer mapping for
80 my ($content_type) = grep { $map->{$_} } @accepted_types;
81
82 return $self->_unsupported_media_type($c, $content_type)
83 if not $content_type;
367b3ff4 84
c0aef9cd 85 # carp about old text/x-json
86 if ($content_type eq 'text/x-json') {
87 $c->log->info('Using deprecated text/x-json content-type.');
88 $c->log->info('Use application/json instead!');
89 }
90
e601adda 91 if ( exists( $map->{$content_type} ) ) {
92 my $mc;
93 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
94 $mc = $map->{$content_type}->[0];
95 $sarg = $map->{$content_type}->[1];
96 } else {
97 $mc = $map->{$content_type};
98 }
99 # TODO: Handle custom serializers more elegantly.. this is a start,
100 # but how do we determine which is Serialize and Deserialize?
101 #if ($mc =~ /^+/) {
102 # $sclass = $mc;
103 # $sclass =~ s/^+//g;
104 #} else {
105 $sclass .= $mc;
106 #}
107 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
108 return $self->_unsupported_media_type($c, $content_type);
109 }
110 } else {
367b3ff4 111 return $self->_unsupported_media_type($c, $content_type);
e601adda 112 }
113 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
114 my $load_class = $sclass;
115 $load_class =~ s/::/\//g;
116 $load_class =~ s/$/.pm/g;
117 eval { require $load_class; };
118 if ($@) {
119 $c->log->error(
d4611771 120 "Error loading $sclass for " . $content_type . ": $!" );
e601adda 121 return $self->_unsupported_media_type($c, $content_type);
122 } else {
123 $self->_loaded_plugins->{$sclass} = 1;
124 }
125 }
126
127 if ($search_path eq "Catalyst::Action::Serialize") {
128 if ($content_type) {
129 $c->response->header( 'Vary' => 'Content-Type' );
9a76221e 130 } elsif ($c->request->accept_only) {
e601adda 131 $c->response->header( 'Vary' => 'Accept' );
132 }
133 $c->response->content_type($content_type);
134 }
135
136 return $sclass, $sarg, $content_type;
137}
138
139sub _unsupported_media_type {
140 my ( $self, $c, $content_type ) = @_;
141 $c->res->content_type('text/plain');
142 $c->res->status(415);
2224bad1 143 if (defined($content_type) && $content_type ne "") {
e601adda 144 $c->res->body(
145 "Content-Type " . $content_type . " is not supported.\r\n" );
146 } else {
147 $c->res->body(
148 "Cannot find a Content-Type supported by your client.\r\n" );
149 }
150 return undef;
151}
152
153sub _serialize_bad_request {
154 my ( $self, $c, $content_type, $error ) = @_;
155 $c->res->content_type('text/plain');
156 $c->res->status(400);
157 $c->res->body(
158 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
159 return undef;
160}
161
1621;
163
164=head1 NAME
165
166B<Catalyst::Action::SerializeBase>
167
168Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
169
170=head1 DESCRIPTION
171
172This module implements the plugin loading and content-type negotiating
173code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
174
175=head1 SEE ALSO
176
177L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
178L<Catalyst::Controller::REST>,
179
5cb5f6bb 180=head1 AUTHORS
e601adda 181
5cb5f6bb 182See L<Catalyst::Action::REST> for authors.
e601adda 183
184=head1 LICENSE
185
186You may distribute this code under the same terms as Perl itself.
187
188=cut