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 my $content_type = $c->request->preferred_content_type || '';
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!');
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,
50 my $sclass = $search_path . "::";
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'};
61 $config = $controller->config;
63 $map = $config->{'map'};
64 # If we don't have a handler for our preferred content type, try
66 if ( ! exists $map->{$content_type} ) {
67 if( exists $config->{'default'} ) {
68 $content_type = $config->{'default'} ;
70 return $self->_unsupported_media_type($c, $content_type);
74 if ( exists( $map->{$content_type} ) ) {
76 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
77 $mc = $map->{$content_type}->[0];
78 $sarg = $map->{$content_type}->[1];
80 $mc = $map->{$content_type};
82 # TODO: Handle custom serializers more elegantly.. this is a start,
83 # but how do we determine which is Serialize and Deserialize?
90 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
91 return $self->_unsupported_media_type($c, $content_type);
94 return $self->_unsupported_media_type($c, $content_type);
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; };
103 "Error loading $sclass for " . $content_type . ": $!" );
104 return $self->_unsupported_media_type($c, $content_type);
106 $self->_loaded_plugins->{$sclass} = 1;
110 if ($search_path eq "Catalyst::Action::Serialize") {
112 $c->response->header( 'Vary' => 'Content-Type' );
113 } elsif ($c->request->accept_only) {
114 $c->response->header( 'Vary' => 'Accept' );
116 $c->response->content_type($content_type);
119 return $sclass, $sarg, $content_type;
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 "") {
128 "Content-Type " . $content_type . " is not supported.\r\n" );
131 "Cannot find a Content-Type supported by your client.\r\n" );
136 sub _serialize_bad_request {
137 my ( $self, $c, $content_type, $error ) = @_;
138 $c->res->content_type('text/plain');
139 $c->res->status(400);
141 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
149 B<Catalyst::Action::SerializeBase>
151 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
155 This module implements the plugin loading and content-type negotiating
156 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
160 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
161 L<Catalyst::Controller::REST>,
165 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
167 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
171 You may distribute this code under the same terms as Perl itself.