comparison Core/RestApi/RestApiHierarchy.h @ 978:ce3106e5843f

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 16:04:58 +0200
parents c550e99c452b
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
975:c550e99c452b 978:ce3106e5843f
35 #include "RestApiGetCall.h" 35 #include "RestApiGetCall.h"
36 #include "RestApiPostCall.h" 36 #include "RestApiPostCall.h"
37 #include "RestApiPutCall.h" 37 #include "RestApiPutCall.h"
38 #include "RestApiDeleteCall.h" 38 #include "RestApiDeleteCall.h"
39 39
40 #include <set>
41
40 namespace Orthanc 42 namespace Orthanc
41 { 43 {
42 class RestApiHierarchy 44 class RestApiHierarchy : public boost::noncopyable
43 { 45 {
44 private: 46 public:
45 class Handlers 47 class Resource : public boost::noncopyable
46 { 48 {
47 private: 49 private:
48 RestApiGetCall::Handler getHandler_; 50 RestApiGetCall::Handler getHandler_;
49 RestApiPostCall::Handler postHandler_; 51 RestApiPostCall::Handler postHandler_;
50 RestApiPutCall::Handler putHandler_; 52 RestApiPutCall::Handler putHandler_;
51 RestApiDeleteCall::Handler deleteHandler_; 53 RestApiDeleteCall::Handler deleteHandler_;
52 54
53 public: 55 public:
54 Handlers(); 56 Resource();
55 57
56 bool HasHandler(HttpMethod method) const; 58 bool HasHandler(HttpMethod method) const;
57
58 RestApiGetCall::Handler GetGetHandler() const;
59
60 RestApiPutCall::Handler GetPutHandler() const;
61
62 RestApiPostCall::Handler GetPostHandler() const;
63
64 RestApiDeleteCall::Handler GetDeleteHandler() const;
65 59
66 void Register(RestApiGetCall::Handler handler) 60 void Register(RestApiGetCall::Handler handler)
67 { 61 {
68 getHandler_ = handler; 62 getHandler_ = handler;
69 } 63 }
82 { 76 {
83 deleteHandler_ = handler; 77 deleteHandler_ = handler;
84 } 78 }
85 79
86 bool IsEmpty() const; 80 bool IsEmpty() const;
81
82 bool Handle(RestApiGetCall& call) const;
83
84 bool Handle(RestApiPutCall& call) const;
85
86 bool Handle(RestApiPostCall& call) const;
87
88 bool Handle(RestApiDeleteCall& call) const;
87 }; 89 };
88 90
89 91
92 class IVisitor : public boost::noncopyable
93 {
94 public:
95 virtual ~IVisitor()
96 {
97 }
98
99 virtual bool Visit(const Resource& resource,
100 const UriComponents& uri,
101 const HttpHandler::Arguments& components,
102 const UriComponents& trailing) = 0;
103 };
104
105
106 private:
90 typedef std::map<std::string, RestApiHierarchy*> Children; 107 typedef std::map<std::string, RestApiHierarchy*> Children;
91 typedef bool (*ResourceCallback) (Handlers& handlers,
92 const UriComponents& uri,
93 const HttpHandler::Arguments& components,
94 const UriComponents& trailing,
95 void* call);
96 108
97 Handlers handlers_; 109 Resource handlers_;
98 Children children_; 110 Children children_;
99 Children wildcardChildren_; 111 Children wildcardChildren_;
100 Handlers universalHandlers_; 112 Resource universalHandlers_;
101
102 113
103 static RestApiHierarchy& AddChild(Children& children, 114 static RestApiHierarchy& AddChild(Children& children,
104 const std::string& name); 115 const std::string& name);
105 116
106 static void DeleteChildren(Children& children); 117 static void DeleteChildren(Children& children);
108 template <typename Handler> 119 template <typename Handler>
109 void RegisterInternal(const RestApiPath& path, 120 void RegisterInternal(const RestApiPath& path,
110 Handler handler, 121 Handler handler,
111 size_t level); 122 size_t level);
112 123
113 bool LookupHandler(HttpHandler::Arguments& components, 124 bool CanGenerateDirectory() const;
114 const UriComponents& uri, 125
115 ResourceCallback callback, 126 bool LookupResource(HttpHandler::Arguments& components,
116 size_t level, 127 const UriComponents& uri,
117 void* call); 128 IVisitor& visitor,
129 size_t level);
118 130
119 bool GetDirectory(Json::Value& result, 131 bool GetDirectory(Json::Value& result,
120 const UriComponents& uri, 132 const UriComponents& uri,
121 size_t level); 133 size_t level);
122
123 static bool GetCallback(Handlers& handlers,
124 const UriComponents& uri,
125 const HttpHandler::Arguments& components,
126 const UriComponents& trailing,
127 void* call);
128
129 static bool PostCallback(Handlers& handlers,
130 const UriComponents& uri,
131 const HttpHandler::Arguments& components,
132 const UriComponents& trailing,
133 void* call);
134
135 static bool PutCallback(Handlers& handlers,
136 const UriComponents& uri,
137 const HttpHandler::Arguments& components,
138 const UriComponents& trailing,
139 void* call);
140
141 static bool DeleteCallback(Handlers& handlers,
142 const UriComponents& uri,
143 const HttpHandler::Arguments& components,
144 const UriComponents& trailing,
145 void* call);
146
147 134
148 public: 135 public:
149 ~RestApiHierarchy(); 136 ~RestApiHierarchy();
150 137
151 void Register(const std::string& uri, 138 void Register(const std::string& uri,
166 const UriComponents& uri) 153 const UriComponents& uri)
167 { 154 {
168 return GetDirectory(result, uri, 0); 155 return GetDirectory(result, uri, 0);
169 } 156 }
170 157
171 bool Handle(RestApiGetCall& call, 158 bool LookupResource(const UriComponents& uri,
172 const UriComponents& uri); 159 IVisitor& visitor);
173 160
174 bool Handle(RestApiPutCall& call, 161 void GetAcceptedMethods(std::set<HttpMethod>& methods,
175 const UriComponents& uri); 162 const UriComponents& uri);
176
177 bool Handle(RestApiPostCall& call,
178 const UriComponents& uri);
179
180 bool Handle(RestApiDeleteCall& call,
181 const UriComponents& uri);
182 }; 163 };
183 } 164 }