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