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 | |
cc188065 |
9 | # VERSION |
f465980c |
10 | |
e601adda |
11 | sub execute { |
12 | my $self = shift; |
13 | my ( $controller, $c, $test ) = @_; |
14 | |
15 | eval { |
16 | require XML::Simple; |
17 | }; |
18 | if ($@) { |
d4611771 |
19 | $c->log->debug("Could not load XML::Simple, refusing to deserialize: $@") |
20 | if $c->debug; |
e601adda |
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 { |
1bb5ad32 |
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 ); |
e601adda |
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( |
d4611771 |
44 | 'I would have deserialized, but there was nothing in the body!') |
45 | if $c->debug; |
e601adda |
46 | } |
47 | return 1; |
48 | } |
49 | |
24748286 |
50 | __PACKAGE__->meta->make_immutable; |
51 | |
e601adda |
52 | 1; |