r49@latte: adam | 2006-12-03 12:30:40 -0800
[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         return 0;
25     }
26
27     my $body = $c->request->body;
28     if ($body) {
29         my $xs = XML::Simple->new('ForceArray' => 0,);
30         my $rdata;
31         eval {
32             $rdata = $xs->XMLin( "$body" );
33         };
34         if ($@) {
35             return $@;
36         }
37         if (exists($rdata->{'data'})) {
38             $c->request->data($rdata->{'data'});
39         } else {
40             $c->request->data($rdata);
41         }
42     } else {
43         $c->log->debug(
44             'I would have deserialized, but there was nothing in the body!');
45     }
46     return 1;
47 }
48
49 1;