comparison Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h @ 0:39585ba26f20

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