resolved
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / SerializeBase.pm
CommitLineData
e601adda 1package Catalyst::Action::SerializeBase;
2
930013e6 3use Moose;
4use namespace::autoclean;
e601adda 5
930013e6 6extends 'Catalyst::Action';
e601adda 7use Module::Pluggable::Object;
9a76221e 8use Catalyst::Request::REST;
5132f5e4 9use Catalyst::Utils ();
9a76221e 10
05009b91 11after BUILDARGS => sub {
12 my $class = shift;
13 my $config = shift;
14 Catalyst::Request::REST->_insert_self_into( $config->{class} );
15};
e601adda 16
05009b91 17has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' );
e601adda 18
19sub _load_content_plugins {
20 my $self = shift;
21 my ( $search_path, $controller, $c ) = @_;
22
23 unless ( defined( $self->_loaded_plugins ) ) {
24 $self->_loaded_plugins( {} );
25 }
26
27 # Load the Serialize Classes
28 unless ( defined( $self->_serialize_plugins ) ) {
29 my @plugins;
30 my $mpo =
31 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
32 @plugins = $mpo->plugins;
33 $self->_serialize_plugins( \@plugins );
34 }
35
e601adda 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,
38 # we'll use it.
39 my $sclass = $search_path . "::";
40 my $sarg;
faf5c20b 41 my $map;
367b3ff4 42
faf5c20b 43 my $config;
44
07682cbc 45 if ( exists $controller->{'serialize'} ) {
9cd203c9 46 $c->log->info("Catalyst::Action::REST - deprecated use of 'serialize' for configuration.");
47 $c->log->info("Please see 'CONFIGURATION' in Catalyst::Controller::REST.");
07682cbc 48 $config = $controller->{'serialize'};
7b8c5be2 49 # if they're using the deprecated config, they may be expecting a
50 # default mapping too.
51 $config->{map} ||= $controller->{map};
faf5c20b 52 } else {
07682cbc 53 $config = $controller;
faf5c20b 54 }
55 $map = $config->{'map'};
c0aef9cd 56
a51e7bbd 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}
63 ) {
64 # convert to array if not already a ref
65 $stashed = [ $stashed ] if not ref $stashed;
66 push @accepted_types, @$stashed;
367b3ff4 67 }
a51e7bbd 68 # then content types requested by caller
69 push @accepted_types, @{ $c->request->accepted_content_types };
70 # then the default
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;
74
b3996af8 75 return $self->unsupported_media_type($c, $content_type)
a51e7bbd 76 if not $content_type;
367b3ff4 77
c0aef9cd 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!');
82 }
83
e601adda 84 if ( exists( $map->{$content_type} ) ) {
85 my $mc;
86 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
87 $mc = $map->{$content_type}->[0];
88 $sarg = $map->{$content_type}->[1];
89 } else {
90 $mc = $map->{$content_type};
91 }
92 # TODO: Handle custom serializers more elegantly.. this is a start,
93 # but how do we determine which is Serialize and Deserialize?
94 #if ($mc =~ /^+/) {
95 # $sclass = $mc;
96 # $sclass =~ s/^+//g;
97 #} else {
98 $sclass .= $mc;
99 #}
100 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
b3996af8 101 return $self->unsupported_media_type($c, $content_type);
e601adda 102 }
103 } else {
b3996af8 104 return $self->unsupported_media_type($c, $content_type);
e601adda 105 }
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; };
111 if ($@) {
112 $c->log->error(
d4611771 113 "Error loading $sclass for " . $content_type . ": $!" );
b3996af8 114 return $self->unsupported_media_type($c, $content_type);
e601adda 115 } else {
116 $self->_loaded_plugins->{$sclass} = 1;
117 }
118 }
119
120 if ($search_path eq "Catalyst::Action::Serialize") {
b00b32de 121 unless( $c->response->header( 'Vary' ) ) {
122 if ($content_type) {
123 $c->response->header( 'Vary' => 'Content-Type' );
124 } elsif ($c->request->accept_only) {
125 $c->response->header( 'Vary' => 'Accept' );
126 }
e601adda 127 }
128 $c->response->content_type($content_type);
129 }
130
131 return $sclass, $sarg, $content_type;
132}
133
b3996af8 134sub unsupported_media_type {
e601adda 135 my ( $self, $c, $content_type ) = @_;
136 $c->res->content_type('text/plain');
137 $c->res->status(415);
2224bad1 138 if (defined($content_type) && $content_type ne "") {
e601adda 139 $c->res->body(
140 "Content-Type " . $content_type . " is not supported.\r\n" );
141 } else {
142 $c->res->body(
143 "Cannot find a Content-Type supported by your client.\r\n" );
144 }
145 return undef;
146}
147
b3996af8 148sub serialize_bad_request {
e601adda 149 my ( $self, $c, $content_type, $error ) = @_;
150 $c->res->content_type('text/plain');
151 $c->res->status(400);
152 $c->res->body(
153 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
154 return undef;
155}
156
05009b91 157__PACKAGE__->meta->make_immutable;
e601adda 158
8d30469f 1591;
160
e601adda 161=head1 NAME
162
26b59bcb 163Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
e601adda 164
165=head1 DESCRIPTION
166
167This module implements the plugin loading and content-type negotiating
168code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
169
170=head1 SEE ALSO
171
172L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
173L<Catalyst::Controller::REST>,
174
5cb5f6bb 175=head1 AUTHORS
e601adda 176
5cb5f6bb 177See L<Catalyst::Action::REST> for authors.
e601adda 178
179=head1 LICENSE
180
181You may distribute this code under the same terms as Perl itself.
182
183=cut