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