remove hardcoded version strings
[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 # VERSION
12
13 after BUILDARGS => sub {
14     my $class  = shift;
15     my $config = shift;
16     Catalyst::Request::REST->_insert_self_into( $config->{class} );
17 };
18
19 has [qw(_serialize_plugins _loaded_plugins)] => ( is => 'rw' );
20
21 sub _load_content_plugins {
22     my $self = shift;
23     my ( $search_path, $controller, $c ) = @_;
24
25     unless ( defined( $self->_loaded_plugins ) ) {
26         $self->_loaded_plugins( {} );
27     }
28
29     # Load the Serialize Classes
30     unless ( defined( $self->_serialize_plugins ) ) {
31         my @plugins;
32         my $mpo =
33           Module::Pluggable::Object->new( 'search_path' => [$search_path], );
34         @plugins = $mpo->plugins;
35         $self->_serialize_plugins( \@plugins );
36     }
37
38     # Finally, we load the class.  If you have a default serializer,
39     # and we still don't have a content-type that exists in the map,
40     # we'll use it.
41     my $sclass = $search_path . "::";
42     my $sarg;
43     my $map;
44
45     my $config;
46     
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};
54     } else {
55         $config = $controller;
56     }
57     $map = $config->{'map'};
58
59     # pick preferred content type
60     my @accepted_types; # priority order, best first
61     # give top priority to content type specified by stash, if any
62     my $content_type_stash_key = $config->{content_type_stash_key};
63     if ($content_type_stash_key
64         and my $stashed = $c->stash->{$content_type_stash_key}
65     ) {
66         # convert to array if not already a ref
67         $stashed = [ $stashed ] if not ref $stashed;
68         push @accepted_types, @$stashed;
69     }
70     # then content types requested by caller
71     push @accepted_types, @{ $c->request->accepted_content_types };
72     # then the default
73     push @accepted_types, $config->{'default'} if $config->{'default'};
74     # pick the best match that we have a serializer mapping for
75     my ($content_type) = grep { $map->{$_} } @accepted_types;
76
77     return $self->unsupported_media_type($c, $content_type)
78         if not $content_type;
79
80     # carp about old text/x-json
81     if ($content_type eq 'text/x-json') {
82         $c->log->info('Using deprecated text/x-json content-type.');
83         $c->log->info('Use application/json instead!');
84     }
85
86     if ( exists( $map->{$content_type} ) ) {
87         my $mc;
88         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
89             $mc   = $map->{$content_type}->[0];
90             $sarg = $map->{$content_type}->[1];
91         } else {
92             $mc = $map->{$content_type};
93         }
94         # TODO: Handle custom serializers more elegantly.. this is a start,
95         # but how do we determine which is Serialize and Deserialize?
96         #if ($mc =~ /^+/) {
97         #    $sclass = $mc;
98         #    $sclass =~ s/^+//g;
99         #} else {
100         $sclass .= $mc;
101         #}
102         if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
103             return $self->unsupported_media_type($c, $content_type);
104         }
105     } else {
106         return $self->unsupported_media_type($c, $content_type);
107     }
108     unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
109         my $load_class = $sclass;
110         $load_class =~ s/::/\//g;
111         $load_class =~ s/$/.pm/g;
112         eval { require $load_class; };
113         if ($@) {
114             $c->log->error(
115                 "Error loading $sclass for " . $content_type . ": $!" );
116             return $self->unsupported_media_type($c, $content_type);
117         } else {
118             $self->_loaded_plugins->{$sclass} = 1;
119         }
120     }
121
122     if ($search_path eq "Catalyst::Action::Serialize") {
123         if ($content_type) {
124             $c->response->header( 'Vary' => 'Content-Type' );
125         } elsif ($c->request->accept_only) {
126             $c->response->header( 'Vary' => 'Accept' );
127         }
128         $c->response->content_type($content_type);
129     }
130
131     return $sclass, $sarg, $content_type;
132 }
133
134 sub unsupported_media_type {
135     my ( $self, $c, $content_type ) = @_;
136     $c->res->content_type('text/plain');
137     $c->res->status(415);
138     if (defined($content_type) && $content_type ne "") {
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
148 sub serialize_bad_request {
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
157 __PACKAGE__->meta->make_immutable;
158
159 1;
160
161 =head1 NAME
162
163 Catalyst::Action::SerializeBase - Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
164
165 =head1 DESCRIPTION
166
167 This module implements the plugin loading and content-type negotiating
168 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
169
170 =head1 SEE ALSO
171
172 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
173 L<Catalyst::Controller::REST>,
174
175 =head1 AUTHORS
176
177 See L<Catalyst::Action::REST> for authors.
178
179 =head1 LICENSE
180
181 You may distribute this code under the same terms as Perl itself.
182
183 =cut