Commit | Line | Data |
e601adda |
1 | package Catalyst::Action::Deserialize::XML::Simple; |
2 | |
930013e6 |
3 | use Moose; |
4 | use namespace::autoclean; |
1bb5ad32 |
5 | use Scalar::Util qw(openhandle); |
e601adda |
6 | |
930013e6 |
7 | extends 'Catalyst::Action'; |
e601adda |
8 | |
1c4b501b |
9 | our $VERSION = '0.93'; |
f465980c |
10 | $VERSION = eval $VERSION; |
11 | |
e601adda |
12 | sub execute { |
13 | my $self = shift; |
14 | my ( $controller, $c, $test ) = @_; |
15 | |
16 | eval { |
17 | require XML::Simple; |
18 | }; |
19 | if ($@) { |
d4611771 |
20 | $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@") |
21 | if $c->debug; |
e601adda |
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 { |
1bb5ad32 |
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 ); |
e601adda |
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( |
d4611771 |
45 | 'I would have deserialized, but there was nothing in the body!') |
46 | if $c->debug; |
e601adda |
47 | } |
48 | return 1; |
49 | } |
50 | |
24748286 |
51 | __PACKAGE__->meta->make_immutable; |
52 | |
e601adda |
53 | 1; |