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