comparison Lib/IMPL/Web/Application/Action.pm @ 321:3dc9260017ad

Added JSON support for the request action
author cin
date Mon, 20 May 2013 01:14:27 +0400
parents 28eba7e0c592
children cca158327c47
comparison
equal deleted inserted replaced
320:28eba7e0c592 321:3dc9260017ad
4 use Carp qw(carp); 4 use Carp qw(carp);
5 5
6 use IMPL::Const qw(:prop); 6 use IMPL::Const qw(:prop);
7 use IMPL::Web::CGIWrapper(); 7 use IMPL::Web::CGIWrapper();
8 use URI; 8 use URI;
9 use JSON;
9 10
10 use IMPL::declare { 11 use IMPL::declare {
11 base => [ 12 base => [
12 'IMPL::Object' => undef, 13 'IMPL::Object' => undef,
13 'IMPL::Object::Autofill' => '@_' 14 'IMPL::Object::Autofill' => '@_'
14 ], 15 ],
15 props => [ 16 props => [
16 application => PROP_RO, 17 application => PROP_RO,
17 query => PROP_RO, 18 query => PROP_RO,
18 context => PROP_RW 19 context => PROP_RW,
20 _jsonData => PROP_RW,
19 ] 21 ]
20 }; 22 };
21 23
22 sub CTOR { 24 sub CTOR {
23 my ($this) = @_; 25 my ($this) = @_;
47 my $value; 49 my $value;
48 50
49 if ( 51 if (
50 $this->requestMethod eq 'GET' 52 $this->requestMethod eq 'GET'
51 or 53 or
52 $this->query->content_type eq 'multipart/form-data' 54 $this->contentType eq 'multipart/form-data'
53 or 55 or
54 $this->query->content_type eq 'application/x-www-form-urlencoded' 56 $this->contentType eq 'application/x-www-form-urlencoded'
55 ) { 57 ) {
56 $value = scalar( $this->query->param($name) ); 58 $value = scalar( $this->query->param($name) );
57 } else { 59 } else {
58 $value = scalar( $this->query->url_param($name) ); 60 $value = scalar( $this->query->url_param($name) );
59 } 61 }
66 68
67 $this->_launder(scalar( $this->query->url_param($name) ), $rx); 69 $this->_launder(scalar( $this->query->url_param($name) ), $rx);
68 } 70 }
69 71
70 sub rawData { 72 sub rawData {
71 my ($this) = @_; 73 my ($this, $decode) = @_;
72 74
73 local $IMPL::Web::CGIWrapper::NO_DECODE = 1; 75 local $IMPL::Web::CGIWrapper::NO_DECODE = $decode ? 0 : 1;
74 if ($this->requestMethod eq 'POST') { 76 if ($this->requestMethod eq 'POST') {
75 return $this->query->param('POSTDATA'); 77 return $this->query->param('POSTDATA');
76 } elsif($this->requestMethod eq 'PUT') { 78 } elsif($this->requestMethod eq 'PUT') {
77 return $this->query->param('PUTDATA'); 79 return $this->query->param('PUTDATA');
78 } 80 }
81 }
82
83 sub jsonData {
84 my ($this) = @_;
85
86 if ($this->contentType =~ m{^application/json} ) {
87 my $data = $this->_jsonData;
88 unless($data) {
89 $data = JSON->new()->decode($this->rawData('decode encoding'));
90 $this->_jsonData($data);
91 }
92
93 return $data;
94 }
95
96 return;
79 } 97 }
80 98
81 sub requestMethod { 99 sub requestMethod {
82 my ($this) = @_; 100 my ($this) = @_;
83 return $this->query->request_method; 101 return $this->query->request_method;