f7843c21c84696173fd6cba9967ee66e5010bdab
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / XML / Simple.pm
1 package Catalyst::Action::Deserialize::XML::Simple;
2
3 use Moose;
4 use namespace::autoclean;
5 use Scalar::Util qw(openhandle);
6
7 extends 'Catalyst::Action';
8
9 our $VERSION = '1.01';
10 $VERSION = eval $VERSION;
11
12 sub execute {
13     my $self = shift;
14     my ( $controller, $c, $test ) = @_;
15
16     eval {
17         require XML::Simple;
18     };
19     if ($@) {
20         $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@")
21             if $c->debug;
22         return 0;
23     }
24
25     my $body = $c->request->body;
26     if ($body) {
27         my $xs = XML::Simple->new('ForceArray' => 0,);
28         my $rdata;
29         eval {
30             if(openhandle $body){ # make sure we rewind the file handle
31                 seek($body, 0, 0); # in case something has already read from it
32             }
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 __PACKAGE__->meta->make_immutable;
52
53 1;