Mercurial > hg > orthanc
annotate Core/RestApi/RestApi.cpp @ 689:2d0a347e8cfc
switch to 2014
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 03 Feb 2014 16:06:58 +0100 |
parents | 443cced10836 |
children | a811bdf8b8eb |
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 #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 | |
659 | 54 it = getArguments_.begin(); it != getArguments_.end(); ++it) |
304 | 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(); | |
656 | 66 it != getHandlers_.end(); ++it) |
209 | 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(); | |
656 | 80 it != putHandlers_.end(); ++it) |
209 | 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(); | |
656 | 94 it != postHandlers_.end(); ++it) |
209 | 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(); | |
656 | 108 it != deleteHandlers_.end(); ++it) |
209 | 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(); | |
656 | 150 it != getHandlers_.end(); ++it) |
209 | 151 { |
152 delete it->first; | |
153 } | |
154 | |
155 for (PutHandlers::iterator it = putHandlers_.begin(); | |
656 | 156 it != putHandlers_.end(); ++it) |
209 | 157 { |
158 delete it->first; | |
159 } | |
160 | |
161 for (PostHandlers::iterator it = postHandlers_.begin(); | |
656 | 162 it != postHandlers_.end(); ++it) |
209 | 163 { |
164 delete it->first; | |
165 } | |
166 | |
167 for (DeleteHandlers::iterator it = deleteHandlers_.begin(); | |
656 | 168 it != deleteHandlers_.end(); ++it) |
209 | 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, | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
183 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 |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
194 if (method == HttpMethod_Get) |
209 | 195 { |
196 for (GetHandlers::const_iterator it = getHandlers_.begin(); | |
656 | 197 it != getHandlers_.end(); ++it) |
209 | 198 { |
199 if (it->first->Match(components, trailing, uri)) | |
200 { | |
416 | 201 //LOG(INFO) << "REST GET call on: " << Toolbox::FlattenUri(uri); |
209 | 202 ok = true; |
659 | 203 GetCall call(restOutput, *this, headers, components, trailing, uri, getArguments); |
209 | 204 it->second(call); |
205 } | |
206 } | |
207 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
208 else if (method == HttpMethod_Put) |
209 | 209 { |
210 for (PutHandlers::const_iterator it = putHandlers_.begin(); | |
656 | 211 it != putHandlers_.end(); ++it) |
209 | 212 { |
213 if (it->first->Match(components, trailing, uri)) | |
214 { | |
416 | 215 //LOG(INFO) << "REST PUT call on: " << Toolbox::FlattenUri(uri); |
209 | 216 ok = true; |
659 | 217 PutCall call(restOutput, *this, headers, components, trailing, uri, postData); |
209 | 218 it->second(call); |
219 } | |
220 } | |
221 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
222 else if (method == HttpMethod_Post) |
209 | 223 { |
224 for (PostHandlers::const_iterator it = postHandlers_.begin(); | |
656 | 225 it != postHandlers_.end(); ++it) |
209 | 226 { |
227 if (it->first->Match(components, trailing, uri)) | |
228 { | |
416 | 229 //LOG(INFO) << "REST POST call on: " << Toolbox::FlattenUri(uri); |
209 | 230 ok = true; |
659 | 231 PostCall call(restOutput, *this, headers, components, trailing, uri, postData); |
209 | 232 it->second(call); |
233 } | |
234 } | |
235 } | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
416
diff
changeset
|
236 else if (method == HttpMethod_Delete) |
209 | 237 { |
238 for (DeleteHandlers::const_iterator it = deleteHandlers_.begin(); | |
656 | 239 it != deleteHandlers_.end(); ++it) |
209 | 240 { |
241 if (it->first->Match(components, trailing, uri)) | |
242 { | |
416 | 243 //LOG(INFO) << "REST DELETE call on: " << Toolbox::FlattenUri(uri); |
209 | 244 ok = true; |
659 | 245 DeleteCall call(restOutput, *this, headers, components, trailing, uri); |
209 | 246 it->second(call); |
247 } | |
248 } | |
249 } | |
250 | |
251 if (!ok) | |
252 { | |
477 | 253 LOG(INFO) << "REST method " << EnumerationToString(method) |
416 | 254 << " not allowed on: " << Toolbox::FlattenUri(uri); |
209 | 255 output.SendMethodNotAllowedError(GetAcceptedMethods(uri)); |
256 } | |
257 } | |
258 | |
259 void RestApi::Register(const std::string& path, | |
260 GetHandler handler) | |
261 { | |
262 getHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
263 } | |
264 | |
265 void RestApi::Register(const std::string& path, | |
266 PutHandler handler) | |
267 { | |
268 putHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
269 } | |
270 | |
271 void RestApi::Register(const std::string& path, | |
272 PostHandler handler) | |
273 { | |
274 postHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
275 } | |
276 | |
277 void RestApi::Register(const std::string& path, | |
278 DeleteHandler handler) | |
279 { | |
280 deleteHandlers_.push_back(std::make_pair(new RestApiPath(path), handler)); | |
281 } | |
282 } |