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