comparison Lib/IMPL/Web/Handler/JSONView.pm @ 256:32aceba4ee6d

corrected ViewHandlers to handle cookies and headers. Dirty hacks to handle binary data RestController doesn't deal with file extensions anymore.
author sergey
date Wed, 12 Dec 2012 04:29:50 +0400
parents f48a1a9f4fa2
children 52aae1b85084
comparison
equal deleted inserted replaced
255:827cf96faa1c 256:32aceba4ee6d
1 package IMPL::Web::Handler::JSONView; 1 package IMPL::Web::Handler::JSONView;
2 use strict; 2 use strict;
3 use JSON; 3 use JSON;
4 4
5 use IMPL::lang qw(is);
5 use IMPL::declare { 6 use IMPL::declare {
6 require => { 7 require => {
7 HttpResponse => 'IMPL::Web::HttpResponse', 8 HttpResponse => 'IMPL::Web::HttpResponse',
8 ViewResult => '-IMPL::Web::ViewResult' 9 ViewResult => '-IMPL::Web::ViewResult'
9 }, 10 },
22 my ($this,$action,$next) = @_; 23 my ($this,$action,$next) = @_;
23 24
24 my $result = $next ? $next->($action) : undef; 25 my $result = $next ? $next->($action) : undef;
25 26
26 27
27 my $model = ( ref $result and eval { $result->isa(ViewResult) } ) 28 my $model = ( ref $result and is($result,ViewResult) )
28 ? $result->model 29 ? $result->model
29 : $result; 30 : $result;
30 31
31 $model = [$model] unless ref $model; 32 $model = [$model] unless ref $model;
32 33
33 return HttpResponse->new({ 34 my %params = (
34 type => $this->contentType, 35 type => $this->contentType,
35 charset => 'utf-8', 36 charset => 'utf-8',
36 body => JSON->new->utf8->pretty->encode($model) 37 body => JSON->new->utf8->pretty->encode($model)
37 }); 38 );
39
40 if(is($result,ViewResult)) {
41 $params{status} = $result->status if $result->status;
42 $params{headers} = $result->headers if $result->headers;
43 $params{cookies} = $result->cookies if $result->cookies;
44 }
45
46 return HttpResponse->new(
47 %params
48 );
38 } 49 }
39 50
40 1; 51 1;
41 52
42 __END__ 53 __END__