2 # Catlyst::Action::SerializeBase.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.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 # 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,
44 my $sclass = $search_path . "::";
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'};
55 $config = $controller->config;
57 $map = $config->{'map'};
58 # If we don't have a handler for our preferred content type, try
60 if ( ! exists $map->{$content_type} ) {
61 if( exists $config->{'default'} ) {
62 $content_type = $config->{'default'} ;
64 return $self->_unsupported_media_type($c, $content_type);
68 if ( exists( $map->{$content_type} ) ) {
70 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
71 $mc = $map->{$content_type}->[0];
72 $sarg = $map->{$content_type}->[1];
74 $mc = $map->{$content_type};
76 # TODO: Handle custom serializers more elegantly.. this is a start,
77 # but how do we determine which is Serialize and Deserialize?
84 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
85 return $self->_unsupported_media_type($c, $content_type);
88 return $self->_unsupported_media_type($c, $content_type);
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; };
97 "Error loading $sclass for " . $content_type . ": $!" )
99 return $self->_unsupported_media_type($c, $content_type);
101 $self->_loaded_plugins->{$sclass} = 1;
105 if ($search_path eq "Catalyst::Action::Serialize") {
107 $c->response->header( 'Vary' => 'Content-Type' );
108 } elsif ($c->request->accept_only) {
109 $c->response->header( 'Vary' => 'Accept' );
111 $c->response->content_type($content_type);
114 return $sclass, $sarg, $content_type;
117 sub _unsupported_media_type {
118 my ( $self, $c, $content_type ) = @_;
119 $c->res->content_type('text/plain');
120 $c->res->status(415);
121 if (defined($content_type) && $content_type ne "") {
123 "Content-Type " . $content_type . " is not supported.\r\n" );
126 "Cannot find a Content-Type supported by your client.\r\n" );
131 sub _serialize_bad_request {
132 my ( $self, $c, $content_type, $error ) = @_;
133 $c->res->content_type('text/plain');
134 $c->res->status(400);
136 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
144 B<Catalyst::Action::SerializeBase>
146 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
150 This module implements the plugin loading and content-type negotiating
151 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
155 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
156 L<Catalyst::Controller::REST>,
160 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
162 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
166 You may distribute this code under the same terms as Perl itself.