7a2980f946eefc1eaff5672447807746e5ee741c
[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 Data::Dump qw(dump);
15 use Catalyst::Request::REST;
16
17 Catalyst->request_class('Catalyst::Request::REST')
18     unless Catalyst->request_class->isa('Catalyst::Request::REST');
19
20 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
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     my $content_type = $c->request->preferred_content_type || '';
40
41     # carp about old text/x-json
42     if ($content_type eq 'text/x-json') {
43         $c->log->info('Using deprecated text/x-json content-type.');
44         $c->log->info('Use application/json instead!');
45     }
46
47     # Finally, we load the class.  If you have a default serializer,
48     # and we still don't have a content-type that exists in the map,
49     # we'll use it.
50     my $sclass = $search_path . "::";
51     my $sarg;
52     my $map;
53
54     my $config;
55     
56     if ( exists $controller->config->{'serialize'} ) {
57         $c->log->info("Using deprecated configuration for Catalyst::Action::REST!");
58         $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide");
59         $config = $controller->config->{'serialize'};
60     } else {
61         $config = $controller->config;
62     }
63     $map = $config->{'map'};
64     # If we don't have a handler for our preferred content type, try
65     # the default
66     if ( ! exists $map->{$content_type} ) {
67         if( exists $config->{'default'} ) {
68             $content_type = $config->{'default'} ;
69         } else {
70             return $self->_unsupported_media_type($c, $content_type);
71         }
72     }
73
74     if ( exists( $map->{$content_type} ) ) {
75         my $mc;
76         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
77             $mc   = $map->{$content_type}->[0];
78             $sarg = $map->{$content_type}->[1];
79         } else {
80             $mc = $map->{$content_type};
81         }
82         # TODO: Handle custom serializers more elegantly.. this is a start,
83         # but how do we determine which is Serialize and Deserialize?
84         #if ($mc =~ /^+/) {
85         #    $sclass = $mc;
86         #    $sclass =~ s/^+//g;
87         #} else {
88         $sclass .= $mc;
89         #}
90         if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
91             return $self->_unsupported_media_type($c, $content_type);
92         }
93     } else {
94         return $self->_unsupported_media_type($c, $content_type);
95     }
96     unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
97         my $load_class = $sclass;
98         $load_class =~ s/::/\//g;
99         $load_class =~ s/$/.pm/g;
100         eval { require $load_class; };
101         if ($@) {
102             $c->log->error(
103                 "Error loading $sclass for " . $content_type . ": $!" );
104             return $self->_unsupported_media_type($c, $content_type);
105         } else {
106             $self->_loaded_plugins->{$sclass} = 1;
107         }
108     }
109
110     if ($search_path eq "Catalyst::Action::Serialize") {
111         if ($content_type) {
112             $c->response->header( 'Vary' => 'Content-Type' );
113         } elsif ($c->request->accept_only) {
114             $c->response->header( 'Vary' => 'Accept' );
115         }
116         $c->response->content_type($content_type);
117     }
118
119     return $sclass, $sarg, $content_type;
120 }
121
122 sub _unsupported_media_type {
123     my ( $self, $c, $content_type ) = @_;
124     $c->res->content_type('text/plain');
125     $c->res->status(415);
126     if (defined($content_type) && $content_type ne "") {
127         $c->res->body(
128             "Content-Type " . $content_type . " is not supported.\r\n" );
129     } else {
130         $c->res->body(
131             "Cannot find a Content-Type supported by your client.\r\n" );
132     }
133     return undef;
134 }
135
136 sub _serialize_bad_request {
137     my ( $self, $c, $content_type, $error ) = @_;
138     $c->res->content_type('text/plain');
139     $c->res->status(400);
140     $c->res->body(
141         "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
142     return undef;
143 }
144
145 1;
146
147 =head1 NAME
148
149 B<Catalyst::Action::SerializeBase>
150
151 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
152
153 =head1 DESCRIPTION
154
155 This module implements the plugin loading and content-type negotiating
156 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
157
158 =head1 SEE ALSO
159
160 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
161 L<Catalyst::Controller::REST>,
162
163 =head1 AUTHOR
164
165 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
166
167 Marchex, Inc. paid me while I developed this module.  (http://www.marchex.com)
168
169 =head1 LICENSE
170
171 You may distribute this code under the same terms as Perl itself.
172
173 =cut
174