annotate Lib/IMPL/Web/Handler/RestController.pm @ 216:e9fd7ff3f54c

sync
author sergey
date Thu, 09 Aug 2012 17:24:07 +0400
parents d6e2ea24af08
children 47f77e6409f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
1 package IMPL::Web::Handler::RestController;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
2 use strict;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
3
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
4 use IMPL::lang qw(:declare :constants);
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
5
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
6
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
7 use IMPL::declare {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
8 require => {
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
9 Exception => 'IMPL::Exception',
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
10 ArgumentExecption => '-IMPL::InvalidArgumentException',
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
11 HttpException => 'IMPL::Web::Exception',
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
12 NotFoundException => 'IMPL::Web::NotFoundException'
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
13 },
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
14 base => {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
15 'IMPL::Object' => undef,
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
16 'IMPL::Object::Autofill' => '@_',
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
17 'IMPL::Object::Serializable' => undef
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
18 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
19 };
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
20
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
21 BEGIN {
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
22 public property root => PROP_GET | PROP_OWNERSET;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
23 public property contract => PROP_GET | PROP_OWNERSET;
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
24 }
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
25
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
26 sub CTOR {
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
27 my ($this) = @_;
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
28
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 202
diff changeset
29 die ArgumentException->new("root") unless $this->root;
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 202
diff changeset
30 die ArgumentException->new("contract") unless $this->contract;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
31 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
32
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
33 sub Invoke {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
34 my ($this,$action) = @_;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
35
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
36 my $query = $action->query;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
37
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
38 my $method = $query->request_method;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
39
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
40 #TODO: path_info is broken for IIS
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
41 my $pathInfo = $query->path_info;
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
42 my @segments;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
43
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
44 if (length $pathInfo) {
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
45
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
46 @segments = split /\//, $pathInfo, -1; # keep trailing empty string if present
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
47
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
48 # remove first segment since it's always empty
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
49 shift @segments;
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
50
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
51 my ($obj,$view) = (pop(@segments) =~ m/(.*?)(?:\.(\w+))?$/);
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
52 push @segments, $obj;
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents: 197
diff changeset
53
208
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
54 }
3d433a977e3b corrected RestController for empty PAT_INFO
sergey
parents: 206
diff changeset
55
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
56
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
57 my $res = $this->contract->Transform($this->root, { id => '' } );
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
58
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
59 while(@segments) {
199
e743a8481327 Added REST support for forms (with only get and post methods)
sergey
parents: 198
diff changeset
60 my $id = shift @segments;
202
5146e17a7b76 IMPL::Web::Application::RestResource fixes, documentation
sergey
parents: 199
diff changeset
61
5146e17a7b76 IMPL::Web::Application::RestResource fixes, documentation
sergey
parents: 199
diff changeset
62 $res = $res->FetchChildResource($id,$action);
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
63
212
292226770180 bugfixes
sergey
parents: 208
diff changeset
64 die NotFoundException->new($pathInfo,$id) unless $res;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
65 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
66
202
5146e17a7b76 IMPL::Web::Application::RestResource fixes, documentation
sergey
parents: 199
diff changeset
67 $res = $res->InvokeHttpMethod($method,$action);
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
68 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
69
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
70 1;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
71
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
72 __END__
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
73
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
74 =pod
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
75
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
76 =head1 NAME
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
77
213
sergey
parents: 212
diff changeset
78 C<IMPL::Web::Handler::RestController> - Транслирует запросы к ресурсам в вызовы методов.
sergey
parents: 212
diff changeset
79
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
80 =head1 SYNOPSIS
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
81
213
sergey
parents: 212
diff changeset
82 Использует контракты для преобразования стандартных C<REST> запросов в вызовы методов объектов.
sergey
parents: 212
diff changeset
83 C<$ENV{PATH_INFO}> используется как путь к нужному ресурсу у которого будет вызван метод указанный в запросе.
sergey
parents: 212
diff changeset
84
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
85 =head1 DESCRIPTION
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
86
213
sergey
parents: 212
diff changeset
87 =head2 Resource model
sergey
parents: 212
diff changeset
88
sergey
parents: 212
diff changeset
89 Ресурсы имеют иерархическую структуру, аналогичную файлам и каталогам, которая описывается контрактом, также
sergey
parents: 212
diff changeset
90 контрак описывает то, как должны обрабатываться методы C<HTTP> запроса, такие как C<GET> и C<POST>.
sergey
parents: 212
diff changeset
91
sergey
parents: 212
diff changeset
92 За корректность реализации данных методов отвечает разработчик.
sergey
parents: 212
diff changeset
93
sergey
parents: 212
diff changeset
94 Каждый ресурс представляет собой коллкецию вложенных ресурсов, путь указанный в C<HTTP> запросе разбивается на
sergey
parents: 212
diff changeset
95 части, затем каждый сегмент последовательно используется для поиска дочернего ресурса. При обработки
sergey
parents: 212
diff changeset
96 первого сегмента используется корневой ресурс. Корневой ресурс должен существовать всегда.
sergey
parents: 212
diff changeset
97
sergey
parents: 212
diff changeset
98 =head2 Contract
sergey
parents: 212
diff changeset
99
sergey
parents: 212
diff changeset
100 Контрактом может быть любое преобразование которое определяет соответсвие между объектами приложения и
sergey
parents: 212
diff changeset
101 ресурсами, доступными через протокол C<HTTP>.
sergey
parents: 212
diff changeset
102
sergey
parents: 212
diff changeset
103
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
104
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
105
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
106 =cut