annotate Core/RestApi/RestApiHierarchy.cpp @ 972:2047e6f033bd

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 14:08:15 +0200
parents 1a3817d84f39
children 83622b0f544c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "RestApiHierarchy.h"
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <cassert>
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 namespace Orthanc
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
39 RestApiHierarchy::Handlers::Handlers() :
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
40 getHandler_(NULL),
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
41 postHandler_(NULL),
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
42 putHandler_(NULL),
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
43 deleteHandler_(NULL)
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
44 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
45 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
46
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
47
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
48 bool RestApiHierarchy::Handlers::HasHandler(HttpMethod method) const
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
49 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
50 switch (method)
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
51 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
52 case HttpMethod_Get:
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
53 return getHandler_ != NULL;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
54
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
55 case HttpMethod_Post:
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
56 return postHandler_ != NULL;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
57
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
58 case HttpMethod_Put:
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
59 return putHandler_ != NULL;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
60
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
61 case HttpMethod_Delete:
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
62 return deleteHandler_ != NULL;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
63
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
64 default:
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
65 throw OrthancException(ErrorCode_ParameterOutOfRange);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
66 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
67 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
68
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
69
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 bool RestApiHierarchy::Handlers::IsEmpty() const
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
72 return (getHandler_ == NULL &&
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
73 postHandler_ == NULL &&
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
74 putHandler_ == NULL &&
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
75 deleteHandler_ == NULL);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
76 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
77
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
78
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
79 RestApi::GetHandler RestApiHierarchy::Handlers::GetGetHandler() const
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
80 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
81 assert(getHandler_ != NULL);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
82 return getHandler_;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
83 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
84
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
85 RestApi::PutHandler RestApiHierarchy::Handlers::GetPutHandler() const
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
86 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
87 assert(putHandler_ != NULL);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
88 return putHandler_;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
89 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
90
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
91 RestApi::PostHandler RestApiHierarchy::Handlers::GetPostHandler() const
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
92 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
93 assert(postHandler_ != NULL);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
94 return postHandler_;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
95 }
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
96
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
97 RestApi::DeleteHandler RestApiHierarchy::Handlers::GetDeleteHandler() const
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
98 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
99 assert(deleteHandler_ != NULL);
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
100 return deleteHandler_;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 RestApiHierarchy& RestApiHierarchy::AddChild(Children& children,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 const std::string& name)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 Children::iterator it = children.find(name);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if (it == children.end())
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 // Create new child
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 RestApiHierarchy *child = new RestApiHierarchy;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 children[name] = child;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 return *child;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 return *it->second;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 void RestApiHierarchy::DeleteChildren(Children& children)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 for (Children::iterator it = children.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 it != children.end(); it++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 delete it->second;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 template <typename Handler>
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 void RestApiHierarchy::RegisterInternal(const RestApiPath& path,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 Handler handler,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 size_t level)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 if (path.GetLevelCount() == level)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 if (path.IsUniversalTrailing())
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 universalHandlers_.Register(handler);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 handlers_.Register(handler);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 RestApiHierarchy* child;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 if (path.IsWildcardLevel(level))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 child = &AddChild(wildcardChildren_, path.GetWildcardName(level));
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 child = &AddChild(children_, path.GetLevelName(level));
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 child->RegisterInternal(path, handler, level + 1);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 bool RestApiHierarchy::LookupHandler(RestApiPath::Components& components,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 ResourceCallback callback,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 size_t level,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 void* call)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 assert(uri.size() >= level);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 UriComponents trailing;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 // Look for an exact match on the resource of interest
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if (uri.size() == level)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 if (!handlers_.IsEmpty() &&
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 callback(handlers_, uri, components, trailing, call))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 // Try and go down in the hierarchy, using an exact match for the child
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 Children::const_iterator child = children_.find(uri[level]);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 if (child != children_.end())
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 if (child->second->LookupHandler(components, uri, callback, level + 1, call))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 // Try and go down in the hierarchy, using wildcard rules for children
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 for (child = wildcardChildren_.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 child != wildcardChildren_.end(); child++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 RestApiPath::Components subComponents = components;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 subComponents[child->first] = uri[level];
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 if (child->second->LookupHandler(components, uri, callback, level + 1, call))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 // As a last resort, call the universal handlers, if any
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 if (!universalHandlers_.IsEmpty())
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 trailing.resize(uri.size() - level);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 size_t pos = 0;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 for (size_t i = level; i < uri.size(); i++, pos++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 trailing[pos] = uri[i];
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 assert(pos == trailing.size());
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 if (callback(universalHandlers_, uri, components, trailing, call))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 return false;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 bool RestApiHierarchy::GetDirectory(Json::Value& result,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 size_t level)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 if (uri.size() == level)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
239 if (!handlers_.HasHandler(HttpMethod_Get) &&
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 universalHandlers_.IsEmpty() &&
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 wildcardChildren_.size() == 0)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 result = Json::arrayValue;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 for (Children::const_iterator it = children_.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 it != children_.end(); it++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 result.append(it->first);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 return false;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 Children::const_iterator child = children_.find(uri[level]);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 if (child != children_.end())
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 if (child->second->GetDirectory(result, uri, level + 1))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 for (child = wildcardChildren_.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 child != wildcardChildren_.end(); child++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 if (child->second->GetDirectory(result, uri, level + 1))
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 return false;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 bool RestApiHierarchy::GetCallback(Handlers& handlers,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 const RestApiPath::Components& components,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 const UriComponents& trailing,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 void* call)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
287 if (handlers.HasHandler(HttpMethod_Get))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
289 handlers.GetGetHandler() (*reinterpret_cast<RestApi::GetCall*>(call));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 }
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
292 else
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
293 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
294 return false;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
295 }
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 bool RestApiHierarchy::PostCallback(Handlers& handlers,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 const RestApiPath::Components& components,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 const UriComponents& trailing,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 void* call)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
305 if (handlers.HasHandler(HttpMethod_Post))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
307 handlers.GetPostHandler() (*reinterpret_cast<RestApi::PostCall*>(call));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 }
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
310 else
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
311 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
312 return false;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
313 }
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 bool RestApiHierarchy::PutCallback(Handlers& handlers,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 const RestApiPath::Components& components,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 const UriComponents& trailing,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 void* call)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
323 if (handlers.HasHandler(HttpMethod_Put))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
325 handlers.GetPutHandler() (*reinterpret_cast<RestApi::PutCall*>(call));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 }
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
328 else
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
329 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
330 return false;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
331 }
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 bool RestApiHierarchy::DeleteCallback(Handlers& handlers,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 const UriComponents& uri,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 const RestApiPath::Components& components,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 const UriComponents& trailing,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 void* call)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
341 if (handlers.HasHandler(HttpMethod_Delete))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 {
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
343 handlers.GetDeleteHandler() (*reinterpret_cast<RestApi::DeleteCall*>(call));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 return true;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 }
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
346 else
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
347 {
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
348 return false;
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
349 }
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 RestApiHierarchy::~RestApiHierarchy()
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 DeleteChildren(children_);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 DeleteChildren(wildcardChildren_);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
359 void RestApiHierarchy::Register(const std::string& uri,
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 RestApi::GetHandler handler)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 {
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
362 RestApiPath path(uri);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 RegisterInternal(path, handler, 0);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
366 void RestApiHierarchy::Register(const std::string& uri,
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 RestApi::PutHandler handler)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 {
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
369 RestApiPath path(uri);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 RegisterInternal(path, handler, 0);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
373 void RestApiHierarchy::Register(const std::string& uri,
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 RestApi::PostHandler handler)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 {
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
376 RestApiPath path(uri);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 RegisterInternal(path, handler, 0);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
380 void RestApiHierarchy::Register(const std::string& uri,
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 RestApi::DeleteHandler handler)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 {
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
383 RestApiPath path(uri);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 RegisterInternal(path, handler, 0);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 if (children_.size() == 0)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 std::string s = " ";
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
392 if (handlers_.HasHandler(HttpMethod_Get))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 s += "GET ";
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
397 if (handlers_.HasHandler(HttpMethod_Post))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 s += "POST ";
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
402 if (handlers_.HasHandler(HttpMethod_Put))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 s += "PUT ";
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
972
2047e6f033bd refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
407 if (handlers_.HasHandler(HttpMethod_Delete))
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 s += "DELETE ";
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 target = s;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 else
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 target = Json::objectValue;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 for (Children::const_iterator it = children_.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 it != children_.end(); it++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 it->second->CreateSiteMap(target[it->first]);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 /*for (Children::const_iterator it = wildcardChildren_.begin();
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 it != wildcardChildren_.end(); it++)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 it->second->CreateSiteMap(target["* (" + it->first + ")"]);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 }*/
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 bool RestApiHierarchy::Handle(RestApi::GetCall& call,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 const UriComponents& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 RestApiPath::Components components;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 return LookupHandler(components, uri, GetCallback, 0, &call);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 bool RestApiHierarchy::Handle(RestApi::PutCall& call,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 const UriComponents& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 RestApiPath::Components components;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 return LookupHandler(components, uri, PutCallback, 0, &call);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 bool RestApiHierarchy::Handle(RestApi::PostCall& call,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 const UriComponents& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 RestApiPath::Components components;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 return LookupHandler(components, uri, PostCallback, 0, &call);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 bool RestApiHierarchy::Handle(RestApi::DeleteCall& call,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 const UriComponents& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 RestApiPath::Components components;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 return LookupHandler(components, uri, DeleteCallback, 0, &call);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 }