Mercurial > hg > orthanc-databases
annotate Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 505:38e428f8179d
more detailed error messages
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Fri, 24 May 2024 16:39:38 +0200 |
parents | 711b136e8fe8 |
children | 54d518dcd74a |
rev | line source |
---|---|
152 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
425
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
425
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
152 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU General Public License as | |
10 * published by the Free Software Foundation, either version 3 of the | |
11 * License, or (at your option) any later version. | |
160 | 12 * |
152 | 13 * This program is distributed in the hope that it will be useful, but |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "OrthancPluginCppWrapper.h" | |
24 | |
25 #include <boost/algorithm/string/predicate.hpp> | |
26 #include <boost/move/unique_ptr.hpp> | |
27 #include <boost/thread.hpp> | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
28 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
29 |
152 | 30 #include <json/reader.h> |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
31 #include <json/version.h> |
152 | 32 #include <json/writer.h> |
33 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
34 #if !defined(JSONCPP_VERSION_MAJOR) || !defined(JSONCPP_VERSION_MINOR) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
35 # error Cannot access the version of JsonCpp |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
36 #endif |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
37 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
38 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
39 /** |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
40 * We use deprecated "Json::Reader", "Json::StyledWriter" and |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
41 * "Json::FastWriter" if JsonCpp < 1.7.0. This choice is rather |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
42 * arbitrary, but if Json >= 1.9.0, gcc generates explicit deprecation |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
43 * warnings (clang was warning in earlier versions). For reference, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
44 * these classes seem to have been deprecated since JsonCpp 1.4.0 (on |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
45 * February 2015) by the following changeset: |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
46 * https://github.com/open-source-parsers/jsoncpp/commit/8df98f6112890d6272734975dd6d70cf8999bb22 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
47 **/ |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
48 #if (JSONCPP_VERSION_MAJOR >= 2 || \ |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
49 (JSONCPP_VERSION_MAJOR == 1 && JSONCPP_VERSION_MINOR >= 8)) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
50 # define JSONCPP_USE_DEPRECATED 0 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
51 #else |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
52 # define JSONCPP_USE_DEPRECATED 1 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
53 #endif |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
54 |
152 | 55 |
56 #if !ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) | |
57 static const OrthancPluginErrorCode OrthancPluginErrorCode_NullPointer = OrthancPluginErrorCode_Plugin; | |
58 #endif | |
59 | |
60 | |
61 namespace OrthancPlugins | |
62 { | |
63 static OrthancPluginContext* globalContext_ = NULL; | |
64 | |
65 | |
66 void SetGlobalContext(OrthancPluginContext* context) | |
67 { | |
68 if (context == NULL) | |
69 { | |
70 ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer); | |
71 } | |
72 else if (globalContext_ == NULL) | |
73 { | |
74 globalContext_ = context; | |
75 } | |
76 else | |
77 { | |
78 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
79 } | |
80 } | |
81 | |
425 | 82 void ResetGlobalContext() |
83 { | |
84 globalContext_ = NULL; | |
85 } | |
152 | 86 |
87 bool HasGlobalContext() | |
88 { | |
89 return globalContext_ != NULL; | |
90 } | |
91 | |
92 | |
93 OrthancPluginContext* GetGlobalContext() | |
94 { | |
95 if (globalContext_ == NULL) | |
96 { | |
97 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
98 } | |
99 else | |
100 { | |
101 return globalContext_; | |
102 } | |
103 } | |
104 | |
105 | |
106 void MemoryBuffer::Check(OrthancPluginErrorCode code) | |
107 { | |
108 if (code != OrthancPluginErrorCode_Success) | |
109 { | |
110 // Prevent using garbage information | |
111 buffer_.data = NULL; | |
112 buffer_.size = 0; | |
113 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
114 } | |
115 } | |
116 | |
117 | |
118 bool MemoryBuffer::CheckHttp(OrthancPluginErrorCode code) | |
119 { | |
120 if (code != OrthancPluginErrorCode_Success) | |
121 { | |
122 // Prevent using garbage information | |
123 buffer_.data = NULL; | |
124 buffer_.size = 0; | |
125 } | |
126 | |
127 if (code == OrthancPluginErrorCode_Success) | |
128 { | |
129 return true; | |
130 } | |
131 else if (code == OrthancPluginErrorCode_UnknownResource || | |
132 code == OrthancPluginErrorCode_InexistentItem) | |
133 { | |
134 return false; | |
135 } | |
136 else | |
137 { | |
138 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
139 } | |
140 } | |
141 | |
142 | |
143 MemoryBuffer::MemoryBuffer() | |
144 { | |
145 buffer_.data = NULL; | |
146 buffer_.size = 0; | |
147 } | |
148 | |
149 | |
150 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
151 MemoryBuffer::MemoryBuffer(const void* buffer, | |
152 size_t size) | |
153 { | |
154 uint32_t s = static_cast<uint32_t>(size); | |
155 if (static_cast<size_t>(s) != size) | |
156 { | |
157 ORTHANC_PLUGINS_THROW_EXCEPTION(NotEnoughMemory); | |
158 } | |
159 else if (OrthancPluginCreateMemoryBuffer(GetGlobalContext(), &buffer_, s) != | |
160 OrthancPluginErrorCode_Success) | |
161 { | |
162 ORTHANC_PLUGINS_THROW_EXCEPTION(NotEnoughMemory); | |
163 } | |
164 else | |
165 { | |
166 memcpy(buffer_.data, buffer, size); | |
167 } | |
168 } | |
169 #endif | |
170 | |
171 | |
172 void MemoryBuffer::Clear() | |
173 { | |
174 if (buffer_.data != NULL) | |
175 { | |
176 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &buffer_); | |
177 buffer_.data = NULL; | |
178 buffer_.size = 0; | |
179 } | |
180 } | |
181 | |
182 | |
183 void MemoryBuffer::Assign(OrthancPluginMemoryBuffer& other) | |
184 { | |
185 Clear(); | |
186 | |
187 buffer_.data = other.data; | |
188 buffer_.size = other.size; | |
189 | |
190 other.data = NULL; | |
191 other.size = 0; | |
192 } | |
193 | |
194 | |
195 void MemoryBuffer::Swap(MemoryBuffer& other) | |
196 { | |
197 std::swap(buffer_.data, other.buffer_.data); | |
198 std::swap(buffer_.size, other.buffer_.size); | |
199 } | |
200 | |
201 | |
202 OrthancPluginMemoryBuffer MemoryBuffer::Release() | |
203 { | |
204 OrthancPluginMemoryBuffer result = buffer_; | |
205 | |
206 buffer_.data = NULL; | |
207 buffer_.size = 0; | |
208 | |
209 return result; | |
210 } | |
211 | |
212 | |
213 void MemoryBuffer::ToString(std::string& target) const | |
214 { | |
215 if (buffer_.size == 0) | |
216 { | |
217 target.clear(); | |
218 } | |
219 else | |
220 { | |
221 target.assign(reinterpret_cast<const char*>(buffer_.data), buffer_.size); | |
222 } | |
223 } | |
224 | |
225 | |
226 void MemoryBuffer::ToJson(Json::Value& target) const | |
227 { | |
228 if (buffer_.data == NULL || | |
229 buffer_.size == 0) | |
230 { | |
231 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
232 } | |
233 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
234 if (!ReadJson(target, buffer_.data, buffer_.size)) |
152 | 235 { |
236 LogError("Cannot convert some memory buffer to JSON"); | |
237 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
238 } | |
239 } | |
240 | |
241 | |
242 bool MemoryBuffer::RestApiGet(const std::string& uri, | |
243 bool applyPlugins) | |
244 { | |
245 Clear(); | |
246 | |
247 if (applyPlugins) | |
248 { | |
249 return CheckHttp(OrthancPluginRestApiGetAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str())); | |
250 } | |
251 else | |
252 { | |
253 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str())); | |
254 } | |
255 } | |
256 | |
405 | 257 // helper class to convert std::map of headers to the plugin SDK C structure |
258 class PluginHttpHeaders | |
259 { | |
260 private: | |
261 std::vector<const char*> headersKeys_; | |
262 std::vector<const char*> headersValues_; | |
263 | |
264 public: | |
265 explicit PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders) | |
266 { | |
267 for (std::map<std::string, std::string>::const_iterator | |
268 it = httpHeaders.begin(); it != httpHeaders.end(); ++it) | |
269 { | |
270 headersKeys_.push_back(it->first.c_str()); | |
271 headersValues_.push_back(it->second.c_str()); | |
272 } | |
273 } | |
274 | |
275 const char* const* GetKeys() | |
276 { | |
277 return (headersKeys_.empty() ? NULL : &headersKeys_[0]); | |
278 } | |
279 | |
280 const char* const* GetValues() | |
281 { | |
282 return (headersValues_.empty() ? NULL : &headersValues_[0]); | |
283 } | |
284 | |
285 uint32_t GetSize() | |
286 { | |
287 return static_cast<uint32_t>(headersKeys_.size()); | |
288 } | |
289 }; | |
290 | |
152 | 291 bool MemoryBuffer::RestApiGet(const std::string& uri, |
292 const std::map<std::string, std::string>& httpHeaders, | |
293 bool applyPlugins) | |
294 { | |
295 Clear(); | |
296 | |
405 | 297 PluginHttpHeaders headers(httpHeaders); |
152 | 298 |
299 return CheckHttp(OrthancPluginRestApiGet2( | |
405 | 300 GetGlobalContext(), &buffer_, uri.c_str(), |
301 headers.GetSize(), | |
302 headers.GetKeys(), | |
303 headers.GetValues(), applyPlugins)); | |
152 | 304 } |
305 | |
306 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
307 const void* body, | |
308 size_t bodySize, | |
309 bool applyPlugins) | |
310 { | |
311 Clear(); | |
312 | |
313 // Cast for compatibility with Orthanc SDK <= 1.5.6 | |
314 const char* b = reinterpret_cast<const char*>(body); | |
315 | |
316 if (applyPlugins) | |
317 { | |
318 return CheckHttp(OrthancPluginRestApiPostAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | |
319 } | |
320 else | |
321 { | |
322 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | |
323 } | |
324 } | |
325 | |
405 | 326 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1 |
327 | |
328 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
329 const void* body, | |
330 size_t bodySize, | |
331 const std::map<std::string, std::string>& httpHeaders, | |
332 bool applyPlugins) | |
333 { | |
334 MemoryBuffer answerHeaders; | |
335 uint16_t httpStatus; | |
336 | |
337 PluginHttpHeaders headers(httpHeaders); | |
338 | |
339 return CheckHttp(OrthancPluginCallRestApi(GetGlobalContext(), | |
340 &buffer_, | |
341 *answerHeaders, | |
342 &httpStatus, | |
343 OrthancPluginHttpMethod_Post, | |
344 uri.c_str(), | |
345 headers.GetSize(), headers.GetKeys(), headers.GetValues(), | |
346 body, bodySize, | |
347 applyPlugins)); | |
348 } | |
349 | |
350 | |
351 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
352 const Json::Value& body, | |
353 const std::map<std::string, std::string>& httpHeaders, | |
354 bool applyPlugins) | |
355 { | |
356 std::string s; | |
357 WriteFastJson(s, body); | |
358 return RestApiPost(uri, s.c_str(), s.size(), httpHeaders, applyPlugins); | |
359 } | |
360 #endif | |
152 | 361 |
362 bool MemoryBuffer::RestApiPut(const std::string& uri, | |
363 const void* body, | |
364 size_t bodySize, | |
365 bool applyPlugins) | |
366 { | |
367 Clear(); | |
368 | |
369 // Cast for compatibility with Orthanc SDK <= 1.5.6 | |
370 const char* b = reinterpret_cast<const char*>(body); | |
371 | |
372 if (applyPlugins) | |
373 { | |
374 return CheckHttp(OrthancPluginRestApiPutAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | |
375 } | |
376 else | |
377 { | |
378 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | |
379 } | |
380 } | |
381 | |
382 | |
272
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
383 static bool ReadJsonInternal(Json::Value& target, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
384 const void* buffer, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
385 size_t size, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
386 bool collectComments) |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
387 { |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
388 #if JSONCPP_USE_DEPRECATED == 1 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
389 Json::Reader reader; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
390 return reader.parse(reinterpret_cast<const char*>(buffer), |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
391 reinterpret_cast<const char*>(buffer) + size, target, collectComments); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
392 #else |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
393 Json::CharReaderBuilder builder; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
394 builder.settings_["collectComments"] = collectComments; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
395 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
396 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
397 assert(reader.get() != NULL); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
398 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
399 JSONCPP_STRING err; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
400 if (reader->parse(reinterpret_cast<const char*>(buffer), |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
401 reinterpret_cast<const char*>(buffer) + size, &target, &err)) |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
402 { |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
403 return true; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
404 } |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
405 else |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
406 { |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
407 LogError("Cannot parse JSON: " + std::string(err)); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
408 return false; |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
409 } |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
410 #endif |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
411 } |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
412 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
413 |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
414 bool ReadJson(Json::Value& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
415 const std::string& source) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
416 { |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
417 return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size()); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
418 } |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
419 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
420 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
421 bool ReadJson(Json::Value& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
422 const void* buffer, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
423 size_t size) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
424 { |
272
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
425 return ReadJsonInternal(target, buffer, size, true); |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
426 } |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
427 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
428 |
272
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
429 bool ReadJsonWithoutComments(Json::Value& target, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
430 const std::string& source) |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
431 { |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
432 return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size()); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
433 } |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
434 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
435 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
436 bool ReadJsonWithoutComments(Json::Value& target, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
437 const void* buffer, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
438 size_t size) |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
439 { |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
440 return ReadJsonInternal(target, buffer, size, false); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
441 } |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
442 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
443 |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
444 void WriteFastJson(std::string& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
445 const Json::Value& source) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
446 { |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
447 #if JSONCPP_USE_DEPRECATED == 1 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
448 Json::FastWriter writer; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
449 target = writer.write(source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
450 #else |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
451 Json::StreamWriterBuilder builder; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
452 builder.settings_["indentation"] = ""; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
453 target = Json::writeString(builder, source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
454 #endif |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
455 } |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
456 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
457 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
458 void WriteStyledJson(std::string& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
459 const Json::Value& source) |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
460 { |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
461 #if JSONCPP_USE_DEPRECATED == 1 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
462 Json::StyledWriter writer; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
463 target = writer.write(source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
464 #else |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
465 Json::StreamWriterBuilder builder; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
466 builder.settings_["indentation"] = " "; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
467 target = Json::writeString(builder, source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
468 #endif |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
469 } |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
470 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
471 |
152 | 472 bool MemoryBuffer::RestApiPost(const std::string& uri, |
473 const Json::Value& body, | |
474 bool applyPlugins) | |
475 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
476 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
477 WriteFastJson(s, body); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
478 return RestApiPost(uri, s, applyPlugins); |
152 | 479 } |
480 | |
481 | |
482 bool MemoryBuffer::RestApiPut(const std::string& uri, | |
483 const Json::Value& body, | |
484 bool applyPlugins) | |
485 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
486 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
487 WriteFastJson(s, body); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
488 return RestApiPut(uri, s, applyPlugins); |
152 | 489 } |
490 | |
491 | |
492 void MemoryBuffer::CreateDicom(const Json::Value& tags, | |
493 OrthancPluginCreateDicomFlags flags) | |
494 { | |
495 Clear(); | |
496 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
497 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
498 WriteFastJson(s, tags); |
152 | 499 |
500 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags)); | |
501 } | |
502 | |
503 void MemoryBuffer::CreateDicom(const Json::Value& tags, | |
504 const OrthancImage& pixelData, | |
505 OrthancPluginCreateDicomFlags flags) | |
506 { | |
507 Clear(); | |
508 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
509 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
510 WriteFastJson(s, tags); |
152 | 511 |
512 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags)); | |
513 } | |
514 | |
515 | |
516 void MemoryBuffer::ReadFile(const std::string& path) | |
517 { | |
518 Clear(); | |
519 Check(OrthancPluginReadFile(GetGlobalContext(), &buffer_, path.c_str())); | |
520 } | |
521 | |
522 | |
523 void MemoryBuffer::GetDicomQuery(const OrthancPluginWorklistQuery* query) | |
524 { | |
525 Clear(); | |
526 Check(OrthancPluginWorklistGetDicomQuery(GetGlobalContext(), &buffer_, query)); | |
527 } | |
528 | |
529 | |
530 void OrthancString::Assign(char* str) | |
531 { | |
532 Clear(); | |
533 | |
534 if (str != NULL) | |
535 { | |
536 str_ = str; | |
537 } | |
538 } | |
539 | |
540 | |
541 void OrthancString::Clear() | |
542 { | |
543 if (str_ != NULL) | |
544 { | |
545 OrthancPluginFreeString(GetGlobalContext(), str_); | |
546 str_ = NULL; | |
547 } | |
548 } | |
549 | |
550 | |
551 void OrthancString::ToString(std::string& target) const | |
552 { | |
553 if (str_ == NULL) | |
554 { | |
555 target.clear(); | |
556 } | |
557 else | |
558 { | |
559 target.assign(str_); | |
560 } | |
561 } | |
562 | |
563 | |
564 void OrthancString::ToJson(Json::Value& target) const | |
565 { | |
566 if (str_ == NULL) | |
567 { | |
568 LogError("Cannot convert an empty memory buffer to JSON"); | |
569 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
570 } | |
571 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
572 if (!ReadJson(target, str_)) |
152 | 573 { |
574 LogError("Cannot convert some memory buffer to JSON"); | |
575 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
576 } | |
577 } | |
578 | |
579 | |
405 | 580 void OrthancString::ToJsonWithoutComments(Json::Value& target) const |
581 { | |
582 if (str_ == NULL) | |
583 { | |
584 LogError("Cannot convert an empty memory buffer to JSON"); | |
585 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
586 } | |
587 | |
588 if (!ReadJsonWithoutComments(target, str_)) | |
589 { | |
590 LogError("Cannot convert some memory buffer to JSON"); | |
591 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
592 } | |
593 } | |
594 | |
595 | |
152 | 596 void MemoryBuffer::DicomToJson(Json::Value& target, |
597 OrthancPluginDicomToJsonFormat format, | |
598 OrthancPluginDicomToJsonFlags flags, | |
599 uint32_t maxStringLength) | |
600 { | |
601 OrthancString str; | |
602 str.Assign(OrthancPluginDicomBufferToJson | |
603 (GetGlobalContext(), GetData(), GetSize(), format, flags, maxStringLength)); | |
604 str.ToJson(target); | |
605 } | |
606 | |
607 | |
608 bool MemoryBuffer::HttpGet(const std::string& url, | |
609 const std::string& username, | |
610 const std::string& password) | |
611 { | |
612 Clear(); | |
613 return CheckHttp(OrthancPluginHttpGet(GetGlobalContext(), &buffer_, url.c_str(), | |
614 username.empty() ? NULL : username.c_str(), | |
615 password.empty() ? NULL : password.c_str())); | |
616 } | |
617 | |
618 | |
619 bool MemoryBuffer::HttpPost(const std::string& url, | |
620 const std::string& body, | |
621 const std::string& username, | |
622 const std::string& password) | |
623 { | |
624 Clear(); | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
625 |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
626 if (body.size() > 0xffffffffu) |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
627 { |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
628 LogError("Cannot handle body size > 4GB"); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
629 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
630 } |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
631 |
152 | 632 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), |
633 body.c_str(), body.size(), | |
634 username.empty() ? NULL : username.c_str(), | |
635 password.empty() ? NULL : password.c_str())); | |
636 } | |
637 | |
638 | |
639 bool MemoryBuffer::HttpPut(const std::string& url, | |
640 const std::string& body, | |
641 const std::string& username, | |
642 const std::string& password) | |
643 { | |
644 Clear(); | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
645 |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
646 if (body.size() > 0xffffffffu) |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
647 { |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
648 LogError("Cannot handle body size > 4GB"); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
649 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
650 } |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
651 |
152 | 652 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), |
653 body.empty() ? NULL : body.c_str(), | |
654 body.size(), | |
655 username.empty() ? NULL : username.c_str(), | |
656 password.empty() ? NULL : password.c_str())); | |
657 } | |
658 | |
659 | |
660 void MemoryBuffer::GetDicomInstance(const std::string& instanceId) | |
661 { | |
662 Clear(); | |
663 Check(OrthancPluginGetDicomForInstance(GetGlobalContext(), &buffer_, instanceId.c_str())); | |
664 } | |
665 | |
666 | |
667 bool HttpDelete(const std::string& url, | |
668 const std::string& username, | |
669 const std::string& password) | |
670 { | |
671 OrthancPluginErrorCode error = OrthancPluginHttpDelete | |
672 (GetGlobalContext(), url.c_str(), | |
673 username.empty() ? NULL : username.c_str(), | |
674 password.empty() ? NULL : password.c_str()); | |
675 | |
676 if (error == OrthancPluginErrorCode_Success) | |
677 { | |
678 return true; | |
679 } | |
680 else if (error == OrthancPluginErrorCode_UnknownResource || | |
681 error == OrthancPluginErrorCode_InexistentItem) | |
682 { | |
683 return false; | |
684 } | |
685 else | |
686 { | |
687 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
688 } | |
689 } | |
690 | |
691 | |
692 void LogError(const std::string& message) | |
693 { | |
694 if (HasGlobalContext()) | |
695 { | |
696 OrthancPluginLogError(GetGlobalContext(), message.c_str()); | |
697 } | |
698 } | |
699 | |
700 | |
701 void LogWarning(const std::string& message) | |
702 { | |
703 if (HasGlobalContext()) | |
704 { | |
705 OrthancPluginLogWarning(GetGlobalContext(), message.c_str()); | |
706 } | |
707 } | |
708 | |
709 | |
710 void LogInfo(const std::string& message) | |
711 { | |
712 if (HasGlobalContext()) | |
713 { | |
714 OrthancPluginLogInfo(GetGlobalContext(), message.c_str()); | |
715 } | |
716 } | |
717 | |
718 | |
719 void OrthancConfiguration::LoadConfiguration() | |
720 { | |
721 OrthancString str; | |
722 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext())); | |
723 | |
724 if (str.GetContent() == NULL) | |
725 { | |
726 LogError("Cannot access the Orthanc configuration"); | |
727 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
728 } | |
729 | |
405 | 730 str.ToJsonWithoutComments(configuration_); |
152 | 731 |
732 if (configuration_.type() != Json::objectValue) | |
733 { | |
734 LogError("Unable to read the Orthanc configuration"); | |
735 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
736 } | |
737 } | |
738 | |
739 | |
740 OrthancConfiguration::OrthancConfiguration() | |
741 { | |
742 LoadConfiguration(); | |
743 } | |
744 | |
745 | |
746 OrthancConfiguration::OrthancConfiguration(bool loadConfiguration) | |
747 { | |
748 if (loadConfiguration) | |
749 { | |
750 LoadConfiguration(); | |
751 } | |
752 else | |
753 { | |
754 configuration_ = Json::objectValue; | |
755 } | |
756 } | |
757 | |
405 | 758 OrthancConfiguration::OrthancConfiguration(const Json::Value& configuration, const std::string& path) : |
759 configuration_(configuration), | |
760 path_(path) | |
761 { | |
762 } | |
763 | |
152 | 764 |
765 std::string OrthancConfiguration::GetPath(const std::string& key) const | |
766 { | |
767 if (path_.empty()) | |
768 { | |
769 return key; | |
770 } | |
771 else | |
772 { | |
773 return path_ + "." + key; | |
774 } | |
775 } | |
776 | |
777 | |
778 bool OrthancConfiguration::IsSection(const std::string& key) const | |
779 { | |
780 assert(configuration_.type() == Json::objectValue); | |
781 | |
782 return (configuration_.isMember(key) && | |
783 configuration_[key].type() == Json::objectValue); | |
784 } | |
785 | |
786 | |
787 void OrthancConfiguration::GetSection(OrthancConfiguration& target, | |
788 const std::string& key) const | |
789 { | |
790 assert(configuration_.type() == Json::objectValue); | |
791 | |
792 target.path_ = GetPath(key); | |
793 | |
794 if (!configuration_.isMember(key)) | |
795 { | |
796 target.configuration_ = Json::objectValue; | |
797 } | |
798 else | |
799 { | |
800 if (configuration_[key].type() != Json::objectValue) | |
801 { | |
802 LogError("The configuration section \"" + target.path_ + | |
803 "\" is not an associative array as expected"); | |
804 | |
805 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
806 } | |
807 | |
808 target.configuration_ = configuration_[key]; | |
809 } | |
810 } | |
811 | |
812 | |
813 bool OrthancConfiguration::LookupStringValue(std::string& target, | |
814 const std::string& key) const | |
815 { | |
816 assert(configuration_.type() == Json::objectValue); | |
817 | |
818 if (!configuration_.isMember(key)) | |
819 { | |
820 return false; | |
821 } | |
822 | |
823 if (configuration_[key].type() != Json::stringValue) | |
824 { | |
825 LogError("The configuration option \"" + GetPath(key) + | |
826 "\" is not a string as expected"); | |
827 | |
828 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
829 } | |
830 | |
831 target = configuration_[key].asString(); | |
832 return true; | |
833 } | |
834 | |
835 | |
836 bool OrthancConfiguration::LookupIntegerValue(int& target, | |
837 const std::string& key) const | |
838 { | |
839 assert(configuration_.type() == Json::objectValue); | |
840 | |
841 if (!configuration_.isMember(key)) | |
842 { | |
843 return false; | |
844 } | |
845 | |
846 switch (configuration_[key].type()) | |
847 { | |
848 case Json::intValue: | |
849 target = configuration_[key].asInt(); | |
850 return true; | |
851 | |
852 case Json::uintValue: | |
853 target = configuration_[key].asUInt(); | |
854 return true; | |
855 | |
856 default: | |
857 LogError("The configuration option \"" + GetPath(key) + | |
858 "\" is not an integer as expected"); | |
859 | |
860 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
861 } | |
862 } | |
863 | |
864 | |
865 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target, | |
866 const std::string& key) const | |
867 { | |
868 int tmp; | |
869 if (!LookupIntegerValue(tmp, key)) | |
870 { | |
871 return false; | |
872 } | |
873 | |
874 if (tmp < 0) | |
875 { | |
876 LogError("The configuration option \"" + GetPath(key) + | |
877 "\" is not a positive integer as expected"); | |
878 | |
879 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
880 } | |
881 else | |
882 { | |
883 target = static_cast<unsigned int>(tmp); | |
884 return true; | |
885 } | |
886 } | |
887 | |
888 | |
889 bool OrthancConfiguration::LookupBooleanValue(bool& target, | |
890 const std::string& key) const | |
891 { | |
892 assert(configuration_.type() == Json::objectValue); | |
893 | |
894 if (!configuration_.isMember(key)) | |
895 { | |
896 return false; | |
897 } | |
898 | |
899 if (configuration_[key].type() != Json::booleanValue) | |
900 { | |
901 LogError("The configuration option \"" + GetPath(key) + | |
902 "\" is not a Boolean as expected"); | |
903 | |
904 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
905 } | |
906 | |
907 target = configuration_[key].asBool(); | |
908 return true; | |
909 } | |
910 | |
911 | |
912 bool OrthancConfiguration::LookupFloatValue(float& target, | |
913 const std::string& key) const | |
914 { | |
915 assert(configuration_.type() == Json::objectValue); | |
916 | |
917 if (!configuration_.isMember(key)) | |
918 { | |
919 return false; | |
920 } | |
921 | |
922 switch (configuration_[key].type()) | |
923 { | |
924 case Json::realValue: | |
925 target = configuration_[key].asFloat(); | |
926 return true; | |
927 | |
928 case Json::intValue: | |
929 target = static_cast<float>(configuration_[key].asInt()); | |
930 return true; | |
931 | |
932 case Json::uintValue: | |
933 target = static_cast<float>(configuration_[key].asUInt()); | |
934 return true; | |
935 | |
936 default: | |
937 LogError("The configuration option \"" + GetPath(key) + | |
938 "\" is not an integer as expected"); | |
939 | |
940 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
941 } | |
942 } | |
943 | |
944 | |
945 bool OrthancConfiguration::LookupListOfStrings(std::list<std::string>& target, | |
946 const std::string& key, | |
947 bool allowSingleString) const | |
948 { | |
949 assert(configuration_.type() == Json::objectValue); | |
950 | |
951 target.clear(); | |
952 | |
953 if (!configuration_.isMember(key)) | |
954 { | |
955 return false; | |
956 } | |
957 | |
958 switch (configuration_[key].type()) | |
959 { | |
960 case Json::arrayValue: | |
961 { | |
962 bool ok = true; | |
963 | |
964 for (Json::Value::ArrayIndex i = 0; ok && i < configuration_[key].size(); i++) | |
965 { | |
966 if (configuration_[key][i].type() == Json::stringValue) | |
967 { | |
968 target.push_back(configuration_[key][i].asString()); | |
969 } | |
970 else | |
971 { | |
972 ok = false; | |
973 } | |
974 } | |
975 | |
976 if (ok) | |
977 { | |
978 return true; | |
979 } | |
980 | |
981 break; | |
982 } | |
983 | |
984 case Json::stringValue: | |
985 if (allowSingleString) | |
986 { | |
987 target.push_back(configuration_[key].asString()); | |
988 return true; | |
989 } | |
990 | |
991 break; | |
992 | |
993 default: | |
994 break; | |
995 } | |
996 | |
997 LogError("The configuration option \"" + GetPath(key) + | |
998 "\" is not a list of strings as expected"); | |
999 | |
1000 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
1001 } | |
1002 | |
1003 | |
1004 bool OrthancConfiguration::LookupSetOfStrings(std::set<std::string>& target, | |
1005 const std::string& key, | |
1006 bool allowSingleString) const | |
1007 { | |
1008 std::list<std::string> lst; | |
1009 | |
1010 if (LookupListOfStrings(lst, key, allowSingleString)) | |
1011 { | |
1012 target.clear(); | |
1013 | |
1014 for (std::list<std::string>::const_iterator | |
1015 it = lst.begin(); it != lst.end(); ++it) | |
1016 { | |
1017 target.insert(*it); | |
1018 } | |
1019 | |
1020 return true; | |
1021 } | |
1022 else | |
1023 { | |
1024 return false; | |
1025 } | |
1026 } | |
1027 | |
1028 | |
1029 std::string OrthancConfiguration::GetStringValue(const std::string& key, | |
1030 const std::string& defaultValue) const | |
1031 { | |
1032 std::string tmp; | |
1033 if (LookupStringValue(tmp, key)) | |
1034 { | |
1035 return tmp; | |
1036 } | |
1037 else | |
1038 { | |
1039 return defaultValue; | |
1040 } | |
1041 } | |
1042 | |
1043 | |
1044 int OrthancConfiguration::GetIntegerValue(const std::string& key, | |
1045 int defaultValue) const | |
1046 { | |
1047 int tmp; | |
1048 if (LookupIntegerValue(tmp, key)) | |
1049 { | |
1050 return tmp; | |
1051 } | |
1052 else | |
1053 { | |
1054 return defaultValue; | |
1055 } | |
1056 } | |
1057 | |
1058 | |
1059 unsigned int OrthancConfiguration::GetUnsignedIntegerValue(const std::string& key, | |
1060 unsigned int defaultValue) const | |
1061 { | |
1062 unsigned int tmp; | |
1063 if (LookupUnsignedIntegerValue(tmp, key)) | |
1064 { | |
1065 return tmp; | |
1066 } | |
1067 else | |
1068 { | |
1069 return defaultValue; | |
1070 } | |
1071 } | |
1072 | |
1073 | |
1074 bool OrthancConfiguration::GetBooleanValue(const std::string& key, | |
1075 bool defaultValue) const | |
1076 { | |
1077 bool tmp; | |
1078 if (LookupBooleanValue(tmp, key)) | |
1079 { | |
1080 return tmp; | |
1081 } | |
1082 else | |
1083 { | |
1084 return defaultValue; | |
1085 } | |
1086 } | |
1087 | |
1088 | |
1089 float OrthancConfiguration::GetFloatValue(const std::string& key, | |
1090 float defaultValue) const | |
1091 { | |
1092 float tmp; | |
1093 if (LookupFloatValue(tmp, key)) | |
1094 { | |
1095 return tmp; | |
1096 } | |
1097 else | |
1098 { | |
1099 return defaultValue; | |
1100 } | |
1101 } | |
1102 | |
1103 | |
1104 void OrthancConfiguration::GetDictionary(std::map<std::string, std::string>& target, | |
1105 const std::string& key) const | |
1106 { | |
1107 assert(configuration_.type() == Json::objectValue); | |
1108 | |
1109 target.clear(); | |
1110 | |
1111 if (!configuration_.isMember(key)) | |
1112 { | |
1113 return; | |
1114 } | |
1115 | |
1116 if (configuration_[key].type() != Json::objectValue) | |
1117 { | |
1118 LogError("The configuration option \"" + GetPath(key) + | |
405 | 1119 "\" is not an object as expected"); |
152 | 1120 |
1121 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
1122 } | |
1123 | |
1124 Json::Value::Members members = configuration_[key].getMemberNames(); | |
1125 | |
1126 for (size_t i = 0; i < members.size(); i++) | |
1127 { | |
1128 const Json::Value& value = configuration_[key][members[i]]; | |
1129 | |
1130 if (value.type() == Json::stringValue) | |
1131 { | |
1132 target[members[i]] = value.asString(); | |
1133 } | |
1134 else | |
1135 { | |
1136 LogError("The configuration option \"" + GetPath(key) + | |
1137 "\" is not a dictionary mapping strings to strings"); | |
1138 | |
1139 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
1140 } | |
1141 } | |
1142 } | |
1143 | |
1144 | |
1145 void OrthancImage::Clear() | |
1146 { | |
1147 if (image_ != NULL) | |
1148 { | |
1149 OrthancPluginFreeImage(GetGlobalContext(), image_); | |
1150 image_ = NULL; | |
1151 } | |
1152 } | |
1153 | |
1154 | |
1155 void OrthancImage::CheckImageAvailable() const | |
1156 { | |
1157 if (image_ == NULL) | |
1158 { | |
1159 LogError("Trying to access a NULL image"); | |
1160 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1161 } | |
1162 } | |
1163 | |
1164 | |
1165 OrthancImage::OrthancImage() : | |
1166 image_(NULL) | |
1167 { | |
1168 } | |
1169 | |
1170 | |
1171 OrthancImage::OrthancImage(OrthancPluginImage* image) : | |
1172 image_(image) | |
1173 { | |
1174 } | |
1175 | |
1176 | |
1177 OrthancImage::OrthancImage(OrthancPluginPixelFormat format, | |
1178 uint32_t width, | |
1179 uint32_t height) | |
1180 { | |
1181 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height); | |
1182 | |
1183 if (image_ == NULL) | |
1184 { | |
1185 LogError("Cannot create an image"); | |
1186 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1187 } | |
1188 } | |
1189 | |
1190 | |
1191 OrthancImage::OrthancImage(OrthancPluginPixelFormat format, | |
1192 uint32_t width, | |
1193 uint32_t height, | |
1194 uint32_t pitch, | |
1195 void* buffer) | |
1196 { | |
1197 image_ = OrthancPluginCreateImageAccessor | |
1198 (GetGlobalContext(), format, width, height, pitch, buffer); | |
1199 | |
1200 if (image_ == NULL) | |
1201 { | |
1202 LogError("Cannot create an image accessor"); | |
1203 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1204 } | |
1205 } | |
1206 | |
1207 void OrthancImage::UncompressPngImage(const void* data, | |
1208 size_t size) | |
1209 { | |
1210 Clear(); | |
1211 | |
1212 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png); | |
1213 | |
1214 if (image_ == NULL) | |
1215 { | |
1216 LogError("Cannot uncompress a PNG image"); | |
1217 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1218 } | |
1219 } | |
1220 | |
1221 | |
1222 void OrthancImage::UncompressJpegImage(const void* data, | |
1223 size_t size) | |
1224 { | |
1225 Clear(); | |
1226 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg); | |
1227 if (image_ == NULL) | |
1228 { | |
1229 LogError("Cannot uncompress a JPEG image"); | |
1230 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1231 } | |
1232 } | |
1233 | |
1234 | |
1235 void OrthancImage::DecodeDicomImage(const void* data, | |
1236 size_t size, | |
1237 unsigned int frame) | |
1238 { | |
1239 Clear(); | |
1240 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame); | |
1241 if (image_ == NULL) | |
1242 { | |
1243 LogError("Cannot uncompress a DICOM image"); | |
1244 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1245 } | |
1246 } | |
1247 | |
1248 | |
1249 OrthancPluginPixelFormat OrthancImage::GetPixelFormat() const | |
1250 { | |
1251 CheckImageAvailable(); | |
1252 return OrthancPluginGetImagePixelFormat(GetGlobalContext(), image_); | |
1253 } | |
1254 | |
1255 | |
1256 unsigned int OrthancImage::GetWidth() const | |
1257 { | |
1258 CheckImageAvailable(); | |
1259 return OrthancPluginGetImageWidth(GetGlobalContext(), image_); | |
1260 } | |
1261 | |
1262 | |
1263 unsigned int OrthancImage::GetHeight() const | |
1264 { | |
1265 CheckImageAvailable(); | |
1266 return OrthancPluginGetImageHeight(GetGlobalContext(), image_); | |
1267 } | |
1268 | |
1269 | |
1270 unsigned int OrthancImage::GetPitch() const | |
1271 { | |
1272 CheckImageAvailable(); | |
1273 return OrthancPluginGetImagePitch(GetGlobalContext(), image_); | |
1274 } | |
1275 | |
1276 | |
1277 void* OrthancImage::GetBuffer() const | |
1278 { | |
1279 CheckImageAvailable(); | |
1280 return OrthancPluginGetImageBuffer(GetGlobalContext(), image_); | |
1281 } | |
1282 | |
1283 | |
1284 void OrthancImage::CompressPngImage(MemoryBuffer& target) const | |
1285 { | |
1286 CheckImageAvailable(); | |
1287 | |
1288 OrthancPlugins::MemoryBuffer answer; | |
1289 OrthancPluginCompressPngImage(GetGlobalContext(), *answer, GetPixelFormat(), | |
1290 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); | |
1291 | |
1292 target.Swap(answer); | |
1293 } | |
1294 | |
1295 | |
1296 void OrthancImage::CompressJpegImage(MemoryBuffer& target, | |
1297 uint8_t quality) const | |
1298 { | |
1299 CheckImageAvailable(); | |
1300 | |
1301 OrthancPlugins::MemoryBuffer answer; | |
1302 OrthancPluginCompressJpegImage(GetGlobalContext(), *answer, GetPixelFormat(), | |
1303 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); | |
1304 | |
1305 target.Swap(answer); | |
1306 } | |
1307 | |
1308 | |
1309 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output) const | |
1310 { | |
1311 CheckImageAvailable(); | |
1312 OrthancPluginCompressAndAnswerPngImage(GetGlobalContext(), output, GetPixelFormat(), | |
1313 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); | |
1314 } | |
1315 | |
1316 | |
1317 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output, | |
1318 uint8_t quality) const | |
1319 { | |
1320 CheckImageAvailable(); | |
1321 OrthancPluginCompressAndAnswerJpegImage(GetGlobalContext(), output, GetPixelFormat(), | |
1322 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); | |
1323 } | |
1324 | |
1325 | |
1326 OrthancPluginImage* OrthancImage::Release() | |
1327 { | |
1328 CheckImageAvailable(); | |
1329 OrthancPluginImage* tmp = image_; | |
1330 image_ = NULL; | |
1331 return tmp; | |
1332 } | |
1333 | |
1334 | |
1335 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 | |
1336 FindMatcher::FindMatcher(const OrthancPluginWorklistQuery* worklist) : | |
1337 matcher_(NULL), | |
1338 worklist_(worklist) | |
1339 { | |
1340 if (worklist_ == NULL) | |
1341 { | |
1342 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1343 } | |
1344 } | |
1345 | |
1346 | |
1347 void FindMatcher::SetupDicom(const void* query, | |
1348 uint32_t size) | |
1349 { | |
1350 worklist_ = NULL; | |
1351 | |
1352 matcher_ = OrthancPluginCreateFindMatcher(GetGlobalContext(), query, size); | |
1353 if (matcher_ == NULL) | |
1354 { | |
1355 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1356 } | |
1357 } | |
1358 | |
1359 | |
1360 FindMatcher::~FindMatcher() | |
1361 { | |
1362 // The "worklist_" field | |
1363 | |
1364 if (matcher_ != NULL) | |
1365 { | |
1366 OrthancPluginFreeFindMatcher(GetGlobalContext(), matcher_); | |
1367 } | |
1368 } | |
1369 | |
1370 | |
1371 | |
1372 bool FindMatcher::IsMatch(const void* dicom, | |
1373 uint32_t size) const | |
1374 { | |
1375 int32_t result; | |
1376 | |
1377 if (matcher_ != NULL) | |
1378 { | |
1379 result = OrthancPluginFindMatcherIsMatch(GetGlobalContext(), matcher_, dicom, size); | |
1380 } | |
1381 else if (worklist_ != NULL) | |
1382 { | |
1383 result = OrthancPluginWorklistIsMatch(GetGlobalContext(), worklist_, dicom, size); | |
1384 } | |
1385 else | |
1386 { | |
1387 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1388 } | |
1389 | |
1390 if (result == 0) | |
1391 { | |
1392 return false; | |
1393 } | |
1394 else if (result == 1) | |
1395 { | |
1396 return true; | |
1397 } | |
1398 else | |
1399 { | |
1400 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1401 } | |
1402 } | |
1403 | |
1404 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ | |
1405 | |
1406 void AnswerJson(const Json::Value& value, | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1407 OrthancPluginRestOutput* output) |
152 | 1408 { |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1409 std::string bodyString; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1410 WriteStyledJson(bodyString, value); |
152 | 1411 OrthancPluginAnswerBuffer(GetGlobalContext(), output, bodyString.c_str(), bodyString.size(), "application/json"); |
1412 } | |
1413 | |
1414 void AnswerString(const std::string& answer, | |
1415 const char* mimeType, | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1416 OrthancPluginRestOutput* output) |
152 | 1417 { |
1418 OrthancPluginAnswerBuffer(GetGlobalContext(), output, answer.c_str(), answer.size(), mimeType); | |
1419 } | |
1420 | |
1421 void AnswerHttpError(uint16_t httpError, OrthancPluginRestOutput *output) | |
1422 { | |
1423 OrthancPluginSendHttpStatusCode(GetGlobalContext(), output, httpError); | |
1424 } | |
1425 | |
1426 void AnswerMethodNotAllowed(OrthancPluginRestOutput *output, const char* allowedMethods) | |
1427 { | |
1428 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowedMethods); | |
1429 } | |
1430 | |
1431 bool RestApiGetString(std::string& result, | |
1432 const std::string& uri, | |
1433 bool applyPlugins) | |
1434 { | |
1435 MemoryBuffer answer; | |
1436 if (!answer.RestApiGet(uri, applyPlugins)) | |
1437 { | |
1438 return false; | |
1439 } | |
1440 else | |
1441 { | |
1442 answer.ToString(result); | |
1443 return true; | |
1444 } | |
1445 } | |
1446 | |
1447 bool RestApiGetString(std::string& result, | |
1448 const std::string& uri, | |
1449 const std::map<std::string, std::string>& httpHeaders, | |
1450 bool applyPlugins) | |
1451 { | |
1452 MemoryBuffer answer; | |
1453 if (!answer.RestApiGet(uri, httpHeaders, applyPlugins)) | |
1454 { | |
1455 return false; | |
1456 } | |
1457 else | |
1458 { | |
1459 answer.ToString(result); | |
1460 return true; | |
1461 } | |
1462 } | |
1463 | |
1464 | |
405 | 1465 bool RestApiGet(Json::Value& result, |
1466 const std::string& uri, | |
1467 const std::map<std::string, std::string>& httpHeaders, | |
1468 bool applyPlugins) | |
1469 { | |
1470 MemoryBuffer answer; | |
1471 | |
1472 if (!answer.RestApiGet(uri, httpHeaders, applyPlugins)) | |
1473 { | |
1474 return false; | |
1475 } | |
1476 else | |
1477 { | |
1478 if (!answer.IsEmpty()) | |
1479 { | |
1480 answer.ToJson(result); | |
1481 } | |
1482 return true; | |
1483 } | |
1484 } | |
1485 | |
152 | 1486 |
1487 bool RestApiGet(Json::Value& result, | |
1488 const std::string& uri, | |
1489 bool applyPlugins) | |
1490 { | |
1491 MemoryBuffer answer; | |
1492 | |
1493 if (!answer.RestApiGet(uri, applyPlugins)) | |
1494 { | |
1495 return false; | |
1496 } | |
1497 else | |
1498 { | |
1499 if (!answer.IsEmpty()) | |
1500 { | |
1501 answer.ToJson(result); | |
1502 } | |
1503 return true; | |
1504 } | |
1505 } | |
1506 | |
1507 | |
1508 bool RestApiPost(std::string& result, | |
1509 const std::string& uri, | |
1510 const void* body, | |
1511 size_t bodySize, | |
1512 bool applyPlugins) | |
1513 { | |
1514 MemoryBuffer answer; | |
1515 | |
1516 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins)) | |
1517 { | |
1518 return false; | |
1519 } | |
1520 else | |
1521 { | |
1522 if (!answer.IsEmpty()) | |
1523 { | |
1524 result.assign(answer.GetData(), answer.GetSize()); | |
1525 } | |
1526 return true; | |
1527 } | |
1528 } | |
1529 | |
1530 | |
1531 bool RestApiPost(Json::Value& result, | |
1532 const std::string& uri, | |
1533 const void* body, | |
1534 size_t bodySize, | |
1535 bool applyPlugins) | |
1536 { | |
1537 MemoryBuffer answer; | |
1538 | |
1539 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins)) | |
1540 { | |
1541 return false; | |
1542 } | |
1543 else | |
1544 { | |
1545 if (!answer.IsEmpty()) | |
1546 { | |
1547 answer.ToJson(result); | |
1548 } | |
1549 return true; | |
1550 } | |
1551 } | |
1552 | |
405 | 1553 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1 |
1554 bool RestApiPost(Json::Value& result, | |
1555 const std::string& uri, | |
1556 const Json::Value& body, | |
1557 const std::map<std::string, std::string>& httpHeaders, | |
1558 bool applyPlugins) | |
1559 { | |
1560 MemoryBuffer answer; | |
1561 | |
1562 if (!answer.RestApiPost(uri, body, httpHeaders, applyPlugins)) | |
1563 { | |
1564 return false; | |
1565 } | |
1566 else | |
1567 { | |
1568 if (!answer.IsEmpty()) | |
1569 { | |
1570 answer.ToJson(result); | |
1571 } | |
1572 return true; | |
1573 } | |
1574 } | |
1575 #endif | |
1576 | |
152 | 1577 |
1578 bool RestApiPost(Json::Value& result, | |
1579 const std::string& uri, | |
1580 const Json::Value& body, | |
1581 bool applyPlugins) | |
1582 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1583 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1584 WriteFastJson(s, body); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1585 return RestApiPost(result, uri, s, applyPlugins); |
152 | 1586 } |
1587 | |
1588 | |
1589 bool RestApiPut(Json::Value& result, | |
1590 const std::string& uri, | |
1591 const void* body, | |
1592 size_t bodySize, | |
1593 bool applyPlugins) | |
1594 { | |
1595 MemoryBuffer answer; | |
1596 | |
1597 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins)) | |
1598 { | |
1599 return false; | |
1600 } | |
1601 else | |
1602 { | |
1603 if (!answer.IsEmpty()) // i.e, on a PUT to metadata/..., orthanc returns an empty response | |
1604 { | |
1605 answer.ToJson(result); | |
1606 } | |
1607 return true; | |
1608 } | |
1609 } | |
1610 | |
1611 | |
1612 bool RestApiPut(Json::Value& result, | |
1613 const std::string& uri, | |
1614 const Json::Value& body, | |
1615 bool applyPlugins) | |
1616 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1617 std::string s; |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1618 WriteFastJson(s, body); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
1619 return RestApiPut(result, uri, s, applyPlugins); |
152 | 1620 } |
1621 | |
1622 | |
1623 bool RestApiDelete(const std::string& uri, | |
1624 bool applyPlugins) | |
1625 { | |
1626 OrthancPluginErrorCode error; | |
1627 | |
1628 if (applyPlugins) | |
1629 { | |
1630 error = OrthancPluginRestApiDeleteAfterPlugins(GetGlobalContext(), uri.c_str()); | |
1631 } | |
1632 else | |
1633 { | |
1634 error = OrthancPluginRestApiDelete(GetGlobalContext(), uri.c_str()); | |
1635 } | |
1636 | |
1637 if (error == OrthancPluginErrorCode_Success) | |
1638 { | |
1639 return true; | |
1640 } | |
1641 else if (error == OrthancPluginErrorCode_UnknownResource || | |
1642 error == OrthancPluginErrorCode_InexistentItem) | |
1643 { | |
1644 return false; | |
1645 } | |
1646 else | |
1647 { | |
1648 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
1649 } | |
1650 } | |
1651 | |
1652 | |
1653 void ReportMinimalOrthancVersion(unsigned int major, | |
1654 unsigned int minor, | |
1655 unsigned int revision) | |
1656 { | |
1657 LogError("Your version of the Orthanc core (" + | |
1658 std::string(GetGlobalContext()->orthancVersion) + | |
1659 ") is too old to run this plugin (version " + | |
1660 boost::lexical_cast<std::string>(major) + "." + | |
1661 boost::lexical_cast<std::string>(minor) + "." + | |
1662 boost::lexical_cast<std::string>(revision) + | |
1663 " is required)"); | |
1664 } | |
1665 | |
405 | 1666 bool CheckMinimalVersion(const char* version, |
1667 unsigned int major, | |
1668 unsigned int minor, | |
1669 unsigned int revision) | |
152 | 1670 { |
405 | 1671 if (!strcmp(version, "mainline")) |
152 | 1672 { |
1673 // Assume compatibility with the mainline | |
1674 return true; | |
1675 } | |
1676 | |
425 | 1677 #ifdef _MSC_VER |
1678 #define ORTHANC_SCANF sscanf_s | |
1679 #else | |
1680 #define ORTHANC_SCANF sscanf | |
1681 #endif | |
1682 | |
405 | 1683 // Parse the version |
425 | 1684 int aa, bb, cc = 0; |
1685 if ((ORTHANC_SCANF(version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 && | |
1686 ORTHANC_SCANF(version, "%4d.%4d", &aa, &bb) != 2) || | |
152 | 1687 aa < 0 || |
1688 bb < 0 || | |
1689 cc < 0) | |
1690 { | |
1691 return false; | |
1692 } | |
1693 | |
1694 unsigned int a = static_cast<unsigned int>(aa); | |
1695 unsigned int b = static_cast<unsigned int>(bb); | |
1696 unsigned int c = static_cast<unsigned int>(cc); | |
1697 | |
1698 // Check the major version number | |
1699 | |
1700 if (a > major) | |
1701 { | |
1702 return true; | |
1703 } | |
1704 | |
1705 if (a < major) | |
1706 { | |
1707 return false; | |
1708 } | |
1709 | |
1710 // Check the minor version number | |
1711 assert(a == major); | |
1712 | |
1713 if (b > minor) | |
1714 { | |
1715 return true; | |
1716 } | |
1717 | |
1718 if (b < minor) | |
1719 { | |
1720 return false; | |
1721 } | |
1722 | |
1723 // Check the patch level version number | |
1724 assert(a == major && b == minor); | |
1725 | |
1726 if (c >= revision) | |
1727 { | |
1728 return true; | |
1729 } | |
1730 else | |
1731 { | |
1732 return false; | |
1733 } | |
1734 } | |
1735 | |
1736 | |
405 | 1737 bool CheckMinimalOrthancVersion(unsigned int major, |
1738 unsigned int minor, | |
1739 unsigned int revision) | |
1740 { | |
1741 if (!HasGlobalContext()) | |
1742 { | |
1743 LogError("Bad Orthanc context in the plugin"); | |
1744 return false; | |
1745 } | |
1746 | |
1747 return CheckMinimalVersion(GetGlobalContext()->orthancVersion, | |
1748 major, minor, revision); | |
1749 } | |
1750 | |
1751 | |
152 | 1752 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) |
1753 const char* AutodetectMimeType(const std::string& path) | |
1754 { | |
1755 const char* mime = OrthancPluginAutodetectMimeType(GetGlobalContext(), path.c_str()); | |
1756 | |
1757 if (mime == NULL) | |
1758 { | |
1759 // Should never happen, just for safety | |
1760 return "application/octet-stream"; | |
1761 } | |
1762 else | |
1763 { | |
1764 return mime; | |
1765 } | |
1766 } | |
1767 #endif | |
1768 | |
1769 | |
1770 #if HAS_ORTHANC_PLUGIN_PEERS == 1 | |
1771 size_t OrthancPeers::GetPeerIndex(const std::string& name) const | |
1772 { | |
1773 size_t index; | |
1774 if (LookupName(index, name)) | |
1775 { | |
1776 return index; | |
1777 } | |
1778 else | |
1779 { | |
1780 LogError("Inexistent peer: " + name); | |
1781 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource); | |
1782 } | |
1783 } | |
1784 | |
1785 | |
1786 OrthancPeers::OrthancPeers() : | |
1787 peers_(NULL), | |
1788 timeout_(0) | |
1789 { | |
1790 peers_ = OrthancPluginGetPeers(GetGlobalContext()); | |
1791 | |
1792 if (peers_ == NULL) | |
1793 { | |
1794 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1795 } | |
1796 | |
1797 uint32_t count = OrthancPluginGetPeersCount(GetGlobalContext(), peers_); | |
1798 | |
1799 for (uint32_t i = 0; i < count; i++) | |
1800 { | |
1801 const char* name = OrthancPluginGetPeerName(GetGlobalContext(), peers_, i); | |
1802 if (name == NULL) | |
1803 { | |
1804 OrthancPluginFreePeers(GetGlobalContext(), peers_); | |
1805 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1806 } | |
1807 | |
1808 index_[name] = i; | |
1809 } | |
1810 } | |
1811 | |
1812 | |
1813 OrthancPeers::~OrthancPeers() | |
1814 { | |
1815 if (peers_ != NULL) | |
1816 { | |
1817 OrthancPluginFreePeers(GetGlobalContext(), peers_); | |
1818 } | |
1819 } | |
1820 | |
1821 | |
1822 bool OrthancPeers::LookupName(size_t& target, | |
1823 const std::string& name) const | |
1824 { | |
1825 Index::const_iterator found = index_.find(name); | |
1826 | |
1827 if (found == index_.end()) | |
1828 { | |
1829 return false; | |
1830 } | |
1831 else | |
1832 { | |
1833 target = found->second; | |
1834 return true; | |
1835 } | |
1836 } | |
1837 | |
1838 | |
1839 std::string OrthancPeers::GetPeerName(size_t index) const | |
1840 { | |
1841 if (index >= index_.size()) | |
1842 { | |
1843 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1844 } | |
1845 else | |
1846 { | |
1847 const char* s = OrthancPluginGetPeerName(GetGlobalContext(), peers_, static_cast<uint32_t>(index)); | |
1848 if (s == NULL) | |
1849 { | |
1850 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1851 } | |
1852 else | |
1853 { | |
1854 return s; | |
1855 } | |
1856 } | |
1857 } | |
1858 | |
1859 | |
1860 std::string OrthancPeers::GetPeerUrl(size_t index) const | |
1861 { | |
1862 if (index >= index_.size()) | |
1863 { | |
1864 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1865 } | |
1866 else | |
1867 { | |
1868 const char* s = OrthancPluginGetPeerUrl(GetGlobalContext(), peers_, static_cast<uint32_t>(index)); | |
1869 if (s == NULL) | |
1870 { | |
1871 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1872 } | |
1873 else | |
1874 { | |
1875 return s; | |
1876 } | |
1877 } | |
1878 } | |
1879 | |
1880 | |
1881 std::string OrthancPeers::GetPeerUrl(const std::string& name) const | |
1882 { | |
1883 return GetPeerUrl(GetPeerIndex(name)); | |
1884 } | |
1885 | |
1886 | |
1887 bool OrthancPeers::LookupUserProperty(std::string& value, | |
1888 size_t index, | |
1889 const std::string& key) const | |
1890 { | |
1891 if (index >= index_.size()) | |
1892 { | |
1893 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1894 } | |
1895 else | |
1896 { | |
1897 const char* s = OrthancPluginGetPeerUserProperty(GetGlobalContext(), peers_, static_cast<uint32_t>(index), key.c_str()); | |
1898 if (s == NULL) | |
1899 { | |
1900 return false; | |
1901 } | |
1902 else | |
1903 { | |
1904 value.assign(s); | |
1905 return true; | |
1906 } | |
1907 } | |
1908 } | |
1909 | |
1910 | |
1911 bool OrthancPeers::LookupUserProperty(std::string& value, | |
1912 const std::string& peer, | |
1913 const std::string& key) const | |
1914 { | |
1915 return LookupUserProperty(value, GetPeerIndex(peer), key); | |
1916 } | |
1917 | |
1918 | |
1919 bool OrthancPeers::DoGet(MemoryBuffer& target, | |
1920 size_t index, | |
405 | 1921 const std::string& uri, |
1922 const std::map<std::string, std::string>& headers) const | |
152 | 1923 { |
1924 if (index >= index_.size()) | |
1925 { | |
1926 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1927 } | |
1928 | |
1929 OrthancPlugins::MemoryBuffer answer; | |
1930 uint16_t status; | |
405 | 1931 PluginHttpHeaders pluginHeaders(headers); |
1932 | |
152 | 1933 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
1934 (GetGlobalContext(), *answer, NULL, &status, peers_, | |
1935 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(), | |
405 | 1936 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), NULL, 0, timeout_); |
152 | 1937 |
1938 if (code == OrthancPluginErrorCode_Success) | |
1939 { | |
1940 target.Swap(answer); | |
1941 return (status == 200); | |
1942 } | |
1943 else | |
1944 { | |
1945 return false; | |
1946 } | |
1947 } | |
1948 | |
1949 | |
1950 bool OrthancPeers::DoGet(MemoryBuffer& target, | |
1951 const std::string& name, | |
405 | 1952 const std::string& uri, |
1953 const std::map<std::string, std::string>& headers) const | |
152 | 1954 { |
1955 size_t index; | |
1956 return (LookupName(index, name) && | |
405 | 1957 DoGet(target, index, uri, headers)); |
152 | 1958 } |
1959 | |
1960 | |
1961 bool OrthancPeers::DoGet(Json::Value& target, | |
1962 size_t index, | |
405 | 1963 const std::string& uri, |
1964 const std::map<std::string, std::string>& headers) const | |
152 | 1965 { |
1966 MemoryBuffer buffer; | |
1967 | |
405 | 1968 if (DoGet(buffer, index, uri, headers)) |
152 | 1969 { |
1970 buffer.ToJson(target); | |
1971 return true; | |
1972 } | |
1973 else | |
1974 { | |
1975 return false; | |
1976 } | |
1977 } | |
1978 | |
1979 | |
1980 bool OrthancPeers::DoGet(Json::Value& target, | |
1981 const std::string& name, | |
405 | 1982 const std::string& uri, |
1983 const std::map<std::string, std::string>& headers) const | |
152 | 1984 { |
1985 MemoryBuffer buffer; | |
1986 | |
405 | 1987 if (DoGet(buffer, name, uri, headers)) |
152 | 1988 { |
1989 buffer.ToJson(target); | |
1990 return true; | |
1991 } | |
1992 else | |
1993 { | |
1994 return false; | |
1995 } | |
1996 } | |
1997 | |
1998 | |
1999 bool OrthancPeers::DoPost(MemoryBuffer& target, | |
2000 const std::string& name, | |
2001 const std::string& uri, | |
405 | 2002 const std::string& body, |
2003 const std::map<std::string, std::string>& headers) const | |
152 | 2004 { |
2005 size_t index; | |
2006 return (LookupName(index, name) && | |
405 | 2007 DoPost(target, index, uri, body, headers)); |
152 | 2008 } |
2009 | |
2010 | |
2011 bool OrthancPeers::DoPost(Json::Value& target, | |
2012 size_t index, | |
2013 const std::string& uri, | |
405 | 2014 const std::string& body, |
2015 const std::map<std::string, std::string>& headers) const | |
152 | 2016 { |
2017 MemoryBuffer buffer; | |
2018 | |
405 | 2019 if (DoPost(buffer, index, uri, body, headers)) |
152 | 2020 { |
2021 buffer.ToJson(target); | |
2022 return true; | |
2023 } | |
2024 else | |
2025 { | |
2026 return false; | |
2027 } | |
2028 } | |
2029 | |
2030 | |
2031 bool OrthancPeers::DoPost(Json::Value& target, | |
2032 const std::string& name, | |
2033 const std::string& uri, | |
405 | 2034 const std::string& body, |
2035 const std::map<std::string, std::string>& headers) const | |
152 | 2036 { |
2037 MemoryBuffer buffer; | |
2038 | |
405 | 2039 if (DoPost(buffer, name, uri, body, headers)) |
152 | 2040 { |
2041 buffer.ToJson(target); | |
2042 return true; | |
2043 } | |
2044 else | |
2045 { | |
2046 return false; | |
2047 } | |
2048 } | |
2049 | |
2050 | |
2051 bool OrthancPeers::DoPost(MemoryBuffer& target, | |
2052 size_t index, | |
2053 const std::string& uri, | |
405 | 2054 const std::string& body, |
2055 const std::map<std::string, std::string>& headers) const | |
152 | 2056 { |
2057 if (index >= index_.size()) | |
2058 { | |
2059 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
2060 } | |
2061 | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2062 if (body.size() > 0xffffffffu) |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2063 { |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2064 LogError("Cannot handle body size > 4GB"); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2065 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2066 } |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2067 |
152 | 2068 OrthancPlugins::MemoryBuffer answer; |
2069 uint16_t status; | |
405 | 2070 PluginHttpHeaders pluginHeaders(headers); |
2071 | |
152 | 2072 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
2073 (GetGlobalContext(), *answer, NULL, &status, peers_, | |
2074 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), | |
405 | 2075 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), body.empty() ? NULL : body.c_str(), body.size(), timeout_); |
152 | 2076 |
2077 if (code == OrthancPluginErrorCode_Success) | |
2078 { | |
2079 target.Swap(answer); | |
2080 return (status == 200); | |
2081 } | |
2082 else | |
2083 { | |
2084 return false; | |
2085 } | |
2086 } | |
2087 | |
2088 | |
2089 bool OrthancPeers::DoPut(size_t index, | |
2090 const std::string& uri, | |
405 | 2091 const std::string& body, |
2092 const std::map<std::string, std::string>& headers) const | |
152 | 2093 { |
2094 if (index >= index_.size()) | |
2095 { | |
2096 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
2097 } | |
2098 | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2099 if (body.size() > 0xffffffffu) |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2100 { |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2101 LogError("Cannot handle body size > 4GB"); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2102 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2103 } |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2104 |
152 | 2105 OrthancPlugins::MemoryBuffer answer; |
2106 uint16_t status; | |
405 | 2107 PluginHttpHeaders pluginHeaders(headers); |
2108 | |
152 | 2109 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
2110 (GetGlobalContext(), *answer, NULL, &status, peers_, | |
2111 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), | |
405 | 2112 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), body.empty() ? NULL : body.c_str(), body.size(), timeout_); |
152 | 2113 |
2114 if (code == OrthancPluginErrorCode_Success) | |
2115 { | |
2116 return (status == 200); | |
2117 } | |
2118 else | |
2119 { | |
2120 return false; | |
2121 } | |
2122 } | |
2123 | |
2124 | |
2125 bool OrthancPeers::DoPut(const std::string& name, | |
2126 const std::string& uri, | |
405 | 2127 const std::string& body, |
2128 const std::map<std::string, std::string>& headers) const | |
152 | 2129 { |
2130 size_t index; | |
2131 return (LookupName(index, name) && | |
405 | 2132 DoPut(index, uri, body, headers)); |
152 | 2133 } |
2134 | |
2135 | |
2136 bool OrthancPeers::DoDelete(size_t index, | |
405 | 2137 const std::string& uri, |
2138 const std::map<std::string, std::string>& headers) const | |
152 | 2139 { |
2140 if (index >= index_.size()) | |
2141 { | |
2142 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
2143 } | |
2144 | |
2145 OrthancPlugins::MemoryBuffer answer; | |
2146 uint16_t status; | |
405 | 2147 PluginHttpHeaders pluginHeaders(headers); |
2148 | |
152 | 2149 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
2150 (GetGlobalContext(), *answer, NULL, &status, peers_, | |
2151 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Delete, uri.c_str(), | |
405 | 2152 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), NULL, 0, timeout_); |
152 | 2153 |
2154 if (code == OrthancPluginErrorCode_Success) | |
2155 { | |
2156 return (status == 200); | |
2157 } | |
2158 else | |
2159 { | |
2160 return false; | |
2161 } | |
2162 } | |
2163 | |
2164 | |
2165 bool OrthancPeers::DoDelete(const std::string& name, | |
405 | 2166 const std::string& uri, |
2167 const std::map<std::string, std::string>& headers) const | |
152 | 2168 { |
2169 size_t index; | |
2170 return (LookupName(index, name) && | |
405 | 2171 DoDelete(index, uri, headers)); |
152 | 2172 } |
2173 #endif | |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | |
2179 /****************************************************************** | |
2180 ** JOBS | |
2181 ******************************************************************/ | |
2182 | |
2183 #if HAS_ORTHANC_PLUGIN_JOB == 1 | |
2184 void OrthancJob::CallbackFinalize(void* job) | |
2185 { | |
2186 if (job != NULL) | |
2187 { | |
2188 delete reinterpret_cast<OrthancJob*>(job); | |
2189 } | |
2190 } | |
2191 | |
2192 | |
2193 float OrthancJob::CallbackGetProgress(void* job) | |
2194 { | |
2195 assert(job != NULL); | |
2196 | |
2197 try | |
2198 { | |
2199 return reinterpret_cast<OrthancJob*>(job)->progress_; | |
2200 } | |
2201 catch (...) | |
2202 { | |
2203 return 0; | |
2204 } | |
2205 } | |
2206 | |
2207 | |
405 | 2208 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 11, 3) |
2209 static OrthancPluginErrorCode CopyStringToMemoryBuffer(OrthancPluginMemoryBuffer* target, | |
2210 const std::string& source) | |
2211 { | |
2212 if (OrthancPluginCreateMemoryBuffer(globalContext_, target, source.size()) != OrthancPluginErrorCode_Success) | |
2213 { | |
2214 return OrthancPluginErrorCode_NotEnoughMemory; | |
2215 } | |
2216 else | |
2217 { | |
2218 if (!source.empty()) | |
2219 { | |
2220 memcpy(target->data, source.c_str(), source.size()); | |
2221 } | |
2222 | |
2223 return OrthancPluginErrorCode_Success; | |
2224 } | |
2225 } | |
2226 #endif | |
2227 | |
2228 | |
2229 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 11, 3) | |
2230 OrthancPluginErrorCode OrthancJob::CallbackGetContent(OrthancPluginMemoryBuffer* target, | |
2231 void* job) | |
2232 { | |
2233 assert(job != NULL); | |
2234 OrthancJob& that = *reinterpret_cast<OrthancJob*>(job); | |
2235 return CopyStringToMemoryBuffer(target, that.content_); | |
2236 } | |
2237 #else | |
152 | 2238 const char* OrthancJob::CallbackGetContent(void* job) |
2239 { | |
2240 assert(job != NULL); | |
2241 | |
2242 try | |
2243 { | |
2244 return reinterpret_cast<OrthancJob*>(job)->content_.c_str(); | |
2245 } | |
2246 catch (...) | |
2247 { | |
2248 return 0; | |
2249 } | |
2250 } | |
405 | 2251 #endif |
2252 | |
2253 | |
2254 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 11, 3) | |
2255 int32_t OrthancJob::CallbackGetSerialized(OrthancPluginMemoryBuffer* target, | |
2256 void* job) | |
2257 { | |
2258 assert(job != NULL); | |
2259 OrthancJob& that = *reinterpret_cast<OrthancJob*>(job); | |
2260 | |
2261 if (that.hasSerialized_) | |
2262 { | |
2263 if (CopyStringToMemoryBuffer(target, that.serialized_) == OrthancPluginErrorCode_Success) | |
2264 { | |
2265 return 1; | |
2266 } | |
2267 else | |
2268 { | |
2269 return -1; | |
2270 } | |
2271 } | |
2272 else | |
2273 { | |
2274 return 0; | |
2275 } | |
2276 } | |
2277 #else | |
152 | 2278 const char* OrthancJob::CallbackGetSerialized(void* job) |
2279 { | |
2280 assert(job != NULL); | |
2281 | |
2282 try | |
2283 { | |
2284 const OrthancJob& tmp = *reinterpret_cast<OrthancJob*>(job); | |
2285 | |
2286 if (tmp.hasSerialized_) | |
2287 { | |
2288 return tmp.serialized_.c_str(); | |
2289 } | |
2290 else | |
2291 { | |
2292 return NULL; | |
2293 } | |
2294 } | |
2295 catch (...) | |
2296 { | |
2297 return 0; | |
2298 } | |
2299 } | |
405 | 2300 #endif |
152 | 2301 |
2302 | |
2303 OrthancPluginJobStepStatus OrthancJob::CallbackStep(void* job) | |
2304 { | |
2305 assert(job != NULL); | |
2306 | |
2307 try | |
2308 { | |
2309 return reinterpret_cast<OrthancJob*>(job)->Step(); | |
2310 } | |
2311 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS&) | |
2312 { | |
2313 return OrthancPluginJobStepStatus_Failure; | |
2314 } | |
2315 catch (...) | |
2316 { | |
2317 return OrthancPluginJobStepStatus_Failure; | |
2318 } | |
2319 } | |
2320 | |
2321 | |
2322 OrthancPluginErrorCode OrthancJob::CallbackStop(void* job, | |
2323 OrthancPluginJobStopReason reason) | |
2324 { | |
2325 assert(job != NULL); | |
2326 | |
2327 try | |
2328 { | |
2329 reinterpret_cast<OrthancJob*>(job)->Stop(reason); | |
2330 return OrthancPluginErrorCode_Success; | |
2331 } | |
2332 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2333 { | |
2334 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2335 } | |
2336 catch (...) | |
2337 { | |
2338 return OrthancPluginErrorCode_Plugin; | |
2339 } | |
2340 } | |
2341 | |
2342 | |
2343 OrthancPluginErrorCode OrthancJob::CallbackReset(void* job) | |
2344 { | |
2345 assert(job != NULL); | |
2346 | |
2347 try | |
2348 { | |
2349 reinterpret_cast<OrthancJob*>(job)->Reset(); | |
2350 return OrthancPluginErrorCode_Success; | |
2351 } | |
2352 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2353 { | |
2354 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2355 } | |
2356 catch (...) | |
2357 { | |
2358 return OrthancPluginErrorCode_Plugin; | |
2359 } | |
2360 } | |
2361 | |
2362 | |
2363 void OrthancJob::ClearContent() | |
2364 { | |
2365 Json::Value empty = Json::objectValue; | |
2366 UpdateContent(empty); | |
2367 } | |
2368 | |
2369 | |
2370 void OrthancJob::UpdateContent(const Json::Value& content) | |
2371 { | |
2372 if (content.type() != Json::objectValue) | |
2373 { | |
2374 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | |
2375 } | |
2376 else | |
2377 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
2378 WriteFastJson(content_, content); |
152 | 2379 } |
2380 } | |
2381 | |
2382 | |
2383 void OrthancJob::ClearSerialized() | |
2384 { | |
2385 hasSerialized_ = false; | |
2386 serialized_.clear(); | |
2387 } | |
2388 | |
2389 | |
2390 void OrthancJob::UpdateSerialized(const Json::Value& serialized) | |
2391 { | |
2392 if (serialized.type() != Json::objectValue) | |
2393 { | |
2394 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | |
2395 } | |
2396 else | |
2397 { | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
2398 WriteFastJson(serialized_, serialized); |
152 | 2399 hasSerialized_ = true; |
2400 } | |
2401 } | |
2402 | |
2403 | |
2404 void OrthancJob::UpdateProgress(float progress) | |
2405 { | |
2406 if (progress < 0 || | |
2407 progress > 1) | |
2408 { | |
2409 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
2410 } | |
2411 | |
2412 progress_ = progress; | |
2413 } | |
2414 | |
2415 | |
2416 OrthancJob::OrthancJob(const std::string& jobType) : | |
2417 jobType_(jobType), | |
2418 progress_(0) | |
2419 { | |
2420 ClearContent(); | |
2421 ClearSerialized(); | |
2422 } | |
2423 | |
2424 | |
2425 OrthancPluginJob* OrthancJob::Create(OrthancJob* job) | |
2426 { | |
2427 if (job == NULL) | |
2428 { | |
2429 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer); | |
2430 } | |
2431 | |
405 | 2432 OrthancPluginJob* orthanc = |
2433 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 11, 3) | |
2434 OrthancPluginCreateJob2 | |
2435 #else | |
2436 OrthancPluginCreateJob | |
2437 #endif | |
2438 (GetGlobalContext(), job, CallbackFinalize, job->jobType_.c_str(), | |
2439 CallbackGetProgress, CallbackGetContent, CallbackGetSerialized, | |
2440 CallbackStep, CallbackStop, CallbackReset); | |
152 | 2441 |
2442 if (orthanc == NULL) | |
2443 { | |
2444 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
2445 } | |
2446 else | |
2447 { | |
2448 return orthanc; | |
2449 } | |
2450 } | |
2451 | |
2452 | |
2453 std::string OrthancJob::Submit(OrthancJob* job, | |
2454 int priority) | |
2455 { | |
2456 if (job == NULL) | |
2457 { | |
2458 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer); | |
2459 } | |
2460 | |
2461 OrthancPluginJob* orthanc = Create(job); | |
2462 | |
2463 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); | |
2464 | |
2465 if (id == NULL) | |
2466 { | |
2467 LogError("Plugin cannot submit job"); | |
2468 OrthancPluginFreeJob(GetGlobalContext(), orthanc); | |
2469 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
2470 } | |
2471 else | |
2472 { | |
2473 std::string tmp(id); | |
2474 tmp.assign(id); | |
2475 OrthancPluginFreeString(GetGlobalContext(), id); | |
2476 | |
2477 return tmp; | |
2478 } | |
2479 } | |
2480 | |
2481 | |
2482 void OrthancJob::SubmitAndWait(Json::Value& result, | |
2483 OrthancJob* job /* takes ownership */, | |
2484 int priority) | |
2485 { | |
2486 std::string id = Submit(job, priority); | |
2487 | |
2488 for (;;) | |
2489 { | |
2490 boost::this_thread::sleep(boost::posix_time::milliseconds(100)); | |
2491 | |
2492 Json::Value status; | |
2493 if (!RestApiGet(status, "/jobs/" + id, false) || | |
2494 !status.isMember("State") || | |
2495 status["State"].type() != Json::stringValue) | |
2496 { | |
2497 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InexistentItem); | |
2498 } | |
2499 | |
2500 const std::string state = status["State"].asString(); | |
2501 if (state == "Success") | |
2502 { | |
2503 if (status.isMember("Content")) | |
2504 { | |
2505 result = status["Content"]; | |
2506 } | |
2507 else | |
2508 { | |
2509 result = Json::objectValue; | |
2510 } | |
2511 | |
2512 return; | |
2513 } | |
2514 else if (state == "Running") | |
2515 { | |
2516 continue; | |
2517 } | |
2518 else if (!status.isMember("ErrorCode") || | |
2519 status["ErrorCode"].type() != Json::intValue) | |
2520 { | |
2521 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InternalError); | |
2522 } | |
2523 else | |
2524 { | |
2525 if (!status.isMember("ErrorDescription") || | |
2526 status["ErrorDescription"].type() != Json::stringValue) | |
2527 { | |
2528 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); | |
2529 } | |
2530 else | |
2531 { | |
2532 #if HAS_ORTHANC_EXCEPTION == 1 | |
2533 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()), | |
2534 status["ErrorDescription"].asString()); | |
2535 #else | |
2536 LogError("Exception while executing the job: " + status["ErrorDescription"].asString()); | |
2537 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); | |
2538 #endif | |
2539 } | |
2540 } | |
2541 } | |
2542 } | |
2543 | |
2544 | |
2545 void OrthancJob::SubmitFromRestApiPost(OrthancPluginRestOutput* output, | |
2546 const Json::Value& body, | |
2547 OrthancJob* job) | |
2548 { | |
2549 static const char* KEY_SYNCHRONOUS = "Synchronous"; | |
2550 static const char* KEY_ASYNCHRONOUS = "Asynchronous"; | |
2551 static const char* KEY_PRIORITY = "Priority"; | |
2552 | |
2553 boost::movelib::unique_ptr<OrthancJob> protection(job); | |
2554 | |
2555 if (body.type() != Json::objectValue) | |
2556 { | |
2557 #if HAS_ORTHANC_EXCEPTION == 1 | |
2558 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2559 "Expected a JSON object in the body"); | |
2560 #else | |
2561 LogError("Expected a JSON object in the body"); | |
2562 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
2563 #endif | |
2564 } | |
2565 | |
2566 bool synchronous = true; | |
2567 | |
2568 if (body.isMember(KEY_SYNCHRONOUS)) | |
2569 { | |
2570 if (body[KEY_SYNCHRONOUS].type() != Json::booleanValue) | |
2571 { | |
2572 #if HAS_ORTHANC_EXCEPTION == 1 | |
2573 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2574 "Option \"" + std::string(KEY_SYNCHRONOUS) + | |
2575 "\" must be Boolean"); | |
2576 #else | |
2577 LogError("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean"); | |
2578 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
2579 #endif | |
2580 } | |
2581 else | |
2582 { | |
2583 synchronous = body[KEY_SYNCHRONOUS].asBool(); | |
2584 } | |
2585 } | |
2586 | |
2587 if (body.isMember(KEY_ASYNCHRONOUS)) | |
2588 { | |
2589 if (body[KEY_ASYNCHRONOUS].type() != Json::booleanValue) | |
2590 { | |
2591 #if HAS_ORTHANC_EXCEPTION == 1 | |
2592 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2593 "Option \"" + std::string(KEY_ASYNCHRONOUS) + | |
2594 "\" must be Boolean"); | |
2595 #else | |
2596 LogError("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean"); | |
2597 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
2598 #endif | |
2599 } | |
2600 else | |
2601 { | |
2602 synchronous = !body[KEY_ASYNCHRONOUS].asBool(); | |
2603 } | |
2604 } | |
2605 | |
2606 int priority = 0; | |
2607 | |
2608 if (body.isMember(KEY_PRIORITY)) | |
2609 { | |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
425
diff
changeset
|
2610 if (body[KEY_PRIORITY].type() != Json::intValue) |
152 | 2611 { |
2612 #if HAS_ORTHANC_EXCEPTION == 1 | |
2613 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2614 "Option \"" + std::string(KEY_PRIORITY) + | |
2615 "\" must be an integer"); | |
2616 #else | |
2617 LogError("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer"); | |
2618 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
2619 #endif | |
2620 } | |
2621 else | |
2622 { | |
2623 priority = !body[KEY_PRIORITY].asInt(); | |
2624 } | |
2625 } | |
2626 | |
2627 Json::Value result; | |
2628 | |
2629 if (synchronous) | |
2630 { | |
2631 OrthancPlugins::OrthancJob::SubmitAndWait(result, protection.release(), priority); | |
2632 } | |
2633 else | |
2634 { | |
2635 std::string id = OrthancPlugins::OrthancJob::Submit(protection.release(), priority); | |
2636 | |
2637 result = Json::objectValue; | |
2638 result["ID"] = id; | |
2639 result["Path"] = "/jobs/" + id; | |
2640 } | |
2641 | |
2642 std::string s = result.toStyledString(); | |
2643 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), | |
2644 s.size(), "application/json"); | |
2645 } | |
2646 | |
2647 #endif | |
2648 | |
2649 | |
2650 | |
2651 | |
2652 /****************************************************************** | |
2653 ** METRICS | |
2654 ******************************************************************/ | |
2655 | |
2656 #if HAS_ORTHANC_PLUGIN_METRICS == 1 | |
2657 MetricsTimer::MetricsTimer(const char* name) : | |
2658 name_(name) | |
2659 { | |
2660 start_ = boost::posix_time::microsec_clock::universal_time(); | |
2661 } | |
2662 | |
2663 MetricsTimer::~MetricsTimer() | |
2664 { | |
2665 const boost::posix_time::ptime stop = boost::posix_time::microsec_clock::universal_time(); | |
2666 const boost::posix_time::time_duration diff = stop - start_; | |
2667 OrthancPluginSetMetricsValue(GetGlobalContext(), name_.c_str(), static_cast<float>(diff.total_milliseconds()), | |
2668 OrthancPluginMetricsType_Timer); | |
2669 } | |
2670 #endif | |
2671 | |
2672 | |
2673 | |
2674 | |
2675 /****************************************************************** | |
2676 ** HTTP CLIENT | |
2677 ******************************************************************/ | |
2678 | |
2679 #if HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 | |
2680 class HttpClient::RequestBodyWrapper : public boost::noncopyable | |
2681 { | |
2682 private: | |
2683 static RequestBodyWrapper& GetObject(void* body) | |
2684 { | |
2685 assert(body != NULL); | |
2686 return *reinterpret_cast<RequestBodyWrapper*>(body); | |
2687 } | |
2688 | |
2689 IRequestBody& body_; | |
2690 bool done_; | |
2691 std::string chunk_; | |
2692 | |
2693 public: | |
2694 RequestBodyWrapper(IRequestBody& body) : | |
2695 body_(body), | |
2696 done_(false) | |
2697 { | |
2698 } | |
2699 | |
2700 static uint8_t IsDone(void* body) | |
2701 { | |
2702 return GetObject(body).done_; | |
2703 } | |
2704 | |
2705 static const void* GetChunkData(void* body) | |
2706 { | |
2707 return GetObject(body).chunk_.c_str(); | |
2708 } | |
2709 | |
2710 static uint32_t GetChunkSize(void* body) | |
2711 { | |
2712 return static_cast<uint32_t>(GetObject(body).chunk_.size()); | |
2713 } | |
2714 | |
2715 static OrthancPluginErrorCode Next(void* body) | |
2716 { | |
2717 RequestBodyWrapper& that = GetObject(body); | |
2718 | |
2719 if (that.done_) | |
2720 { | |
2721 return OrthancPluginErrorCode_BadSequenceOfCalls; | |
2722 } | |
2723 else | |
2724 { | |
2725 try | |
2726 { | |
2727 that.done_ = !that.body_.ReadNextChunk(that.chunk_); | |
2728 return OrthancPluginErrorCode_Success; | |
2729 } | |
2730 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2731 { | |
2732 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2733 } | |
2734 catch (...) | |
2735 { | |
405 | 2736 return OrthancPluginErrorCode_Plugin; |
152 | 2737 } |
2738 } | |
2739 } | |
2740 }; | |
2741 | |
2742 | |
2743 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
2744 static OrthancPluginErrorCode AnswerAddHeaderCallback(void* answer, | |
2745 const char* key, | |
2746 const char* value) | |
2747 { | |
2748 assert(answer != NULL && key != NULL && value != NULL); | |
2749 | |
2750 try | |
2751 { | |
2752 reinterpret_cast<HttpClient::IAnswer*>(answer)->AddHeader(key, value); | |
2753 return OrthancPluginErrorCode_Success; | |
2754 } | |
2755 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2756 { | |
2757 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2758 } | |
2759 catch (...) | |
2760 { | |
2761 return OrthancPluginErrorCode_Plugin; | |
2762 } | |
2763 } | |
2764 #endif | |
2765 | |
2766 | |
2767 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
2768 static OrthancPluginErrorCode AnswerAddChunkCallback(void* answer, | |
2769 const void* data, | |
2770 uint32_t size) | |
2771 { | |
2772 assert(answer != NULL); | |
2773 | |
2774 try | |
2775 { | |
2776 reinterpret_cast<HttpClient::IAnswer*>(answer)->AddChunk(data, size); | |
2777 return OrthancPluginErrorCode_Success; | |
2778 } | |
2779 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2780 { | |
2781 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2782 } | |
2783 catch (...) | |
2784 { | |
2785 return OrthancPluginErrorCode_Plugin; | |
2786 } | |
2787 } | |
2788 #endif | |
2789 | |
2790 | |
2791 HttpClient::HttpClient() : | |
2792 httpStatus_(0), | |
2793 method_(OrthancPluginHttpMethod_Get), | |
2794 timeout_(0), | |
2795 pkcs11_(false), | |
2796 chunkedBody_(NULL), | |
2797 allowChunkedTransfers_(true) | |
2798 { | |
2799 } | |
2800 | |
2801 | |
2802 void HttpClient::AddHeaders(const HttpHeaders& headers) | |
2803 { | |
2804 for (HttpHeaders::const_iterator it = headers.begin(); | |
2805 it != headers.end(); ++it) | |
2806 { | |
2807 headers_[it->first] = it->second; | |
2808 } | |
2809 } | |
2810 | |
2811 | |
2812 void HttpClient::SetCredentials(const std::string& username, | |
2813 const std::string& password) | |
2814 { | |
2815 username_ = username; | |
2816 password_ = password; | |
2817 } | |
2818 | |
2819 | |
2820 void HttpClient::ClearCredentials() | |
2821 { | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2822 username_.clear(); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
2823 password_.clear(); |
152 | 2824 } |
2825 | |
2826 | |
2827 void HttpClient::SetCertificate(const std::string& certificateFile, | |
2828 const std::string& keyFile, | |
2829 const std::string& keyPassword) | |
2830 { | |
2831 certificateFile_ = certificateFile; | |
2832 certificateKeyFile_ = keyFile; | |
2833 certificateKeyPassword_ = keyPassword; | |
2834 } | |
2835 | |
2836 | |
2837 void HttpClient::ClearCertificate() | |
2838 { | |
2839 certificateFile_.clear(); | |
2840 certificateKeyFile_.clear(); | |
2841 certificateKeyPassword_.clear(); | |
2842 } | |
2843 | |
2844 | |
2845 void HttpClient::ClearBody() | |
2846 { | |
2847 fullBody_.clear(); | |
2848 chunkedBody_ = NULL; | |
2849 } | |
2850 | |
2851 | |
2852 void HttpClient::SwapBody(std::string& body) | |
2853 { | |
2854 fullBody_.swap(body); | |
2855 chunkedBody_ = NULL; | |
2856 } | |
2857 | |
2858 | |
2859 void HttpClient::SetBody(const std::string& body) | |
2860 { | |
2861 fullBody_ = body; | |
2862 chunkedBody_ = NULL; | |
2863 } | |
2864 | |
2865 | |
2866 void HttpClient::SetBody(IRequestBody& body) | |
2867 { | |
2868 fullBody_.clear(); | |
2869 chunkedBody_ = &body; | |
2870 } | |
2871 | |
2872 | |
2873 namespace | |
2874 { | |
2875 class HeadersWrapper : public boost::noncopyable | |
2876 { | |
2877 private: | |
2878 std::vector<const char*> headersKeys_; | |
2879 std::vector<const char*> headersValues_; | |
2880 | |
2881 public: | |
2882 HeadersWrapper(const HttpClient::HttpHeaders& headers) | |
2883 { | |
2884 headersKeys_.reserve(headers.size()); | |
2885 headersValues_.reserve(headers.size()); | |
2886 | |
2887 for (HttpClient::HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); ++it) | |
2888 { | |
2889 headersKeys_.push_back(it->first.c_str()); | |
2890 headersValues_.push_back(it->second.c_str()); | |
2891 } | |
2892 } | |
2893 | |
2894 void AddStaticString(const char* key, | |
2895 const char* value) | |
2896 { | |
2897 headersKeys_.push_back(key); | |
2898 headersValues_.push_back(value); | |
2899 } | |
2900 | |
2901 uint32_t GetCount() const | |
2902 { | |
2903 return headersKeys_.size(); | |
2904 } | |
2905 | |
2906 const char* const* GetKeys() const | |
2907 { | |
2908 return headersKeys_.empty() ? NULL : &headersKeys_[0]; | |
2909 } | |
2910 | |
2911 const char* const* GetValues() const | |
2912 { | |
2913 return headersValues_.empty() ? NULL : &headersValues_[0]; | |
2914 } | |
2915 }; | |
2916 | |
2917 | |
2918 class MemoryRequestBody : public HttpClient::IRequestBody | |
2919 { | |
2920 private: | |
2921 std::string body_; | |
2922 bool done_; | |
2923 | |
2924 public: | |
2925 MemoryRequestBody(const std::string& body) : | |
2926 body_(body), | |
2927 done_(false) | |
2928 { | |
2929 if (body_.empty()) | |
2930 { | |
2931 done_ = true; | |
2932 } | |
2933 } | |
2934 | |
2935 virtual bool ReadNextChunk(std::string& chunk) | |
2936 { | |
2937 if (done_) | |
2938 { | |
2939 return false; | |
2940 } | |
2941 else | |
2942 { | |
2943 chunk.swap(body_); | |
2944 done_ = true; | |
2945 return true; | |
2946 } | |
2947 } | |
2948 }; | |
2949 | |
2950 | |
2951 // This class mimics Orthanc::ChunkedBuffer | |
2952 class ChunkedBuffer : public boost::noncopyable | |
2953 { | |
2954 private: | |
2955 typedef std::list<std::string*> Content; | |
2956 | |
2957 Content content_; | |
2958 size_t size_; | |
2959 | |
2960 public: | |
2961 ChunkedBuffer() : | |
2962 size_(0) | |
2963 { | |
2964 } | |
2965 | |
2966 ~ChunkedBuffer() | |
2967 { | |
2968 Clear(); | |
2969 } | |
2970 | |
2971 void Clear() | |
2972 { | |
2973 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
2974 { | |
2975 assert(*it != NULL); | |
2976 delete *it; | |
2977 } | |
2978 | |
284
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2979 size_ = 0; |
152 | 2980 content_.clear(); |
2981 } | |
2982 | |
284
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2983 /** |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2984 * Since Orthanc 1.9.3, this function also clears the content of |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2985 * the ChunkedBuffer in order to mimic the behavior of the |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2986 * original class "Orthanc::ChunkedBuffer". This prevents the |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2987 * forgetting of calling "Clear()" in order to reduce memory |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2988 * consumption. |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2989 **/ |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
2990 void Flatten(std::string& target) |
152 | 2991 { |
2992 target.resize(size_); | |
2993 | |
2994 size_t pos = 0; | |
2995 | |
2996 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it) | |
2997 { | |
2998 assert(*it != NULL); | |
2999 size_t s = (*it)->size(); | |
3000 | |
3001 if (s != 0) | |
3002 { | |
3003 memcpy(&target[pos], (*it)->c_str(), s); | |
3004 pos += s; | |
3005 } | |
284
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3006 |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3007 delete *it; |
152 | 3008 } |
3009 | |
284
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3010 assert(pos == target.size()); |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3011 |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3012 size_ = 0; |
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3013 content_.clear(); |
152 | 3014 } |
3015 | |
3016 void AddChunk(const void* data, | |
3017 size_t size) | |
3018 { | |
3019 content_.push_back(new std::string(reinterpret_cast<const char*>(data), size)); | |
3020 size_ += size; | |
3021 } | |
3022 | |
3023 void AddChunk(const std::string& chunk) | |
3024 { | |
3025 content_.push_back(new std::string(chunk)); | |
3026 size_ += chunk.size(); | |
3027 } | |
3028 }; | |
3029 | |
3030 | |
3031 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
3032 class MemoryAnswer : public HttpClient::IAnswer | |
3033 { | |
3034 private: | |
3035 HttpClient::HttpHeaders headers_; | |
3036 ChunkedBuffer body_; | |
3037 | |
3038 public: | |
3039 const HttpClient::HttpHeaders& GetHeaders() const | |
3040 { | |
3041 return headers_; | |
3042 } | |
3043 | |
284
64bf8914f02e
sync + no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
272
diff
changeset
|
3044 ChunkedBuffer& GetBody() |
152 | 3045 { |
3046 return body_; | |
3047 } | |
3048 | |
3049 virtual void AddHeader(const std::string& key, | |
3050 const std::string& value) | |
3051 { | |
3052 headers_[key] = value; | |
3053 } | |
3054 | |
3055 virtual void AddChunk(const void* data, | |
3056 size_t size) | |
3057 { | |
3058 body_.AddChunk(data, size); | |
3059 } | |
3060 }; | |
3061 #endif | |
3062 } | |
3063 | |
3064 | |
3065 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
3066 void HttpClient::ExecuteWithStream(uint16_t& httpStatus, | |
3067 IAnswer& answer, | |
3068 IRequestBody& body) const | |
3069 { | |
3070 HeadersWrapper h(headers_); | |
3071 | |
3072 if (method_ == OrthancPluginHttpMethod_Post || | |
3073 method_ == OrthancPluginHttpMethod_Put) | |
3074 { | |
3075 // Automatically set the "Transfer-Encoding" header if absent | |
3076 bool found = false; | |
3077 | |
3078 for (HttpHeaders::const_iterator it = headers_.begin(); it != headers_.end(); ++it) | |
3079 { | |
3080 if (boost::iequals(it->first, "Transfer-Encoding")) | |
3081 { | |
3082 found = true; | |
3083 break; | |
3084 } | |
3085 } | |
3086 | |
3087 if (!found) | |
3088 { | |
3089 h.AddStaticString("Transfer-Encoding", "chunked"); | |
3090 } | |
3091 } | |
3092 | |
3093 RequestBodyWrapper request(body); | |
3094 | |
3095 OrthancPluginErrorCode error = OrthancPluginChunkedHttpClient( | |
3096 GetGlobalContext(), | |
3097 &answer, | |
3098 AnswerAddChunkCallback, | |
3099 AnswerAddHeaderCallback, | |
3100 &httpStatus, | |
3101 method_, | |
3102 url_.c_str(), | |
3103 h.GetCount(), | |
3104 h.GetKeys(), | |
3105 h.GetValues(), | |
3106 &request, | |
3107 RequestBodyWrapper::IsDone, | |
3108 RequestBodyWrapper::GetChunkData, | |
3109 RequestBodyWrapper::GetChunkSize, | |
3110 RequestBodyWrapper::Next, | |
3111 username_.empty() ? NULL : username_.c_str(), | |
3112 password_.empty() ? NULL : password_.c_str(), | |
3113 timeout_, | |
3114 certificateFile_.empty() ? NULL : certificateFile_.c_str(), | |
3115 certificateFile_.empty() ? NULL : certificateKeyFile_.c_str(), | |
3116 certificateFile_.empty() ? NULL : certificateKeyPassword_.c_str(), | |
3117 pkcs11_ ? 1 : 0); | |
3118 | |
3119 if (error != OrthancPluginErrorCode_Success) | |
3120 { | |
3121 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
3122 } | |
3123 } | |
3124 #endif | |
3125 | |
3126 | |
3127 void HttpClient::ExecuteWithoutStream(uint16_t& httpStatus, | |
3128 HttpHeaders& answerHeaders, | |
3129 std::string& answerBody, | |
3130 const std::string& body) const | |
3131 { | |
3132 HeadersWrapper headers(headers_); | |
3133 | |
3134 MemoryBuffer answerBodyBuffer, answerHeadersBuffer; | |
3135 | |
358
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3136 if (body.size() > 0xffffffffu) |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3137 { |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3138 LogError("Cannot handle body size > 4GB"); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3139 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3140 } |
1280b40d6696
switch to OpenSSL 3.0.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
3141 |
152 | 3142 OrthancPluginErrorCode error = OrthancPluginHttpClient( |
3143 GetGlobalContext(), | |
3144 *answerBodyBuffer, | |
3145 *answerHeadersBuffer, | |
3146 &httpStatus, | |
3147 method_, | |
3148 url_.c_str(), | |
3149 headers.GetCount(), | |
3150 headers.GetKeys(), | |
3151 headers.GetValues(), | |
3152 body.empty() ? NULL : body.c_str(), | |
3153 body.size(), | |
3154 username_.empty() ? NULL : username_.c_str(), | |
3155 password_.empty() ? NULL : password_.c_str(), | |
3156 timeout_, | |
3157 certificateFile_.empty() ? NULL : certificateFile_.c_str(), | |
3158 certificateFile_.empty() ? NULL : certificateKeyFile_.c_str(), | |
3159 certificateFile_.empty() ? NULL : certificateKeyPassword_.c_str(), | |
3160 pkcs11_ ? 1 : 0); | |
3161 | |
3162 if (error != OrthancPluginErrorCode_Success) | |
3163 { | |
3164 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
3165 } | |
3166 | |
3167 Json::Value v; | |
3168 answerHeadersBuffer.ToJson(v); | |
3169 | |
3170 if (v.type() != Json::objectValue) | |
3171 { | |
3172 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
3173 } | |
3174 | |
3175 Json::Value::Members members = v.getMemberNames(); | |
3176 answerHeaders.clear(); | |
3177 | |
3178 for (size_t i = 0; i < members.size(); i++) | |
3179 { | |
3180 const Json::Value& h = v[members[i]]; | |
3181 if (h.type() != Json::stringValue) | |
3182 { | |
3183 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
3184 } | |
3185 else | |
3186 { | |
3187 answerHeaders[members[i]] = h.asString(); | |
3188 } | |
3189 } | |
3190 | |
3191 answerBodyBuffer.ToString(answerBody); | |
3192 } | |
3193 | |
3194 | |
3195 void HttpClient::Execute(IAnswer& answer) | |
3196 { | |
3197 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
3198 if (allowChunkedTransfers_) | |
3199 { | |
3200 if (chunkedBody_ != NULL) | |
3201 { | |
3202 ExecuteWithStream(httpStatus_, answer, *chunkedBody_); | |
3203 } | |
3204 else | |
3205 { | |
3206 MemoryRequestBody wrapper(fullBody_); | |
3207 ExecuteWithStream(httpStatus_, answer, wrapper); | |
3208 } | |
3209 | |
3210 return; | |
3211 } | |
3212 #endif | |
3213 | |
3214 // Compatibility mode for Orthanc SDK <= 1.5.6 or if chunked | |
3215 // transfers are disabled. This results in higher memory usage | |
3216 // (all chunks from the answer body are sent at once) | |
3217 | |
3218 HttpHeaders answerHeaders; | |
3219 std::string answerBody; | |
3220 Execute(answerHeaders, answerBody); | |
3221 | |
3222 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | |
3223 it != answerHeaders.end(); ++it) | |
3224 { | |
3225 answer.AddHeader(it->first, it->second); | |
3226 } | |
3227 | |
3228 if (!answerBody.empty()) | |
3229 { | |
3230 answer.AddChunk(answerBody.c_str(), answerBody.size()); | |
3231 } | |
3232 } | |
3233 | |
3234 | |
3235 void HttpClient::Execute(HttpHeaders& answerHeaders /* out */, | |
3236 std::string& answerBody /* out */) | |
3237 { | |
3238 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
3239 if (allowChunkedTransfers_) | |
3240 { | |
3241 MemoryAnswer answer; | |
3242 Execute(answer); | |
3243 answerHeaders = answer.GetHeaders(); | |
3244 answer.GetBody().Flatten(answerBody); | |
3245 return; | |
3246 } | |
3247 #endif | |
3248 | |
3249 // Compatibility mode for Orthanc SDK <= 1.5.6 or if chunked | |
3250 // transfers are disabled. This results in higher memory usage | |
3251 // (all chunks from the request body are sent at once) | |
3252 | |
3253 if (chunkedBody_ != NULL) | |
3254 { | |
3255 ChunkedBuffer buffer; | |
3256 | |
3257 std::string chunk; | |
3258 while (chunkedBody_->ReadNextChunk(chunk)) | |
3259 { | |
3260 buffer.AddChunk(chunk); | |
3261 } | |
3262 | |
3263 std::string body; | |
3264 buffer.Flatten(body); | |
3265 | |
3266 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, body); | |
3267 } | |
3268 else | |
3269 { | |
3270 ExecuteWithoutStream(httpStatus_, answerHeaders, answerBody, fullBody_); | |
3271 } | |
3272 } | |
3273 | |
3274 | |
3275 void HttpClient::Execute(HttpHeaders& answerHeaders /* out */, | |
3276 Json::Value& answerBody /* out */) | |
3277 { | |
3278 std::string body; | |
3279 Execute(answerHeaders, body); | |
3280 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
160
diff
changeset
|
3281 if (!ReadJson(answerBody, body)) |
152 | 3282 { |
3283 LogError("Cannot convert HTTP answer body to JSON"); | |
3284 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
3285 } | |
3286 } | |
3287 | |
3288 | |
3289 void HttpClient::Execute() | |
3290 { | |
3291 HttpHeaders answerHeaders; | |
3292 std::string body; | |
3293 Execute(answerHeaders, body); | |
3294 } | |
3295 | |
3296 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ | |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 /****************************************************************** | |
3303 ** CHUNKED HTTP SERVER | |
3304 ******************************************************************/ | |
3305 | |
3306 namespace Internals | |
3307 { | |
3308 void NullRestCallback(OrthancPluginRestOutput* output, | |
3309 const char* url, | |
3310 const OrthancPluginHttpRequest* request) | |
3311 { | |
3312 } | |
3313 | |
3314 IChunkedRequestReader *NullChunkedRestCallback(const char* url, | |
3315 const OrthancPluginHttpRequest* request) | |
3316 { | |
3317 return NULL; | |
3318 } | |
3319 | |
3320 | |
3321 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1 | |
3322 | |
3323 OrthancPluginErrorCode ChunkedRequestReaderAddChunk( | |
3324 OrthancPluginServerChunkedRequestReader* reader, | |
3325 const void* data, | |
3326 uint32_t size) | |
3327 { | |
3328 try | |
3329 { | |
3330 if (reader == NULL) | |
3331 { | |
3332 return OrthancPluginErrorCode_InternalError; | |
3333 } | |
3334 | |
3335 reinterpret_cast<IChunkedRequestReader*>(reader)->AddChunk(data, size); | |
3336 return OrthancPluginErrorCode_Success; | |
3337 } | |
3338 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3339 { | |
3340 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3341 } | |
3342 catch (boost::bad_lexical_cast&) | |
3343 { | |
3344 return OrthancPluginErrorCode_BadFileFormat; | |
3345 } | |
3346 catch (...) | |
3347 { | |
3348 return OrthancPluginErrorCode_Plugin; | |
3349 } | |
3350 } | |
3351 | |
3352 | |
3353 OrthancPluginErrorCode ChunkedRequestReaderExecute( | |
3354 OrthancPluginServerChunkedRequestReader* reader, | |
3355 OrthancPluginRestOutput* output) | |
3356 { | |
3357 try | |
3358 { | |
3359 if (reader == NULL) | |
3360 { | |
3361 return OrthancPluginErrorCode_InternalError; | |
3362 } | |
3363 | |
3364 reinterpret_cast<IChunkedRequestReader*>(reader)->Execute(output); | |
3365 return OrthancPluginErrorCode_Success; | |
3366 } | |
3367 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3368 { | |
3369 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3370 } | |
3371 catch (boost::bad_lexical_cast&) | |
3372 { | |
3373 return OrthancPluginErrorCode_BadFileFormat; | |
3374 } | |
3375 catch (...) | |
3376 { | |
3377 return OrthancPluginErrorCode_Plugin; | |
3378 } | |
3379 } | |
3380 | |
3381 | |
3382 void ChunkedRequestReaderFinalize( | |
3383 OrthancPluginServerChunkedRequestReader* reader) | |
3384 { | |
3385 if (reader != NULL) | |
3386 { | |
3387 delete reinterpret_cast<IChunkedRequestReader*>(reader); | |
3388 } | |
3389 } | |
3390 | |
3391 #else | |
3392 | |
3393 OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output, | |
3394 const char* url, | |
3395 const OrthancPluginHttpRequest* request, | |
3396 RestCallback GetHandler, | |
3397 ChunkedRestCallback PostHandler, | |
3398 RestCallback DeleteHandler, | |
3399 ChunkedRestCallback PutHandler) | |
3400 { | |
3401 try | |
3402 { | |
3403 std::string allowed; | |
3404 | |
3405 if (GetHandler != Internals::NullRestCallback) | |
3406 { | |
3407 allowed += "GET"; | |
3408 } | |
3409 | |
3410 if (PostHandler != Internals::NullChunkedRestCallback) | |
3411 { | |
3412 if (!allowed.empty()) | |
3413 { | |
3414 allowed += ","; | |
3415 } | |
3416 | |
3417 allowed += "POST"; | |
3418 } | |
3419 | |
3420 if (DeleteHandler != Internals::NullRestCallback) | |
3421 { | |
3422 if (!allowed.empty()) | |
3423 { | |
3424 allowed += ","; | |
3425 } | |
3426 | |
3427 allowed += "DELETE"; | |
3428 } | |
3429 | |
3430 if (PutHandler != Internals::NullChunkedRestCallback) | |
3431 { | |
3432 if (!allowed.empty()) | |
3433 { | |
3434 allowed += ","; | |
3435 } | |
3436 | |
3437 allowed += "PUT"; | |
3438 } | |
3439 | |
3440 switch (request->method) | |
3441 { | |
3442 case OrthancPluginHttpMethod_Get: | |
3443 if (GetHandler == Internals::NullRestCallback) | |
3444 { | |
3445 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
3446 } | |
3447 else | |
3448 { | |
3449 GetHandler(output, url, request); | |
3450 } | |
3451 | |
3452 break; | |
3453 | |
3454 case OrthancPluginHttpMethod_Post: | |
3455 if (PostHandler == Internals::NullChunkedRestCallback) | |
3456 { | |
3457 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
3458 } | |
3459 else | |
3460 { | |
3461 boost::movelib::unique_ptr<IChunkedRequestReader> reader(PostHandler(url, request)); | |
3462 if (reader.get() == NULL) | |
3463 { | |
3464 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3465 } | |
3466 else | |
3467 { | |
3468 reader->AddChunk(request->body, request->bodySize); | |
3469 reader->Execute(output); | |
3470 } | |
3471 } | |
3472 | |
3473 break; | |
3474 | |
3475 case OrthancPluginHttpMethod_Delete: | |
3476 if (DeleteHandler == Internals::NullRestCallback) | |
3477 { | |
3478 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
3479 } | |
3480 else | |
3481 { | |
3482 DeleteHandler(output, url, request); | |
3483 } | |
3484 | |
3485 break; | |
3486 | |
3487 case OrthancPluginHttpMethod_Put: | |
3488 if (PutHandler == Internals::NullChunkedRestCallback) | |
3489 { | |
3490 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
3491 } | |
3492 else | |
3493 { | |
3494 boost::movelib::unique_ptr<IChunkedRequestReader> reader(PutHandler(url, request)); | |
3495 if (reader.get() == NULL) | |
3496 { | |
3497 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3498 } | |
3499 else | |
3500 { | |
3501 reader->AddChunk(request->body, request->bodySize); | |
3502 reader->Execute(output); | |
3503 } | |
3504 } | |
3505 | |
3506 break; | |
3507 | |
3508 default: | |
3509 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
3510 } | |
3511 | |
3512 return OrthancPluginErrorCode_Success; | |
3513 } | |
3514 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3515 { | |
3516 #if HAS_ORTHANC_EXCEPTION == 1 && HAS_ORTHANC_PLUGIN_EXCEPTION_DETAILS == 1 | |
3517 if (HasGlobalContext() && | |
3518 e.HasDetails()) | |
3519 { | |
3520 // The "false" instructs Orthanc not to log the detailed | |
3521 // error message. This is to avoid duplicating the details, | |
3522 // because "OrthancException" already does it on construction. | |
3523 OrthancPluginSetHttpErrorDetails | |
3524 (GetGlobalContext(), output, e.GetDetails(), false); | |
3525 } | |
3526 #endif | |
3527 | |
3528 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3529 } | |
3530 catch (boost::bad_lexical_cast&) | |
3531 { | |
3532 return OrthancPluginErrorCode_BadFileFormat; | |
3533 } | |
3534 catch (...) | |
3535 { | |
3536 return OrthancPluginErrorCode_Plugin; | |
3537 } | |
3538 } | |
3539 #endif | |
3540 } | |
3541 | |
3542 | |
3543 #if HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP == 1 | |
3544 OrthancPluginErrorCode IStorageCommitmentScpHandler::Lookup( | |
3545 OrthancPluginStorageCommitmentFailureReason* target, | |
3546 void* rawHandler, | |
3547 const char* sopClassUid, | |
3548 const char* sopInstanceUid) | |
3549 { | |
3550 assert(target != NULL && | |
3551 rawHandler != NULL); | |
3552 | |
3553 try | |
3554 { | |
3555 IStorageCommitmentScpHandler& handler = *reinterpret_cast<IStorageCommitmentScpHandler*>(rawHandler); | |
3556 *target = handler.Lookup(sopClassUid, sopInstanceUid); | |
3557 return OrthancPluginErrorCode_Success; | |
3558 } | |
3559 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3560 { | |
3561 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3562 } | |
3563 catch (...) | |
3564 { | |
3565 return OrthancPluginErrorCode_Plugin; | |
3566 } | |
3567 } | |
3568 #endif | |
3569 | |
3570 | |
3571 #if HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP == 1 | |
3572 void IStorageCommitmentScpHandler::Destructor(void* rawHandler) | |
3573 { | |
3574 assert(rawHandler != NULL); | |
3575 delete reinterpret_cast<IStorageCommitmentScpHandler*>(rawHandler); | |
3576 } | |
3577 #endif | |
3578 | |
3579 | |
3580 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
3581 DicomInstance::DicomInstance(const OrthancPluginDicomInstance* instance) : | |
3582 toFree_(false), | |
3583 instance_(instance) | |
3584 { | |
3585 } | |
3586 #else | |
3587 DicomInstance::DicomInstance(OrthancPluginDicomInstance* instance) : | |
3588 toFree_(false), | |
3589 instance_(instance) | |
3590 { | |
3591 } | |
3592 #endif | |
3593 | |
3594 | |
3595 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3596 DicomInstance::DicomInstance(const void* buffer, | |
3597 size_t size) : | |
3598 toFree_(true), | |
3599 instance_(OrthancPluginCreateDicomInstance(GetGlobalContext(), buffer, size)) | |
3600 { | |
3601 if (instance_ == NULL) | |
3602 { | |
3603 ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer); | |
3604 } | |
3605 } | |
3606 #endif | |
3607 | |
3608 | |
3609 DicomInstance::~DicomInstance() | |
3610 { | |
3611 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3612 if (toFree_ && | |
3613 instance_ != NULL) | |
3614 { | |
3615 OrthancPluginFreeDicomInstance( | |
3616 GetGlobalContext(), const_cast<OrthancPluginDicomInstance*>(instance_)); | |
3617 } | |
3618 #endif | |
3619 } | |
3620 | |
3621 | |
3622 std::string DicomInstance::GetRemoteAet() const | |
3623 { | |
3624 const char* s = OrthancPluginGetInstanceRemoteAet(GetGlobalContext(), instance_); | |
3625 if (s == NULL) | |
3626 { | |
3627 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3628 } | |
3629 else | |
3630 { | |
3631 return std::string(s); | |
3632 } | |
3633 } | |
3634 | |
3635 | |
3636 void DicomInstance::GetJson(Json::Value& target) const | |
3637 { | |
3638 OrthancString s; | |
3639 s.Assign(OrthancPluginGetInstanceJson(GetGlobalContext(), instance_)); | |
3640 s.ToJson(target); | |
3641 } | |
3642 | |
3643 | |
3644 void DicomInstance::GetSimplifiedJson(Json::Value& target) const | |
3645 { | |
3646 OrthancString s; | |
3647 s.Assign(OrthancPluginGetInstanceSimplifiedJson(GetGlobalContext(), instance_)); | |
3648 s.ToJson(target); | |
3649 } | |
3650 | |
3651 | |
3652 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
3653 std::string DicomInstance::GetTransferSyntaxUid() const | |
3654 { | |
3655 OrthancString s; | |
3656 s.Assign(OrthancPluginGetInstanceTransferSyntaxUid(GetGlobalContext(), instance_)); | |
3657 | |
3658 std::string result; | |
3659 s.ToString(result); | |
3660 return result; | |
3661 } | |
3662 #endif | |
3663 | |
3664 | |
3665 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
3666 bool DicomInstance::HasPixelData() const | |
3667 { | |
3668 int32_t result = OrthancPluginHasInstancePixelData(GetGlobalContext(), instance_); | |
3669 if (result < 0) | |
3670 { | |
3671 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3672 } | |
3673 else | |
3674 { | |
3675 return (result != 0); | |
3676 } | |
3677 } | |
3678 #endif | |
3679 | |
3680 | |
3681 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3682 void DicomInstance::GetRawFrame(std::string& target, | |
3683 unsigned int frameIndex) const | |
3684 { | |
3685 MemoryBuffer buffer; | |
3686 OrthancPluginErrorCode code = OrthancPluginGetInstanceRawFrame( | |
3687 GetGlobalContext(), *buffer, instance_, frameIndex); | |
3688 | |
3689 if (code == OrthancPluginErrorCode_Success) | |
3690 { | |
3691 buffer.ToString(target); | |
3692 } | |
3693 else | |
3694 { | |
3695 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
3696 } | |
3697 } | |
3698 #endif | |
3699 | |
3700 | |
3701 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3702 OrthancImage* DicomInstance::GetDecodedFrame(unsigned int frameIndex) const | |
3703 { | |
3704 OrthancPluginImage* image = OrthancPluginGetInstanceDecodedFrame( | |
3705 GetGlobalContext(), instance_, frameIndex); | |
3706 | |
3707 if (image == NULL) | |
3708 { | |
3709 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3710 } | |
3711 else | |
3712 { | |
3713 return new OrthancImage(image); | |
3714 } | |
3715 } | |
3716 #endif | |
3717 | |
3718 | |
3719 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3720 void DicomInstance::Serialize(std::string& target) const | |
3721 { | |
3722 MemoryBuffer buffer; | |
3723 OrthancPluginErrorCode code = OrthancPluginSerializeDicomInstance( | |
3724 GetGlobalContext(), *buffer, instance_); | |
3725 | |
3726 if (code == OrthancPluginErrorCode_Success) | |
3727 { | |
3728 buffer.ToString(target); | |
3729 } | |
3730 else | |
3731 { | |
3732 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
3733 } | |
3734 } | |
3735 #endif | |
3736 | |
3737 | |
3738 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
3739 DicomInstance* DicomInstance::Transcode(const void* buffer, | |
3740 size_t size, | |
3741 const std::string& transferSyntax) | |
3742 { | |
3743 OrthancPluginDicomInstance* instance = OrthancPluginTranscodeDicomInstance( | |
3744 GetGlobalContext(), buffer, size, transferSyntax.c_str()); | |
3745 | |
3746 if (instance == NULL) | |
3747 { | |
3748 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3749 } | |
3750 else | |
3751 { | |
3752 boost::movelib::unique_ptr<DicomInstance> result(new DicomInstance(instance)); | |
3753 result->toFree_ = true; | |
3754 return result.release(); | |
3755 } | |
3756 } | |
3757 #endif | |
405 | 3758 |
3759 | |
425 | 3760 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 1) |
3761 DicomInstance* DicomInstance::Load(const std::string& instanceId, | |
3762 OrthancPluginLoadDicomInstanceMode mode) | |
3763 { | |
3764 OrthancPluginDicomInstance* instance = OrthancPluginLoadDicomInstance( | |
3765 GetGlobalContext(), instanceId.c_str(), mode); | |
3766 | |
3767 if (instance == NULL) | |
3768 { | |
3769 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
3770 } | |
3771 else | |
3772 { | |
3773 boost::movelib::unique_ptr<DicomInstance> result(new DicomInstance(instance)); | |
3774 result->toFree_ = true; | |
3775 return result.release(); | |
3776 } | |
3777 } | |
3778 #endif | |
3779 | |
3780 | |
405 | 3781 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 |
3782 static std::vector<std::string> WebDavConvertPath(uint32_t pathSize, | |
3783 const char* const* pathItems) | |
3784 { | |
3785 std::vector<std::string> result(pathSize); | |
3786 | |
3787 for (uint32_t i = 0; i < pathSize; i++) | |
3788 { | |
3789 result[i] = pathItems[i]; | |
3790 } | |
3791 | |
3792 return result; | |
3793 } | |
3794 #endif | |
3795 | |
3796 | |
3797 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3798 static OrthancPluginErrorCode WebDavIsExistingFolder(uint8_t* isExisting, | |
3799 uint32_t pathSize, | |
3800 const char* const* pathItems, | |
3801 void* payload) | |
3802 { | |
3803 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3804 | |
3805 try | |
3806 { | |
3807 *isExisting = (that.IsExistingFolder(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3808 return OrthancPluginErrorCode_Success; | |
3809 } | |
3810 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3811 { | |
3812 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3813 } | |
3814 catch (...) | |
3815 { | |
3816 return OrthancPluginErrorCode_Plugin; | |
3817 } | |
3818 } | |
3819 #endif | |
3820 | |
3821 | |
3822 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3823 static OrthancPluginErrorCode WebDavListFolder(uint8_t* isExisting, | |
3824 OrthancPluginWebDavCollection* collection, | |
3825 OrthancPluginWebDavAddFile addFile, | |
3826 OrthancPluginWebDavAddFolder addFolder, | |
3827 uint32_t pathSize, | |
3828 const char* const* pathItems, | |
3829 void* payload) | |
3830 { | |
3831 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3832 | |
3833 try | |
3834 { | |
3835 std::list<IWebDavCollection::FileInfo> files; | |
3836 std::list<IWebDavCollection::FolderInfo> subfolders; | |
3837 | |
3838 if (!that.ListFolder(files, subfolders, WebDavConvertPath(pathSize, pathItems))) | |
3839 { | |
3840 *isExisting = 0; | |
3841 } | |
3842 else | |
3843 { | |
3844 *isExisting = 1; | |
3845 | |
3846 for (std::list<IWebDavCollection::FileInfo>::const_iterator | |
3847 it = files.begin(); it != files.end(); ++it) | |
3848 { | |
3849 OrthancPluginErrorCode code = addFile( | |
3850 collection, it->GetName().c_str(), it->GetContentSize(), | |
3851 it->GetMimeType().c_str(), it->GetDateTime().c_str()); | |
3852 | |
3853 if (code != OrthancPluginErrorCode_Success) | |
3854 { | |
3855 return code; | |
3856 } | |
3857 } | |
3858 | |
3859 for (std::list<IWebDavCollection::FolderInfo>::const_iterator it = | |
3860 subfolders.begin(); it != subfolders.end(); ++it) | |
3861 { | |
3862 OrthancPluginErrorCode code = addFolder( | |
3863 collection, it->GetName().c_str(), it->GetDateTime().c_str()); | |
3864 | |
3865 if (code != OrthancPluginErrorCode_Success) | |
3866 { | |
3867 return code; | |
3868 } | |
3869 } | |
3870 } | |
3871 | |
3872 return OrthancPluginErrorCode_Success; | |
3873 } | |
3874 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3875 { | |
3876 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3877 } | |
3878 catch (...) | |
3879 { | |
3880 return OrthancPluginErrorCode_Plugin; | |
3881 } | |
3882 } | |
3883 #endif | |
3884 | |
3885 | |
3886 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3887 static OrthancPluginErrorCode WebDavRetrieveFile(OrthancPluginWebDavCollection* collection, | |
3888 OrthancPluginWebDavRetrieveFile retrieveFile, | |
3889 uint32_t pathSize, | |
3890 const char* const* pathItems, | |
3891 void* payload) | |
3892 { | |
3893 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3894 | |
3895 try | |
3896 { | |
3897 std::string content, mime, dateTime; | |
3898 | |
3899 if (that.GetFile(content, mime, dateTime, WebDavConvertPath(pathSize, pathItems))) | |
3900 { | |
3901 return retrieveFile(collection, content.empty() ? NULL : content.c_str(), | |
3902 content.size(), mime.c_str(), dateTime.c_str()); | |
3903 } | |
3904 else | |
3905 { | |
3906 // Inexisting file | |
3907 return OrthancPluginErrorCode_Success; | |
3908 } | |
3909 } | |
3910 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3911 { | |
3912 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3913 } | |
3914 catch (...) | |
3915 { | |
3916 return OrthancPluginErrorCode_InternalError; | |
3917 } | |
3918 } | |
3919 #endif | |
3920 | |
3921 | |
3922 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3923 static OrthancPluginErrorCode WebDavStoreFileCallback(uint8_t* isReadOnly, /* out */ | |
3924 uint32_t pathSize, | |
3925 const char* const* pathItems, | |
3926 const void* data, | |
3927 uint64_t size, | |
3928 void* payload) | |
3929 { | |
3930 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3931 | |
3932 try | |
3933 { | |
3934 if (static_cast<uint64_t>(static_cast<size_t>(size)) != size) | |
3935 { | |
3936 ORTHANC_PLUGINS_THROW_EXCEPTION(NotEnoughMemory); | |
3937 } | |
3938 | |
3939 *isReadOnly = (that.StoreFile(WebDavConvertPath(pathSize, pathItems), data, | |
3940 static_cast<size_t>(size)) ? 1 : 0); | |
3941 return OrthancPluginErrorCode_Success; | |
3942 } | |
3943 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3944 { | |
3945 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3946 } | |
3947 catch (...) | |
3948 { | |
3949 return OrthancPluginErrorCode_InternalError; | |
3950 } | |
3951 } | |
3952 #endif | |
3953 | |
3954 | |
3955 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3956 static OrthancPluginErrorCode WebDavCreateFolderCallback(uint8_t* isReadOnly, /* out */ | |
3957 uint32_t pathSize, | |
3958 const char* const* pathItems, | |
3959 void* payload) | |
3960 { | |
3961 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3962 | |
3963 try | |
3964 { | |
3965 *isReadOnly = (that.CreateFolder(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3966 return OrthancPluginErrorCode_Success; | |
3967 } | |
3968 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3969 { | |
3970 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3971 } | |
3972 catch (...) | |
3973 { | |
3974 return OrthancPluginErrorCode_InternalError; | |
3975 } | |
3976 } | |
3977 #endif | |
3978 | |
3979 | |
3980 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3981 static OrthancPluginErrorCode WebDavDeleteItemCallback(uint8_t* isReadOnly, /* out */ | |
3982 uint32_t pathSize, | |
3983 const char* const* pathItems, | |
3984 void* payload) | |
3985 { | |
3986 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3987 | |
3988 try | |
3989 { | |
3990 *isReadOnly = (that.DeleteItem(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3991 return OrthancPluginErrorCode_Success; | |
3992 } | |
3993 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3994 { | |
3995 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3996 } | |
3997 catch (...) | |
3998 { | |
3999 return OrthancPluginErrorCode_InternalError; | |
4000 } | |
4001 } | |
4002 #endif | |
4003 | |
4004 | |
4005 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
4006 void IWebDavCollection::Register(const std::string& uri, | |
4007 IWebDavCollection& collection) | |
4008 { | |
4009 OrthancPluginErrorCode code = OrthancPluginRegisterWebDavCollection( | |
4010 GetGlobalContext(), uri.c_str(), WebDavIsExistingFolder, WebDavListFolder, WebDavRetrieveFile, | |
4011 WebDavStoreFileCallback, WebDavCreateFolderCallback, WebDavDeleteItemCallback, &collection); | |
4012 | |
4013 if (code != OrthancPluginErrorCode_Success) | |
4014 { | |
4015 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
4016 } | |
4017 } | |
4018 #endif | |
4019 | |
4020 void GetHttpHeaders(std::map<std::string, std::string>& result, const OrthancPluginHttpRequest* request) | |
4021 { | |
4022 result.clear(); | |
4023 | |
4024 for (uint32_t i = 0; i < request->headersCount; ++i) | |
4025 { | |
4026 result[request->headersKeys[i]] = request->headersValues[i]; | |
4027 } | |
4028 } | |
491 | 4029 |
4030 #if !ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) | |
4031 static void SetPluginProperty(const std::string& pluginIdentifier, | |
4032 _OrthancPluginProperty property, | |
4033 const std::string& value) | |
4034 { | |
4035 _OrthancPluginSetPluginProperty params; | |
4036 params.plugin = pluginIdentifier.c_str(); | |
4037 params.property = property; | |
4038 params.value = value.c_str(); | |
4039 | |
4040 GetGlobalContext()->InvokeService(GetGlobalContext(), _OrthancPluginService_SetPluginProperty, ¶ms); | |
4041 } | |
4042 #endif | |
4043 | |
4044 void SetRootUri(const std::string& pluginIdentifier, | |
4045 const std::string& uri) | |
4046 { | |
4047 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) | |
4048 OrthancPluginSetRootUri2(GetGlobalContext(), pluginIdentifier.c_str(), uri.c_str()); | |
4049 #else | |
4050 SetPluginProperty(pluginIdentifier, _OrthancPluginProperty_RootUri, uri); | |
4051 #endif | |
4052 } | |
4053 | |
4054 void SetDescription(const std::string& pluginIdentifier, | |
4055 const std::string& description) | |
4056 { | |
4057 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) | |
4058 OrthancPluginSetDescription2(GetGlobalContext(), pluginIdentifier.c_str(), description.c_str()); | |
4059 #else | |
4060 SetPluginProperty(pluginIdentifier, _OrthancPluginProperty_Description, description); | |
4061 #endif | |
4062 } | |
4063 | |
4064 void ExtendOrthancExplorer(const std::string& pluginIdentifier, | |
4065 const std::string& javascript) | |
4066 { | |
4067 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 4) | |
4068 OrthancPluginExtendOrthancExplorer2(GetGlobalContext(), pluginIdentifier.c_str(), javascript.c_str()); | |
4069 #else | |
4070 SetPluginProperty(pluginIdentifier, _OrthancPluginProperty_OrthancExplorer, javascript); | |
4071 #endif | |
4072 } | |
152 | 4073 } |