2 # Catlyst::Action::SerializeBase.pm
3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
7 package Catalyst::Action::SerializeBase;
12 use base 'Catalyst::Action';
13 use Module::Pluggable::Object;
14 use Catalyst::Request::REST;
15 use Catalyst::Utils ();
20 Catalyst::Request::REST->_insert_self_into(
21 Catalyst::Utils::class2appclass($config->{class})
23 return $class->SUPER::new($config, @_);
26 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
28 sub _load_content_plugins {
30 my ( $search_path, $controller, $c ) = @_;
32 unless ( defined( $self->_loaded_plugins ) ) {
33 $self->_loaded_plugins( {} );
36 # Load the Serialize Classes
37 unless ( defined( $self->_serialize_plugins ) ) {
40 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
41 @plugins = $mpo->plugins;
42 $self->_serialize_plugins( \@plugins );
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,
48 my $sclass = $search_path . "::";
54 if ( exists $controller->{'serialize'} ) {
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");
57 $config = $controller->{'serialize'};
58 # if they're using the deprecated config, they may be expecting a
59 # default mapping too.
60 $config->{map} ||= $controller->{map};
62 $config = $controller;
64 $map = $config->{'map'};
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}
73 # convert to array if not already a ref
74 $stashed = [ $stashed ] if not ref $stashed;
75 push @accepted_types, @$stashed;
77 # then content types requested by caller
78 push @accepted_types, @{ $c->request->accepted_content_types };
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;
84 return $self->_unsupported_media_type($c, $content_type)
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!');
93 if ( exists( $map->{$content_type} ) ) {
95 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
96 $mc = $map->{$content_type}->[0];
97 $sarg = $map->{$content_type}->[1];
99 $mc = $map->{$content_type};
101 # TODO: Handle custom serializers more elegantly.. this is a start,
102 # but how do we determine which is Serialize and Deserialize?
105 # $sclass =~ s/^+//g;
109 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
110 return $self->_unsupported_media_type($c, $content_type);
113 return $self->_unsupported_media_type($c, $content_type);
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; };
122 "Error loading $sclass for " . $content_type . ": $!" );
123 return $self->_unsupported_media_type($c, $content_type);
125 $self->_loaded_plugins->{$sclass} = 1;
129 if ($search_path eq "Catalyst::Action::Serialize") {
131 $c->response->header( 'Vary' => 'Content-Type' );
132 } elsif ($c->request->accept_only) {
133 $c->response->header( 'Vary' => 'Accept' );
135 $c->response->content_type($content_type);
138 return $sclass, $sarg, $content_type;
141 sub _unsupported_media_type {
142 my ( $self, $c, $content_type ) = @_;
143 $c->res->content_type('text/plain');
144 $c->res->status(415);
145 if (defined($content_type) && $content_type ne "") {
147 "Content-Type " . $content_type . " is not supported.\r\n" );
150 "Cannot find a Content-Type supported by your client.\r\n" );
155 sub _serialize_bad_request {
156 my ( $self, $c, $content_type, $error ) = @_;
157 $c->res->content_type('text/plain');
158 $c->res->status(400);
160 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
168 B<Catalyst::Action::SerializeBase>
170 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
174 This module implements the plugin loading and content-type negotiating
175 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
179 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
180 L<Catalyst::Controller::REST>,
184 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
186 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
190 You may distribute this code under the same terms as Perl itself.