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