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 Data::Dump qw(dump);
15 use Catalyst::Request::REST;
16 use Catalyst::Utils ();
21 Catalyst::Request::REST->_insert_self_into( $config->{class} );
22 return $class->SUPER::new($config, @_);
25 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
27 sub _load_content_plugins {
29 my ( $search_path, $controller, $c ) = @_;
31 unless ( defined( $self->_loaded_plugins ) ) {
32 $self->_loaded_plugins( {} );
35 # Load the Serialize Classes
36 unless ( defined( $self->_serialize_plugins ) ) {
39 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
40 @plugins = $mpo->plugins;
41 $self->_serialize_plugins( \@plugins );
44 # Finally, we load the class. If you have a default serializer,
45 # and we still don't have a content-type that exists in the map,
47 my $sclass = $search_path . "::";
53 if ( exists $controller->{'serialize'} ) {
54 $c->log->info("Using deprecated configuration for Catalyst::Action::REST!");
55 $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide");
56 $config = $controller->{'serialize'};
58 $config = $controller;
60 $map = $config->{'map'};
62 # pick preferred content type
63 my @accepted_types; # priority order, best first
64 # give top priority to content type specified by stash, if any
65 my $content_type_stash_key = $config->{content_type_stash_key};
66 if ($content_type_stash_key
67 and my $stashed = $c->stash->{$content_type_stash_key}
69 # convert to array if not already a ref
70 $stashed = [ $stashed ] if not ref $stashed;
71 push @accepted_types, @$stashed;
73 # then content types requested by caller
74 push @accepted_types, @{ $c->request->accepted_content_types };
76 push @accepted_types, $config->{'default'} if $config->{'default'};
77 # pick the best match that we have a serializer mapping for
78 my ($content_type) = grep { $map->{$_} } @accepted_types;
80 return $self->_unsupported_media_type($c, $content_type)
83 # carp about old text/x-json
84 if ($content_type eq 'text/x-json') {
85 $c->log->info('Using deprecated text/x-json content-type.');
86 $c->log->info('Use application/json instead!');
89 if ( exists( $map->{$content_type} ) ) {
91 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
92 $mc = $map->{$content_type}->[0];
93 $sarg = $map->{$content_type}->[1];
95 $mc = $map->{$content_type};
97 # TODO: Handle custom serializers more elegantly.. this is a start,
98 # but how do we determine which is Serialize and Deserialize?
101 # $sclass =~ s/^+//g;
105 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
106 return $self->_unsupported_media_type($c, $content_type);
109 return $self->_unsupported_media_type($c, $content_type);
111 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
112 my $load_class = $sclass;
113 $load_class =~ s/::/\//g;
114 $load_class =~ s/$/.pm/g;
115 eval { require $load_class; };
118 "Error loading $sclass for " . $content_type . ": $!" );
119 return $self->_unsupported_media_type($c, $content_type);
121 $self->_loaded_plugins->{$sclass} = 1;
125 if ($search_path eq "Catalyst::Action::Serialize") {
127 $c->response->header( 'Vary' => 'Content-Type' );
128 } elsif ($c->request->accept_only) {
129 $c->response->header( 'Vary' => 'Accept' );
131 $c->response->content_type($content_type);
134 return $sclass, $sarg, $content_type;
137 sub _unsupported_media_type {
138 my ( $self, $c, $content_type ) = @_;
139 $c->res->content_type('text/plain');
140 $c->res->status(415);
141 if (defined($content_type) && $content_type ne "") {
143 "Content-Type " . $content_type . " is not supported.\r\n" );
146 "Cannot find a Content-Type supported by your client.\r\n" );
151 sub _serialize_bad_request {
152 my ( $self, $c, $content_type, $error ) = @_;
153 $c->res->content_type('text/plain');
154 $c->res->status(400);
156 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
164 B<Catalyst::Action::SerializeBase>
166 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
170 This module implements the plugin loading and content-type negotiating
171 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
175 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
176 L<Catalyst::Controller::REST>,
180 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
182 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
186 You may distribute this code under the same terms as Perl itself.