Mercurial > hg > orthanc-databases
annotate Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h @ 343:f0704e57476b OrthancMySQL-4.1
closing OrthancMySQL-4.1
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 16:08:48 +0200 |
parents | c7dc70a0a477 |
children | 1280b40d6696 |
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 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
152 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
160 | 11 * |
152 | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
24 #include "OrthancPluginException.h" | |
25 | |
26 #include <orthanc/OrthancCPlugin.h> | |
27 #include <boost/noncopyable.hpp> | |
28 #include <boost/lexical_cast.hpp> | |
29 #include <boost/date_time/posix_time/posix_time.hpp> | |
30 #include <json/value.h> | |
31 #include <vector> | |
32 #include <list> | |
33 #include <set> | |
34 #include <map> | |
35 | |
36 | |
37 | |
38 /** | |
39 * The definition of ORTHANC_PLUGINS_VERSION_IS_ABOVE below is for | |
40 * backward compatibility with Orthanc SDK <= 1.3.0. | |
41 * | |
42 * $ hg diff -r Orthanc-1.3.0:Orthanc-1.3.1 ../../../Plugins/Include/orthanc/OrthancCPlugin.h | |
43 * | |
44 **/ | |
45 #if !defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) | |
46 #define ORTHANC_PLUGINS_VERSION_IS_ABOVE(major, minor, revision) \ | |
47 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER > major || \ | |
48 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == major && \ | |
49 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER > minor || \ | |
50 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER == minor && \ | |
51 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER >= revision)))) | |
52 #endif | |
53 | |
54 | |
55 #if !defined(ORTHANC_FRAMEWORK_VERSION_IS_ABOVE) | |
56 #define ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(major, minor, revision) \ | |
57 (ORTHANC_VERSION_MAJOR > major || \ | |
58 (ORTHANC_VERSION_MAJOR == major && \ | |
59 (ORTHANC_VERSION_MINOR > minor || \ | |
60 (ORTHANC_VERSION_MINOR == minor && \ | |
61 ORTHANC_VERSION_REVISION >= revision)))) | |
62 #endif | |
63 | |
64 | |
65 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) | |
66 // The "OrthancPluginFindMatcher()" primitive was introduced in Orthanc 1.2.0 | |
67 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 1 | |
68 #else | |
69 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 0 | |
70 #endif | |
71 | |
72 | |
73 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 4, 2) | |
74 # define HAS_ORTHANC_PLUGIN_PEERS 1 | |
75 # define HAS_ORTHANC_PLUGIN_JOB 1 | |
76 #else | |
77 # define HAS_ORTHANC_PLUGIN_PEERS 0 | |
78 # define HAS_ORTHANC_PLUGIN_JOB 0 | |
79 #endif | |
80 | |
81 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) | |
82 # define HAS_ORTHANC_PLUGIN_EXCEPTION_DETAILS 1 | |
83 #else | |
84 # define HAS_ORTHANC_PLUGIN_EXCEPTION_DETAILS 0 | |
85 #endif | |
86 | |
87 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 4) | |
88 # define HAS_ORTHANC_PLUGIN_METRICS 1 | |
89 #else | |
90 # define HAS_ORTHANC_PLUGIN_METRICS 0 | |
91 #endif | |
92 | |
93 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 1, 0) | |
94 # define HAS_ORTHANC_PLUGIN_HTTP_CLIENT 1 | |
95 #else | |
96 # define HAS_ORTHANC_PLUGIN_HTTP_CLIENT 0 | |
97 #endif | |
98 | |
99 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 7) | |
100 # define HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT 1 | |
101 #else | |
102 # define HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT 0 | |
103 #endif | |
104 | |
105 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 7) | |
106 # define HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER 1 | |
107 #else | |
108 # define HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER 0 | |
109 #endif | |
110 | |
111 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 0) | |
112 # define HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP 1 | |
113 #else | |
114 # define HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP 0 | |
115 #endif | |
116 | |
117 | |
118 | |
119 namespace OrthancPlugins | |
120 { | |
121 typedef void (*RestCallback) (OrthancPluginRestOutput* output, | |
122 const char* url, | |
123 const OrthancPluginHttpRequest* request); | |
124 | |
125 void SetGlobalContext(OrthancPluginContext* context); | |
126 | |
127 bool HasGlobalContext(); | |
128 | |
129 OrthancPluginContext* GetGlobalContext(); | |
130 | |
131 | |
132 class OrthancImage; | |
133 | |
134 | |
135 class MemoryBuffer : public boost::noncopyable | |
136 { | |
137 private: | |
138 OrthancPluginMemoryBuffer buffer_; | |
139 | |
140 void Check(OrthancPluginErrorCode code); | |
141 | |
142 bool CheckHttp(OrthancPluginErrorCode code); | |
143 | |
144 public: | |
145 MemoryBuffer(); | |
146 | |
147 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
148 // This constructor makes a copy of the given buffer in the memory | |
149 // handled by the Orthanc core | |
150 MemoryBuffer(const void* buffer, | |
151 size_t size); | |
152 #endif | |
153 | |
154 ~MemoryBuffer() | |
155 { | |
156 Clear(); | |
157 } | |
158 | |
159 OrthancPluginMemoryBuffer* operator*() | |
160 { | |
161 return &buffer_; | |
162 } | |
163 | |
164 // This transfers ownership from "other" to "this" | |
165 void Assign(OrthancPluginMemoryBuffer& other); | |
166 | |
167 void Swap(MemoryBuffer& other); | |
168 | |
169 OrthancPluginMemoryBuffer Release(); | |
170 | |
171 const char* GetData() const | |
172 { | |
173 if (buffer_.size > 0) | |
174 { | |
175 return reinterpret_cast<const char*>(buffer_.data); | |
176 } | |
177 else | |
178 { | |
179 return NULL; | |
180 } | |
181 } | |
182 | |
183 size_t GetSize() const | |
184 { | |
185 return buffer_.size; | |
186 } | |
187 | |
188 bool IsEmpty() const | |
189 { | |
190 return GetSize() == 0 || GetData() == NULL; | |
191 } | |
192 | |
193 void Clear(); | |
194 | |
195 void ToString(std::string& target) const; | |
196 | |
197 void ToJson(Json::Value& target) const; | |
198 | |
199 bool RestApiGet(const std::string& uri, | |
200 bool applyPlugins); | |
201 | |
202 bool RestApiGet(const std::string& uri, | |
203 const std::map<std::string, std::string>& httpHeaders, | |
204 bool applyPlugins); | |
205 | |
206 bool RestApiPost(const std::string& uri, | |
207 const void* body, | |
208 size_t bodySize, | |
209 bool applyPlugins); | |
210 | |
211 bool RestApiPut(const std::string& uri, | |
212 const void* body, | |
213 size_t bodySize, | |
214 bool applyPlugins); | |
215 | |
216 bool RestApiPost(const std::string& uri, | |
217 const Json::Value& body, | |
218 bool applyPlugins); | |
219 | |
220 bool RestApiPut(const std::string& uri, | |
221 const Json::Value& body, | |
222 bool applyPlugins); | |
223 | |
224 bool RestApiPost(const std::string& uri, | |
225 const std::string& body, | |
226 bool applyPlugins) | |
227 { | |
228 return RestApiPost(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins); | |
229 } | |
230 | |
231 bool RestApiPut(const std::string& uri, | |
232 const std::string& body, | |
233 bool applyPlugins) | |
234 { | |
235 return RestApiPut(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins); | |
236 } | |
237 | |
238 void CreateDicom(const Json::Value& tags, | |
239 OrthancPluginCreateDicomFlags flags); | |
240 | |
241 void CreateDicom(const Json::Value& tags, | |
242 const OrthancImage& pixelData, | |
243 OrthancPluginCreateDicomFlags flags); | |
244 | |
245 void ReadFile(const std::string& path); | |
246 | |
247 void GetDicomQuery(const OrthancPluginWorklistQuery* query); | |
248 | |
249 void DicomToJson(Json::Value& target, | |
250 OrthancPluginDicomToJsonFormat format, | |
251 OrthancPluginDicomToJsonFlags flags, | |
252 uint32_t maxStringLength); | |
253 | |
254 bool HttpGet(const std::string& url, | |
255 const std::string& username, | |
256 const std::string& password); | |
257 | |
258 bool HttpPost(const std::string& url, | |
259 const std::string& body, | |
260 const std::string& username, | |
261 const std::string& password); | |
262 | |
263 bool HttpPut(const std::string& url, | |
264 const std::string& body, | |
265 const std::string& username, | |
266 const std::string& password); | |
267 | |
268 void GetDicomInstance(const std::string& instanceId); | |
269 }; | |
270 | |
271 | |
272 class OrthancString : public boost::noncopyable | |
273 { | |
274 private: | |
275 char* str_; | |
276 | |
277 void Clear(); | |
278 | |
279 public: | |
280 OrthancString() : | |
281 str_(NULL) | |
282 { | |
283 } | |
284 | |
285 ~OrthancString() | |
286 { | |
287 Clear(); | |
288 } | |
289 | |
290 // This transfers ownership, warning: The string must have been | |
291 // allocated by the Orthanc core | |
292 void Assign(char* str); | |
293 | |
294 const char* GetContent() const | |
295 { | |
296 return str_; | |
297 } | |
298 | |
299 void ToString(std::string& target) const; | |
300 | |
301 void ToJson(Json::Value& target) const; | |
302 }; | |
303 | |
304 | |
305 class OrthancConfiguration : public boost::noncopyable | |
306 { | |
307 private: | |
308 Json::Value configuration_; // Necessarily a Json::objectValue | |
309 std::string path_; | |
310 | |
311 std::string GetPath(const std::string& key) const; | |
312 | |
313 void LoadConfiguration(); | |
314 | |
315 public: | |
316 OrthancConfiguration(); | |
317 | |
318 explicit OrthancConfiguration(bool load); | |
319 | |
320 const Json::Value& GetJson() const | |
321 { | |
322 return configuration_; | |
323 } | |
324 | |
325 bool IsSection(const std::string& key) const; | |
326 | |
327 void GetSection(OrthancConfiguration& target, | |
328 const std::string& key) const; | |
329 | |
330 bool LookupStringValue(std::string& target, | |
331 const std::string& key) const; | |
332 | |
333 bool LookupIntegerValue(int& target, | |
334 const std::string& key) const; | |
335 | |
336 bool LookupUnsignedIntegerValue(unsigned int& target, | |
337 const std::string& key) const; | |
338 | |
339 bool LookupBooleanValue(bool& target, | |
340 const std::string& key) const; | |
341 | |
342 bool LookupFloatValue(float& target, | |
343 const std::string& key) const; | |
344 | |
345 bool LookupListOfStrings(std::list<std::string>& target, | |
346 const std::string& key, | |
347 bool allowSingleString) const; | |
348 | |
349 bool LookupSetOfStrings(std::set<std::string>& target, | |
350 const std::string& key, | |
351 bool allowSingleString) const; | |
352 | |
353 std::string GetStringValue(const std::string& key, | |
354 const std::string& defaultValue) const; | |
355 | |
356 int GetIntegerValue(const std::string& key, | |
357 int defaultValue) const; | |
358 | |
359 unsigned int GetUnsignedIntegerValue(const std::string& key, | |
360 unsigned int defaultValue) const; | |
361 | |
362 bool GetBooleanValue(const std::string& key, | |
363 bool defaultValue) const; | |
364 | |
365 float GetFloatValue(const std::string& key, | |
366 float defaultValue) const; | |
367 | |
368 void GetDictionary(std::map<std::string, std::string>& target, | |
369 const std::string& key) const; | |
370 }; | |
371 | |
372 class OrthancImage : public boost::noncopyable | |
373 { | |
374 private: | |
375 OrthancPluginImage* image_; | |
376 | |
377 void Clear(); | |
378 | |
379 void CheckImageAvailable() const; | |
380 | |
381 public: | |
382 OrthancImage(); | |
383 | |
384 explicit OrthancImage(OrthancPluginImage* image); | |
385 | |
386 OrthancImage(OrthancPluginPixelFormat format, | |
387 uint32_t width, | |
388 uint32_t height); | |
389 | |
390 OrthancImage(OrthancPluginPixelFormat format, | |
391 uint32_t width, | |
392 uint32_t height, | |
393 uint32_t pitch, | |
394 void* buffer); | |
395 | |
396 ~OrthancImage() | |
397 { | |
398 Clear(); | |
399 } | |
400 | |
401 void UncompressPngImage(const void* data, | |
402 size_t size); | |
403 | |
404 void UncompressJpegImage(const void* data, | |
405 size_t size); | |
406 | |
407 void DecodeDicomImage(const void* data, | |
408 size_t size, | |
409 unsigned int frame); | |
410 | |
411 OrthancPluginPixelFormat GetPixelFormat() const; | |
412 | |
413 unsigned int GetWidth() const; | |
414 | |
415 unsigned int GetHeight() const; | |
416 | |
417 unsigned int GetPitch() const; | |
418 | |
419 void* GetBuffer() const; | |
420 | |
421 const OrthancPluginImage* GetObject() const | |
422 { | |
423 return image_; | |
424 } | |
425 | |
426 void CompressPngImage(MemoryBuffer& target) const; | |
427 | |
428 void CompressJpegImage(MemoryBuffer& target, | |
429 uint8_t quality) const; | |
430 | |
431 void AnswerPngImage(OrthancPluginRestOutput* output) const; | |
432 | |
433 void AnswerJpegImage(OrthancPluginRestOutput* output, | |
434 uint8_t quality) const; | |
435 | |
436 void* GetWriteableBuffer(); | |
437 | |
438 OrthancPluginImage* Release(); | |
439 }; | |
440 | |
441 | |
442 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 | |
443 class FindMatcher : public boost::noncopyable | |
444 { | |
445 private: | |
446 OrthancPluginFindMatcher* matcher_; | |
447 const OrthancPluginWorklistQuery* worklist_; | |
448 | |
449 void SetupDicom(const void* query, | |
450 uint32_t size); | |
451 | |
452 public: | |
453 explicit FindMatcher(const OrthancPluginWorklistQuery* worklist); | |
454 | |
455 FindMatcher(const void* query, | |
456 uint32_t size) | |
457 { | |
458 SetupDicom(query, size); | |
459 } | |
460 | |
461 explicit FindMatcher(const MemoryBuffer& dicom) | |
462 { | |
463 SetupDicom(dicom.GetData(), dicom.GetSize()); | |
464 } | |
465 | |
466 ~FindMatcher(); | |
467 | |
468 bool IsMatch(const void* dicom, | |
469 uint32_t size) const; | |
470 | |
471 bool IsMatch(const MemoryBuffer& dicom) const | |
472 { | |
473 return IsMatch(dicom.GetData(), dicom.GetSize()); | |
474 } | |
475 }; | |
476 #endif | |
477 | |
478 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
479 bool ReadJson(Json::Value& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
480 const std::string& source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
481 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
482 bool ReadJson(Json::Value& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
483 const void* buffer, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
484 size_t size); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
485 |
272
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
486 bool ReadJsonWithoutComments(Json::Value& target, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
487 const std::string& source); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
488 |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
489 bool ReadJsonWithoutComments(Json::Value& target, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
490 const void* buffer, |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
491 size_t size); |
c7dc70a0a477
upgrade to Orthanc SDK 1.9.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
492 |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
493 void WriteFastJson(std::string& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
494 const Json::Value& source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
495 |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
496 void WriteStyledJson(std::string& target, |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
497 const Json::Value& source); |
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
498 |
152 | 499 bool RestApiGet(Json::Value& result, |
500 const std::string& uri, | |
501 bool applyPlugins); | |
502 | |
503 bool RestApiGetString(std::string& result, | |
504 const std::string& uri, | |
505 bool applyPlugins); | |
506 | |
507 bool RestApiGetString(std::string& result, | |
508 const std::string& uri, | |
509 const std::map<std::string, std::string>& httpHeaders, | |
510 bool applyPlugins); | |
511 | |
512 bool RestApiPost(std::string& result, | |
513 const std::string& uri, | |
514 const void* body, | |
515 size_t bodySize, | |
516 bool applyPlugins); | |
517 | |
518 bool RestApiPost(Json::Value& result, | |
519 const std::string& uri, | |
520 const void* body, | |
521 size_t bodySize, | |
522 bool applyPlugins); | |
523 | |
524 bool RestApiPost(Json::Value& result, | |
525 const std::string& uri, | |
526 const Json::Value& body, | |
527 bool applyPlugins); | |
528 | |
529 inline bool RestApiPost(Json::Value& result, | |
530 const std::string& uri, | |
531 const std::string& body, | |
532 bool applyPlugins) | |
533 { | |
534 return RestApiPost(result, uri, body.empty() ? NULL : body.c_str(), | |
535 body.size(), applyPlugins); | |
536 } | |
537 | |
538 inline bool RestApiPost(Json::Value& result, | |
539 const std::string& uri, | |
540 const MemoryBuffer& body, | |
541 bool applyPlugins) | |
542 { | |
543 return RestApiPost(result, uri, body.GetData(), | |
544 body.GetSize(), applyPlugins); | |
545 } | |
546 | |
547 bool RestApiPut(Json::Value& result, | |
548 const std::string& uri, | |
549 const void* body, | |
550 size_t bodySize, | |
551 bool applyPlugins); | |
552 | |
553 bool RestApiPut(Json::Value& result, | |
554 const std::string& uri, | |
555 const Json::Value& body, | |
556 bool applyPlugins); | |
557 | |
558 inline bool RestApiPut(Json::Value& result, | |
559 const std::string& uri, | |
560 const std::string& body, | |
561 bool applyPlugins) | |
562 { | |
563 return RestApiPut(result, uri, body.empty() ? NULL : body.c_str(), | |
564 body.size(), applyPlugins); | |
565 } | |
566 | |
567 bool RestApiDelete(const std::string& uri, | |
568 bool applyPlugins); | |
569 | |
570 bool HttpDelete(const std::string& url, | |
571 const std::string& username, | |
572 const std::string& password); | |
573 | |
574 void AnswerJson(const Json::Value& value, | |
575 OrthancPluginRestOutput* output); | |
576 | |
577 void AnswerString(const std::string& answer, | |
578 const char* mimeType, | |
579 OrthancPluginRestOutput* output); | |
580 | |
581 void AnswerHttpError(uint16_t httpError, | |
582 OrthancPluginRestOutput* output); | |
583 | |
584 void AnswerMethodNotAllowed(OrthancPluginRestOutput* output, const char* allowedMethods); | |
585 | |
586 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) | |
587 const char* AutodetectMimeType(const std::string& path); | |
588 #endif | |
589 | |
590 void LogError(const std::string& message); | |
591 | |
592 void LogWarning(const std::string& message); | |
593 | |
594 void LogInfo(const std::string& message); | |
595 | |
596 void ReportMinimalOrthancVersion(unsigned int major, | |
597 unsigned int minor, | |
598 unsigned int revision); | |
599 | |
600 bool CheckMinimalOrthancVersion(unsigned int major, | |
601 unsigned int minor, | |
602 unsigned int revision); | |
603 | |
604 | |
605 namespace Internals | |
606 { | |
607 template <RestCallback Callback> | |
608 static OrthancPluginErrorCode Protect(OrthancPluginRestOutput* output, | |
609 const char* url, | |
610 const OrthancPluginHttpRequest* request) | |
611 { | |
612 try | |
613 { | |
614 Callback(output, url, request); | |
615 return OrthancPluginErrorCode_Success; | |
616 } | |
617 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
618 { | |
619 #if HAS_ORTHANC_EXCEPTION == 1 && HAS_ORTHANC_PLUGIN_EXCEPTION_DETAILS == 1 | |
620 if (HasGlobalContext() && | |
621 e.HasDetails()) | |
622 { | |
623 // The "false" instructs Orthanc not to log the detailed | |
624 // error message. This is to avoid duplicating the details, | |
625 // because "OrthancException" already does it on construction. | |
626 OrthancPluginSetHttpErrorDetails | |
627 (GetGlobalContext(), output, e.GetDetails(), false); | |
628 } | |
629 #endif | |
630 | |
631 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
632 } | |
633 catch (boost::bad_lexical_cast&) | |
634 { | |
635 return OrthancPluginErrorCode_BadFileFormat; | |
636 } | |
637 catch (...) | |
638 { | |
639 return OrthancPluginErrorCode_Plugin; | |
640 } | |
641 } | |
642 } | |
643 | |
644 | |
645 template <RestCallback Callback> | |
646 void RegisterRestCallback(const std::string& uri, | |
647 bool isThreadSafe) | |
648 { | |
649 if (isThreadSafe) | |
650 { | |
651 OrthancPluginRegisterRestCallbackNoLock | |
652 (GetGlobalContext(), uri.c_str(), Internals::Protect<Callback>); | |
653 } | |
654 else | |
655 { | |
656 OrthancPluginRegisterRestCallback | |
657 (GetGlobalContext(), uri.c_str(), Internals::Protect<Callback>); | |
658 } | |
659 } | |
660 | |
661 | |
662 #if HAS_ORTHANC_PLUGIN_PEERS == 1 | |
663 class OrthancPeers : public boost::noncopyable | |
664 { | |
665 private: | |
666 typedef std::map<std::string, uint32_t> Index; | |
667 | |
668 OrthancPluginPeers *peers_; | |
669 Index index_; | |
670 uint32_t timeout_; | |
671 | |
672 size_t GetPeerIndex(const std::string& name) const; | |
673 | |
674 public: | |
675 OrthancPeers(); | |
676 | |
677 ~OrthancPeers(); | |
678 | |
679 uint32_t GetTimeout() const | |
680 { | |
681 return timeout_; | |
682 } | |
683 | |
684 void SetTimeout(uint32_t timeout) | |
685 { | |
686 timeout_ = timeout; | |
687 } | |
688 | |
689 bool LookupName(size_t& target, | |
690 const std::string& name) const; | |
691 | |
692 std::string GetPeerName(size_t index) const; | |
693 | |
694 std::string GetPeerUrl(size_t index) const; | |
695 | |
696 std::string GetPeerUrl(const std::string& name) const; | |
697 | |
698 size_t GetPeersCount() const | |
699 { | |
700 return index_.size(); | |
701 } | |
702 | |
703 bool LookupUserProperty(std::string& value, | |
704 size_t index, | |
705 const std::string& key) const; | |
706 | |
707 bool LookupUserProperty(std::string& value, | |
708 const std::string& peer, | |
709 const std::string& key) const; | |
710 | |
711 bool DoGet(MemoryBuffer& target, | |
712 size_t index, | |
713 const std::string& uri) const; | |
714 | |
715 bool DoGet(MemoryBuffer& target, | |
716 const std::string& name, | |
717 const std::string& uri) const; | |
718 | |
719 bool DoGet(Json::Value& target, | |
720 size_t index, | |
721 const std::string& uri) const; | |
722 | |
723 bool DoGet(Json::Value& target, | |
724 const std::string& name, | |
725 const std::string& uri) const; | |
726 | |
727 bool DoPost(MemoryBuffer& target, | |
728 size_t index, | |
729 const std::string& uri, | |
730 const std::string& body) const; | |
731 | |
732 bool DoPost(MemoryBuffer& target, | |
733 const std::string& name, | |
734 const std::string& uri, | |
735 const std::string& body) const; | |
736 | |
737 bool DoPost(Json::Value& target, | |
738 size_t index, | |
739 const std::string& uri, | |
740 const std::string& body) const; | |
741 | |
742 bool DoPost(Json::Value& target, | |
743 const std::string& name, | |
744 const std::string& uri, | |
745 const std::string& body) const; | |
746 | |
747 bool DoPut(size_t index, | |
748 const std::string& uri, | |
749 const std::string& body) const; | |
750 | |
751 bool DoPut(const std::string& name, | |
752 const std::string& uri, | |
753 const std::string& body) const; | |
754 | |
755 bool DoDelete(size_t index, | |
756 const std::string& uri) const; | |
757 | |
758 bool DoDelete(const std::string& name, | |
759 const std::string& uri) const; | |
760 }; | |
761 #endif | |
762 | |
763 | |
764 | |
765 #if HAS_ORTHANC_PLUGIN_JOB == 1 | |
766 class OrthancJob : public boost::noncopyable | |
767 { | |
768 private: | |
769 std::string jobType_; | |
770 std::string content_; | |
771 bool hasSerialized_; | |
772 std::string serialized_; | |
773 float progress_; | |
774 | |
775 static void CallbackFinalize(void* job); | |
776 | |
777 static float CallbackGetProgress(void* job); | |
778 | |
779 static const char* CallbackGetContent(void* job); | |
780 | |
781 static const char* CallbackGetSerialized(void* job); | |
782 | |
783 static OrthancPluginJobStepStatus CallbackStep(void* job); | |
784 | |
785 static OrthancPluginErrorCode CallbackStop(void* job, | |
786 OrthancPluginJobStopReason reason); | |
787 | |
788 static OrthancPluginErrorCode CallbackReset(void* job); | |
789 | |
790 protected: | |
791 void ClearContent(); | |
792 | |
793 void UpdateContent(const Json::Value& content); | |
794 | |
795 void ClearSerialized(); | |
796 | |
797 void UpdateSerialized(const Json::Value& serialized); | |
798 | |
799 void UpdateProgress(float progress); | |
800 | |
801 public: | |
170 | 802 explicit OrthancJob(const std::string& jobType); |
152 | 803 |
804 virtual ~OrthancJob() | |
805 { | |
806 } | |
807 | |
808 virtual OrthancPluginJobStepStatus Step() = 0; | |
809 | |
810 virtual void Stop(OrthancPluginJobStopReason reason) = 0; | |
811 | |
812 virtual void Reset() = 0; | |
813 | |
814 static OrthancPluginJob* Create(OrthancJob* job /* takes ownership */); | |
815 | |
816 static std::string Submit(OrthancJob* job /* takes ownership */, | |
817 int priority); | |
818 | |
819 static void SubmitAndWait(Json::Value& result, | |
820 OrthancJob* job /* takes ownership */, | |
821 int priority); | |
822 | |
823 // Submit a job from a POST on the REST API with the same | |
824 // conventions as in the Orthanc core (according to the | |
825 // "Synchronous" and "Priority" options) | |
826 static void SubmitFromRestApiPost(OrthancPluginRestOutput* output, | |
827 const Json::Value& body, | |
828 OrthancJob* job); | |
829 }; | |
830 #endif | |
831 | |
832 | |
833 #if HAS_ORTHANC_PLUGIN_METRICS == 1 | |
834 inline void SetMetricsValue(char* name, | |
835 float value) | |
836 { | |
837 OrthancPluginSetMetricsValue(GetGlobalContext(), name, | |
838 value, OrthancPluginMetricsType_Default); | |
839 } | |
840 | |
841 class MetricsTimer : public boost::noncopyable | |
842 { | |
843 private: | |
844 std::string name_; | |
845 boost::posix_time::ptime start_; | |
846 | |
847 public: | |
848 explicit MetricsTimer(const char* name); | |
849 | |
850 ~MetricsTimer(); | |
851 }; | |
852 #endif | |
853 | |
854 | |
855 #if HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 | |
856 class HttpClient : public boost::noncopyable | |
857 { | |
858 public: | |
859 typedef std::map<std::string, std::string> HttpHeaders; | |
860 | |
861 class IRequestBody : public boost::noncopyable | |
862 { | |
863 public: | |
864 virtual ~IRequestBody() | |
865 { | |
866 } | |
867 | |
868 virtual bool ReadNextChunk(std::string& chunk) = 0; | |
869 }; | |
870 | |
871 | |
872 class IAnswer : public boost::noncopyable | |
873 { | |
874 public: | |
875 virtual ~IAnswer() | |
876 { | |
877 } | |
878 | |
879 virtual void AddHeader(const std::string& key, | |
880 const std::string& value) = 0; | |
881 | |
882 virtual void AddChunk(const void* data, | |
883 size_t size) = 0; | |
884 }; | |
885 | |
886 | |
887 private: | |
888 class RequestBodyWrapper; | |
889 | |
890 uint16_t httpStatus_; | |
891 OrthancPluginHttpMethod method_; | |
892 std::string url_; | |
893 HttpHeaders headers_; | |
894 std::string username_; | |
895 std::string password_; | |
896 uint32_t timeout_; | |
897 std::string certificateFile_; | |
898 std::string certificateKeyFile_; | |
899 std::string certificateKeyPassword_; | |
900 bool pkcs11_; | |
901 std::string fullBody_; | |
902 IRequestBody* chunkedBody_; | |
903 bool allowChunkedTransfers_; | |
904 | |
905 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_CLIENT == 1 | |
906 void ExecuteWithStream(uint16_t& httpStatus, // out | |
907 IAnswer& answer, // out | |
908 IRequestBody& body) const; | |
909 #endif | |
910 | |
911 void ExecuteWithoutStream(uint16_t& httpStatus, // out | |
912 HttpHeaders& answerHeaders, // out | |
913 std::string& answerBody, // out | |
914 const std::string& body) const; | |
915 | |
916 public: | |
917 HttpClient(); | |
918 | |
919 uint16_t GetHttpStatus() const | |
920 { | |
921 return httpStatus_; | |
922 } | |
923 | |
924 void SetMethod(OrthancPluginHttpMethod method) | |
925 { | |
926 method_ = method; | |
927 } | |
928 | |
929 const std::string& GetUrl() const | |
930 { | |
931 return url_; | |
932 } | |
933 | |
934 void SetUrl(const std::string& url) | |
935 { | |
936 url_ = url; | |
937 } | |
938 | |
939 void SetHeaders(const HttpHeaders& headers) | |
940 { | |
941 headers_ = headers; | |
942 } | |
943 | |
944 void AddHeader(const std::string& key, | |
945 const std::string& value) | |
946 { | |
947 headers_[key] = value; | |
948 } | |
949 | |
950 void AddHeaders(const HttpHeaders& headers); | |
951 | |
952 void SetCredentials(const std::string& username, | |
953 const std::string& password); | |
954 | |
955 void ClearCredentials(); | |
956 | |
957 void SetTimeout(unsigned int timeout) // 0 for default timeout | |
958 { | |
959 timeout_ = timeout; | |
960 } | |
961 | |
962 void SetCertificate(const std::string& certificateFile, | |
963 const std::string& keyFile, | |
964 const std::string& keyPassword); | |
965 | |
966 void ClearCertificate(); | |
967 | |
968 void SetPkcs11(bool pkcs11) | |
969 { | |
970 pkcs11_ = pkcs11; | |
971 } | |
972 | |
973 void ClearBody(); | |
974 | |
975 void SwapBody(std::string& body); | |
976 | |
977 void SetBody(const std::string& body); | |
978 | |
979 void SetBody(IRequestBody& body); | |
980 | |
981 // This function can be used to disable chunked transfers if the | |
982 // remote server is Orthanc with a version <= 1.5.6. | |
983 void SetChunkedTransfersAllowed(bool allow) | |
984 { | |
985 allowChunkedTransfers_ = allow; | |
986 } | |
987 | |
988 bool IsChunkedTransfersAllowed() const | |
989 { | |
990 return allowChunkedTransfers_; | |
991 } | |
992 | |
993 void Execute(IAnswer& answer); | |
994 | |
995 void Execute(HttpHeaders& answerHeaders /* out */, | |
996 std::string& answerBody /* out */); | |
997 | |
998 void Execute(HttpHeaders& answerHeaders /* out */, | |
999 Json::Value& answerBody /* out */); | |
1000 | |
1001 void Execute(); | |
1002 }; | |
1003 #endif | |
1004 | |
1005 | |
1006 | |
1007 class IChunkedRequestReader : public boost::noncopyable | |
1008 { | |
1009 public: | |
1010 virtual ~IChunkedRequestReader() | |
1011 { | |
1012 } | |
1013 | |
1014 virtual void AddChunk(const void* data, | |
1015 size_t size) = 0; | |
1016 | |
1017 virtual void Execute(OrthancPluginRestOutput* output) = 0; | |
1018 }; | |
1019 | |
1020 | |
1021 typedef IChunkedRequestReader* (*ChunkedRestCallback) (const char* url, | |
1022 const OrthancPluginHttpRequest* request); | |
1023 | |
1024 | |
1025 namespace Internals | |
1026 { | |
1027 void NullRestCallback(OrthancPluginRestOutput* output, | |
1028 const char* url, | |
1029 const OrthancPluginHttpRequest* request); | |
1030 | |
1031 IChunkedRequestReader *NullChunkedRestCallback(const char* url, | |
1032 const OrthancPluginHttpRequest* request); | |
1033 | |
1034 | |
1035 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1 | |
1036 template <ChunkedRestCallback Callback> | |
1037 static OrthancPluginErrorCode ChunkedProtect(OrthancPluginServerChunkedRequestReader** reader, | |
1038 const char* url, | |
1039 const OrthancPluginHttpRequest* request) | |
1040 { | |
1041 try | |
1042 { | |
1043 if (reader == NULL) | |
1044 { | |
1045 return OrthancPluginErrorCode_InternalError; | |
1046 } | |
1047 else | |
1048 { | |
1049 *reader = reinterpret_cast<OrthancPluginServerChunkedRequestReader*>(Callback(url, request)); | |
1050 if (*reader == NULL) | |
1051 { | |
1052 return OrthancPluginErrorCode_Plugin; | |
1053 } | |
1054 else | |
1055 { | |
1056 return OrthancPluginErrorCode_Success; | |
1057 } | |
1058 } | |
1059 } | |
1060 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
1061 { | |
1062 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
1063 } | |
1064 catch (boost::bad_lexical_cast&) | |
1065 { | |
1066 return OrthancPluginErrorCode_BadFileFormat; | |
1067 } | |
1068 catch (...) | |
1069 { | |
1070 return OrthancPluginErrorCode_Plugin; | |
1071 } | |
1072 } | |
1073 | |
1074 OrthancPluginErrorCode ChunkedRequestReaderAddChunk( | |
1075 OrthancPluginServerChunkedRequestReader* reader, | |
1076 const void* data, | |
1077 uint32_t size); | |
1078 | |
1079 OrthancPluginErrorCode ChunkedRequestReaderExecute( | |
1080 OrthancPluginServerChunkedRequestReader* reader, | |
1081 OrthancPluginRestOutput* output); | |
1082 | |
1083 void ChunkedRequestReaderFinalize( | |
1084 OrthancPluginServerChunkedRequestReader* reader); | |
1085 | |
1086 #else | |
1087 | |
1088 OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output, | |
1089 const char* url, | |
1090 const OrthancPluginHttpRequest* request, | |
1091 RestCallback GetHandler, | |
1092 ChunkedRestCallback PostHandler, | |
1093 RestCallback DeleteHandler, | |
1094 ChunkedRestCallback PutHandler); | |
1095 | |
1096 template< | |
1097 RestCallback GetHandler, | |
1098 ChunkedRestCallback PostHandler, | |
1099 RestCallback DeleteHandler, | |
1100 ChunkedRestCallback PutHandler | |
1101 > | |
1102 inline OrthancPluginErrorCode ChunkedRestCompatibility(OrthancPluginRestOutput* output, | |
1103 const char* url, | |
1104 const OrthancPluginHttpRequest* request) | |
1105 { | |
1106 return ChunkedRestCompatibility(output, url, request, GetHandler, | |
1107 PostHandler, DeleteHandler, PutHandler); | |
1108 } | |
1109 #endif | |
1110 } | |
1111 | |
1112 | |
1113 | |
1114 // NB: We use a templated class instead of a templated function, because | |
1115 // default values are only available in functions since C++11 | |
1116 template< | |
1117 RestCallback GetHandler = Internals::NullRestCallback, | |
1118 ChunkedRestCallback PostHandler = Internals::NullChunkedRestCallback, | |
1119 RestCallback DeleteHandler = Internals::NullRestCallback, | |
1120 ChunkedRestCallback PutHandler = Internals::NullChunkedRestCallback | |
1121 > | |
1122 class ChunkedRestRegistration : public boost::noncopyable | |
1123 { | |
1124 public: | |
1125 static void Apply(const std::string& uri) | |
1126 { | |
1127 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1 | |
1128 OrthancPluginRegisterChunkedRestCallback( | |
1129 GetGlobalContext(), uri.c_str(), | |
1130 GetHandler == Internals::NullRestCallback ? NULL : Internals::Protect<GetHandler>, | |
1131 PostHandler == Internals::NullChunkedRestCallback ? NULL : Internals::ChunkedProtect<PostHandler>, | |
1132 DeleteHandler == Internals::NullRestCallback ? NULL : Internals::Protect<DeleteHandler>, | |
1133 PutHandler == Internals::NullChunkedRestCallback ? NULL : Internals::ChunkedProtect<PutHandler>, | |
1134 Internals::ChunkedRequestReaderAddChunk, | |
1135 Internals::ChunkedRequestReaderExecute, | |
1136 Internals::ChunkedRequestReaderFinalize); | |
1137 #else | |
1138 OrthancPluginRegisterRestCallbackNoLock( | |
1139 GetGlobalContext(), uri.c_str(), | |
1140 Internals::ChunkedRestCompatibility<GetHandler, PostHandler, DeleteHandler, PutHandler>); | |
1141 #endif | |
1142 } | |
1143 }; | |
1144 | |
1145 | |
1146 | |
1147 #if HAS_ORTHANC_PLUGIN_STORAGE_COMMITMENT_SCP == 1 | |
1148 class IStorageCommitmentScpHandler : public boost::noncopyable | |
1149 { | |
1150 public: | |
1151 virtual ~IStorageCommitmentScpHandler() | |
1152 { | |
1153 } | |
1154 | |
1155 virtual OrthancPluginStorageCommitmentFailureReason Lookup(const std::string& sopClassUid, | |
1156 const std::string& sopInstanceUid) = 0; | |
1157 | |
1158 static OrthancPluginErrorCode Lookup(OrthancPluginStorageCommitmentFailureReason* target, | |
1159 void* rawHandler, | |
1160 const char* sopClassUid, | |
1161 const char* sopInstanceUid); | |
1162 | |
1163 static void Destructor(void* rawHandler); | |
1164 }; | |
1165 #endif | |
1166 | |
1167 | |
1168 class DicomInstance : public boost::noncopyable | |
1169 { | |
1170 private: | |
1171 bool toFree_; | |
1172 | |
1173 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
1174 const OrthancPluginDicomInstance* instance_; | |
1175 #else | |
1176 OrthancPluginDicomInstance* instance_; | |
1177 #endif | |
1178 | |
1179 public: | |
1180 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
167 | 1181 explicit DicomInstance(const OrthancPluginDicomInstance* instance); |
152 | 1182 #else |
167 | 1183 explicit DicomInstance(OrthancPluginDicomInstance* instance); |
152 | 1184 #endif |
1185 | |
1186 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1187 DicomInstance(const void* buffer, | |
1188 size_t size); | |
1189 #endif | |
1190 | |
1191 ~DicomInstance(); | |
1192 | |
1193 std::string GetRemoteAet() const; | |
1194 | |
1195 const void* GetBuffer() const | |
1196 { | |
1197 return OrthancPluginGetInstanceData(GetGlobalContext(), instance_); | |
1198 } | |
1199 | |
1200 size_t GetSize() const | |
1201 { | |
1202 return static_cast<size_t>(OrthancPluginGetInstanceSize(GetGlobalContext(), instance_)); | |
1203 } | |
1204 | |
1205 void GetJson(Json::Value& target) const; | |
1206 | |
1207 void GetSimplifiedJson(Json::Value& target) const; | |
1208 | |
1209 OrthancPluginInstanceOrigin GetOrigin() const | |
1210 { | |
1211 return OrthancPluginGetInstanceOrigin(GetGlobalContext(), instance_); | |
1212 } | |
1213 | |
1214 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
1215 std::string GetTransferSyntaxUid() const; | |
1216 #endif | |
1217 | |
1218 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 6, 1) | |
1219 bool HasPixelData() const; | |
1220 #endif | |
1221 | |
1222 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1223 unsigned int GetFramesCount() const | |
1224 { | |
1225 return OrthancPluginGetInstanceFramesCount(GetGlobalContext(), instance_); | |
1226 } | |
1227 #endif | |
1228 | |
1229 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1230 void GetRawFrame(std::string& target, | |
1231 unsigned int frameIndex) const; | |
1232 #endif | |
1233 | |
1234 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1235 OrthancImage* GetDecodedFrame(unsigned int frameIndex) const; | |
1236 #endif | |
1237 | |
1238 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1239 void Serialize(std::string& target) const; | |
1240 #endif | |
1241 | |
1242 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 7, 0) | |
1243 static DicomInstance* Transcode(const void* buffer, | |
1244 size_t size, | |
1245 const std::string& transferSyntax); | |
1246 #endif | |
1247 }; | |
1248 } |