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