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.90';
12 $VERSION = eval $VERSION;
14 after BUILDARGS => sub {
17 Catalyst::Request::REST->_insert_self_into( $config->{class} );
20 has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' );
22 sub _load_content_plugins {
24 my ( $search_path, $controller, $c ) = @_;
26 unless ( defined( $self->_loaded_plugins ) ) {
27 $self->_loaded_plugins( {} );
30 # Load the Serialize Classes
31 unless ( defined( $self->_serialize_plugins ) ) {
34 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
35 @plugins = $mpo->plugins;
36 $self->_serialize_plugins( \@plugins );
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,
42 my $sclass = $search_path . "::";
48 if ( exists $controller->{'serialize'} ) {
49 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
50 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
51 $config = $controller->{'serialize'};
52 # if they're using the deprecated config, they may be expecting a
53 # default mapping too.
54 $config->{map} ||= $controller->{map};
56 $config = $controller;
58 $map = $config->{'map'};
60 # pick preferred content type
61 my @accepted_types; # priority order, best first
62 # give top priority to content type specified by stash, if any
63 my $content_type_stash_key = $config->{content_type_stash_key};
64 if ($content_type_stash_key
65 and my $stashed = $c->stash->{$content_type_stash_key}
67 # convert to array if not already a ref
68 $stashed = [ $stashed ] if not ref $stashed;
69 push @accepted_types, @$stashed;
71 # then content types requested by caller
73 $search_path =~ m{Deserialize}
74 ? @{ $c->request->accepted_content_types }
75 : @{ $c->request->accepted_response_content_types }
79 push @accepted_types, $config->{'default'} if $config->{'default'};
80 # pick the best match that we have a serializer mapping for
81 my ($content_type) = grep { $map->{$_} } @accepted_types;
83 return $self->_unsupported_media_type($c, $content_type)
86 # carp about old text/x-json
87 if ($content_type eq 'text/x-json') {
88 $c->log->info('Using deprecated text/x-json content-type.');
89 $c->log->info('Use application/json instead!');
92 if ( exists( $map->{$content_type} ) ) {
94 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
95 $mc = $map->{$content_type}->[0];
96 $sarg = $map->{$content_type}->[1];
98 $mc = $map->{$content_type};
100 # TODO: Handle custom serializers more elegantly.. this is a start,
101 # but how do we determine which is Serialize and Deserialize?
104 # $sclass =~ s/^+//g;
108 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
109 return $self->_unsupported_media_type($c, $content_type);
112 return $self->_unsupported_media_type($c, $content_type);
114 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
115 my $load_class = $sclass;
116 $load_class =~ s/::/\//g;
117 $load_class =~ s/$/.pm/g;
118 eval { require $load_class; };
121 "Error loading $sclass for " . $content_type . ": $!" );
122 return $self->_unsupported_media_type($c, $content_type);
124 $self->_loaded_plugins->{$sclass} = 1;
128 if ($search_path eq "Catalyst::Action::Serialize") {
130 $c->response->header( 'Vary' => 'Content-Type' );
131 } elsif ($c->request->accept_only) {
132 $c->response->header( 'Vary' => 'Accept' );
134 $c->response->content_type($content_type);
137 return $sclass, $sarg, $content_type;
140 sub _unsupported_media_type {
141 my ( $self, $c, $content_type ) = @_;
142 $c->res->content_type('text/plain');
143 $c->res->status(415);
144 if (defined($content_type) && $content_type ne "") {
146 "Content-Type " . $content_type . " is not supported.\r\n" );
149 "Cannot find a Content-Type supported by your client.\r\n" );
154 sub _serialize_bad_request {
155 my ( $self, $c, $content_type, $error ) = @_;
156 $c->res->content_type('text/plain');
157 $c->res->status(400);
159 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
163 __PACKAGE__->meta->make_immutable;
169 Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
173 This module implements the plugin loading and content-type negotiating
174 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
178 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
179 L<Catalyst::Controller::REST>,
183 See L<Catalyst::Action::REST> for authors.
187 You may distribute this code under the same terms as Perl itself.