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