ditch Data::Dump
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / SerializeBase.pm
1 #
2 # Catlyst::Action::SerializeBase.pm
3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 #
5 # $Id$
6
7 package Catalyst::Action::SerializeBase;
8
9 use strict;
10 use warnings;
11
12 use base 'Catalyst::Action';
13 use Module::Pluggable::Object;
14 use Catalyst::Request::REST;
15 use Catalyst::Utils ();
16
17 sub new {
18   my $class  = shift;
19   my $config = shift;
20   Catalyst::Request::REST->_insert_self_into(
21     Catalyst::Utils::class2appclass($config->{class})
22   );
23   return $class->SUPER::new($config, @_);
24 }
25
26 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
27
28 sub _load_content_plugins {
29     my $self = shift;
30     my ( $search_path, $controller, $c ) = @_;
31
32     unless ( defined( $self->_loaded_plugins ) ) {
33         $self->_loaded_plugins( {} );
34     }
35
36     # Load the Serialize Classes
37     unless ( defined( $self->_serialize_plugins ) ) {
38         my @plugins;
39         my $mpo =
40           Module::Pluggable::Object->new( 'search_path' => [$search_path], );
41         @plugins = $mpo->plugins;
42         $self->_serialize_plugins( \@plugins );
43     }
44
45     # Finally, we load the class.  If you have a default serializer,
46     # and we still don't have a content-type that exists in the map,
47     # we'll use it.
48     my $sclass = $search_path . "::";
49     my $sarg;
50     my $map;
51
52     my $config;
53     
54     if ( exists $controller->{'serialize'} ) {
55         $c->log->info("Using deprecated configuration for Catalyst::Action::REST!");
56         $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide");
57         $config = $controller->{'serialize'};
58     } else {
59         $config = $controller;
60     }
61     $map = $config->{'map'};
62
63     # pick preferred content type
64     my @accepted_types; # priority order, best first
65     # give top priority to content type specified by stash, if any
66     my $content_type_stash_key = $config->{content_type_stash_key};
67     if ($content_type_stash_key
68         and my $stashed = $c->stash->{$content_type_stash_key}
69     ) {
70         # convert to array if not already a ref
71         $stashed = [ $stashed ] if not ref $stashed;
72         push @accepted_types, @$stashed;
73     }
74     # then content types requested by caller
75     push @accepted_types, @{ $c->request->accepted_content_types };
76     # then the default
77     push @accepted_types, $config->{'default'} if $config->{'default'};
78     # pick the best match that we have a serializer mapping for
79     my ($content_type) = grep { $map->{$_} } @accepted_types;
80
81     return $self->_unsupported_media_type($c, $content_type)
82         if not $content_type;
83
84     # carp about old text/x-json
85     if ($content_type eq 'text/x-json') {
86         $c->log->info('Using deprecated text/x-json content-type.');
87         $c->log->info('Use application/json instead!');
88     }
89
90     if ( exists( $map->{$content_type} ) ) {
91         my $mc;
92         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
93             $mc   = $map->{$content_type}->[0];
94             $sarg = $map->{$content_type}->[1];
95         } else {
96             $mc = $map->{$content_type};
97         }
98         # TODO: Handle custom serializers more elegantly.. this is a start,
99         # but how do we determine which is Serialize and Deserialize?
100         #if ($mc =~ /^+/) {
101         #    $sclass = $mc;
102         #    $sclass =~ s/^+//g;
103         #} else {
104         $sclass .= $mc;
105         #}
106         if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
107             return $self->_unsupported_media_type($c, $content_type);
108         }
109     } else {
110         return $self->_unsupported_media_type($c, $content_type);
111     }
112     unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
113         my $load_class = $sclass;
114         $load_class =~ s/::/\//g;
115         $load_class =~ s/$/.pm/g;
116         eval { require $load_class; };
117         if ($@) {
118             $c->log->error(
119                 "Error loading $sclass for " . $content_type . ": $!" );
120             return $self->_unsupported_media_type($c, $content_type);
121         } else {
122             $self->_loaded_plugins->{$sclass} = 1;
123         }
124     }
125
126     if ($search_path eq "Catalyst::Action::Serialize") {
127         if ($content_type) {
128             $c->response->header( 'Vary' => 'Content-Type' );
129         } elsif ($c->request->accept_only) {
130             $c->response->header( 'Vary' => 'Accept' );
131         }
132         $c->response->content_type($content_type);
133     }
134
135     return $sclass, $sarg, $content_type;
136 }
137
138 sub _unsupported_media_type {
139     my ( $self, $c, $content_type ) = @_;
140     $c->res->content_type('text/plain');
141     $c->res->status(415);
142     if (defined($content_type) && $content_type ne "") {
143         $c->res->body(
144             "Content-Type " . $content_type . " is not supported.\r\n" );
145     } else {
146         $c->res->body(
147             "Cannot find a Content-Type supported by your client.\r\n" );
148     }
149     return undef;
150 }
151
152 sub _serialize_bad_request {
153     my ( $self, $c, $content_type, $error ) = @_;
154     $c->res->content_type('text/plain');
155     $c->res->status(400);
156     $c->res->body(
157         "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
158     return undef;
159 }
160
161 1;
162
163 =head1 NAME
164
165 B<Catalyst::Action::SerializeBase>
166
167 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
168
169 =head1 DESCRIPTION
170
171 This module implements the plugin loading and content-type negotiating
172 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
173
174 =head1 SEE ALSO
175
176 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
177 L<Catalyst::Controller::REST>,
178
179 =head1 AUTHOR
180
181 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
182
183 Marchex, Inc. paid me while I developed this module.  (http://www.marchex.com)
184
185 =head1 LICENSE
186
187 You may distribute this code under the same terms as Perl itself.
188
189 =cut
190