comparison 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
comparison
equal deleted inserted replaced
970:1a3817d84f39 972:2047e6f033bd
34 34
35 #include <cassert> 35 #include <cassert>
36 36
37 namespace Orthanc 37 namespace Orthanc
38 { 38 {
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
39 bool RestApiHierarchy::Handlers::IsEmpty() const 70 bool RestApiHierarchy::Handlers::IsEmpty() const
40 { 71 {
41 return (getHandlers_.empty() && 72 return (getHandler_ == NULL &&
42 postHandlers_.empty() && 73 postHandler_ == NULL &&
43 putHandlers_.empty() && 74 putHandler_ == NULL &&
44 deleteHandlers_.empty()); 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_;
45 } 101 }
46 102
47 103
48 RestApiHierarchy& RestApiHierarchy::AddChild(Children& children, 104 RestApiHierarchy& RestApiHierarchy::AddChild(Children& children,
49 const std::string& name) 105 const std::string& name)
178 const UriComponents& uri, 234 const UriComponents& uri,
179 size_t level) 235 size_t level)
180 { 236 {
181 if (uri.size() == level) 237 if (uri.size() == level)
182 { 238 {
183 if (!handlers_.HasGet() && 239 if (!handlers_.HasHandler(HttpMethod_Get) &&
184 universalHandlers_.IsEmpty() && 240 universalHandlers_.IsEmpty() &&
185 wildcardChildren_.size() == 0) 241 wildcardChildren_.size() == 0)
186 { 242 {
187 result = Json::arrayValue; 243 result = Json::arrayValue;
188 244
226 const UriComponents& uri, 282 const UriComponents& uri,
227 const RestApiPath::Components& components, 283 const RestApiPath::Components& components,
228 const UriComponents& trailing, 284 const UriComponents& trailing,
229 void* call) 285 void* call)
230 { 286 {
231 for (Handlers::GetHandlers::iterator 287 if (handlers.HasHandler(HttpMethod_Get))
232 it = handlers.getHandlers_.begin(); 288 {
233 it != handlers.getHandlers_.end(); it++) 289 handlers.GetGetHandler() (*reinterpret_cast<RestApi::GetCall*>(call));
234 {
235 // TODO RETURN BOOL
236
237 (*it) (*reinterpret_cast<RestApi::GetCall*>(call));
238 return true; 290 return true;
239 } 291 }
240 292 else
241 return false; 293 {
294 return false;
295 }
242 } 296 }
243 297
244 298
245 bool RestApiHierarchy::PostCallback(Handlers& handlers, 299 bool RestApiHierarchy::PostCallback(Handlers& handlers,
246 const UriComponents& uri, 300 const UriComponents& uri,
247 const RestApiPath::Components& components, 301 const RestApiPath::Components& components,
248 const UriComponents& trailing, 302 const UriComponents& trailing,
249 void* call) 303 void* call)
250 { 304 {
251 for (Handlers::PostHandlers::iterator 305 if (handlers.HasHandler(HttpMethod_Post))
252 it = handlers.postHandlers_.begin(); 306 {
253 it != handlers.postHandlers_.end(); it++) 307 handlers.GetPostHandler() (*reinterpret_cast<RestApi::PostCall*>(call));
254 {
255 // TODO RETURN BOOL
256
257 (*it) (*reinterpret_cast<RestApi::PostCall*>(call));
258 return true; 308 return true;
259 } 309 }
260 310 else
261 return false; 311 {
312 return false;
313 }
262 } 314 }
263 315
264 316
265 bool RestApiHierarchy::PutCallback(Handlers& handlers, 317 bool RestApiHierarchy::PutCallback(Handlers& handlers,
266 const UriComponents& uri, 318 const UriComponents& uri,
267 const RestApiPath::Components& components, 319 const RestApiPath::Components& components,
268 const UriComponents& trailing, 320 const UriComponents& trailing,
269 void* call) 321 void* call)
270 { 322 {
271 for (Handlers::PutHandlers::iterator 323 if (handlers.HasHandler(HttpMethod_Put))
272 it = handlers.putHandlers_.begin(); 324 {
273 it != handlers.putHandlers_.end(); it++) 325 handlers.GetPutHandler() (*reinterpret_cast<RestApi::PutCall*>(call));
274 {
275 // TODO RETURN BOOL
276
277 (*it) (*reinterpret_cast<RestApi::PutCall*>(call));
278 return true; 326 return true;
279 } 327 }
280 328 else
281 return false; 329 {
330 return false;
331 }
282 } 332 }
283 333
284 334
285 bool RestApiHierarchy::DeleteCallback(Handlers& handlers, 335 bool RestApiHierarchy::DeleteCallback(Handlers& handlers,
286 const UriComponents& uri, 336 const UriComponents& uri,
287 const RestApiPath::Components& components, 337 const RestApiPath::Components& components,
288 const UriComponents& trailing, 338 const UriComponents& trailing,
289 void* call) 339 void* call)
290 { 340 {
291 for (Handlers::DeleteHandlers::iterator 341 if (handlers.HasHandler(HttpMethod_Delete))
292 it = handlers.deleteHandlers_.begin(); 342 {
293 it != handlers.deleteHandlers_.end(); it++) 343 handlers.GetDeleteHandler() (*reinterpret_cast<RestApi::DeleteCall*>(call));
294 {
295 // TODO RETURN BOOL
296
297 (*it) (*reinterpret_cast<RestApi::DeleteCall*>(call));
298 return true; 344 return true;
299 } 345 }
300 346 else
301 return false; 347 {
348 return false;
349 }
302 } 350 }
303 351
304 352
305 RestApiHierarchy::~RestApiHierarchy() 353 RestApiHierarchy::~RestApiHierarchy()
306 { 354 {
339 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const 387 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const
340 { 388 {
341 if (children_.size() == 0) 389 if (children_.size() == 0)
342 { 390 {
343 std::string s = " "; 391 std::string s = " ";
344 if (handlers_.getHandlers_.size() != 0) 392 if (handlers_.HasHandler(HttpMethod_Get))
345 { 393 {
346 s += "GET "; 394 s += "GET ";
347 } 395 }
348 396
349 if (handlers_.postHandlers_.size() != 0) 397 if (handlers_.HasHandler(HttpMethod_Post))
350 { 398 {
351 s += "POST "; 399 s += "POST ";
352 } 400 }
353 401
354 if (handlers_.putHandlers_.size() != 0) 402 if (handlers_.HasHandler(HttpMethod_Put))
355 { 403 {
356 s += "PUT "; 404 s += "PUT ";
357 } 405 }
358 406
359 if (handlers_.deleteHandlers_.size() != 0) 407 if (handlers_.HasHandler(HttpMethod_Delete))
360 { 408 {
361 s += "DELETE "; 409 s += "DELETE ";
362 } 410 }
363 411
364 target = s; 412 target = s;