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 . "::";
47 if ( exists $controller->{'serialize'} ) {
48 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
49 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
50 $config = $controller->{'serialize'};
51 # if they're using the deprecated config, they may be expecting a
52 # default mapping too.
53 $config->{map} ||= $controller->{map};
55 $config = $controller;
57 $map = $config->{'map'};
58 $default = $config->{'default'} if $config->{'default'};
60 # If we're in RFC 7231 compliance mode we need to determine if we're
61 # serializing or deserializing, then set the request object to
62 # look at the appropriate set of supported content types.
63 $compliance_mode = $config->{'compliance_mode'};
64 if($compliance_mode) {
65 my $serialize_mode = (split '::', $search_path)[-1];
66 if($serialize_mode eq 'Deserialize') {
67 # Tell the request object to only look at the Content-Type header
68 $c->request->set_content_type_only();
70 # If we're in compliance mode and doing deserializing we want
71 # to use the allowed content types for deserializing, not the
73 $map = $config->{'deserialize_map'};
74 $default = $config->{'deserialize_default'} if $config->{'deserialize_default'};
75 } elsif($serialize_mode eq 'Serialize') {
76 # Tell the request object to only look at the Accept header
77 $c->request->set_accept_only();
81 # pick preferred content type
82 my @accepted_types; # priority order, best first
83 # give top priority to content type specified by stash, if any
84 my $content_type_stash_key = $config->{content_type_stash_key};
85 if ($content_type_stash_key
86 and my $stashed = $c->stash->{$content_type_stash_key}
88 # convert to array if not already a ref
89 $stashed = [ $stashed ] if not ref $stashed;
90 push @accepted_types, @$stashed;
92 # then content types requested by caller
93 push @accepted_types, @{ $c->request->accepted_content_types };
95 push @accepted_types, $default if $default;
96 # pick the best match that we have a serializer mapping for
97 my ($content_type) = grep { $map->{$_} } @accepted_types;
99 return $self->unsupported_media_type($c, $content_type)
100 if not $content_type;
102 # carp about old text/x-json
103 if ($content_type eq 'text/x-json') {
104 $c->log->info('Using deprecated text/x-json content-type.');
105 $c->log->info('Use application/json instead!');
108 if ( exists( $map->{$content_type} ) ) {
110 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
111 $mc = $map->{$content_type}->[0];
112 $sarg = $map->{$content_type}->[1];
114 $mc = $map->{$content_type};
116 # TODO: Handle custom serializers more elegantly.. this is a start,
117 # but how do we determine which is Serialize and Deserialize?
120 # $sclass =~ s/^+//g;
124 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
125 return $self->unsupported_media_type($c, $content_type);
128 return $self->unsupported_media_type($c, $content_type);
130 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
131 my $load_class = $sclass;
132 $load_class =~ s/::/\//g;
133 $load_class =~ s/$/.pm/g;
134 eval { require $load_class; };
137 "Error loading $sclass for " . $content_type . ": $!" );
138 return $self->unsupported_media_type($c, $content_type);
140 $self->_loaded_plugins->{$sclass} = 1;
144 if ($search_path eq "Catalyst::Action::Serialize") {
145 unless( $c->response->header( 'Vary' ) ) {
147 $c->response->header( 'Vary' => 'Content-Type' );
148 } elsif ($c->request->accept_only) {
149 $c->response->header( 'Vary' => 'Accept' );
152 $c->response->content_type($content_type);
155 return $sclass, $sarg, $content_type;
158 sub unsupported_media_type {
159 my ( $self, $c, $content_type ) = @_;
160 $c->res->content_type('text/plain');
161 $c->res->status(415);
162 if (defined($content_type) && $content_type ne "") {
164 "Content-Type " . $content_type . " is not supported.\r\n" );
167 "Cannot find a Content-Type supported by your client.\r\n" );
172 sub serialize_bad_request {
173 my ( $self, $c, $content_type, $error ) = @_;
174 $c->res->content_type('text/plain');
175 $c->res->status(400);
177 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
181 __PACKAGE__->meta->make_immutable;
187 Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
191 This module implements the plugin loading and content-type negotiating
192 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
196 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
197 L<Catalyst::Controller::REST>,
201 See L<Catalyst::Action::REST> for authors.
205 You may distribute this code under the same terms as Perl itself.