r1682@mbp: claco | 2008-06-16 19:54:33 -0400
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / SerializeBase.pm
CommitLineData
e601adda 1#
2# Catlyst::Action::SerializeBase.pm
be3c588a 3# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
e601adda 4#
5# $Id$
6
7package Catalyst::Action::SerializeBase;
8
9use strict;
10use warnings;
11
12use base 'Catalyst::Action';
13use Module::Pluggable::Object;
14use Data::Dump qw(dump);
9a76221e 15use Catalyst::Request::REST;
16
17Catalyst->request_class('Catalyst::Request::REST')
18 unless Catalyst->request_class->isa('Catalyst::Request::REST');
e601adda 19
20__PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
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
d4611771 39 my $content_type = $c->request->preferred_content_type || '';
e601adda 40
d6fb033c 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
e601adda 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;
faf5c20b 52 my $map;
367b3ff4 53
faf5c20b 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'};
367b3ff4 64 # If we don't have a handler for our preferred content type, try
65 # the default
66 if ( ! exists $map->{$content_type} ) {
faf5c20b 67 if( exists $config->{'default'} ) {
68 $content_type = $config->{'default'} ;
367b3ff4 69 } else {
70 return $self->_unsupported_media_type($c, $content_type);
71 }
72 }
73
e601adda 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 {
367b3ff4 94 return $self->_unsupported_media_type($c, $content_type);
e601adda 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(
d4611771 103 "Error loading $sclass for " . $content_type . ": $!" );
e601adda 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' );
9a76221e 113 } elsif ($c->request->accept_only) {
e601adda 114 $c->response->header( 'Vary' => 'Accept' );
115 }
116 $c->response->content_type($content_type);
117 }
118
119 return $sclass, $sarg, $content_type;
120}
121
122sub _unsupported_media_type {
123 my ( $self, $c, $content_type ) = @_;
124 $c->res->content_type('text/plain');
125 $c->res->status(415);
2224bad1 126 if (defined($content_type) && $content_type ne "") {
e601adda 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
136sub _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
1451;
146
147=head1 NAME
148
149B<Catalyst::Action::SerializeBase>
150
151Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
152
153=head1 DESCRIPTION
154
155This module implements the plugin loading and content-type negotiating
156code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
157
158=head1 SEE ALSO
159
160L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
161L<Catalyst::Controller::REST>,
162
163=head1 AUTHOR
164
165Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
166
167Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
168
169=head1 LICENSE
170
171You may distribute this code under the same terms as Perl itself.
172
173=cut
174