no need for OurPkgVersion with modern dzil
[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 sub execute {
10     my $self = shift;
11     my ( $controller, $c, $test ) = @_;
12
13     eval {
14         require XML::Simple;
15     };
16     if ($@) {
17         $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@")
18             if $c->debug;
19         return 0;
20     }
21
22     my $body = $c->request->body;
23     if ($body) {
24         my $xs = XML::Simple->new('ForceArray' => 0,);
25         my $rdata;
26         eval {
27             if(openhandle $body){ # make sure we rewind the file handle
28                 seek($body, 0, 0); # in case something has already read from it
29             }
30             $rdata = $xs->XMLin( $body );
31         };
32         if ($@) {
33             return $@;
34         }
35         if (exists($rdata->{'data'})) {
36             $c->request->data($rdata->{'data'});
37         } else {
38             $c->request->data($rdata);
39         }
40     } else {
41         $c->log->debug(
42             'I would have deserialized, but there was nothing in the body!')
43                 if $c->debug;
44     }
45     return 1;
46 }
47
48 __PACKAGE__->meta->make_immutable;
49
50 1;