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 HTTP::Headers::Util qw(split_header_words);
17 __PACKAGE__->mk_accessors(qw(_serialize_plugins _loaded_plugins));
19 sub _load_content_plugins {
21 my ( $search_path, $controller, $c ) = @_;
23 unless ( defined( $self->_loaded_plugins ) ) {
24 $self->_loaded_plugins( {} );
27 # Load the Serialize Classes
28 unless ( defined( $self->_serialize_plugins ) ) {
31 Module::Pluggable::Object->new( 'search_path' => [$search_path], );
32 @plugins = $mpo->plugins;
33 $self->_serialize_plugins( \@plugins );
36 # First, we use the content type in the HTTP Request. It wins all.
37 my $content_type = $c->request->content_type || '';
39 # Second, we allow GET requests to tunnel the content-type as
41 if (!$content_type && $c->req->method eq "GET" && $c->req->param('content-type')) {
43 $content_type = $c->req->param('content-type');
46 # Third, we parse the Accept header, and see if the client
47 # takes a format we understand.
49 # This is taken from chansen's Apache2::UploadProgress.
51 if ( !$content_type && $c->request->header('Accept') ) {
52 my $accept_header = $c->request->header('Accept');
56 foreach my $pair ( split_header_words($accept_header) ) {
58 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
60 unless ( defined $qvalue ) {
61 $qvalue = 1 - ( ++$counter / 1000 );
64 $accept{$type} = sprintf( '%.3f', $qvalue );
67 foreach my $type ( sort { $accept{$b} <=> $accept{$a} } keys %accept )
70 if ( exists $controller->config->{'serialize'}->{'map'}->{$type} )
72 $content_type = $type;
79 # Finally, we load the class. If you have a default serializer,
80 # and we still don't have a content-type that exists in the map,
82 my $sclass = $search_path . "::";
84 my $map = $controller->config->{'serialize'}->{'map'};
85 if ( exists( $map->{$content_type} ) ) {
87 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
88 $mc = $map->{$content_type}->[0];
89 $sarg = $map->{$content_type}->[1];
91 $mc = $map->{$content_type};
93 # TODO: Handle custom serializers more elegantly.. this is a start,
94 # but how do we determine which is Serialize and Deserialize?
101 if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) {
102 return $self->_unsupported_media_type($c, $content_type);
105 if ( exists( $controller->config->{'serialize'}->{'default'} ) ) {
106 $sclass .= $controller->config->{'serialize'}->{'default'};
108 return $self->_unsupported_media_type($c, $content_type);
111 unless ( exists( $self->_loaded_plugins->{$sclass} ) ) {
112 my $load_class = $sclass;
113 $load_class =~ s/::/\//g;
114 $load_class =~ s/$/.pm/g;
115 eval { require $load_class; };
118 "Error loading $sclass for " . $content_type . ": $!" )
119 if $c->log->is_debug;
120 return $self->_unsupported_media_type($c, $content_type);
122 $self->_loaded_plugins->{$sclass} = 1;
126 if ($search_path eq "Catalyst::Action::Serialize") {
128 $c->response->header( 'Vary' => 'Content-Type' );
129 } elsif ($used_accept) {
130 $c->response->header( 'Vary' => 'Accept' );
132 $c->response->content_type($content_type);
135 return $sclass, $sarg, $content_type;
138 sub _unsupported_media_type {
139 my ( $self, $c, $content_type ) = @_;
140 $c->res->content_type('text/plain');
141 $c->res->status(415);
142 if (defined($content_type)) {
144 "Content-Type " . $content_type . " is not supported.\r\n" );
147 "Cannot find a Content-Type supported by your client.\r\n" );
152 sub _serialize_bad_request {
153 my ( $self, $c, $content_type, $error ) = @_;
154 $c->res->content_type('text/plain');
155 $c->res->status(400);
157 "Content-Type " . $content_type . " had a problem with your request.\r\n***ERROR***\r\n$error" );
165 B<Catalyst::Action::SerializeBase>
167 Base class for Catalyst::Action::Serialize and Catlayst::Action::Deserialize.
171 This module implements the plugin loading and content-type negotiating
172 code for L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>.
176 L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>,
177 L<Catalyst::Controller::REST>,
181 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway.
183 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
187 You may distribute this code under the same terms as Perl itself.