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 ();
14 Catalyst::Request::REST->_insert_self_into( $config->{class} );
15 return $class->SUPER::new($config, @_);
18 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
20 sub _load_content_plugins {
22 my ( $search_path, $controller, $c ) = @_;
24 unless ( defined( $self->_loaded_plugins ) ) {
25 $self->_loaded_plugins( {} );
28 # Load the Serialize Classes
29 unless ( defined( $self->_serialize_plugins ) ) {
32 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
33 @plugins = $mpo->plugins;
34 $self->_serialize_plugins( \@plugins );
37 # Finally, we load the class. If you have a default serializer,
38 # and we still don't have a content-type that exists in the map,
40 my $sclass = $search_path . "::";
46 if ( exists $controller->{'serialize'} ) {
47 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
48 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
49 $config = $controller->{'serialize'};
50 # if they're using the deprecated config, they may be expecting a
51 # default mapping too.
52 $config->{map} ||= $controller->{map};
54 $config = $controller;
56 $map = $config->{'map'};
58 # pick preferred content type
59 my @accepted_types; # priority order, best first
60 # give top priority to content type specified by stash, if any
61 my $content_type_stash_key = $config->{content_type_stash_key};
62 if ($content_type_stash_key
63 and my $stashed = $c->stash->{$content_type_stash_key}
65 # convert to array if not already a ref
66 $stashed = [ $stashed ] if not ref $stashed;
67 push @accepted_types, @$stashed;
69 # then content types requested by caller
70 push @accepted_types, @{ $c->request->accepted_content_types };
72 push @accepted_types, $config->{'default'} if $config->{'default'};
73 # pick the best match that we have a serializer mapping for
74 my ($content_type) = grep { $map->{$_} } @accepted_types;
76 return $self->_unsupported_media_type($c, $content_type)
79 # carp about old text/x-json
80 if ($content_type eq 'text/x-json') {
81 $c->log->info('Using deprecated text/x-json content-type.');
82 $c->log->info('Use application/json instead!');
85 if ( exists( $map->{$content_type} ) ) {
87 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
88 $mc = $map->{$content_type}->[0];
89 $sarg = $map->{$content_type}->[1];
91 $mc = $map->{$content_type};
93 # TODO: Handle custom serializers more elegantly.. this is a start,
94 # but how do we determine which is Serialize and Deserialize?
101 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
102 return $self->_unsupported_media_type($c, $content_type);
105 return $self->_unsupported_media_type($c, $content_type);
107 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
108 my $load_class = $sclass;
109 $load_class =~ s/::/\//g;
110 $load_class =~ s/$/.pm/g;
111 eval { require $load_class; };
114 "Error loading $sclass for " . $content_type . ": $!" );
115 return $self->_unsupported_media_type($c, $content_type);
117 $self->_loaded_plugins->{$sclass} = 1;
121 if ($search_path eq "Catalyst::Action::Serialize") {
123 $c->response->header( 'Vary' => 'Content-Type' );
124 } elsif ($c->request->accept_only) {
125 $c->response->header( 'Vary' => 'Accept' );
127 $c->response->content_type($content_type);
130 return $sclass, $sarg, $content_type;
133 sub _unsupported_media_type {
134 my ( $self, $c, $content_type ) = @_;
135 $c->res->content_type('text/plain');
136 $c->res->status(415);
137 if (defined($content_type) && $content_type ne "") {
139 "Content-Type " . $content_type . " is not supported.\r\n" );
142 "Cannot find a Content-Type supported by your client.\r\n" );
147 sub _serialize_bad_request {
148 my ( $self, $c, $content_type, $error ) = @_;
149 $c->res->content_type('text/plain');
150 $c->res->status(400);
152 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
160 B<Catalyst::Action::SerializeBase>
162 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
166 This module implements the plugin loading and content-type negotiating
167 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
171 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
172 L<Catalyst::Controller::REST>,
176 See L<Catalyst::Action::REST> for authors.
180 You may distribute this code under the same terms as Perl itself.