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 after BUILDARGS => sub {
14 Catalyst::Request::REST->_insert_self_into( $config->{class} );
17 has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' );
19 sub _load_content_plugins {
21 my ( $search_path, $controller, $c ) = @_;
23 unless ( defined( $self->_loaded_plugins ) ) {
24 $self->_loaded_plugins( {} );
27 # Load the Serialize Classes
28 unless ( defined( $self->_serialize_plugins ) ) {
31 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
32 @plugins = $mpo->plugins;
33 $self->_serialize_plugins( \@plugins );
36 # Finally, we load the class. If you have a default serializer,
37 # and we still don't have a content-type that exists in the map,
39 my $sclass = $search_path . "::";
45 if ( exists $controller->{'serialize'} ) {
46 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
47 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
48 $config = $controller->{'serialize'};
49 # if they're using the deprecated config, they may be expecting a
50 # default mapping too.
51 $config->{map} ||= $controller->{map};
53 $config = $controller;
55 $map = $config->{'map'};
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}
64 # convert to array if not already a ref
65 $stashed = [ $stashed ] if not ref $stashed;
66 push @accepted_types, @$stashed;
68 # then content types requested by caller
69 push @accepted_types, @{ $c->request->accepted_content_types };
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;
75 return $self->unsupported_media_type($c, $content_type)
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!');
84 if ( exists( $map->{$content_type} ) ) {
86 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
87 $mc = $map->{$content_type}->[0];
88 $sarg = $map->{$content_type}->[1];
90 $mc = $map->{$content_type};
92 # TODO: Handle custom serializers more elegantly.. this is a start,
93 # but how do we determine which is Serialize and Deserialize?
100 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
101 return $self->unsupported_media_type($c, $content_type);
104 return $self->unsupported_media_type($c, $content_type);
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; };
113 "Error loading $sclass for " . $content_type . ": $!" );
114 return $self->unsupported_media_type($c, $content_type);
116 $self->_loaded_plugins->{$sclass} = 1;
120 if ($search_path eq "Catalyst::Action::Serialize") {
122 $c->response->header( 'Vary' => 'Content-Type' );
123 } elsif ($c->request->accept_only) {
124 $c->response->header( 'Vary' => 'Accept' );
126 $c->response->content_type($content_type);
129 return $sclass, $sarg, $content_type;
132 sub unsupported_media_type {
133 my ( $self, $c, $content_type ) = @_;
134 $c->res->content_type('text/plain');
135 $c->res->status(415);
136 if (defined($content_type) && $content_type ne "") {
138 "Content-Type " . $content_type . " is not supported.\r\n" );
141 "Cannot find a Content-Type supported by your client.\r\n" );
146 sub serialize_bad_request {
147 my ( $self, $c, $content_type, $error ) = @_;
148 $c->res->content_type('text/plain');
149 $c->res->status(400);
151 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
155 __PACKAGE__->meta->make_immutable;
161 Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
165 This module implements the plugin loading and content-type negotiating
166 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
170 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
171 L<Catalyst::Controller::REST>,
175 See L<Catalyst::Action::REST> for authors.
179 You may distribute this code under the same terms as Perl itself.