2 # Catlyst::Action::SerializeBase.pm
3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
7 package Catalyst::Action::SerializeBase;
12 use base 'Catalyst::Action';
13 use Module::Pluggable::Object;
14 use Data::Dump qw(dump);
15 use Catalyst::Request::REST;
17 Catalyst->request_class('Catalyst::Request::REST')
18 unless Catalyst->request_class->isa('Catalyst::Request::REST');
20 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
22 sub _load_content_plugins {
24 my ( $search_path, $controller, $c ) = @_;
26 unless ( defined( $self->_loaded_plugins ) ) {
27 $self->_loaded_plugins( {} );
30 # Load the Serialize Classes
31 unless ( defined( $self->_serialize_plugins ) ) {
34 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
35 @plugins = $mpo->plugins;
36 $self->_serialize_plugins( \@plugins );
39 # Finally, we load the class. If you have a default serializer,
40 # and we still don't have a content-type that exists in the map,
42 my $sclass = $search_path . "::";
48 if ( exists $controller->config->{'serialize'} ) {
49 $c->log->info("Using deprecated configuration for Catalyst::Action::REST!");
50 $c->log->info("Please see perldoc Catalyst::Action::REST for the update guide");
51 $config = $controller->config->{'serialize'};
53 $config = $controller->config;
55 $map = $config->{'map'};
56 # If we don't have a handler for our preferred content type, try
59 my ($content_type) = grep { $map->{$_} } @{$c->request->accepted_content_types};
61 unless ( defined $content_type ) {
62 if( exists $config->{'default'} ) {
63 $content_type = $config->{'default'} ;
65 return $self->_unsupported_media_type($c, $content_type);
69 # carp about old text/x-json
70 if ($content_type eq 'text/x-json') {
71 $c->log->info('Using deprecated text/x-json content-type.');
72 $c->log->info('Use application/json instead!');
75 if ( exists( $map->{$content_type} ) ) {
77 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
78 $mc = $map->{$content_type}->[0];
79 $sarg = $map->{$content_type}->[1];
81 $mc = $map->{$content_type};
83 # TODO: Handle custom serializers more elegantly.. this is a start,
84 # but how do we determine which is Serialize and Deserialize?
91 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
92 return $self->_unsupported_media_type($c, $content_type);
95 return $self->_unsupported_media_type($c, $content_type);
97 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
98 my $load_class = $sclass;
99 $load_class =~ s/::/\//g;
100 $load_class =~ s/$/.pm/g;
101 eval { require $load_class; };
104 "Error loading $sclass for " . $content_type . ": $!" );
105 return $self->_unsupported_media_type($c, $content_type);
107 $self->_loaded_plugins->{$sclass} = 1;
111 if ($search_path eq "Catalyst::Action::Serialize") {
113 $c->response->header( 'Vary' => 'Content-Type' );
114 } elsif ($c->request->accept_only) {
115 $c->response->header( 'Vary' => 'Accept' );
117 $c->response->content_type($content_type);
120 return $sclass, $sarg, $content_type;
123 sub _unsupported_media_type {
124 my ( $self, $c, $content_type ) = @_;
125 $c->res->content_type('text/plain');
126 $c->res->status(415);
127 if (defined($content_type) && $content_type ne "") {
129 "Content-Type " . $content_type . " is not supported.\r\n" );
132 "Cannot find a Content-Type supported by your client.\r\n" );
137 sub _serialize_bad_request {
138 my ( $self, $c, $content_type, $error ) = @_;
139 $c->res->content_type('text/plain');
140 $c->res->status(400);
142 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
150 B<Catalyst::Action::SerializeBase>
152 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
156 This module implements the plugin loading and content-type negotiating
157 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
161 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
162 L<Catalyst::Controller::REST>,
166 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
168 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
172 You may distribute this code under the same terms as Perl itself.