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'};
48 # If we don't have a handler for our preferred content type, try
50 if ( ! exists $map->{$content_type} ) {
51 if( exists $controller->config->{'serialize'}->{'default'} ) {
52 $content_type = $controller->config->{'serialize'}->{'default'} ;
54 return $self->_unsupported_media_type($c, $content_type);
58 if ( exists( $map->{$content_type} ) ) {
60 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
61 $mc = $map->{$content_type}->[0];
62 $sarg = $map->{$content_type}->[1];
64 $mc = $map->{$content_type};
66 # TODO: Handle custom serializers more elegantly.. this is a start,
67 # but how do we determine which is Serialize and Deserialize?
74 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
75 return $self->_unsupported_media_type($c, $content_type);
78 return $self->_unsupported_media_type($c, $content_type);
80 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
81 my $load_class = $sclass;
82 $load_class =~ s/::/\//g;
83 $load_class =~ s/$/.pm/g;
84 eval { require $load_class; };
87 "Error loading $sclass for " . $content_type . ": $!" )
89 return $self->_unsupported_media_type($c, $content_type);
91 $self->_loaded_plugins->{$sclass} = 1;
95 if ($search_path eq "Catalyst::Action::Serialize") {
97 $c->response->header( 'Vary' => 'Content-Type' );
98 } elsif ($c->request->accept_only) {
99 $c->response->header( 'Vary' => 'Accept' );
101 $c->response->content_type($content_type);
104 return $sclass, $sarg, $content_type;
107 sub _unsupported_media_type {
108 my ( $self, $c, $content_type ) = @_;
109 $c->res->content_type('text/plain');
110 $c->res->status(415);
111 if (defined($content_type) && $content_type ne "") {
113 "Content-Type " . $content_type . " is not supported.\r\n" );
116 "Cannot find a Content-Type supported by your client.\r\n" );
121 sub _serialize_bad_request {
122 my ( $self, $c, $content_type, $error ) = @_;
123 $c->res->content_type('text/plain');
124 $c->res->status(400);
126 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
134 B<Catalyst::Action::SerializeBase>
136 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
140 This module implements the plugin loading and content-type negotiating
141 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
145 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
146 L<Catalyst::Controller::REST>,
150 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
152 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
156 You may distribute this code under the same terms as Perl itself.