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 . "::";
46 my $map = $controller->config->{'serialize'}->{'map'};
47 if ( exists( $map->{$content_type} ) ) {
49 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
50 $mc = $map->{$content_type}->[0];
51 $sarg = $map->{$content_type}->[1];
53 $mc = $map->{$content_type};
55 # TODO: Handle custom serializers more elegantly.. this is a start,
56 # but how do we determine which is Serialize and Deserialize?
63 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
64 return $self->_unsupported_media_type($c, $content_type);
67 if ( exists( $controller->config->{'serialize'}->{'default'} ) ) {
68 $sclass .= $controller->config->{'serialize'}->{'default'};
70 return $self->_unsupported_media_type($c, $content_type);
73 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
74 my $load_class = $sclass;
75 $load_class =~ s/::/\//g;
76 $load_class =~ s/$/.pm/g;
77 eval { require $load_class; };
80 "Error loading $sclass for " . $content_type . ": $!" )
82 return $self->_unsupported_media_type($c, $content_type);
84 $self->_loaded_plugins->{$sclass} = 1;
88 if ($search_path eq "Catalyst::Action::Serialize") {
90 $c->response->header( 'Vary' => 'Content-Type' );
91 } elsif ($c->request->accept_only) {
92 $c->response->header( 'Vary' => 'Accept' );
94 $c->response->content_type($content_type);
97 return $sclass, $sarg, $content_type;
100 sub _unsupported_media_type {
101 my ( $self, $c, $content_type ) = @_;
102 $c->res->content_type('text/plain');
103 $c->res->status(415);
104 if (defined($content_type) && $content_type ne "") {
106 "Content-Type " . $content_type . " is not supported.\r\n" );
109 "Cannot find a Content-Type supported by your client.\r\n" );
114 sub _serialize_bad_request {
115 my ( $self, $c, $content_type, $error ) = @_;
116 $c->res->content_type('text/plain');
117 $c->res->status(400);
119 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
127 B<Catalyst::Action::SerializeBase>
129 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
133 This module implements the plugin loading and content-type negotiating
134 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
138 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
139 L<Catalyst::Controller::REST>,
143 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
145 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
149 You may distribute this code under the same terms as Perl itself.