comparison Core/RestApi/RestApi.h @ 976:6968356679c0 plugins

integration mainline->plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 14:55:43 +0200
parents 7d88f3f4a3b3 83622b0f544c
children 2a9e08136860
comparison
equal deleted inserted replaced
973:ab6475e33473 976:6968356679c0
33 #pragma once 33 #pragma once
34 34
35 #include "../HttpServer/HttpHandler.h" 35 #include "../HttpServer/HttpHandler.h"
36 #include "RestApiPath.h" 36 #include "RestApiPath.h"
37 #include "RestApiOutput.h" 37 #include "RestApiOutput.h"
38 #include "RestApiGetCall.h"
39 #include "RestApiPutCall.h"
40 #include "RestApiPostCall.h"
41 #include "RestApiDeleteCall.h"
38 42
39 #include <list> 43 #include <list>
40 44
41 namespace Orthanc 45 namespace Orthanc
42 { 46 {
43 class RestApi : public HttpHandler 47 class RestApi : public HttpHandler
44 { 48 {
45 public:
46 class Call
47 {
48 friend class RestApi;
49
50 private:
51 RestApiOutput& output_;
52 RestApi& context_;
53 const HttpHandler::Arguments& httpHeaders_;
54 const RestApiPath::Components& uriComponents_;
55 const UriComponents& trailing_;
56 const UriComponents& fullUri_;
57
58 Call(RestApiOutput& output,
59 RestApi& context,
60 const HttpHandler::Arguments& httpHeaders,
61 const RestApiPath::Components& uriComponents,
62 const UriComponents& trailing,
63 const UriComponents& fullUri) :
64 output_(output),
65 context_(context),
66 httpHeaders_(httpHeaders),
67 uriComponents_(uriComponents),
68 trailing_(trailing),
69 fullUri_(fullUri)
70 {
71 }
72
73 protected:
74 static bool ParseJsonRequestInternal(Json::Value& result,
75 const char* request);
76
77 public:
78 RestApiOutput& GetOutput()
79 {
80 return output_;
81 }
82
83 RestApi& GetContext()
84 {
85 return context_;
86 }
87
88 const UriComponents& GetFullUri() const
89 {
90 return fullUri_;
91 }
92
93 const UriComponents& GetTrailingUri() const
94 {
95 return trailing_;
96 }
97
98 std::string GetUriComponent(const std::string& name,
99 const std::string& defaultValue) const
100 {
101 return HttpHandler::GetArgument(uriComponents_, name, defaultValue);
102 }
103
104 std::string GetHttpHeader(const std::string& name,
105 const std::string& defaultValue) const
106 {
107 return HttpHandler::GetArgument(httpHeaders_, name, defaultValue);
108 }
109
110 const HttpHandler::Arguments& GetHttpHeaders() const
111 {
112 return httpHeaders_;
113 }
114
115 void ParseCookies(HttpHandler::Arguments& result) const
116 {
117 HttpHandler::ParseCookies(result, httpHeaders_);
118 }
119
120 virtual bool ParseJsonRequest(Json::Value& result) const = 0;
121 };
122
123
124 class GetCall : public Call
125 {
126 friend class RestApi;
127
128 private:
129 const HttpHandler::Arguments& getArguments_;
130
131 public:
132 GetCall(RestApiOutput& output,
133 RestApi& context,
134 const HttpHandler::Arguments& httpHeaders,
135 const RestApiPath::Components& uriComponents,
136 const UriComponents& trailing,
137 const UriComponents& fullUri,
138 const HttpHandler::Arguments& getArguments) :
139 Call(output, context, httpHeaders, uriComponents, trailing, fullUri),
140 getArguments_(getArguments)
141 {
142 }
143
144 std::string GetArgument(const std::string& name,
145 const std::string& defaultValue) const
146 {
147 return HttpHandler::GetArgument(getArguments_, name, defaultValue);
148 }
149
150 bool HasArgument(const std::string& name) const
151 {
152 return getArguments_.find(name) != getArguments_.end();
153 }
154
155 virtual bool ParseJsonRequest(Json::Value& result) const;
156 };
157
158
159 class PutCall : public Call
160 {
161 friend class RestApi;
162
163 private:
164 const std::string& data_;
165
166 public:
167 PutCall(RestApiOutput& output,
168 RestApi& context,
169 const HttpHandler::Arguments& httpHeaders,
170 const RestApiPath::Components& uriComponents,
171 const UriComponents& trailing,
172 const UriComponents& fullUri,
173 const std::string& data) :
174 Call(output, context, httpHeaders, uriComponents, trailing, fullUri),
175 data_(data)
176 {
177 }
178
179 const std::string& GetPutBody() const
180 {
181 return data_;
182 }
183
184 virtual bool ParseJsonRequest(Json::Value& result) const
185 {
186 return ParseJsonRequestInternal(result, GetPutBody().c_str());
187 }
188 };
189
190 class PostCall : public Call
191 {
192 friend class RestApi;
193
194 private:
195 const std::string& data_;
196
197 public:
198 PostCall(RestApiOutput& output,
199 RestApi& context,
200 const HttpHandler::Arguments& httpHeaders,
201 const RestApiPath::Components& uriComponents,
202 const UriComponents& trailing,
203 const UriComponents& fullUri,
204 const std::string& data) :
205 Call(output, context, httpHeaders, uriComponents, trailing, fullUri),
206 data_(data)
207 {
208 }
209
210 const std::string& GetPostBody() const
211 {
212 return data_;
213 }
214
215 virtual bool ParseJsonRequest(Json::Value& result) const
216 {
217 return ParseJsonRequestInternal(result, GetPostBody().c_str());
218 }
219 };
220
221 class DeleteCall : public Call
222 {
223 public:
224 DeleteCall(RestApiOutput& output,
225 RestApi& context,
226 const HttpHandler::Arguments& httpHeaders,
227 const RestApiPath::Components& uriComponents,
228 const UriComponents& trailing,
229 const UriComponents& fullUri) :
230 Call(output, context, httpHeaders, uriComponents, trailing, fullUri)
231 {
232 }
233
234 virtual bool ParseJsonRequest(Json::Value& result) const
235 {
236 result.clear();
237 return true;
238 }
239 };
240
241 typedef void (*GetHandler) (GetCall& call);
242
243 typedef void (*DeleteHandler) (DeleteCall& call);
244
245 typedef void (*PutHandler) (PutCall& call);
246
247 typedef void (*PostHandler) (PostCall& call);
248
249 private: 49 private:
250 typedef std::list< std::pair<RestApiPath*, GetHandler> > GetHandlers; 50 typedef std::list< std::pair<RestApiPath*, RestApiGetCall::Handler> > GetHandlers;
251 typedef std::list< std::pair<RestApiPath*, PutHandler> > PutHandlers; 51 typedef std::list< std::pair<RestApiPath*, RestApiPutCall::Handler> > PutHandlers;
252 typedef std::list< std::pair<RestApiPath*, PostHandler> > PostHandlers; 52 typedef std::list< std::pair<RestApiPath*, RestApiPostCall::Handler> > PostHandlers;
253 typedef std::list< std::pair<RestApiPath*, DeleteHandler> > DeleteHandlers; 53 typedef std::list< std::pair<RestApiPath*, RestApiDeleteCall::Handler> > DeleteHandlers;
254 54
255 GetHandlers getHandlers_; 55 GetHandlers getHandlers_;
256 PutHandlers putHandlers_; 56 PutHandlers putHandlers_;
257 PostHandlers postHandlers_; 57 PostHandlers postHandlers_;
258 DeleteHandlers deleteHandlers_; 58 DeleteHandlers deleteHandlers_;
277 const Arguments& headers, 77 const Arguments& headers,
278 const Arguments& getArguments, 78 const Arguments& getArguments,
279 const std::string& postData); 79 const std::string& postData);
280 80
281 void Register(const std::string& path, 81 void Register(const std::string& path,
282 GetHandler handler); 82 RestApiGetCall::Handler handler);
283 83
284 void Register(const std::string& path, 84 void Register(const std::string& path,
285 PutHandler handler); 85 RestApiPutCall::Handler handler);
286 86
287 void Register(const std::string& path, 87 void Register(const std::string& path,
288 PostHandler handler); 88 RestApiPostCall::Handler handler);
289 89
290 void Register(const std::string& path, 90 void Register(const std::string& path,
291 DeleteHandler handler); 91 RestApiDeleteCall::Handler handler);
292 }; 92 };
293 } 93 }