# HG changeset patch # User Sebastien Jodogne # Date 1404130131 -7200 # Node ID ab6475e33473f9d739e992264b413b6bb7ecbaef # Parent 509e146c3cb3004340b38256927fb463587f38e6# Parent 2047e6f033bdaddb281f49d0308ba452695ee8cc integration mainline->plugins diff -r 509e146c3cb3 -r ab6475e33473 Core/RestApi/RestApiHierarchy.cpp --- a/Core/RestApi/RestApiHierarchy.cpp Mon Jun 30 13:36:01 2014 +0200 +++ b/Core/RestApi/RestApiHierarchy.cpp Mon Jun 30 14:08:51 2014 +0200 @@ -36,12 +36,68 @@ namespace Orthanc { + RestApiHierarchy::Handlers::Handlers() : + getHandler_(NULL), + postHandler_(NULL), + putHandler_(NULL), + deleteHandler_(NULL) + { + } + + + bool RestApiHierarchy::Handlers::HasHandler(HttpMethod method) const + { + switch (method) + { + case HttpMethod_Get: + return getHandler_ != NULL; + + case HttpMethod_Post: + return postHandler_ != NULL; + + case HttpMethod_Put: + return putHandler_ != NULL; + + case HttpMethod_Delete: + return deleteHandler_ != NULL; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + bool RestApiHierarchy::Handlers::IsEmpty() const { - return (getHandlers_.empty() && - postHandlers_.empty() && - putHandlers_.empty() && - deleteHandlers_.empty()); + return (getHandler_ == NULL && + postHandler_ == NULL && + putHandler_ == NULL && + deleteHandler_ == NULL); + } + + + RestApi::GetHandler RestApiHierarchy::Handlers::GetGetHandler() const + { + assert(getHandler_ != NULL); + return getHandler_; + } + + RestApi::PutHandler RestApiHierarchy::Handlers::GetPutHandler() const + { + assert(putHandler_ != NULL); + return putHandler_; + } + + RestApi::PostHandler RestApiHierarchy::Handlers::GetPostHandler() const + { + assert(postHandler_ != NULL); + return postHandler_; + } + + RestApi::DeleteHandler RestApiHierarchy::Handlers::GetDeleteHandler() const + { + assert(deleteHandler_ != NULL); + return deleteHandler_; } @@ -180,7 +236,7 @@ { if (uri.size() == level) { - if (!handlers_.HasGet() && + if (!handlers_.HasHandler(HttpMethod_Get) && universalHandlers_.IsEmpty() && wildcardChildren_.size() == 0) { @@ -228,17 +284,15 @@ const UriComponents& trailing, void* call) { - for (Handlers::GetHandlers::iterator - it = handlers.getHandlers_.begin(); - it != handlers.getHandlers_.end(); it++) + if (handlers.HasHandler(HttpMethod_Get)) { - // TODO RETURN BOOL - - (*it) (*reinterpret_cast(call)); + handlers.GetGetHandler() (*reinterpret_cast(call)); return true; } - - return false; + else + { + return false; + } } @@ -248,17 +302,15 @@ const UriComponents& trailing, void* call) { - for (Handlers::PostHandlers::iterator - it = handlers.postHandlers_.begin(); - it != handlers.postHandlers_.end(); it++) + if (handlers.HasHandler(HttpMethod_Post)) { - // TODO RETURN BOOL - - (*it) (*reinterpret_cast(call)); + handlers.GetPostHandler() (*reinterpret_cast(call)); return true; } - - return false; + else + { + return false; + } } @@ -268,17 +320,15 @@ const UriComponents& trailing, void* call) { - for (Handlers::PutHandlers::iterator - it = handlers.putHandlers_.begin(); - it != handlers.putHandlers_.end(); it++) + if (handlers.HasHandler(HttpMethod_Put)) { - // TODO RETURN BOOL - - (*it) (*reinterpret_cast(call)); + handlers.GetPutHandler() (*reinterpret_cast(call)); return true; } - - return false; + else + { + return false; + } } @@ -288,17 +338,15 @@ const UriComponents& trailing, void* call) { - for (Handlers::DeleteHandlers::iterator - it = handlers.deleteHandlers_.begin(); - it != handlers.deleteHandlers_.end(); it++) + if (handlers.HasHandler(HttpMethod_Delete)) { - // TODO RETURN BOOL - - (*it) (*reinterpret_cast(call)); + handlers.GetDeleteHandler() (*reinterpret_cast(call)); return true; } - - return false; + else + { + return false; + } } @@ -341,22 +389,22 @@ if (children_.size() == 0) { std::string s = " "; - if (handlers_.getHandlers_.size() != 0) + if (handlers_.HasHandler(HttpMethod_Get)) { s += "GET "; } - if (handlers_.postHandlers_.size() != 0) + if (handlers_.HasHandler(HttpMethod_Post)) { s += "POST "; } - if (handlers_.putHandlers_.size() != 0) + if (handlers_.HasHandler(HttpMethod_Put)) { s += "PUT "; } - if (handlers_.deleteHandlers_.size() != 0) + if (handlers_.HasHandler(HttpMethod_Delete)) { s += "DELETE "; } diff -r 509e146c3cb3 -r ab6475e33473 Core/RestApi/RestApiHierarchy.h --- a/Core/RestApi/RestApiHierarchy.h Mon Jun 30 13:36:01 2014 +0200 +++ b/Core/RestApi/RestApiHierarchy.h Mon Jun 30 14:08:51 2014 +0200 @@ -34,48 +34,55 @@ #include "RestApi.h" +#include "../OrthancException.h" + #include +#include namespace Orthanc { class RestApiHierarchy { private: - struct Handlers + class Handlers { - typedef std::list GetHandlers; - typedef std::list PostHandlers; - typedef std::list PutHandlers; - typedef std::list DeleteHandlers; + private: + RestApi::GetHandler getHandler_; + RestApi::PostHandler postHandler_; + RestApi::PutHandler putHandler_; + RestApi::DeleteHandler deleteHandler_; - GetHandlers getHandlers_; - PostHandlers postHandlers_; - PutHandlers putHandlers_; - DeleteHandlers deleteHandlers_; + public: + Handlers(); + + bool HasHandler(HttpMethod method) const; - bool HasGet() const - { - return getHandlers_.size() > 0; - } + RestApi::GetHandler GetGetHandler() const; + + RestApi::PutHandler GetPutHandler() const; + + RestApi::PostHandler GetPostHandler() const; + + RestApi::DeleteHandler GetDeleteHandler() const; void Register(RestApi::GetHandler handler) { - getHandlers_.push_back(handler); + getHandler_ = handler; } void Register(RestApi::PutHandler handler) { - putHandlers_.push_back(handler); + putHandler_ = handler; } void Register(RestApi::PostHandler handler) { - postHandlers_.push_back(handler); + postHandler_ = handler; } void Register(RestApi::DeleteHandler handler) { - deleteHandlers_.push_back(handler); + deleteHandler_ = handler; } bool IsEmpty() const;