Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.cpp @ 402:d2c69150a979
bulk storescu
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Apr 2013 11:50:51 +0200 |
parents | bdd72233b105 |
children | 1188cb0ddaa5 |
rev | line source |
---|---|
209 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
398 | 3 * Copyright (C) 2012-2013 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 #include "RestApi.h" | |
34 | |
249 | 35 #include <stdlib.h> // To define "_exit()" under Windows |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
36 #include <glog/logging.h> |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
37 |
209 | 38 namespace Orthanc |
39 { | |
331
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
40 bool RestApi::Call::ParseJsonRequestInternal(Json::Value& result, |
5a96dac27959
base class for rest calls made public
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
304
diff
changeset
|
41 const char* request) |
304 | 42 { |
43 result.clear(); | |
44 Json::Reader reader; | |
45 return reader.parse(request, result); | |
46 } | |
47 | |
48 | |
49 bool RestApi::GetCall::ParseJsonRequest(Json::Value& result) const | |
50 { | |
51 result.clear(); | |
52 | |
53 for (HttpHandler::Arguments::const_iterator | |
54 it = getArguments_->begin(); it != getArguments_->end(); it++) | |
55 { | |
335 | 56 result[it->first] = it->second; |
304 | 57 } |
58 | |
59 return true; | |
60 } | |
61 | |
62 | |
209 | 63 bool RestApi::IsGetAccepted(const UriComponents& uri) |
64 { | |
65 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
66 it != getHandlers_.end(); it++) | |
67 { | |
68 if (it->first->Match(uri)) | |
69 { | |
70 return true; | |
71 } | |
72 } | |
73 | |
74 return false; | |
75 } | |
76 | |
77 bool RestApi::IsPutAccepted(const UriComponents& uri) | |
78 { | |
79 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
80 it != putHandlers_.end(); it++) | |
81 { | |
82 if (it->first->Match(uri)) | |
83 { | |
84 return true; | |
85 } | |
86 } | |
87 | |
88 return false; | |
89 } | |
90 | |
91 bool RestApi::IsPostAccepted(const UriComponents& uri) | |
92 { | |
93 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
94 it != postHandlers_.end(); it++) | |
95 { | |
96 if (it->first->Match(uri)) | |
97 { | |
98 return true; | |
99 } | |
100 } | |
101 | |
102 return false; | |
103 } | |
104 | |
105 bool RestApi::IsDeleteAccepted(const UriComponents& uri) | |
106 { | |
107 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
108 it != deleteHandlers_.end(); it++) | |
109 { | |
110 if (it->first->Match(uri)) | |
111 { | |
112 return true; | |
113 } | |
114 } | |
115 | |
116 return false; | |
117 } | |
118 | |
119 static void AddMethod(std::string& target, | |
120 const std::string& method) | |
121 { | |
122 if (target.size() > 0) | |
123 target += "," + method; | |
124 else | |
125 target = method; | |
126 } | |
127 | |
128 std::string RestApi::GetAcceptedMethods(const UriComponents& uri) | |
129 { | |
130 std::string s; | |
131 | |
132 if (IsGetAccepted(uri)) | |
133 AddMethod(s, "GET"); | |
134 | |
135 if (IsPutAccepted(uri)) | |
136 AddMethod(s, "PUT"); | |
137 | |
138 if (IsPostAccepted(uri)) | |
139 AddMethod(s, "POST"); | |
140 | |
141 if (IsDeleteAccepted(uri)) | |
142 AddMethod(s, "DELETE"); | |
143 | |
144 return s; | |
145 } | |
146 | |
147 RestApi::~RestApi() | |
148 { | |
149 for (GetHandlers::iterator it = getHandlers_.begin(); | |
150 it != getHandlers_.end(); it++) | |
151 { | |
152 delete it->first; | |
153 } | |
154 | |
155 for (PutHandlers::iterator it = putHandlers_.begin(); | |
156 it != putHandlers_.end(); it++) | |
157 { | |
158 delete it->first; | |
159 } | |
160 | |
161 for (PostHandlers::iterator it = postHandlers_.begin(); | |
162 it != postHandlers_.end(); it++) | |
163 { | |
164 delete it->first; | |
165 } | |
166 | |
167 for (DeleteHandlers::iterator it = deleteHandlers_.begin(); | |
168 it != deleteHandlers_.end(); it++) | |
169 { | |
170 delete it->first; | |
171 } | |
172 } | |
173 | |
174 bool RestApi::IsServedUri(const UriComponents& uri) | |
175 { | |
176 return (IsGetAccepted(uri) || | |
177 IsPutAccepted(uri) || | |
178 IsPostAccepted(uri) || | |
179 IsDeleteAccepted(uri)); | |
180 } | |
181 | |
182 void RestApi::Handle(HttpOutput& output, | |
355 | 183 Orthanc_HttpMethod method, |
209 | 184 const UriComponents& uri, |
185 const Arguments& headers, | |
186 const Arguments& getArguments, | |
187 const std::string& postData) | |
188 { | |
189 bool ok = false; | |
190 RestApiOutput restOutput(output); | |
191 RestApiPath::Components components; | |
192 UriComponents trailing; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
193 |
355 | 194 if (method == Orthanc_HttpMethod_Get) |
209 | 195 { |
196 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
197 it != getHandlers_.end(); it++) | |
198 { | |
199 if (it->first->Match(components, trailing, uri)) | |
200 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
201 LOG(INFO) << "REST GET call on: " << Toolbox::FlattenUri(uri); |
209 | 202 ok = true; |
203 GetCall call; | |
204 call.output_ = &restOutput; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
205 call.context_ = this; |
209 | 206 call.httpHeaders_ = &headers; |
207 call.uriComponents_ = &components; | |
208 call.trailing_ = &trailing; | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
209 call.fullUri_ = &uri; |
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
210 |
209 | 211 call.getArguments_ = &getArguments; |
212 it->second(call); | |
213 } | |
214 } | |
215 } | |
355 | 216 else if (method == Orthanc_HttpMethod_Put) |
209 | 217 { |
218 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
219 it != putHandlers_.end(); it++) | |
220 { | |
221 if (it->first->Match(components, trailing, uri)) | |
222 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
223 LOG(INFO) << "REST PUT call on: " << Toolbox::FlattenUri(uri); |
209 | 224 ok = true; |
225 PutCall call; | |
226 call.output_ = &restOutput; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
227 call.context_ = this; |
209 | 228 call.httpHeaders_ = &headers; |
229 call.uriComponents_ = &components; | |
230 call.trailing_ = &trailing; | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
231 call.fullUri_ = &uri; |
209 | 232 |
233 call.data_ = &postData; | |
234 it->second(call); | |
235 } | |
236 } | |
237 } | |
355 | 238 else if (method == Orthanc_HttpMethod_Post) |
209 | 239 { |
240 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
241 it != postHandlers_.end(); it++) | |
242 { | |
243 if (it->first->Match(components, trailing, uri)) | |
244 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
245 LOG(INFO) << "REST POST call on: " << Toolbox::FlattenUri(uri); |
209 | 246 ok = true; |
247 PostCall call; | |
248 call.output_ = &restOutput; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
249 call.context_ = this; |
209 | 250 call.httpHeaders_ = &headers; |
251 call.uriComponents_ = &components; | |
252 call.trailing_ = &trailing; | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
253 call.fullUri_ = &uri; |
209 | 254 |
255 call.data_ = &postData; | |
256 it->second(call); | |
257 } | |
258 } | |
259 } | |
355 | 260 else if (method == Orthanc_HttpMethod_Delete) |
209 | 261 { |
262 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
263 it != deleteHandlers_.end(); it++) | |
264 { | |
265 if (it->first->Match(components, trailing, uri)) | |
266 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
267 LOG(INFO) << "REST DELETE call on: " << Toolbox::FlattenUri(uri); |
209 | 268 ok = true; |
269 DeleteCall call; | |
270 call.output_ = &restOutput; | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
271 call.context_ = this; |
209 | 272 call.httpHeaders_ = &headers; |
273 call.uriComponents_ = &components; | |
274 call.trailing_ = &trailing; | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
275 call.fullUri_ = &uri; |
209 | 276 it->second(call); |
277 } | |
278 } | |
279 } | |
280 | |
281 if (!ok) | |
282 { | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
283 LOG(INFO) << "REST method " << method << " not allowed on: " << Toolbox::FlattenUri(uri); |
209 | 284 output.SendMethodNotAllowedError(GetAcceptedMethods(uri)); |
285 } | |
286 } | |
287 | |
288 void RestApi::Register(const std::string& path, | |
289 GetHandler handler) | |
290 { | |
291 getHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
292 } | |
293 | |
294 void RestApi::Register(const std::string& path, | |
295 PutHandler handler) | |
296 { | |
297 putHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
298 } | |
299 | |
300 void RestApi::Register(const std::string& path, | |
301 PostHandler handler) | |
302 { | |
303 postHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
304 } | |
305 | |
306 void RestApi::Register(const std::string& path, | |
307 DeleteHandler handler) | |
308 { | |
309 deleteHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
310 } | |
311 } |