r1144@mbp: claco | 2008-01-03 19:43:42 -0500
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / XML / Simple.pm
1 #
2 # Catlyst::Action::Deserialize::XML::Simple.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
5 #
6 # $Id$
7
8 package Catalyst::Action::Deserialize::XML::Simple;
9
10 use strict;
11 use warnings;
12
13 use base 'Catalyst::Action';
14
15 sub execute {
16     my $self = shift;
17     my ( $controller, $c, $test ) = @_;
18
19     eval {
20         require XML::Simple;
21     };
22     if ($@) {
23         $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@")
24             if $c->debug;
25         return 0;
26     }
27
28     my $body = $c->request->body;
29     if ($body) {
30         my $xs = XML::Simple->new('ForceArray' => 0,);
31         my $rdata;
32         eval {
33             $rdata = $xs->XMLin( "$body" );
34         };
35         if ($@) {
36             return $@;
37         }
38         if (exists($rdata->{'data'})) {
39             $c->request->data($rdata->{'data'});
40         } else {
41             $c->request->data($rdata);
42         }
43     } else {
44         $c->log->debug(
45             'I would have deserialized, but there was nothing in the body!')
46                 if $c->debug;
47     }
48     return 1;
49 }
50
51 1;