comparison Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 166:6fada29b6759

updated copyright, as Orthanc Team now replaces Osimis
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 May 2024 22:27:45 +0200
parents c78ec45f3695
children
comparison
equal deleted inserted replaced
165:5bfe975e5c24 166:6fada29b6759
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium 5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium 7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 * 8 *
8 * This program is free software: you can redistribute it and/or 9 * 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 * 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 * published by the Free Software Foundation, either version 3 of the
59 60
60 61
61 namespace OrthancPlugins 62 namespace OrthancPlugins
62 { 63 {
63 static OrthancPluginContext* globalContext_ = NULL; 64 static OrthancPluginContext* globalContext_ = NULL;
65 static std::string pluginName_;
64 66
65 67
66 void SetGlobalContext(OrthancPluginContext* context) 68 void SetGlobalContext(OrthancPluginContext* context)
67 { 69 {
68 if (context == NULL) 70 if (context == NULL)
77 { 79 {
78 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); 80 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
79 } 81 }
80 } 82 }
81 83
84
85 void SetGlobalContext(OrthancPluginContext* context,
86 const char* pluginName)
87 {
88 SetGlobalContext(context);
89 pluginName_ = pluginName;
90 }
91
92
82 void ResetGlobalContext() 93 void ResetGlobalContext()
83 { 94 {
84 globalContext_ = NULL; 95 globalContext_ = NULL;
96 pluginName_.clear();
85 } 97 }
86 98
87 bool HasGlobalContext() 99 bool HasGlobalContext()
88 { 100 {
89 return globalContext_ != NULL; 101 return globalContext_ != NULL;
97 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); 109 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls);
98 } 110 }
99 else 111 else
100 { 112 {
101 return globalContext_; 113 return globalContext_;
114 }
115 }
116
117
118 #if HAS_ORTHANC_PLUGIN_LOG_MESSAGE == 1
119 void LogMessage(OrthancPluginLogLevel level,
120 const char* file,
121 uint32_t line,
122 const std::string& message)
123 {
124 if (HasGlobalContext())
125 {
126 #if HAS_ORTHANC_PLUGIN_LOG_MESSAGE == 1
127 const char* pluginName = (pluginName_.empty() ? NULL : pluginName_.c_str());
128 OrthancPluginLogMessage(GetGlobalContext(), message.c_str(), pluginName, file, line, OrthancPluginLogCategory_Generic, level);
129 #else
130 switch (level)
131 {
132 case OrthancPluginLogLevel_Error:
133 OrthancPluginLogError(GetGlobalContext(), message.c_str());
134 break;
135
136 case OrthancPluginLogLevel_Warning:
137 OrthancPluginLogWarning(GetGlobalContext(), message.c_str());
138 break;
139
140 case OrthancPluginLogLevel_Info:
141 OrthancPluginLogInfo(GetGlobalContext(), message.c_str());
142 break;
143
144 default:
145 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
146 }
147 #endif
148 }
149 }
150 #endif
151
152
153 void LogError(const std::string& message)
154 {
155 if (HasGlobalContext())
156 {
157 OrthancPluginLogError(GetGlobalContext(), message.c_str());
158 }
159 }
160
161 void LogWarning(const std::string& message)
162 {
163 if (HasGlobalContext())
164 {
165 OrthancPluginLogWarning(GetGlobalContext(), message.c_str());
166 }
167 }
168
169 void LogInfo(const std::string& message)
170 {
171 if (HasGlobalContext())
172 {
173 OrthancPluginLogInfo(GetGlobalContext(), message.c_str());
102 } 174 }
103 } 175 }
104 176
105 177
106 void MemoryBuffer::Check(OrthancPluginErrorCode code) 178 void MemoryBuffer::Check(OrthancPluginErrorCode code)
231 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 303 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
232 } 304 }
233 305
234 if (!ReadJson(target, buffer_.data, buffer_.size)) 306 if (!ReadJson(target, buffer_.data, buffer_.size))
235 { 307 {
236 LogError("Cannot convert some memory buffer to JSON"); 308 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
237 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 309 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
238 } 310 }
239 } 311 }
240 312
241 313
263 335
264 public: 336 public:
265 explicit PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders) 337 explicit PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders)
266 { 338 {
267 for (std::map<std::string, std::string>::const_iterator 339 for (std::map<std::string, std::string>::const_iterator
268 it = httpHeaders.begin(); it != httpHeaders.end(); ++it) 340 it = httpHeaders.begin(); it != httpHeaders.end(); ++it)
269 { 341 {
270 headersKeys_.push_back(it->first.c_str()); 342 headersKeys_.push_back(it->first.c_str());
271 headersValues_.push_back(it->second.c_str()); 343 headersValues_.push_back(it->second.c_str());
272 } 344 }
273 } 345 }
402 { 474 {
403 return true; 475 return true;
404 } 476 }
405 else 477 else
406 { 478 {
407 LogError("Cannot parse JSON: " + std::string(err)); 479 ORTHANC_PLUGINS_LOG_ERROR("Cannot parse JSON: " + std::string(err));
408 return false; 480 return false;
409 } 481 }
410 #endif 482 #endif
411 } 483 }
412 484
563 635
564 void OrthancString::ToJson(Json::Value& target) const 636 void OrthancString::ToJson(Json::Value& target) const
565 { 637 {
566 if (str_ == NULL) 638 if (str_ == NULL)
567 { 639 {
568 LogError("Cannot convert an empty memory buffer to JSON"); 640 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert an empty memory buffer to JSON");
569 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 641 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
570 } 642 }
571 643
572 if (!ReadJson(target, str_)) 644 if (!ReadJson(target, str_))
573 { 645 {
574 LogError("Cannot convert some memory buffer to JSON"); 646 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
575 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 647 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
576 } 648 }
577 } 649 }
578 650
579 651
580 void OrthancString::ToJsonWithoutComments(Json::Value& target) const 652 void OrthancString::ToJsonWithoutComments(Json::Value& target) const
581 { 653 {
582 if (str_ == NULL) 654 if (str_ == NULL)
583 { 655 {
584 LogError("Cannot convert an empty memory buffer to JSON"); 656 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert an empty memory buffer to JSON");
585 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 657 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
586 } 658 }
587 659
588 if (!ReadJsonWithoutComments(target, str_)) 660 if (!ReadJsonWithoutComments(target, str_))
589 { 661 {
590 LogError("Cannot convert some memory buffer to JSON"); 662 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert some memory buffer to JSON");
591 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 663 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
592 } 664 }
593 } 665 }
594 666
595 667
623 { 695 {
624 Clear(); 696 Clear();
625 697
626 if (body.size() > 0xffffffffu) 698 if (body.size() > 0xffffffffu)
627 { 699 {
628 LogError("Cannot handle body size > 4GB"); 700 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
629 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 701 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
630 } 702 }
631 703
632 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), 704 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(),
633 body.c_str(), body.size(), 705 body.c_str(), body.size(),
643 { 715 {
644 Clear(); 716 Clear();
645 717
646 if (body.size() > 0xffffffffu) 718 if (body.size() > 0xffffffffu)
647 { 719 {
648 LogError("Cannot handle body size > 4GB"); 720 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
649 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 721 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
650 } 722 }
651 723
652 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), 724 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(),
653 body.empty() ? NULL : body.c_str(), 725 body.empty() ? NULL : body.c_str(),
686 { 758 {
687 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); 759 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
688 } 760 }
689 } 761 }
690 762
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() 763 void OrthancConfiguration::LoadConfiguration()
720 { 764 {
721 OrthancString str; 765 OrthancString str;
722 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext())); 766 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext()));
723 767
724 if (str.GetContent() == NULL) 768 if (str.GetContent() == NULL)
725 { 769 {
726 LogError("Cannot access the Orthanc configuration"); 770 ORTHANC_PLUGINS_LOG_ERROR("Cannot access the Orthanc configuration");
727 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 771 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
728 } 772 }
729 773
730 str.ToJsonWithoutComments(configuration_); 774 str.ToJsonWithoutComments(configuration_);
731 775
732 if (configuration_.type() != Json::objectValue) 776 if (configuration_.type() != Json::objectValue)
733 { 777 {
734 LogError("Unable to read the Orthanc configuration"); 778 ORTHANC_PLUGINS_LOG_ERROR("Unable to read the Orthanc configuration");
735 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 779 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
736 } 780 }
737 } 781 }
738 782
739 783
797 } 841 }
798 else 842 else
799 { 843 {
800 if (configuration_[key].type() != Json::objectValue) 844 if (configuration_[key].type() != Json::objectValue)
801 { 845 {
802 LogError("The configuration section \"" + target.path_ + 846 ORTHANC_PLUGINS_LOG_ERROR("The configuration section \"" + target.path_ +
803 "\" is not an associative array as expected"); 847 "\" is not an associative array as expected");
804 848
805 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 849 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
806 } 850 }
807 851
808 target.configuration_ = configuration_[key]; 852 target.configuration_ = configuration_[key];
820 return false; 864 return false;
821 } 865 }
822 866
823 if (configuration_[key].type() != Json::stringValue) 867 if (configuration_[key].type() != Json::stringValue)
824 { 868 {
825 LogError("The configuration option \"" + GetPath(key) + 869 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
826 "\" is not a string as expected"); 870 "\" is not a string as expected");
827 871
828 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 872 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
829 } 873 }
830 874
831 target = configuration_[key].asString(); 875 target = configuration_[key].asString();
852 case Json::uintValue: 896 case Json::uintValue:
853 target = configuration_[key].asUInt(); 897 target = configuration_[key].asUInt();
854 return true; 898 return true;
855 899
856 default: 900 default:
857 LogError("The configuration option \"" + GetPath(key) + 901 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
858 "\" is not an integer as expected"); 902 "\" is not an integer as expected");
859 903
860 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 904 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
861 } 905 }
862 } 906 }
863 907
871 return false; 915 return false;
872 } 916 }
873 917
874 if (tmp < 0) 918 if (tmp < 0)
875 { 919 {
876 LogError("The configuration option \"" + GetPath(key) + 920 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
877 "\" is not a positive integer as expected"); 921 "\" is not a positive integer as expected");
878 922
879 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 923 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
880 } 924 }
881 else 925 else
882 { 926 {
896 return false; 940 return false;
897 } 941 }
898 942
899 if (configuration_[key].type() != Json::booleanValue) 943 if (configuration_[key].type() != Json::booleanValue)
900 { 944 {
901 LogError("The configuration option \"" + GetPath(key) + 945 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
902 "\" is not a Boolean as expected"); 946 "\" is not a Boolean as expected");
903 947
904 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 948 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
905 } 949 }
906 950
907 target = configuration_[key].asBool(); 951 target = configuration_[key].asBool();
932 case Json::uintValue: 976 case Json::uintValue:
933 target = static_cast<float>(configuration_[key].asUInt()); 977 target = static_cast<float>(configuration_[key].asUInt());
934 return true; 978 return true;
935 979
936 default: 980 default:
937 LogError("The configuration option \"" + GetPath(key) + 981 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
938 "\" is not an integer as expected"); 982 "\" is not an integer as expected");
939 983
940 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 984 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
941 } 985 }
942 } 986 }
943 987
992 1036
993 default: 1037 default:
994 break; 1038 break;
995 } 1039 }
996 1040
997 LogError("The configuration option \"" + GetPath(key) + 1041 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
998 "\" is not a list of strings as expected"); 1042 "\" is not a list of strings as expected");
999 1043
1000 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1044 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1001 } 1045 }
1002 1046
1003 1047
1113 return; 1157 return;
1114 } 1158 }
1115 1159
1116 if (configuration_[key].type() != Json::objectValue) 1160 if (configuration_[key].type() != Json::objectValue)
1117 { 1161 {
1118 LogError("The configuration option \"" + GetPath(key) + 1162 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
1119 "\" is not an object as expected"); 1163 "\" is not an object as expected");
1120 1164
1121 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1165 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1122 } 1166 }
1123 1167
1124 Json::Value::Members members = configuration_[key].getMemberNames(); 1168 Json::Value::Members members = configuration_[key].getMemberNames();
1131 { 1175 {
1132 target[members[i]] = value.asString(); 1176 target[members[i]] = value.asString();
1133 } 1177 }
1134 else 1178 else
1135 { 1179 {
1136 LogError("The configuration option \"" + GetPath(key) + 1180 ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
1137 "\" is not a dictionary mapping strings to strings"); 1181 "\" is not a dictionary mapping strings to strings");
1138 1182
1139 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 1183 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
1140 } 1184 }
1141 } 1185 }
1142 } 1186 }
1154 1198
1155 void OrthancImage::CheckImageAvailable() const 1199 void OrthancImage::CheckImageAvailable() const
1156 { 1200 {
1157 if (image_ == NULL) 1201 if (image_ == NULL)
1158 { 1202 {
1159 LogError("Trying to access a NULL image"); 1203 ORTHANC_PLUGINS_LOG_ERROR("Trying to access a NULL image");
1160 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1204 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1161 } 1205 }
1162 } 1206 }
1163 1207
1164 1208
1180 { 1224 {
1181 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height); 1225 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height);
1182 1226
1183 if (image_ == NULL) 1227 if (image_ == NULL)
1184 { 1228 {
1185 LogError("Cannot create an image"); 1229 ORTHANC_PLUGINS_LOG_ERROR("Cannot create an image");
1186 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1230 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1187 } 1231 }
1188 } 1232 }
1189 1233
1190 1234
1197 image_ = OrthancPluginCreateImageAccessor 1241 image_ = OrthancPluginCreateImageAccessor
1198 (GetGlobalContext(), format, width, height, pitch, buffer); 1242 (GetGlobalContext(), format, width, height, pitch, buffer);
1199 1243
1200 if (image_ == NULL) 1244 if (image_ == NULL)
1201 { 1245 {
1202 LogError("Cannot create an image accessor"); 1246 ORTHANC_PLUGINS_LOG_ERROR("Cannot create an image accessor");
1203 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 1247 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
1204 } 1248 }
1205 } 1249 }
1206 1250
1207 void OrthancImage::UncompressPngImage(const void* data, 1251 void OrthancImage::UncompressPngImage(const void* data,
1211 1255
1212 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png); 1256 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png);
1213 1257
1214 if (image_ == NULL) 1258 if (image_ == NULL)
1215 { 1259 {
1216 LogError("Cannot uncompress a PNG image"); 1260 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a PNG image");
1217 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1261 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1218 } 1262 }
1219 } 1263 }
1220 1264
1221 1265
1224 { 1268 {
1225 Clear(); 1269 Clear();
1226 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg); 1270 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg);
1227 if (image_ == NULL) 1271 if (image_ == NULL)
1228 { 1272 {
1229 LogError("Cannot uncompress a JPEG image"); 1273 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a JPEG image");
1230 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1274 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1231 } 1275 }
1232 } 1276 }
1233 1277
1234 1278
1238 { 1282 {
1239 Clear(); 1283 Clear();
1240 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame); 1284 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame);
1241 if (image_ == NULL) 1285 if (image_ == NULL)
1242 { 1286 {
1243 LogError("Cannot uncompress a DICOM image"); 1287 ORTHANC_PLUGINS_LOG_ERROR("Cannot uncompress a DICOM image");
1244 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 1288 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
1245 } 1289 }
1246 } 1290 }
1247 1291
1248 1292
1652 1696
1653 void ReportMinimalOrthancVersion(unsigned int major, 1697 void ReportMinimalOrthancVersion(unsigned int major,
1654 unsigned int minor, 1698 unsigned int minor,
1655 unsigned int revision) 1699 unsigned int revision)
1656 { 1700 {
1657 LogError("Your version of the Orthanc core (" + 1701 ORTHANC_PLUGINS_LOG_ERROR("Your version of the Orthanc core (" +
1658 std::string(GetGlobalContext()->orthancVersion) + 1702 std::string(GetGlobalContext()->orthancVersion) +
1659 ") is too old to run this plugin (version " + 1703 ") is too old to run this plugin (version " +
1660 boost::lexical_cast<std::string>(major) + "." + 1704 boost::lexical_cast<std::string>(major) + "." +
1661 boost::lexical_cast<std::string>(minor) + "." + 1705 boost::lexical_cast<std::string>(minor) + "." +
1662 boost::lexical_cast<std::string>(revision) + 1706 boost::lexical_cast<std::string>(revision) +
1663 " is required)"); 1707 " is required)");
1664 } 1708 }
1665 1709
1666 bool CheckMinimalVersion(const char* version, 1710 bool CheckMinimalVersion(const char* version,
1667 unsigned int major, 1711 unsigned int major,
1668 unsigned int minor, 1712 unsigned int minor,
1682 1726
1683 // Parse the version 1727 // Parse the version
1684 int aa, bb, cc = 0; 1728 int aa, bb, cc = 0;
1685 if ((ORTHANC_SCANF(version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 && 1729 if ((ORTHANC_SCANF(version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 &&
1686 ORTHANC_SCANF(version, "%4d.%4d", &aa, &bb) != 2) || 1730 ORTHANC_SCANF(version, "%4d.%4d", &aa, &bb) != 2) ||
1687 aa < 0 || 1731 aa < 0 ||
1688 bb < 0 || 1732 bb < 0 ||
1689 cc < 0) 1733 cc < 0)
1690 { 1734 {
1691 return false; 1735 return false;
1692 } 1736 }
1693 1737
1694 unsigned int a = static_cast<unsigned int>(aa); 1738 unsigned int a = static_cast<unsigned int>(aa);
1738 unsigned int minor, 1782 unsigned int minor,
1739 unsigned int revision) 1783 unsigned int revision)
1740 { 1784 {
1741 if (!HasGlobalContext()) 1785 if (!HasGlobalContext())
1742 { 1786 {
1743 LogError("Bad Orthanc context in the plugin"); 1787 ORTHANC_PLUGINS_LOG_ERROR("Bad Orthanc context in the plugin");
1744 return false; 1788 return false;
1745 } 1789 }
1746 1790
1747 return CheckMinimalVersion(GetGlobalContext()->orthancVersion, 1791 return CheckMinimalVersion(GetGlobalContext()->orthancVersion,
1748 major, minor, revision); 1792 major, minor, revision);
1775 { 1819 {
1776 return index; 1820 return index;
1777 } 1821 }
1778 else 1822 else
1779 { 1823 {
1780 LogError("Inexistent peer: " + name); 1824 ORTHANC_PLUGINS_LOG_ERROR("Inexistent peer: " + name);
1781 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource); 1825 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource);
1782 } 1826 }
1783 } 1827 }
1784 1828
1785 1829
2059 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 2103 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
2060 } 2104 }
2061 2105
2062 if (body.size() > 0xffffffffu) 2106 if (body.size() > 0xffffffffu)
2063 { 2107 {
2064 LogError("Cannot handle body size > 4GB"); 2108 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
2065 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 2109 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
2066 } 2110 }
2067 2111
2068 OrthancPlugins::MemoryBuffer answer; 2112 OrthancPlugins::MemoryBuffer answer;
2069 uint16_t status; 2113 uint16_t status;
2096 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); 2140 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange);
2097 } 2141 }
2098 2142
2099 if (body.size() > 0xffffffffu) 2143 if (body.size() > 0xffffffffu)
2100 { 2144 {
2101 LogError("Cannot handle body size > 4GB"); 2145 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
2102 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 2146 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
2103 } 2147 }
2104 2148
2105 OrthancPlugins::MemoryBuffer answer; 2149 OrthancPlugins::MemoryBuffer answer;
2106 uint16_t status; 2150 uint16_t status;
2462 2506
2463 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); 2507 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority);
2464 2508
2465 if (id == NULL) 2509 if (id == NULL)
2466 { 2510 {
2467 LogError("Plugin cannot submit job"); 2511 ORTHANC_PLUGINS_LOG_ERROR("Plugin cannot submit job");
2468 OrthancPluginFreeJob(GetGlobalContext(), orthanc); 2512 OrthancPluginFreeJob(GetGlobalContext(), orthanc);
2469 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); 2513 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin);
2470 } 2514 }
2471 else 2515 else
2472 { 2516 {
2531 { 2575 {
2532 #if HAS_ORTHANC_EXCEPTION == 1 2576 #if HAS_ORTHANC_EXCEPTION == 1
2533 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()), 2577 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()),
2534 status["ErrorDescription"].asString()); 2578 status["ErrorDescription"].asString());
2535 #else 2579 #else
2536 LogError("Exception while executing the job: " + status["ErrorDescription"].asString()); 2580 ORTHANC_PLUGINS_LOG_ERROR("Exception while executing the job: " + status["ErrorDescription"].asString());
2537 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); 2581 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt());
2538 #endif 2582 #endif
2539 } 2583 }
2540 } 2584 }
2541 } 2585 }
2556 { 2600 {
2557 #if HAS_ORTHANC_EXCEPTION == 1 2601 #if HAS_ORTHANC_EXCEPTION == 1
2558 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2602 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2559 "Expected a JSON object in the body"); 2603 "Expected a JSON object in the body");
2560 #else 2604 #else
2561 LogError("Expected a JSON object in the body"); 2605 ORTHANC_PLUGINS_LOG_ERROR("Expected a JSON object in the body");
2562 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2606 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2563 #endif 2607 #endif
2564 } 2608 }
2565 2609
2566 bool synchronous = true; 2610 bool synchronous = true;
2572 #if HAS_ORTHANC_EXCEPTION == 1 2616 #if HAS_ORTHANC_EXCEPTION == 1
2573 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2617 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2574 "Option \"" + std::string(KEY_SYNCHRONOUS) + 2618 "Option \"" + std::string(KEY_SYNCHRONOUS) +
2575 "\" must be Boolean"); 2619 "\" must be Boolean");
2576 #else 2620 #else
2577 LogError("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean"); 2621 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_SYNCHRONOUS) + "\" must be Boolean");
2578 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2622 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2579 #endif 2623 #endif
2580 } 2624 }
2581 else 2625 else
2582 { 2626 {
2591 #if HAS_ORTHANC_EXCEPTION == 1 2635 #if HAS_ORTHANC_EXCEPTION == 1
2592 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2636 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2593 "Option \"" + std::string(KEY_ASYNCHRONOUS) + 2637 "Option \"" + std::string(KEY_ASYNCHRONOUS) +
2594 "\" must be Boolean"); 2638 "\" must be Boolean");
2595 #else 2639 #else
2596 LogError("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean"); 2640 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_ASYNCHRONOUS) + "\" must be Boolean");
2597 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2641 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2598 #endif 2642 #endif
2599 } 2643 }
2600 else 2644 else
2601 { 2645 {
2612 #if HAS_ORTHANC_EXCEPTION == 1 2656 #if HAS_ORTHANC_EXCEPTION == 1
2613 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 2657 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
2614 "Option \"" + std::string(KEY_PRIORITY) + 2658 "Option \"" + std::string(KEY_PRIORITY) +
2615 "\" must be an integer"); 2659 "\" must be an integer");
2616 #else 2660 #else
2617 LogError("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer"); 2661 ORTHANC_PLUGINS_LOG_ERROR("Option \"" + std::string(KEY_PRIORITY) + "\" must be an integer");
2618 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 2662 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
2619 #endif 2663 #endif
2620 } 2664 }
2621 else 2665 else
2622 { 2666 {
3133 3177
3134 MemoryBuffer answerBodyBuffer, answerHeadersBuffer; 3178 MemoryBuffer answerBodyBuffer, answerHeadersBuffer;
3135 3179
3136 if (body.size() > 0xffffffffu) 3180 if (body.size() > 0xffffffffu)
3137 { 3181 {
3138 LogError("Cannot handle body size > 4GB"); 3182 ORTHANC_PLUGINS_LOG_ERROR("Cannot handle body size > 4GB");
3139 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); 3183 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
3140 } 3184 }
3141 3185
3142 OrthancPluginErrorCode error = OrthancPluginHttpClient( 3186 OrthancPluginErrorCode error = OrthancPluginHttpClient(
3143 GetGlobalContext(), 3187 GetGlobalContext(),
3278 std::string body; 3322 std::string body;
3279 Execute(answerHeaders, body); 3323 Execute(answerHeaders, body);
3280 3324
3281 if (!ReadJson(answerBody, body)) 3325 if (!ReadJson(answerBody, body))
3282 { 3326 {
3283 LogError("Cannot convert HTTP answer body to JSON"); 3327 ORTHANC_PLUGINS_LOG_ERROR("Cannot convert HTTP answer body to JSON");
3284 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 3328 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
3285 } 3329 }
3286 } 3330 }
3287 3331
3288 3332