remove hardcoded version strings
[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 # VERSION
10
11 sub execute {
12     my $self = shift;
13     my ( $controller, $c, $test ) = @_;
14
15     eval {
16         require XML::Simple;
17     };
18     if ($@) {
19         $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@")
20             if $c->debug;
21         return 0;
22     }
23
24     my $body = $c->request->body;
25     if ($body) {
26         my $xs = XML::Simple->new('ForceArray' => 0,);
27         my $rdata;
28         eval {
29             if(openhandle $body){ # make sure we rewind the file handle
30                 seek($body, 0, 0); # in case something has already read from it
31             }
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                 if $c->debug;
46     }
47     return 1;
48 }
49
50 __PACKAGE__->meta->make_immutable;
51
52 1;