1 package Catalyst::Action::SerializeBase;
4 use namespace::autoclean;
6 extends 'Catalyst::Action';
7 use Module::Pluggable::Object;
8 use Catalyst::Request::REST;
9 use Catalyst::Utils ();
11 our $VERSION = '0.81';
12 $VERSION = eval $VERSION;
17 Catalyst::Request::REST->_insert_self_into( $config->{class} );
18 return $class->SUPER::new($config, @_);
21 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
23 sub _load_content_plugins {
25 my ( $search_path, $controller, $c ) = @_;
27 unless ( defined( $self->_loaded_plugins ) ) {
28 $self->_loaded_plugins( {} );
31 # Load the Serialize Classes
32 unless ( defined( $self->_serialize_plugins ) ) {
35 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
36 @plugins = $mpo->plugins;
37 $self->_serialize_plugins( \@plugins );
40 # Finally, we load the class. If you have a default serializer,
41 # and we still don't have a content-type that exists in the map,
43 my $sclass = $search_path . "::";
49 if ( exists $controller->{'serialize'} ) {
50 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
51 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
52 $config = $controller->{'serialize'};
53 # if they're using the deprecated config, they may be expecting a
54 # default mapping too.
55 $config->{map} ||= $controller->{map};
57 $config = $controller;
59 $map = $config->{'map'};
61 # pick preferred content type
62 my @accepted_types; # priority order, best first
63 # give top priority to content type specified by stash, if any
64 my $content_type_stash_key = $config->{content_type_stash_key};
65 if ($content_type_stash_key
66 and my $stashed = $c->stash->{$content_type_stash_key}
68 # convert to array if not already a ref
69 $stashed = [ $stashed ] if not ref $stashed;
70 push @accepted_types, @$stashed;
72 # then content types requested by caller
73 push @accepted_types, @{ $c->request->accepted_content_types };
75 push @accepted_types, $config->{'default'} if $config->{'default'};
76 # pick the best match that we have a serializer mapping for
77 my ($content_type) = grep { $map->{$_} } @accepted_types;
79 return $self->_unsupported_media_type($c, $content_type)
82 # carp about old text/x-json
83 if ($content_type eq 'text/x-json') {
84 $c->log->info('Using deprecated text/x-json content-type.');
85 $c->log->info('Use application/json instead!');
88 if ( exists( $map->{$content_type} ) ) {
90 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
91 $mc = $map->{$content_type}->[0];
92 $sarg = $map->{$content_type}->[1];
94 $mc = $map->{$content_type};
96 # TODO: Handle custom serializers more elegantly.. this is a start,
97 # but how do we determine which is Serialize and Deserialize?
100 # $sclass =~ s/^+//g;
104 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
105 return $self->_unsupported_media_type($c, $content_type);
108 return $self->_unsupported_media_type($c, $content_type);
110 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
111 my $load_class = $sclass;
112 $load_class =~ s/::/\//g;
113 $load_class =~ s/$/.pm/g;
114 eval { require $load_class; };
117 "Error loading $sclass for " . $content_type . ": $!" );
118 return $self->_unsupported_media_type($c, $content_type);
120 $self->_loaded_plugins->{$sclass} = 1;
124 if ($search_path eq "Catalyst::Action::Serialize") {
126 $c->response->header( 'Vary' => 'Content-Type' );
127 } elsif ($c->request->accept_only) {
128 $c->response->header( 'Vary' => 'Accept' );
130 $c->response->content_type($content_type);
133 return $sclass, $sarg, $content_type;
136 sub _unsupported_media_type {
137 my ( $self, $c, $content_type ) = @_;
138 $c->res->content_type('text/plain');
139 $c->res->status(415);
140 if (defined($content_type) && $content_type ne "") {
142 "Content-Type " . $content_type . " is not supported.\r\n" );
145 "Cannot find a Content-Type supported by your client.\r\n" );
150 sub _serialize_bad_request {
151 my ( $self, $c, $content_type, $error ) = @_;
152 $c->res->content_type('text/plain');
153 $c->res->status(400);
155 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
163 B<Catalyst::Action::SerializeBase>
165 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
169 This module implements the plugin loading and content-type negotiating
170 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
174 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
175 L<Catalyst::Controller::REST>,
179 See L<Catalyst::Action::REST> for authors.
183 You may distribute this code under the same terms as Perl itself.