Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.h @ 875:d21031a7a6e4
build instructions
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 11 Jun 2014 13:23:05 +0200 |
parents | 2d0a347e8cfc |
children | 7e8cde5905fd 83622b0f544c |
rev | line source |
---|---|
209 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
209 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #pragma once | |
34 | |
35 #include "../HttpServer/HttpHandler.h" | |
36 #include "RestApiPath.h" | |
37 #include "RestApiOutput.h" | |
38 | |
217 | 39 #include <list> |
40 | |
209 | 41 namespace Orthanc |
42 { | |
43 class RestApi : public HttpHandler | |
44 { | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
45 public: |
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
46 class Call |
209 | 47 { |
48 friend class RestApi; | |
49 | |
50 private: | |
659 | 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 } | |
209 | 72 |
304 | 73 protected: |
74 static bool ParseJsonRequestInternal(Json::Value& result, | |
75 const char* request); | |
76 | |
209 | 77 public: |
78 RestApiOutput& GetOutput() | |
79 { | |
659 | 80 return output_; |
209 | 81 } |
82 | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
83 RestApi& GetContext() |
209 | 84 { |
659 | 85 return context_; |
209 | 86 } |
87 | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
88 const UriComponents& GetFullUri() const |
209 | 89 { |
659 | 90 return fullUri_; |
209 | 91 } |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
92 |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
93 const UriComponents& GetTrailingUri() const |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
94 { |
659 | 95 return trailing_; |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
96 } |
209 | 97 |
98 std::string GetUriComponent(const std::string& name, | |
231 | 99 const std::string& defaultValue) const |
209 | 100 { |
659 | 101 return HttpHandler::GetArgument(uriComponents_, name, defaultValue); |
209 | 102 } |
212 | 103 |
104 std::string GetHttpHeader(const std::string& name, | |
231 | 105 const std::string& defaultValue) const |
212 | 106 { |
659 | 107 return HttpHandler::GetArgument(httpHeaders_, name, defaultValue); |
212 | 108 } |
304 | 109 |
332 | 110 const HttpHandler::Arguments& GetHttpHeaders() const |
111 { | |
659 | 112 return httpHeaders_; |
332 | 113 } |
114 | |
330 | 115 void ParseCookies(HttpHandler::Arguments& result) const |
116 { | |
659 | 117 HttpHandler::ParseCookies(result, httpHeaders_); |
330 | 118 } |
119 | |
304 | 120 virtual bool ParseJsonRequest(Json::Value& result) const = 0; |
209 | 121 }; |
122 | |
123 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
124 class GetCall : public Call |
209 | 125 { |
126 friend class RestApi; | |
127 | |
128 private: | |
659 | 129 const HttpHandler::Arguments& getArguments_; |
209 | 130 |
131 public: | |
659 | 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 | |
209 | 144 std::string GetArgument(const std::string& name, |
231 | 145 const std::string& defaultValue) const |
209 | 146 { |
659 | 147 return HttpHandler::GetArgument(getArguments_, name, defaultValue); |
209 | 148 } |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
149 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
150 bool HasArgument(const std::string& name) const |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
151 { |
659 | 152 return getArguments_.find(name) != getArguments_.end(); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
153 } |
304 | 154 |
155 virtual bool ParseJsonRequest(Json::Value& result) const; | |
209 | 156 }; |
157 | |
659 | 158 |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
159 class PutCall : public Call |
209 | 160 { |
161 friend class RestApi; | |
162 | |
163 private: | |
659 | 164 const std::string& data_; |
209 | 165 |
166 public: | |
659 | 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 | |
304 | 179 const std::string& GetPutBody() const |
209 | 180 { |
659 | 181 return data_; |
209 | 182 } |
304 | 183 |
184 virtual bool ParseJsonRequest(Json::Value& result) const | |
185 { | |
186 return ParseJsonRequestInternal(result, GetPutBody().c_str()); | |
187 } | |
209 | 188 }; |
189 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
190 class PostCall : public Call |
209 | 191 { |
192 friend class RestApi; | |
193 | |
194 private: | |
659 | 195 const std::string& data_; |
209 | 196 |
197 public: | |
659 | 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 | |
304 | 210 const std::string& GetPostBody() const |
209 | 211 { |
659 | 212 return data_; |
209 | 213 } |
304 | 214 |
215 virtual bool ParseJsonRequest(Json::Value& result) const | |
216 { | |
217 return ParseJsonRequestInternal(result, GetPostBody().c_str()); | |
218 } | |
209 | 219 }; |
220 | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
221 class DeleteCall : public Call |
209 | 222 { |
304 | 223 public: |
659 | 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 | |
304 | 234 virtual bool ParseJsonRequest(Json::Value& result) const |
235 { | |
236 result.clear(); | |
237 return true; | |
238 } | |
209 | 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: | |
250 typedef std::list< std::pair<RestApiPath*, GetHandler> > GetHandlers; | |
251 typedef std::list< std::pair<RestApiPath*, PutHandler> > PutHandlers; | |
252 typedef std::list< std::pair<RestApiPath*, PostHandler> > PostHandlers; | |
253 typedef std::list< std::pair<RestApiPath*, DeleteHandler> > DeleteHandlers; | |
254 | |
255 GetHandlers getHandlers_; | |
256 PutHandlers putHandlers_; | |
257 PostHandlers postHandlers_; | |
258 DeleteHandlers deleteHandlers_; | |
259 | |
260 bool IsGetAccepted(const UriComponents& uri); | |
261 bool IsPutAccepted(const UriComponents& uri); | |
262 bool IsPostAccepted(const UriComponents& uri); | |
263 bool IsDeleteAccepted(const UriComponents& uri); | |
264 | |
265 std::string GetAcceptedMethods(const UriComponents& uri); | |
266 | |
267 public: | |
268 RestApi() | |
269 { | |
270 } | |
271 | |
272 ~RestApi(); | |
273 | |
274 virtual bool IsServedUri(const UriComponents& uri); | |
275 | |
276 virtual void Handle(HttpOutput& output, | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
277 HttpMethod method, |
209 | 278 const UriComponents& uri, |
279 const Arguments& headers, | |
280 const Arguments& getArguments, | |
281 const std::string& postData); | |
282 | |
283 void Register(const std::string& path, | |
284 GetHandler handler); | |
285 | |
286 void Register(const std::string& path, | |
287 PutHandler handler); | |
288 | |
289 void Register(const std::string& path, | |
290 PostHandler handler); | |
291 | |
292 void Register(const std::string& path, | |
293 DeleteHandler handler); | |
294 }; | |
295 } |