comparison Core/RestApi/RestApiHierarchy.cpp @ 981:ef02bd1c2f0e

fix RestApiHierarchy
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 17:44:11 +0200
parents ce3106e5843f
children 0332e6e8c679
comparison
equal deleted inserted replaced
978:ce3106e5843f 981:ef02bd1c2f0e
201 bool RestApiHierarchy::LookupResource(HttpHandler::Arguments& components, 201 bool RestApiHierarchy::LookupResource(HttpHandler::Arguments& components,
202 const UriComponents& uri, 202 const UriComponents& uri,
203 IVisitor& visitor, 203 IVisitor& visitor,
204 size_t level) 204 size_t level)
205 { 205 {
206 assert(uri.size() >= level); 206 if (uri.size() != 0 &&
207 level > uri.size())
208 {
209 return false;
210 }
211
207 UriComponents trailing; 212 UriComponents trailing;
208 213
209 // Look for an exact match on the resource of interest 214 // Look for an exact match on the resource of interest
210 if (uri.size() == level) 215 if (uri.size() == 0 ||
216 level == uri.size())
211 { 217 {
212 if (!handlers_.IsEmpty() && 218 if (!handlers_.IsEmpty() &&
213 visitor.Visit(handlers_, uri, components, trailing)) 219 visitor.Visit(handlers_, uri, components, trailing))
214 { 220 {
215 return true; 221 return true;
216 } 222 }
217 } 223 }
218 224
219 225
220 // Try and go down in the hierarchy, using an exact match for the child 226 if (level < uri.size()) // A recursive call is possible
221 Children::const_iterator child = children_.find(uri[level]); 227 {
222 if (child != children_.end()) 228 // Try and go down in the hierarchy, using an exact match for the child
223 { 229 Children::const_iterator child = children_.find(uri[level]);
224 if (child->second->LookupResource(components, uri, visitor, level + 1)) 230 if (child != children_.end())
225 { 231 {
226 return true; 232 if (child->second->LookupResource(components, uri, visitor, level + 1))
227 } 233 {
228 } 234 return true;
229 235 }
230 236 }
231 // Try and go down in the hierarchy, using wildcard rules for children 237
232 for (child = wildcardChildren_.begin(); 238 // Try and go down in the hierarchy, using wildcard rules for children
233 child != wildcardChildren_.end(); child++) 239 for (child = wildcardChildren_.begin();
234 { 240 child != wildcardChildren_.end(); child++)
235 HttpHandler::Arguments subComponents = components; 241 {
236 subComponents[child->first] = uri[level]; 242 HttpHandler::Arguments subComponents = components;
237 243 subComponents[child->first] = uri[level];
238 if (child->second->LookupResource(components, uri, visitor, level + 1)) 244
239 { 245 if (child->second->LookupResource(subComponents, uri, visitor, level + 1))
240 return true; 246 {
241 } 247 return true;
248 }
249 }
242 } 250 }
243 251
244 252
245 // As a last resort, call the universal handlers, if any 253 // As a last resort, call the universal handlers, if any
246 if (!universalHandlers_.IsEmpty()) 254 if (!universalHandlers_.IsEmpty())
352 RegisterInternal(path, handler, 0); 360 RegisterInternal(path, handler, 0);
353 } 361 }
354 362
355 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const 363 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const
356 { 364 {
357 if (children_.size() == 0) 365 target = Json::objectValue;
358 { 366
359 /*std::string s = " "; 367 /*std::string s = " ";
360 if (handlers_.HasHandler(HttpMethod_Get)) 368 if (handlers_.HasHandler(HttpMethod_Get))
361 { 369 {
362 s += "GET "; 370 s += "GET ";
363 } 371 }
364 372
365 if (handlers_.HasHandler(HttpMethod_Post)) 373 if (handlers_.HasHandler(HttpMethod_Post))
366 { 374 {
367 s += "POST "; 375 s += "POST ";
368 } 376 }
369 377
370 if (handlers_.HasHandler(HttpMethod_Put)) 378 if (handlers_.HasHandler(HttpMethod_Put))
371 { 379 {
372 s += "PUT "; 380 s += "PUT ";
373 } 381 }
374 382
375 if (handlers_.HasHandler(HttpMethod_Delete)) 383 if (handlers_.HasHandler(HttpMethod_Delete))
376 { 384 {
377 s += "DELETE "; 385 s += "DELETE ";
378 } 386 }
379 387
380 target = s;*/ 388 target = s;*/
381
382 target = Json::objectValue;
383 }
384 else
385 {
386 target = Json::objectValue;
387 389
388 for (Children::const_iterator it = children_.begin(); 390 for (Children::const_iterator it = children_.begin();
389 it != children_.end(); it++) 391 it != children_.end(); it++)
390 { 392 {
391 it->second->CreateSiteMap(target[it->first]); 393 it->second->CreateSiteMap(target[it->first]);
392 }
393 } 394 }
394 395
395 /*for (Children::const_iterator it = wildcardChildren_.begin(); 396 for (Children::const_iterator it = wildcardChildren_.begin();
396 it != wildcardChildren_.end(); it++) 397 it != wildcardChildren_.end(); it++)
397 { 398 {
398 it->second->CreateSiteMap(target["* (" + it->first + ")"]); 399 it->second->CreateSiteMap(target["<" + it->first + ">"]);
399 }*/ 400 }
400 } 401 }
401 402
402 403
403 bool RestApiHierarchy::LookupResource(const UriComponents& uri, 404 bool RestApiHierarchy::LookupResource(const UriComponents& uri,
404 IVisitor& visitor) 405 IVisitor& visitor)