87950aa96eece0f9dcebd984d98ca37e64f5fe77
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / SerializeBase.pm
1 package Catalyst::Action::SerializeBase;
2
3 use Moose;
4 use namespace::autoclean;
5
6 extends 'Catalyst::Action';
7 use Module::Pluggable::Object;
8 use Catalyst::Request::REST;
9 use Catalyst::Utils ();
10
11 our $VERSION = '0.81';
12 $VERSION = eval $VERSION;
13
14 after BUILDARGS => sub {
15     my $class  = shift;
16     my $config = shift;
17     Catalyst::Request::REST->_insert_self_into( $config->{class} );
18 };
19
20 has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' );
21
22 sub _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
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;
44     my $map;
45
46     my $config;
47     
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};
55     } else {
56         $config = $controller;
57     }
58     $map = $config->{'map'};
59
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;
70     }
71     # then content types requested by caller
72     push @accepted_types, @{ $c->request->accepted_content_types };
73     # then the default
74     push @accepted_types, $config->{'default'} if $config->{'default'};
75     # pick the best match that we have a serializer mapping for
76     my ($content_type) = grep { $map->{$_} } @accepted_types;
77
78     return $self->_unsupported_media_type($c, $content_type)
79         if not $content_type;
80
81     # carp about old text/x-json
82     if ($content_type eq 'text/x-json') {
83         $c->log->info('Using deprecated text/x-json content-type.');
84         $c->log->info('Use application/json instead!');
85     }
86
87     if ( exists( $map->{$content_type} ) ) {
88         my $mc;
89         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
90             $mc   = $map->{$content_type}->[0];
91             $sarg = $map->{$content_type}->[1];
92         } else {
93             $mc = $map->{$content_type};
94         }
95         # TODO: Handle custom serializers more elegantly.. this is a start,
96         # but how do we determine which is Serialize and Deserialize?
97         #if ($mc =~ /^+/) {
98         #    $sclass = $mc;
99         #    $sclass =~ s/^+//g;
100         #} else {
101         $sclass .= $mc;
102         #}
103         if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
104             return $self->_unsupported_media_type($c, $content_type);
105         }
106     } else {
107         return $self->_unsupported_media_type($c, $content_type);
108     }
109     unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
110         my $load_class = $sclass;
111         $load_class =~ s/::/\//g;
112         $load_class =~ s/$/.pm/g;
113         eval { require $load_class; };
114         if ($@) {
115             $c->log->error(
116                 "Error loading $sclass for " . $content_type . ": $!" );
117             return $self->_unsupported_media_type($c, $content_type);
118         } else {
119             $self->_loaded_plugins->{$sclass} = 1;
120         }
121     }
122
123     if ($search_path eq "Catalyst::Action::Serialize") {
124         if ($content_type) {
125             $c->response->header( 'Vary' => 'Content-Type' );
126         } elsif ($c->request->accept_only) {
127             $c->response->header( 'Vary' => 'Accept' );
128         }
129         $c->response->content_type($content_type);
130     }
131
132     return $sclass, $sarg, $content_type;
133 }
134
135 sub _unsupported_media_type {
136     my ( $self, $c, $content_type ) = @_;
137     $c->res->content_type('text/plain');
138     $c->res->status(415);
139     if (defined($content_type) && $content_type ne "") {
140         $c->res->body(
141             "Content-Type " . $content_type . " is not supported.\r\n" );
142     } else {
143         $c->res->body(
144             "Cannot find a Content-Type supported by your client.\r\n" );
145     }
146     return undef;
147 }
148
149 sub _serialize_bad_request {
150     my ( $self, $c, $content_type, $error ) = @_;
151     $c->res->content_type('text/plain');
152     $c->res->status(400);
153     $c->res->body(
154         "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
155     return undef;
156 }
157
158 __PACKAGE__->meta->make_immutable;
159
160 =head1 NAME
161
162 Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
163
164 =head1 DESCRIPTION
165
166 This module implements the plugin loading and content-type negotiating
167 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
168
169 =head1 SEE ALSO
170
171 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
172 L<Catalyst::Controller::REST>,
173
174 =head1 AUTHORS
175
176 See L<Catalyst::Action::REST> for authors.
177
178 =head1 LICENSE
179
180 You may distribute this code under the same terms as Perl itself.
181
182 =cut