annotate Lib/IMPL/Web/Application/ActionResult.pm @ 214:4683002758aa

sync
author sergey
date Mon, 06 Aug 2012 17:27:47 +0400
parents d6e2ea24af08
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
213
sergey
parents:
diff changeset
1 use strict;
sergey
parents:
diff changeset
2 package IMPL::Web::Application::ActionResult;
sergey
parents:
diff changeset
3
sergey
parents:
diff changeset
4 use CGI();
sergey
parents:
diff changeset
5 use IMPL::lang qw(:declare);
sergey
parents:
diff changeset
6 use IMPL::declare {
sergey
parents:
diff changeset
7 require => {
sergey
parents:
diff changeset
8 Exception => 'IMPL::Exception',
sergey
parents:
diff changeset
9 ArgumentException => '-IMPL::InvalidArgumentException'
sergey
parents:
diff changeset
10 },
sergey
parents:
diff changeset
11 base => [
sergey
parents:
diff changeset
12 'IMPL::Object' => undef,
sergey
parents:
diff changeset
13 'IMPL::Object::Autofill' => '@_'
sergey
parents:
diff changeset
14 ]
sergey
parents:
diff changeset
15 };
sergey
parents:
diff changeset
16
sergey
parents:
diff changeset
17 BEGIN {
sergey
parents:
diff changeset
18 public property status => PROP_ALL;
sergey
parents:
diff changeset
19 public property type => PROP_ALL;
sergey
parents:
diff changeset
20 public property charset => PROP_ALL;
sergey
parents:
diff changeset
21 public property cookies => PROP_ALL;
sergey
parents:
diff changeset
22 public property headers => PROP_ALL;
sergey
parents:
diff changeset
23 public property body => PROP_ALL;
sergey
parents:
diff changeset
24 }
sergey
parents:
diff changeset
25
sergey
parents:
diff changeset
26 sub CTOR {
sergey
parents:
diff changeset
27 my ($this) = @_;
sergey
parents:
diff changeset
28
sergey
parents:
diff changeset
29 $this->headers({});
sergey
parents:
diff changeset
30 $this->cookies({});
sergey
parents:
diff changeset
31 }
sergey
parents:
diff changeset
32
sergey
parents:
diff changeset
33 sub PrintResponse {
sergey
parents:
diff changeset
34 my ($this,$out) = @_;
sergey
parents:
diff changeset
35
sergey
parents:
diff changeset
36 my $q = CGI->new({});
sergey
parents:
diff changeset
37
sergey
parents:
diff changeset
38 my %headers = %{$this->headers};
sergey
parents:
diff changeset
39
sergey
parents:
diff changeset
40 if(my $cookies = $this->cookies) {
sergey
parents:
diff changeset
41 $headers{-cookie} = [map _createCookie($_,$cookies->{$_}), keys %$cookies] if $cookies;
sergey
parents:
diff changeset
42 }
sergey
parents:
diff changeset
43
sergey
parents:
diff changeset
44 $headers{'-status'} = $this->status || '200 OK';
sergey
parents:
diff changeset
45 $headers{'-type'} = $this->type || 'text/html';
sergey
parents:
diff changeset
46
sergey
parents:
diff changeset
47 if(my $charset = $this->charset) {
sergey
parents:
diff changeset
48 $q->charset($charset);
sergey
parents:
diff changeset
49 binmode $out, ":encoding($charset)";
sergey
parents:
diff changeset
50 }
sergey
parents:
diff changeset
51
sergey
parents:
diff changeset
52 $q->header(\%headers);
sergey
parents:
diff changeset
53
sergey
parents:
diff changeset
54 if(my $body = $this->body) {
sergey
parents:
diff changeset
55 if(ref $body eq 'CODE') {
sergey
parents:
diff changeset
56 $body->($out);
sergey
parents:
diff changeset
57 } else {
sergey
parents:
diff changeset
58 print $out $body;
sergey
parents:
diff changeset
59 }
sergey
parents:
diff changeset
60 }
sergey
parents:
diff changeset
61 }
sergey
parents:
diff changeset
62
sergey
parents:
diff changeset
63 #used to map a pair name valie to a valid cookie object
sergey
parents:
diff changeset
64 sub _createCookie {
sergey
parents:
diff changeset
65 return UNIVERSAL::isa($_[1], 'CGI::Cookie') ? $_[1] : CGI::Cookie->new(-name => $_[0], -value => $_[1] );
sergey
parents:
diff changeset
66 }
sergey
parents:
diff changeset
67
sergey
parents:
diff changeset
68 1;
sergey
parents:
diff changeset
69
sergey
parents:
diff changeset
70 __END__
sergey
parents:
diff changeset
71
sergey
parents:
diff changeset
72 =pod
sergey
parents:
diff changeset
73
sergey
parents:
diff changeset
74 =head1 NAME
sergey
parents:
diff changeset
75
sergey
parents:
diff changeset
76 C<IMPL::Web::Application::ActionResult> - Результат обработки C<HTTP> запроса.
sergey
parents:
diff changeset
77
sergey
parents:
diff changeset
78 =head1 SYNOPSIS
sergey
parents:
diff changeset
79
sergey
parents:
diff changeset
80 =head1 DESCRIPTION
sergey
parents:
diff changeset
81
sergey
parents:
diff changeset
82 Базовый класс для ответов приложения на C<HTTP> запрос. Каждый вид ответа,
sergey
parents:
diff changeset
83 например
sergey
parents:
diff changeset
84
sergey
parents:
diff changeset
85 Данный объект используется для формирования и передачи данных C<HTTP> ответа
sergey
parents:
diff changeset
86 напрямую. Основными полями являются C<body> и C<status>.
sergey
parents:
diff changeset
87
sergey
parents:
diff changeset
88 Кроме свойств относящихся непосредственно к самому C<HTTP> ответу, данный объект
sergey
parents:
diff changeset
89 может содержать свойства относящиеся к процессу обработки запроса, например
sergey
parents:
diff changeset
90 механизму формирования представления.
sergey
parents:
diff changeset
91
sergey
parents:
diff changeset
92 =head1 MEMBERS
sergey
parents:
diff changeset
93
sergey
parents:
diff changeset
94 =head2 C<[get,set]status>
sergey
parents:
diff changeset
95
sergey
parents:
diff changeset
96 Статус который будет отправлен сервером клиенту, например, C<200 OK> или
sergey
parents:
diff changeset
97 C<204 No response>. Если не указан, то будет C<200 OK>.
sergey
parents:
diff changeset
98
sergey
parents:
diff changeset
99 =head2 C<[get,set]type>
sergey
parents:
diff changeset
100
sergey
parents:
diff changeset
101 Тип содержимого, которое будет передано клиенту, если не указано, будет
sergey
parents:
diff changeset
102 C<text/html>.
sergey
parents:
diff changeset
103
sergey
parents:
diff changeset
104 =head2 C<[get,set]charset>
sergey
parents:
diff changeset
105
sergey
parents:
diff changeset
106 Кодировка в которой будут переданны данные. Следует задавать если и только, если
sergey
parents:
diff changeset
107 передается текстовая информация. Если указана кодировка, то она будет
sergey
parents:
diff changeset
108 автоматически применена к потоку, который будет передан методу C<PrintResponse>.
sergey
parents:
diff changeset
109
sergey
parents:
diff changeset
110 =head2 C<[get,set]cookies>
sergey
parents:
diff changeset
111
sergey
parents:
diff changeset
112 Опционально. Ссылка на хеш с печеньками.
sergey
parents:
diff changeset
113
sergey
parents:
diff changeset
114 =head2 C<[get,set]headers>
sergey
parents:
diff changeset
115
sergey
parents:
diff changeset
116 Опционально. Ссылка на хеш с дополнительными полями заголовка ответа. Формат
sergey
parents:
diff changeset
117 имен полей как у модуля C<CGI>.
sergey
parents:
diff changeset
118
sergey
parents:
diff changeset
119 =head2 C<[get,set]body>
sergey
parents:
diff changeset
120
sergey
parents:
diff changeset
121 Тело ответа. Может быть как простой скаляр, который будет приведен к строке и
sergey
parents:
diff changeset
122 выдан в поток вывода метода C<PrintResponse>. Также может быть ссылкой на
sergey
parents:
diff changeset
123 процедуру, в таком случае будет вызвана эта процедура и ей будет передан
sergey
parents:
diff changeset
124 первым параметром поток для вывода тела ответа.
sergey
parents:
diff changeset
125
sergey
parents:
diff changeset
126 =head2 C<PrintResponse($outStream)>
sergey
parents:
diff changeset
127
sergey
parents:
diff changeset
128 Формирует заголовок и выводит ответ сервера в указанный параметром поток.
sergey
parents:
diff changeset
129
sergey
parents:
diff changeset
130 =cut