969
|
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 {
|
972
|
39 RestApiHierarchy::Handlers::Handlers() :
|
|
40 getHandler_(NULL),
|
|
41 postHandler_(NULL),
|
|
42 putHandler_(NULL),
|
|
43 deleteHandler_(NULL)
|
|
44 {
|
|
45 }
|
|
46
|
|
47
|
|
48 bool RestApiHierarchy::Handlers::HasHandler(HttpMethod method) const
|
|
49 {
|
|
50 switch (method)
|
|
51 {
|
|
52 case HttpMethod_Get:
|
|
53 return getHandler_ != NULL;
|
|
54
|
|
55 case HttpMethod_Post:
|
|
56 return postHandler_ != NULL;
|
|
57
|
|
58 case HttpMethod_Put:
|
|
59 return putHandler_ != NULL;
|
|
60
|
|
61 case HttpMethod_Delete:
|
|
62 return deleteHandler_ != NULL;
|
|
63
|
|
64 default:
|
|
65 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
66 }
|
|
67 }
|
|
68
|
|
69
|
969
|
70 bool RestApiHierarchy::Handlers::IsEmpty() const
|
|
71 {
|
972
|
72 return (getHandler_ == NULL &&
|
|
73 postHandler_ == NULL &&
|
|
74 putHandler_ == NULL &&
|
|
75 deleteHandler_ == NULL);
|
|
76 }
|
|
77
|
|
78
|
|
79 RestApi::GetHandler RestApiHierarchy::Handlers::GetGetHandler() const
|
|
80 {
|
|
81 assert(getHandler_ != NULL);
|
|
82 return getHandler_;
|
|
83 }
|
|
84
|
|
85 RestApi::PutHandler RestApiHierarchy::Handlers::GetPutHandler() const
|
|
86 {
|
|
87 assert(putHandler_ != NULL);
|
|
88 return putHandler_;
|
|
89 }
|
|
90
|
|
91 RestApi::PostHandler RestApiHierarchy::Handlers::GetPostHandler() const
|
|
92 {
|
|
93 assert(postHandler_ != NULL);
|
|
94 return postHandler_;
|
|
95 }
|
|
96
|
|
97 RestApi::DeleteHandler RestApiHierarchy::Handlers::GetDeleteHandler() const
|
|
98 {
|
|
99 assert(deleteHandler_ != NULL);
|
|
100 return deleteHandler_;
|
969
|
101 }
|
|
102
|
|
103
|
|
104 RestApiHierarchy& RestApiHierarchy::AddChild(Children& children,
|
|
105 const std::string& name)
|
|
106 {
|
|
107 Children::iterator it = children.find(name);
|
|
108
|
|
109 if (it == children.end())
|
|
110 {
|
|
111 // Create new child
|
|
112 RestApiHierarchy *child = new RestApiHierarchy;
|
|
113 children[name] = child;
|
|
114 return *child;
|
|
115 }
|
|
116 else
|
|
117 {
|
|
118 return *it->second;
|
|
119 }
|
|
120 }
|
|
121
|
|
122
|
|
123 void RestApiHierarchy::DeleteChildren(Children& children)
|
|
124 {
|
|
125 for (Children::iterator it = children.begin();
|
|
126 it != children.end(); it++)
|
|
127 {
|
|
128 delete it->second;
|
|
129 }
|
|
130 }
|
|
131
|
|
132
|
|
133 template <typename Handler>
|
|
134 void RestApiHierarchy::RegisterInternal(const RestApiPath& path,
|
|
135 Handler handler,
|
|
136 size_t level)
|
|
137 {
|
|
138 if (path.GetLevelCount() == level)
|
|
139 {
|
|
140 if (path.IsUniversalTrailing())
|
|
141 {
|
|
142 universalHandlers_.Register(handler);
|
|
143 }
|
|
144 else
|
|
145 {
|
|
146 handlers_.Register(handler);
|
|
147 }
|
|
148 }
|
|
149 else
|
|
150 {
|
|
151 RestApiHierarchy* child;
|
|
152 if (path.IsWildcardLevel(level))
|
|
153 {
|
|
154 child = &AddChild(wildcardChildren_, path.GetWildcardName(level));
|
|
155 }
|
|
156 else
|
|
157 {
|
|
158 child = &AddChild(children_, path.GetLevelName(level));
|
|
159 }
|
|
160
|
|
161 child->RegisterInternal(path, handler, level + 1);
|
|
162 }
|
|
163 }
|
|
164
|
|
165
|
|
166 bool RestApiHierarchy::LookupHandler(RestApiPath::Components& components,
|
|
167 const UriComponents& uri,
|
|
168 ResourceCallback callback,
|
|
169 size_t level,
|
|
170 void* call)
|
|
171 {
|
|
172 assert(uri.size() >= level);
|
|
173 UriComponents trailing;
|
|
174
|
|
175 // Look for an exact match on the resource of interest
|
|
176 if (uri.size() == level)
|
|
177 {
|
|
178 if (!handlers_.IsEmpty() &&
|
|
179 callback(handlers_, uri, components, trailing, call))
|
|
180 {
|
|
181 return true;
|
|
182 }
|
|
183 }
|
|
184
|
|
185
|
|
186 // Try and go down in the hierarchy, using an exact match for the child
|
|
187 Children::const_iterator child = children_.find(uri[level]);
|
|
188 if (child != children_.end())
|
|
189 {
|
|
190 if (child->second->LookupHandler(components, uri, callback, level + 1, call))
|
|
191 {
|
|
192 return true;
|
|
193 }
|
|
194 }
|
|
195
|
|
196
|
|
197 // Try and go down in the hierarchy, using wildcard rules for children
|
|
198 for (child = wildcardChildren_.begin();
|
|
199 child != wildcardChildren_.end(); child++)
|
|
200 {
|
|
201 RestApiPath::Components subComponents = components;
|
|
202 subComponents[child->first] = uri[level];
|
|
203
|
|
204 if (child->second->LookupHandler(components, uri, callback, level + 1, call))
|
|
205 {
|
|
206 return true;
|
|
207 }
|
|
208 }
|
|
209
|
|
210
|
|
211 // As a last resort, call the universal handlers, if any
|
|
212 if (!universalHandlers_.IsEmpty())
|
|
213 {
|
|
214 trailing.resize(uri.size() - level);
|
|
215 size_t pos = 0;
|
|
216 for (size_t i = level; i < uri.size(); i++, pos++)
|
|
217 {
|
|
218 trailing[pos] = uri[i];
|
|
219 }
|
|
220
|
|
221 assert(pos == trailing.size());
|
|
222
|
|
223 if (callback(universalHandlers_, uri, components, trailing, call))
|
|
224 {
|
|
225 return true;
|
|
226 }
|
|
227 }
|
|
228
|
|
229 return false;
|
|
230 }
|
|
231
|
|
232
|
|
233 bool RestApiHierarchy::GetDirectory(Json::Value& result,
|
|
234 const UriComponents& uri,
|
|
235 size_t level)
|
|
236 {
|
|
237 if (uri.size() == level)
|
|
238 {
|
972
|
239 if (!handlers_.HasHandler(HttpMethod_Get) &&
|
969
|
240 universalHandlers_.IsEmpty() &&
|
|
241 wildcardChildren_.size() == 0)
|
|
242 {
|
|
243 result = Json::arrayValue;
|
|
244
|
|
245 for (Children::const_iterator it = children_.begin();
|
|
246 it != children_.end(); it++)
|
|
247 {
|
|
248 result.append(it->first);
|
|
249 }
|
|
250
|
|
251 return true;
|
|
252 }
|
|
253 else
|
|
254 {
|
|
255 return false;
|
|
256 }
|
|
257 }
|
|
258
|
|
259 Children::const_iterator child = children_.find(uri[level]);
|
|
260 if (child != children_.end())
|
|
261 {
|
|
262 if (child->second->GetDirectory(result, uri, level + 1))
|
|
263 {
|
|
264 return true;
|
|
265 }
|
|
266 }
|
|
267
|
|
268 for (child = wildcardChildren_.begin();
|
|
269 child != wildcardChildren_.end(); child++)
|
|
270 {
|
|
271 if (child->second->GetDirectory(result, uri, level + 1))
|
|
272 {
|
|
273 return true;
|
|
274 }
|
|
275 }
|
|
276
|
|
277 return false;
|
|
278 }
|
|
279
|
|
280
|
|
281 bool RestApiHierarchy::GetCallback(Handlers& handlers,
|
|
282 const UriComponents& uri,
|
|
283 const RestApiPath::Components& components,
|
|
284 const UriComponents& trailing,
|
|
285 void* call)
|
|
286 {
|
972
|
287 if (handlers.HasHandler(HttpMethod_Get))
|
969
|
288 {
|
972
|
289 handlers.GetGetHandler() (*reinterpret_cast<RestApi::GetCall*>(call));
|
969
|
290 return true;
|
|
291 }
|
972
|
292 else
|
|
293 {
|
|
294 return false;
|
|
295 }
|
969
|
296 }
|
|
297
|
|
298
|
|
299 bool RestApiHierarchy::PostCallback(Handlers& handlers,
|
|
300 const UriComponents& uri,
|
|
301 const RestApiPath::Components& components,
|
|
302 const UriComponents& trailing,
|
|
303 void* call)
|
|
304 {
|
972
|
305 if (handlers.HasHandler(HttpMethod_Post))
|
969
|
306 {
|
972
|
307 handlers.GetPostHandler() (*reinterpret_cast<RestApi::PostCall*>(call));
|
969
|
308 return true;
|
|
309 }
|
972
|
310 else
|
|
311 {
|
|
312 return false;
|
|
313 }
|
969
|
314 }
|
|
315
|
|
316
|
|
317 bool RestApiHierarchy::PutCallback(Handlers& handlers,
|
|
318 const UriComponents& uri,
|
|
319 const RestApiPath::Components& components,
|
|
320 const UriComponents& trailing,
|
|
321 void* call)
|
|
322 {
|
972
|
323 if (handlers.HasHandler(HttpMethod_Put))
|
969
|
324 {
|
972
|
325 handlers.GetPutHandler() (*reinterpret_cast<RestApi::PutCall*>(call));
|
969
|
326 return true;
|
|
327 }
|
972
|
328 else
|
|
329 {
|
|
330 return false;
|
|
331 }
|
969
|
332 }
|
|
333
|
|
334
|
|
335 bool RestApiHierarchy::DeleteCallback(Handlers& handlers,
|
|
336 const UriComponents& uri,
|
|
337 const RestApiPath::Components& components,
|
|
338 const UriComponents& trailing,
|
|
339 void* call)
|
|
340 {
|
972
|
341 if (handlers.HasHandler(HttpMethod_Delete))
|
969
|
342 {
|
972
|
343 handlers.GetDeleteHandler() (*reinterpret_cast<RestApi::DeleteCall*>(call));
|
969
|
344 return true;
|
|
345 }
|
972
|
346 else
|
|
347 {
|
|
348 return false;
|
|
349 }
|
969
|
350 }
|
|
351
|
|
352
|
|
353 RestApiHierarchy::~RestApiHierarchy()
|
|
354 {
|
|
355 DeleteChildren(children_);
|
|
356 DeleteChildren(wildcardChildren_);
|
|
357 }
|
|
358
|
970
|
359 void RestApiHierarchy::Register(const std::string& uri,
|
969
|
360 RestApi::GetHandler handler)
|
|
361 {
|
970
|
362 RestApiPath path(uri);
|
969
|
363 RegisterInternal(path, handler, 0);
|
|
364 }
|
|
365
|
970
|
366 void RestApiHierarchy::Register(const std::string& uri,
|
969
|
367 RestApi::PutHandler handler)
|
|
368 {
|
970
|
369 RestApiPath path(uri);
|
969
|
370 RegisterInternal(path, handler, 0);
|
|
371 }
|
|
372
|
970
|
373 void RestApiHierarchy::Register(const std::string& uri,
|
969
|
374 RestApi::PostHandler handler)
|
|
375 {
|
970
|
376 RestApiPath path(uri);
|
969
|
377 RegisterInternal(path, handler, 0);
|
|
378 }
|
|
379
|
970
|
380 void RestApiHierarchy::Register(const std::string& uri,
|
969
|
381 RestApi::DeleteHandler handler)
|
|
382 {
|
970
|
383 RestApiPath path(uri);
|
969
|
384 RegisterInternal(path, handler, 0);
|
|
385 }
|
|
386
|
|
387 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const
|
|
388 {
|
|
389 if (children_.size() == 0)
|
|
390 {
|
|
391 std::string s = " ";
|
972
|
392 if (handlers_.HasHandler(HttpMethod_Get))
|
969
|
393 {
|
|
394 s += "GET ";
|
|
395 }
|
|
396
|
972
|
397 if (handlers_.HasHandler(HttpMethod_Post))
|
969
|
398 {
|
|
399 s += "POST ";
|
|
400 }
|
|
401
|
972
|
402 if (handlers_.HasHandler(HttpMethod_Put))
|
969
|
403 {
|
|
404 s += "PUT ";
|
|
405 }
|
|
406
|
972
|
407 if (handlers_.HasHandler(HttpMethod_Delete))
|
969
|
408 {
|
|
409 s += "DELETE ";
|
|
410 }
|
|
411
|
|
412 target = s;
|
|
413 }
|
|
414 else
|
|
415 {
|
|
416 target = Json::objectValue;
|
|
417
|
|
418 for (Children::const_iterator it = children_.begin();
|
|
419 it != children_.end(); it++)
|
|
420 {
|
|
421 it->second->CreateSiteMap(target[it->first]);
|
|
422 }
|
|
423 }
|
|
424
|
|
425 /*for (Children::const_iterator it = wildcardChildren_.begin();
|
|
426 it != wildcardChildren_.end(); it++)
|
|
427 {
|
|
428 it->second->CreateSiteMap(target["* (" + it->first + ")"]);
|
|
429 }*/
|
|
430 }
|
|
431
|
|
432 bool RestApiHierarchy::Handle(RestApi::GetCall& call,
|
|
433 const UriComponents& uri)
|
|
434 {
|
|
435 RestApiPath::Components components;
|
|
436 return LookupHandler(components, uri, GetCallback, 0, &call);
|
|
437 }
|
|
438
|
|
439 bool RestApiHierarchy::Handle(RestApi::PutCall& call,
|
|
440 const UriComponents& uri)
|
|
441 {
|
|
442 RestApiPath::Components components;
|
|
443 return LookupHandler(components, uri, PutCallback, 0, &call);
|
|
444 }
|
|
445
|
|
446 bool RestApiHierarchy::Handle(RestApi::PostCall& call,
|
|
447 const UriComponents& uri)
|
|
448 {
|
|
449 RestApiPath::Components components;
|
|
450 return LookupHandler(components, uri, PostCallback, 0, &call);
|
|
451 }
|
|
452
|
|
453 bool RestApiHierarchy::Handle(RestApi::DeleteCall& call,
|
|
454 const UriComponents& uri)
|
|
455 {
|
|
456 RestApiPath::Components components;
|
|
457 return LookupHandler(components, uri, DeleteCallback, 0, &call);
|
|
458 }
|
|
459
|
|
460 }
|