Added logging to 4xx status handlers
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1#
2# Catlyst::Action::Deserialize
3# Created by: Adam Jacob, Marchex, <adam@marchex.com>
4#
5# $Id$
6
7package Catalyst::Action::Deserialize;
8
9use strict;
10use warnings;
11
12use base 'Catalyst::Action';
13use Module::Pluggable::Object;
14use Catalyst::Request::REST;
15
16__PACKAGE__->mk_accessors(qw(plugins));
17
18sub execute {
19 my $self = shift;
20 my ( $controller, $c, $test ) = @_;
21
eccb2137 22 my $nreq = bless( $c->request, 'Catalyst::Request::REST' );
7ad87df9 23 $c->request($nreq);
24
eccb2137 25 unless ( defined( $self->plugins ) ) {
7ad87df9 26 my $mpo = Module::Pluggable::Object->new(
eccb2137 27 'require' => 1,
28 'search_path' => ['Catalyst::Action::Deserialize'],
7ad87df9 29 );
30 my @plugins = $mpo->plugins;
eccb2137 31 $self->plugins( \@plugins );
7ad87df9 32 }
33 my $content_type = $c->request->content_type;
eccb2137 34 my $sclass = 'Catalyst::Action::Deserialize::';
7ad87df9 35 my $sarg;
36 my $map = $controller->serialize->{'map'};
eccb2137 37 if ( exists( $map->{$content_type} ) ) {
7ad87df9 38 my $mc;
eccb2137 39 if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
40 $mc = $map->{$content_type}->[0];
7ad87df9 41 $sarg = $map->{$content_type}->[1];
42 } else {
43 $mc = $map->{$content_type};
44 }
45 $sclass .= $mc;
eccb2137 46 if ( !grep( /^$sclass$/, @{ $self->plugins } ) ) {
7ad87df9 47 die "Cannot find plugin $sclass for $content_type!";
48 }
49 } else {
eccb2137 50 if ( exists( $controller->serialize->{'default'} ) ) {
7ad87df9 51 $sclass .= $controller->serialize->{'default'};
52 } else {
53 die "I cannot find a default serializer!";
54 }
55 }
56
57 my @demethods = qw(POST PUT OPTIONS);
eccb2137 58 my $method = $c->request->method;
59 if ( grep /^$method$/, @demethods ) {
60 if ( defined($sarg) ) {
61 $sclass->execute( $controller, $c, $sarg );
7ad87df9 62 } else {
eccb2137 63 $sclass->execute( $controller, $c );
7ad87df9 64 }
65 $self->NEXT::execute( @_, );
66 } else {
eccb2137 67 $self->NEXT::execute(@_);
7ad87df9 68 }
eccb2137 69}
7ad87df9 70
711;