Mercurial > hg > orthanc
annotate Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3039:0293cb3d8a37
fix for msvc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 20 Dec 2018 09:26:09 +0100 |
parents | ff65c925f57a |
children | 4e43e67f8ecf |
rev | line source |
---|---|
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2268
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU General Public License as |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * published by the Free Software Foundation, either version 3 of the |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * License, or (at your option) any later version. |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * In addition, as a special exception, the copyright holders of this |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * program give permission to link the code of its release with the |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * that use the same license as the "OpenSSL" library), and distribute |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * the linked executables. You must obey the GNU General Public License |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * in all respects for all of the code used other than "OpenSSL". If you |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * modify file(s) with this exception, you may extend this exception to |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 * your version of the file(s), but you are not obligated to do so. If |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 * you do not wish to do so, delete this exception statement from your |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 * version. If you delete this exception statement from all source files |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 * in the program, then also delete it here. |
2850 | 23 * |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 * This program is distributed in the hope that it will be useful, but |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 * WITHOUT ANY WARRANTY; without even the implied warranty of |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 * General Public License for more details. |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 * |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 * You should have received a copy of the GNU General Public License |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 **/ |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 #include "OrthancPluginCppWrapper.h" |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 #include <json/reader.h> |
2058
43cd2ab060c7
cont OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2048
diff
changeset
|
37 #include <json/writer.h> |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 namespace OrthancPlugins |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 { |
3028 | 42 static OrthancPluginContext* globalContext_ = NULL; |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2851
diff
changeset
|
43 |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2851
diff
changeset
|
44 |
3028 | 45 void SetGlobalContext(OrthancPluginContext* context) |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2851
diff
changeset
|
46 { |
3028 | 47 if (context == NULL) |
48 { | |
49 ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer); | |
50 } | |
51 else if (globalContext_ == NULL) | |
52 { | |
53 globalContext_ = context; | |
54 } | |
55 else | |
56 { | |
57 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
58 } | |
3026 | 59 } |
60 | |
61 | |
3028 | 62 bool HasGlobalContext() |
63 { | |
64 return globalContext_ != NULL; | |
65 } | |
66 | |
67 | |
68 OrthancPluginContext* GetGlobalContext() | |
69 { | |
70 if (globalContext_ == NULL) | |
71 { | |
72 ORTHANC_PLUGINS_THROW_EXCEPTION(BadSequenceOfCalls); | |
73 } | |
74 else | |
75 { | |
76 return globalContext_; | |
77 } | |
78 } | |
2958
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2851
diff
changeset
|
79 |
bb7a66efbeb1
OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2851
diff
changeset
|
80 |
3028 | 81 void MemoryBuffer::Check(OrthancPluginErrorCode code) |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2059
diff
changeset
|
82 { |
3028 | 83 if (code != OrthancPluginErrorCode_Success) |
84 { | |
85 // Prevent using garbage information | |
86 buffer_.data = NULL; | |
87 buffer_.size = 0; | |
88 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
89 } | |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2059
diff
changeset
|
90 } |
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2059
diff
changeset
|
91 |
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2059
diff
changeset
|
92 |
3028 | 93 bool MemoryBuffer::CheckHttp(OrthancPluginErrorCode code) |
2264
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
94 { |
3028 | 95 if (code != OrthancPluginErrorCode_Success) |
96 { | |
97 // Prevent using garbage information | |
98 buffer_.data = NULL; | |
99 buffer_.size = 0; | |
100 } | |
101 | |
102 if (code == OrthancPluginErrorCode_Success) | |
103 { | |
104 return true; | |
105 } | |
106 else if (code == OrthancPluginErrorCode_UnknownResource || | |
107 code == OrthancPluginErrorCode_InexistentItem) | |
108 { | |
109 return false; | |
110 } | |
111 else | |
112 { | |
113 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
114 } | |
2264
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
115 } |
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
116 |
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
117 |
3028 | 118 MemoryBuffer::MemoryBuffer() |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 { |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 buffer_.data = NULL; |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 buffer_.size = 0; |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 |
3028 | 124 |
125 void MemoryBuffer::Clear() | |
3026 | 126 { |
3028 | 127 if (buffer_.data != NULL) |
128 { | |
129 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &buffer_); | |
130 buffer_.data = NULL; | |
131 buffer_.size = 0; | |
132 } | |
3026 | 133 } |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 |
3026 | 135 |
3028 | 136 void MemoryBuffer::Assign(OrthancPluginMemoryBuffer& other) |
137 { | |
138 Clear(); | |
139 | |
140 buffer_.data = other.data; | |
141 buffer_.size = other.size; | |
142 | |
143 other.data = NULL; | |
144 other.size = 0; | |
145 } | |
3026 | 146 |
147 | |
3028 | 148 OrthancPluginMemoryBuffer MemoryBuffer::Release() |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 { |
3028 | 150 OrthancPluginMemoryBuffer result = buffer_; |
151 | |
3026 | 152 buffer_.data = NULL; |
153 buffer_.size = 0; | |
3028 | 154 |
155 return result; | |
3026 | 156 } |
3028 | 157 |
158 | |
159 void MemoryBuffer::ToString(std::string& target) const | |
160 { | |
161 if (buffer_.size == 0) | |
162 { | |
163 target.clear(); | |
164 } | |
165 else | |
166 { | |
167 target.assign(reinterpret_cast<const char*>(buffer_.data), buffer_.size); | |
168 } | |
169 } | |
3026 | 170 |
171 | |
3028 | 172 void MemoryBuffer::ToJson(Json::Value& target) const |
173 { | |
174 if (buffer_.data == NULL || | |
175 buffer_.size == 0) | |
176 { | |
177 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
178 } | |
3026 | 179 |
3028 | 180 const char* tmp = reinterpret_cast<const char*>(buffer_.data); |
3026 | 181 |
3028 | 182 Json::Reader reader; |
183 if (!reader.parse(tmp, tmp + buffer_.size, target)) | |
184 { | |
185 LogError("Cannot convert some memory buffer to JSON"); | |
186 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
187 } | |
188 } | |
3026 | 189 |
190 | |
3028 | 191 bool MemoryBuffer::RestApiGet(const std::string& uri, |
192 bool applyPlugins) | |
3026 | 193 { |
3028 | 194 Clear(); |
3026 | 195 |
3028 | 196 if (applyPlugins) |
197 { | |
198 return CheckHttp(OrthancPluginRestApiGetAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str())); | |
199 } | |
200 else | |
201 { | |
202 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str())); | |
203 } | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
204 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
205 |
3028 | 206 bool MemoryBuffer::RestApiGet(const std::string& uri, |
207 const std::map<std::string, std::string>& httpHeaders, | |
208 bool applyPlugins) | |
3026 | 209 { |
3028 | 210 Clear(); |
211 | |
212 std::vector<const char*> headersKeys; | |
213 std::vector<const char*> headersValues; | |
3039 | 214 |
215 for (std::map<std::string, std::string>::const_iterator | |
216 it = httpHeaders.begin(); it != httpHeaders.end(); it++) | |
3028 | 217 { |
218 headersKeys.push_back(it->first.c_str()); | |
219 headersValues.push_back(it->second.c_str()); | |
220 } | |
221 | |
3039 | 222 return CheckHttp(OrthancPluginRestApiGet2( |
223 GetGlobalContext(), &buffer_, uri.c_str(), httpHeaders.size(), | |
224 (headersKeys.empty() ? NULL : &headersKeys[0]), | |
225 (headersValues.empty() ? NULL : &headersValues[0]), applyPlugins)); | |
3026 | 226 } |
3028 | 227 |
228 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
229 const char* body, | |
230 size_t bodySize, | |
231 bool applyPlugins) | |
232 { | |
233 Clear(); | |
234 | |
235 if (applyPlugins) | |
236 { | |
237 return CheckHttp(OrthancPluginRestApiPostAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize)); | |
238 } | |
239 else | |
240 { | |
241 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize)); | |
242 } | |
243 } | |
3026 | 244 |
245 | |
3028 | 246 bool MemoryBuffer::RestApiPut(const std::string& uri, |
247 const char* body, | |
248 size_t bodySize, | |
249 bool applyPlugins) | |
250 { | |
251 Clear(); | |
3026 | 252 |
3028 | 253 if (applyPlugins) |
254 { | |
255 return CheckHttp(OrthancPluginRestApiPutAfterPlugins(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize)); | |
256 } | |
257 else | |
258 { | |
259 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), body, bodySize)); | |
260 } | |
3026 | 261 } |
3028 | 262 |
3026 | 263 |
3028 | 264 bool MemoryBuffer::RestApiPost(const std::string& uri, |
265 const Json::Value& body, | |
266 bool applyPlugins) | |
3026 | 267 { |
3028 | 268 Json::FastWriter writer; |
269 return RestApiPost(uri, writer.write(body), applyPlugins); | |
3026 | 270 } |
271 | |
3028 | 272 |
273 bool MemoryBuffer::RestApiPut(const std::string& uri, | |
274 const Json::Value& body, | |
275 bool applyPlugins) | |
276 { | |
277 Json::FastWriter writer; | |
278 return RestApiPut(uri, writer.write(body), applyPlugins); | |
279 } | |
3026 | 280 |
3028 | 281 |
282 void MemoryBuffer::CreateDicom(const Json::Value& tags, | |
283 OrthancPluginCreateDicomFlags flags) | |
284 { | |
285 Clear(); | |
286 | |
287 Json::FastWriter writer; | |
288 std::string s = writer.write(tags); | |
3026 | 289 |
3028 | 290 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags)); |
3026 | 291 } |
3028 | 292 |
293 void MemoryBuffer::CreateDicom(const Json::Value& tags, | |
294 const OrthancImage& pixelData, | |
295 OrthancPluginCreateDicomFlags flags) | |
3026 | 296 { |
3028 | 297 Clear(); |
298 | |
299 Json::FastWriter writer; | |
300 std::string s = writer.write(tags); | |
301 | |
302 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags)); | |
3026 | 303 } |
3028 | 304 |
305 | |
306 void MemoryBuffer::ReadFile(const std::string& path) | |
307 { | |
308 Clear(); | |
309 Check(OrthancPluginReadFile(GetGlobalContext(), &buffer_, path.c_str())); | |
310 } | |
3026 | 311 |
312 | |
3028 | 313 void MemoryBuffer::GetDicomQuery(const OrthancPluginWorklistQuery* query) |
3026 | 314 { |
3028 | 315 Clear(); |
316 Check(OrthancPluginWorklistGetDicomQuery(GetGlobalContext(), &buffer_, query)); | |
3026 | 317 } |
318 | |
319 | |
3028 | 320 void OrthancString::Assign(char* str) |
321 { | |
322 if (str == NULL) | |
323 { | |
324 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
325 } | |
326 else | |
327 { | |
328 Clear(); | |
329 str_ = str; | |
330 } | |
331 } | |
3026 | 332 |
333 | |
3028 | 334 void OrthancString::Clear() |
335 { | |
336 if (str_ != NULL) | |
337 { | |
338 OrthancPluginFreeString(GetGlobalContext(), str_); | |
339 str_ = NULL; | |
340 } | |
341 } | |
3026 | 342 |
343 | |
3028 | 344 void OrthancString::ToString(std::string& target) const |
345 { | |
346 if (str_ == NULL) | |
347 { | |
348 target.clear(); | |
349 } | |
350 else | |
351 { | |
352 target.assign(str_); | |
353 } | |
354 } | |
3026 | 355 |
356 | |
3028 | 357 void OrthancString::ToJson(Json::Value& target) const |
358 { | |
359 if (str_ == NULL) | |
360 { | |
361 LogError("Cannot convert an empty memory buffer to JSON"); | |
362 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
363 } | |
364 | |
365 Json::Reader reader; | |
366 if (!reader.parse(str_, target)) | |
367 { | |
368 LogError("Cannot convert some memory buffer to JSON"); | |
369 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
370 } | |
371 } | |
3026 | 372 |
373 | |
3028 | 374 void MemoryBuffer::DicomToJson(Json::Value& target, |
375 OrthancPluginDicomToJsonFormat format, | |
376 OrthancPluginDicomToJsonFlags flags, | |
377 uint32_t maxStringLength) | |
378 { | |
379 OrthancString str; | |
380 str.Assign(OrthancPluginDicomBufferToJson | |
381 (GetGlobalContext(), GetData(), GetSize(), format, flags, maxStringLength)); | |
382 str.ToJson(target); | |
383 } | |
3026 | 384 |
385 | |
3028 | 386 bool MemoryBuffer::HttpGet(const std::string& url, |
387 const std::string& username, | |
388 const std::string& password) | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
389 { |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
390 Clear(); |
3028 | 391 return CheckHttp(OrthancPluginHttpGet(GetGlobalContext(), &buffer_, url.c_str(), |
392 username.empty() ? NULL : username.c_str(), | |
393 password.empty() ? NULL : password.c_str())); | |
3026 | 394 } |
395 | |
396 | |
3028 | 397 bool MemoryBuffer::HttpPost(const std::string& url, |
398 const std::string& body, | |
399 const std::string& username, | |
400 const std::string& password) | |
3026 | 401 { |
3028 | 402 Clear(); |
403 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), | |
404 body.c_str(), body.size(), | |
405 username.empty() ? NULL : username.c_str(), | |
406 password.empty() ? NULL : password.c_str())); | |
3026 | 407 } |
408 | |
409 | |
3028 | 410 bool MemoryBuffer::HttpPut(const std::string& url, |
411 const std::string& body, | |
412 const std::string& username, | |
413 const std::string& password) | |
3026 | 414 { |
3028 | 415 Clear(); |
416 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), | |
417 body.empty() ? NULL : body.c_str(), | |
418 body.size(), | |
419 username.empty() ? NULL : username.c_str(), | |
420 password.empty() ? NULL : password.c_str())); | |
3026 | 421 } |
422 | |
3028 | 423 |
424 void MemoryBuffer::GetDicomInstance(const std::string& instanceId) | |
3026 | 425 { |
3028 | 426 Clear(); |
427 Check(OrthancPluginGetDicomForInstance(GetGlobalContext(), &buffer_, instanceId.c_str())); | |
3026 | 428 } |
429 | |
430 | |
3028 | 431 bool HttpDelete(const std::string& url, |
432 const std::string& username, | |
433 const std::string& password) | |
434 { | |
435 OrthancPluginErrorCode error = OrthancPluginHttpDelete | |
436 (GetGlobalContext(), url.c_str(), | |
437 username.empty() ? NULL : username.c_str(), | |
438 password.empty() ? NULL : password.c_str()); | |
439 | |
440 if (error == OrthancPluginErrorCode_Success) | |
441 { | |
442 return true; | |
443 } | |
444 else if (error == OrthancPluginErrorCode_UnknownResource || | |
445 error == OrthancPluginErrorCode_InexistentItem) | |
446 { | |
447 return false; | |
448 } | |
449 else | |
450 { | |
451 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
452 } | |
453 } | |
3026 | 454 |
455 | |
3028 | 456 void LogError(const std::string& message) |
457 { | |
458 if (HasGlobalContext()) | |
459 { | |
460 OrthancPluginLogError(GetGlobalContext(), message.c_str()); | |
461 } | |
462 } | |
3026 | 463 |
464 | |
3028 | 465 void LogWarning(const std::string& message) |
466 { | |
467 if (HasGlobalContext()) | |
468 { | |
469 OrthancPluginLogWarning(GetGlobalContext(), message.c_str()); | |
470 } | |
471 } | |
472 | |
473 | |
474 void LogInfo(const std::string& message) | |
475 { | |
476 if (HasGlobalContext()) | |
477 { | |
478 OrthancPluginLogInfo(GetGlobalContext(), message.c_str()); | |
479 } | |
480 } | |
3026 | 481 |
482 | |
3028 | 483 OrthancConfiguration::OrthancConfiguration() |
484 { | |
485 OrthancString str; | |
486 str.Assign(OrthancPluginGetConfiguration(GetGlobalContext())); | |
3026 | 487 |
3028 | 488 if (str.GetContent() == NULL) |
489 { | |
490 LogError("Cannot access the Orthanc configuration"); | |
491 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
492 } | |
3026 | 493 |
3028 | 494 str.ToJson(configuration_); |
495 | |
496 if (configuration_.type() != Json::objectValue) | |
497 { | |
498 LogError("Unable to read the Orthanc configuration"); | |
499 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
500 } | |
501 } | |
3026 | 502 |
503 | |
3028 | 504 std::string OrthancConfiguration::GetPath(const std::string& key) const |
3026 | 505 { |
3028 | 506 if (path_.empty()) |
507 { | |
508 return key; | |
509 } | |
510 else | |
511 { | |
512 return path_ + "." + key; | |
513 } | |
3026 | 514 } |
3028 | 515 |
516 | |
517 bool OrthancConfiguration::IsSection(const std::string& key) const | |
3026 | 518 { |
3028 | 519 assert(configuration_.type() == Json::objectValue); |
520 | |
521 return (configuration_.isMember(key) && | |
522 configuration_[key].type() == Json::objectValue); | |
3026 | 523 } |
524 | |
525 | |
3028 | 526 void OrthancConfiguration::GetSection(OrthancConfiguration& target, |
527 const std::string& key) const | |
3026 | 528 { |
3028 | 529 assert(configuration_.type() == Json::objectValue); |
3026 | 530 |
3028 | 531 target.path_ = GetPath(key); |
3026 | 532 |
3028 | 533 if (!configuration_.isMember(key)) |
534 { | |
535 target.configuration_ = Json::objectValue; | |
536 } | |
537 else | |
538 { | |
539 if (configuration_[key].type() != Json::objectValue) | |
540 { | |
541 LogError("The configuration section \"" + target.path_ + | |
542 "\" is not an associative array as expected"); | |
543 | |
544 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
545 } | |
546 | |
547 target.configuration_ = configuration_[key]; | |
548 } | |
3026 | 549 } |
550 | |
551 | |
3028 | 552 bool OrthancConfiguration::LookupStringValue(std::string& target, |
553 const std::string& key) const | |
554 { | |
555 assert(configuration_.type() == Json::objectValue); | |
556 | |
557 if (!configuration_.isMember(key)) | |
558 { | |
559 return false; | |
560 } | |
3026 | 561 |
3028 | 562 if (configuration_[key].type() != Json::stringValue) |
563 { | |
564 LogError("The configuration option \"" + GetPath(key) + | |
565 "\" is not a string as expected"); | |
566 | |
567 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
568 } | |
569 | |
570 target = configuration_[key].asString(); | |
571 return true; | |
3026 | 572 } |
573 | |
574 | |
3028 | 575 bool OrthancConfiguration::LookupIntegerValue(int& target, |
576 const std::string& key) const | |
3026 | 577 { |
3028 | 578 assert(configuration_.type() == Json::objectValue); |
579 | |
580 if (!configuration_.isMember(key)) | |
581 { | |
582 return false; | |
583 } | |
584 | |
585 switch (configuration_[key].type()) | |
586 { | |
587 case Json::intValue: | |
588 target = configuration_[key].asInt(); | |
589 return true; | |
590 | |
591 case Json::uintValue: | |
592 target = configuration_[key].asUInt(); | |
593 return true; | |
594 | |
595 default: | |
596 LogError("The configuration option \"" + GetPath(key) + | |
597 "\" is not an integer as expected"); | |
598 | |
599 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
600 } | |
3026 | 601 } |
602 | |
603 | |
3028 | 604 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target, |
605 const std::string& key) const | |
606 { | |
607 int tmp; | |
608 if (!LookupIntegerValue(tmp, key)) | |
609 { | |
610 return false; | |
611 } | |
3026 | 612 |
3028 | 613 if (tmp < 0) |
614 { | |
615 LogError("The configuration option \"" + GetPath(key) + | |
616 "\" is not a positive integer as expected"); | |
617 | |
618 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
619 } | |
620 else | |
621 { | |
622 target = static_cast<unsigned int>(tmp); | |
623 return true; | |
624 } | |
625 } | |
3026 | 626 |
627 | |
3028 | 628 bool OrthancConfiguration::LookupBooleanValue(bool& target, |
629 const std::string& key) const | |
3026 | 630 { |
3028 | 631 assert(configuration_.type() == Json::objectValue); |
632 | |
633 if (!configuration_.isMember(key)) | |
3026 | 634 { |
3028 | 635 return false; |
636 } | |
637 | |
638 if (configuration_[key].type() != Json::booleanValue) | |
639 { | |
640 LogError("The configuration option \"" + GetPath(key) + | |
641 "\" is not a Boolean as expected"); | |
3026 | 642 |
643 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
644 } | |
645 | |
3028 | 646 target = configuration_[key].asBool(); |
647 return true; | |
3026 | 648 } |
3028 | 649 |
650 | |
651 bool OrthancConfiguration::LookupFloatValue(float& target, | |
652 const std::string& key) const | |
653 { | |
654 assert(configuration_.type() == Json::objectValue); | |
655 | |
656 if (!configuration_.isMember(key)) | |
657 { | |
658 return false; | |
659 } | |
660 | |
661 switch (configuration_[key].type()) | |
662 { | |
663 case Json::realValue: | |
664 target = configuration_[key].asFloat(); | |
665 return true; | |
666 | |
667 case Json::intValue: | |
668 target = static_cast<float>(configuration_[key].asInt()); | |
669 return true; | |
670 | |
671 case Json::uintValue: | |
672 target = static_cast<float>(configuration_[key].asUInt()); | |
673 return true; | |
674 | |
675 default: | |
676 LogError("The configuration option \"" + GetPath(key) + | |
677 "\" is not an integer as expected"); | |
678 | |
679 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
680 } | |
681 } | |
3026 | 682 |
683 | |
3028 | 684 bool OrthancConfiguration::LookupListOfStrings(std::list<std::string>& target, |
685 const std::string& key, | |
686 bool allowSingleString) const | |
3026 | 687 { |
3028 | 688 assert(configuration_.type() == Json::objectValue); |
689 | |
690 target.clear(); | |
691 | |
692 if (!configuration_.isMember(key)) | |
693 { | |
694 return false; | |
695 } | |
696 | |
697 switch (configuration_[key].type()) | |
698 { | |
699 case Json::arrayValue: | |
700 { | |
701 bool ok = true; | |
3026 | 702 |
3028 | 703 for (Json::Value::ArrayIndex i = 0; ok && i < configuration_[key].size(); i++) |
704 { | |
705 if (configuration_[key][i].type() == Json::stringValue) | |
706 { | |
707 target.push_back(configuration_[key][i].asString()); | |
708 } | |
709 else | |
710 { | |
711 ok = false; | |
712 } | |
713 } | |
714 | |
715 if (ok) | |
716 { | |
717 return true; | |
718 } | |
719 | |
720 break; | |
721 } | |
722 | |
723 case Json::stringValue: | |
724 if (allowSingleString) | |
725 { | |
726 target.push_back(configuration_[key].asString()); | |
727 return true; | |
728 } | |
729 | |
730 break; | |
731 | |
732 default: | |
733 break; | |
734 } | |
735 | |
3026 | 736 LogError("The configuration option \"" + GetPath(key) + |
3028 | 737 "\" is not a list of strings as expected"); |
3026 | 738 |
739 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
740 } | |
741 | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
742 |
3028 | 743 bool OrthancConfiguration::LookupSetOfStrings(std::set<std::string>& target, |
744 const std::string& key, | |
745 bool allowSingleString) const | |
746 { | |
747 std::list<std::string> lst; | |
748 | |
749 if (LookupListOfStrings(lst, key, allowSingleString)) | |
750 { | |
751 target.clear(); | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
752 |
3028 | 753 for (std::list<std::string>::const_iterator |
754 it = lst.begin(); it != lst.end(); ++it) | |
755 { | |
756 target.insert(*it); | |
757 } | |
3026 | 758 |
3028 | 759 return true; |
760 } | |
761 else | |
762 { | |
763 return false; | |
764 } | |
3026 | 765 } |
766 | |
3028 | 767 |
768 std::string OrthancConfiguration::GetStringValue(const std::string& key, | |
769 const std::string& defaultValue) const | |
3026 | 770 { |
3028 | 771 std::string tmp; |
772 if (LookupStringValue(tmp, key)) | |
773 { | |
774 return tmp; | |
775 } | |
776 else | |
777 { | |
778 return defaultValue; | |
779 } | |
3026 | 780 } |
781 | |
782 | |
3028 | 783 int OrthancConfiguration::GetIntegerValue(const std::string& key, |
784 int defaultValue) const | |
3026 | 785 { |
3028 | 786 int tmp; |
787 if (LookupIntegerValue(tmp, key)) | |
788 { | |
789 return tmp; | |
790 } | |
791 else | |
792 { | |
793 return defaultValue; | |
794 } | |
3026 | 795 } |
796 | |
3028 | 797 |
798 unsigned int OrthancConfiguration::GetUnsignedIntegerValue(const std::string& key, | |
799 unsigned int defaultValue) const | |
3026 | 800 { |
3028 | 801 unsigned int tmp; |
802 if (LookupUnsignedIntegerValue(tmp, key)) | |
803 { | |
804 return tmp; | |
805 } | |
806 else | |
807 { | |
808 return defaultValue; | |
809 } | |
3026 | 810 } |
2792
57e2d65d37ce
OrthancPlugins::MemoryBuffer::Release(), fixing openssl link
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
811 |
3026 | 812 |
3028 | 813 bool OrthancConfiguration::GetBooleanValue(const std::string& key, |
814 bool defaultValue) const | |
3026 | 815 { |
3028 | 816 bool tmp; |
817 if (LookupBooleanValue(tmp, key)) | |
818 { | |
819 return tmp; | |
820 } | |
821 else | |
822 { | |
823 return defaultValue; | |
824 } | |
3026 | 825 } |
826 | |
3028 | 827 |
828 float OrthancConfiguration::GetFloatValue(const std::string& key, | |
829 float defaultValue) const | |
3026 | 830 { |
3028 | 831 float tmp; |
832 if (LookupFloatValue(tmp, key)) | |
833 { | |
834 return tmp; | |
835 } | |
836 else | |
837 { | |
838 return defaultValue; | |
839 } | |
3026 | 840 } |
841 | |
842 | |
3028 | 843 void OrthancConfiguration::GetDictionary(std::map<std::string, std::string>& target, |
844 const std::string& key) const | |
3026 | 845 { |
3028 | 846 assert(configuration_.type() == Json::objectValue); |
847 | |
848 target.clear(); | |
849 | |
850 if (!configuration_.isMember(key)) | |
851 { | |
852 return; | |
853 } | |
3026 | 854 |
3028 | 855 if (configuration_[key].type() != Json::objectValue) |
856 { | |
857 LogError("The configuration option \"" + GetPath(key) + | |
858 "\" is not a string as expected"); | |
859 | |
860 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
861 } | |
3026 | 862 |
3028 | 863 Json::Value::Members members = configuration_[key].getMemberNames(); |
864 | |
865 for (size_t i = 0; i < members.size(); i++) | |
3026 | 866 { |
3028 | 867 const Json::Value& value = configuration_[key][members[i]]; |
868 | |
869 if (value.type() == Json::stringValue) | |
3026 | 870 { |
3028 | 871 target[members[i]] = value.asString(); |
3026 | 872 } |
873 else | |
874 { | |
3028 | 875 LogError("The configuration option \"" + GetPath(key) + |
876 "\" is not a dictionary mapping strings to strings"); | |
877 | |
878 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
3026 | 879 } |
880 } | |
3028 | 881 } |
3026 | 882 |
3028 | 883 |
884 void OrthancImage::Clear() | |
885 { | |
886 if (image_ != NULL) | |
887 { | |
888 OrthancPluginFreeImage(GetGlobalContext(), image_); | |
889 image_ = NULL; | |
890 } | |
891 } | |
892 | |
893 | |
894 void OrthancImage::CheckImageAvailable() | |
895 { | |
896 if (image_ == NULL) | |
897 { | |
898 LogError("Trying to access a NULL image"); | |
899 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
900 } | |
901 } | |
902 | |
903 | |
904 OrthancImage::OrthancImage() : | |
905 image_(NULL) | |
906 { | |
907 } | |
908 | |
909 | |
910 OrthancImage::OrthancImage(OrthancPluginImage* image) : | |
911 image_(image) | |
912 { | |
913 } | |
914 | |
915 | |
916 OrthancImage::OrthancImage(OrthancPluginPixelFormat format, | |
917 uint32_t width, | |
918 uint32_t height) | |
919 { | |
920 image_ = OrthancPluginCreateImage(GetGlobalContext(), format, width, height); | |
921 | |
922 if (image_ == NULL) | |
923 { | |
924 LogError("Cannot create an image"); | |
925 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
926 } | |
927 } | |
928 | |
929 | |
930 OrthancImage::OrthancImage(OrthancPluginPixelFormat format, | |
931 uint32_t width, | |
932 uint32_t height, | |
933 uint32_t pitch, | |
934 void* buffer) | |
935 { | |
936 image_ = OrthancPluginCreateImageAccessor | |
937 (GetGlobalContext(), format, width, height, pitch, buffer); | |
938 | |
939 if (image_ == NULL) | |
940 { | |
941 LogError("Cannot create an image accessor"); | |
942 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
943 } | |
944 } | |
945 | |
946 void OrthancImage::UncompressPngImage(const void* data, | |
947 size_t size) | |
948 { | |
949 Clear(); | |
950 | |
951 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Png); | |
952 | |
953 if (image_ == NULL) | |
954 { | |
955 LogError("Cannot uncompress a PNG image"); | |
956 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
957 } | |
958 } | |
959 | |
960 | |
961 void OrthancImage::UncompressJpegImage(const void* data, | |
962 size_t size) | |
963 { | |
964 Clear(); | |
965 image_ = OrthancPluginUncompressImage(GetGlobalContext(), data, size, OrthancPluginImageFormat_Jpeg); | |
966 if (image_ == NULL) | |
967 { | |
968 LogError("Cannot uncompress a JPEG image"); | |
969 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
970 } | |
971 } | |
972 | |
973 | |
974 void OrthancImage::DecodeDicomImage(const void* data, | |
975 size_t size, | |
976 unsigned int frame) | |
977 { | |
978 Clear(); | |
979 image_ = OrthancPluginDecodeDicomImage(GetGlobalContext(), data, size, frame); | |
980 if (image_ == NULL) | |
981 { | |
982 LogError("Cannot uncompress a DICOM image"); | |
983 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
984 } | |
985 } | |
986 | |
987 | |
988 OrthancPluginPixelFormat OrthancImage::GetPixelFormat() | |
989 { | |
990 CheckImageAvailable(); | |
991 return OrthancPluginGetImagePixelFormat(GetGlobalContext(), image_); | |
992 } | |
993 | |
994 | |
995 unsigned int OrthancImage::GetWidth() | |
996 { | |
997 CheckImageAvailable(); | |
998 return OrthancPluginGetImageWidth(GetGlobalContext(), image_); | |
999 } | |
1000 | |
1001 | |
1002 unsigned int OrthancImage::GetHeight() | |
1003 { | |
1004 CheckImageAvailable(); | |
1005 return OrthancPluginGetImageHeight(GetGlobalContext(), image_); | |
1006 } | |
1007 | |
1008 | |
1009 unsigned int OrthancImage::GetPitch() | |
1010 { | |
1011 CheckImageAvailable(); | |
1012 return OrthancPluginGetImagePitch(GetGlobalContext(), image_); | |
1013 } | |
1014 | |
1015 | |
1016 const void* OrthancImage::GetBuffer() | |
1017 { | |
1018 CheckImageAvailable(); | |
1019 return OrthancPluginGetImageBuffer(GetGlobalContext(), image_); | |
1020 } | |
1021 | |
1022 | |
1023 void OrthancImage::CompressPngImage(MemoryBuffer& target) | |
1024 { | |
1025 CheckImageAvailable(); | |
1026 | |
1027 OrthancPluginMemoryBuffer tmp; | |
1028 OrthancPluginCompressPngImage(GetGlobalContext(), &tmp, GetPixelFormat(), | |
1029 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); | |
1030 | |
1031 target.Assign(tmp); | |
1032 } | |
1033 | |
1034 | |
1035 void OrthancImage::CompressJpegImage(MemoryBuffer& target, | |
1036 uint8_t quality) | |
1037 { | |
1038 CheckImageAvailable(); | |
1039 | |
1040 OrthancPluginMemoryBuffer tmp; | |
1041 OrthancPluginCompressJpegImage(GetGlobalContext(), &tmp, GetPixelFormat(), | |
1042 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); | |
1043 | |
1044 target.Assign(tmp); | |
1045 } | |
1046 | |
1047 | |
1048 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output) | |
1049 { | |
1050 CheckImageAvailable(); | |
1051 OrthancPluginCompressAndAnswerPngImage(GetGlobalContext(), output, GetPixelFormat(), | |
1052 GetWidth(), GetHeight(), GetPitch(), GetBuffer()); | |
1053 } | |
1054 | |
1055 | |
1056 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output, | |
1057 uint8_t quality) | |
1058 { | |
1059 CheckImageAvailable(); | |
1060 OrthancPluginCompressAndAnswerJpegImage(GetGlobalContext(), output, GetPixelFormat(), | |
1061 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality); | |
1062 } | |
1063 | |
1064 | |
1065 | |
1066 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 | |
1067 FindMatcher::FindMatcher(const OrthancPluginWorklistQuery* worklist) : | |
1068 matcher_(NULL), | |
1069 worklist_(worklist) | |
1070 { | |
1071 if (worklist_ == NULL) | |
1072 { | |
1073 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
1074 } | |
1075 } | |
1076 | |
1077 | |
1078 void FindMatcher::SetupDicom(const void* query, | |
1079 uint32_t size) | |
1080 { | |
1081 worklist_ = NULL; | |
1082 | |
1083 matcher_ = OrthancPluginCreateFindMatcher(GetGlobalContext(), query, size); | |
1084 if (matcher_ == NULL) | |
1085 { | |
1086 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1087 } | |
1088 } | |
1089 | |
1090 | |
1091 FindMatcher::~FindMatcher() | |
1092 { | |
1093 // The "worklist_" field | |
1094 | |
1095 if (matcher_ != NULL) | |
1096 { | |
1097 OrthancPluginFreeFindMatcher(GetGlobalContext(), matcher_); | |
1098 } | |
1099 } | |
1100 | |
1101 | |
1102 | |
1103 bool FindMatcher::IsMatch(const void* dicom, | |
1104 uint32_t size) const | |
1105 { | |
1106 int32_t result; | |
1107 | |
1108 if (matcher_ != NULL) | |
1109 { | |
1110 result = OrthancPluginFindMatcherIsMatch(GetGlobalContext(), matcher_, dicom, size); | |
1111 } | |
1112 else if (worklist_ != NULL) | |
1113 { | |
1114 result = OrthancPluginWorklistIsMatch(GetGlobalContext(), worklist_, dicom, size); | |
1115 } | |
1116 else | |
1117 { | |
1118 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1119 } | |
1120 | |
1121 if (result == 0) | |
1122 { | |
1123 return false; | |
1124 } | |
1125 else if (result == 1) | |
1126 { | |
1127 return true; | |
1128 } | |
1129 else | |
1130 { | |
1131 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1132 } | |
1133 } | |
1134 | |
1135 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ | |
1136 | |
1137 void AnswerJson(const Json::Value& value, | |
1138 OrthancPluginRestOutput* output | |
1139 ) | |
1140 { | |
1141 Json::StyledWriter writer; | |
1142 std::string bodyString = writer.write(value); | |
1143 | |
1144 OrthancPluginAnswerBuffer(GetGlobalContext(), output, bodyString.c_str(), bodyString.size(), "application/json"); | |
1145 } | |
1146 | |
1147 void AnswerString(const std::string& answer, | |
1148 const char* mimeType, | |
1149 OrthancPluginRestOutput* output | |
1150 ) | |
1151 { | |
1152 OrthancPluginAnswerBuffer(GetGlobalContext(), output, answer.c_str(), answer.size(), mimeType); | |
1153 } | |
1154 | |
1155 void AnswerHttpError(uint16_t httpError, OrthancPluginRestOutput *output) | |
1156 { | |
1157 OrthancPluginSendHttpStatusCode(GetGlobalContext(), output, httpError); | |
1158 } | |
1159 | |
1160 void AnswerMethodNotAllowed(OrthancPluginRestOutput *output, const char* allowedMethods) | |
1161 { | |
1162 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowedMethods); | |
1163 } | |
1164 | |
1165 bool RestApiGetString(std::string& result, | |
1166 const std::string& uri, | |
1167 bool applyPlugins) | |
1168 { | |
1169 MemoryBuffer answer; | |
1170 if (!answer.RestApiGet(uri, applyPlugins)) | |
1171 { | |
1172 return false; | |
1173 } | |
1174 else | |
1175 { | |
1176 answer.ToString(result); | |
1177 return true; | |
1178 } | |
1179 } | |
1180 | |
1181 bool RestApiGetString(std::string& result, | |
1182 const std::string& uri, | |
1183 const std::map<std::string, std::string>& httpHeaders, | |
1184 bool applyPlugins) | |
1185 { | |
1186 MemoryBuffer answer; | |
1187 if (!answer.RestApiGet(uri, httpHeaders, applyPlugins)) | |
1188 { | |
1189 return false; | |
1190 } | |
1191 else | |
1192 { | |
1193 answer.ToString(result); | |
1194 return true; | |
1195 } | |
1196 } | |
1197 | |
1198 | |
1199 | |
1200 bool RestApiGet(Json::Value& result, | |
1201 const std::string& uri, | |
1202 bool applyPlugins) | |
1203 { | |
1204 MemoryBuffer answer; | |
1205 | |
1206 if (!answer.RestApiGet(uri, applyPlugins)) | |
1207 { | |
1208 return false; | |
1209 } | |
1210 else | |
1211 { | |
1212 if (!answer.IsEmpty()) | |
1213 { | |
1214 answer.ToJson(result); | |
1215 } | |
1216 return true; | |
1217 } | |
1218 } | |
1219 | |
1220 | |
1221 bool RestApiPost(Json::Value& result, | |
1222 const std::string& uri, | |
1223 const char* body, | |
1224 size_t bodySize, | |
1225 bool applyPlugins) | |
1226 { | |
1227 MemoryBuffer answer; | |
1228 | |
1229 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins)) | |
1230 { | |
1231 return false; | |
1232 } | |
1233 else | |
1234 { | |
1235 if (!answer.IsEmpty()) | |
1236 { | |
1237 answer.ToJson(result); | |
1238 } | |
1239 return true; | |
1240 } | |
1241 } | |
1242 | |
1243 | |
1244 bool RestApiPost(Json::Value& result, | |
1245 const std::string& uri, | |
1246 const Json::Value& body, | |
1247 bool applyPlugins) | |
1248 { | |
1249 Json::FastWriter writer; | |
1250 return RestApiPost(result, uri, writer.write(body), applyPlugins); | |
1251 } | |
1252 | |
1253 | |
1254 bool RestApiPut(Json::Value& result, | |
1255 const std::string& uri, | |
1256 const char* body, | |
1257 size_t bodySize, | |
1258 bool applyPlugins) | |
1259 { | |
1260 MemoryBuffer answer; | |
1261 | |
1262 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins)) | |
1263 { | |
1264 return false; | |
1265 } | |
1266 else | |
1267 { | |
1268 if (!answer.IsEmpty()) // i.e, on a PUT to metadata/..., orthand returns an empty response | |
1269 { | |
1270 answer.ToJson(result); | |
1271 } | |
1272 return true; | |
1273 } | |
1274 } | |
1275 | |
1276 | |
1277 bool RestApiPut(Json::Value& result, | |
1278 const std::string& uri, | |
1279 const Json::Value& body, | |
1280 bool applyPlugins) | |
1281 { | |
1282 Json::FastWriter writer; | |
1283 return RestApiPut(result, uri, writer.write(body), applyPlugins); | |
1284 } | |
1285 | |
1286 | |
1287 bool RestApiDelete(const std::string& uri, | |
1288 bool applyPlugins) | |
1289 { | |
1290 OrthancPluginErrorCode error; | |
1291 | |
1292 if (applyPlugins) | |
1293 { | |
1294 error = OrthancPluginRestApiDeleteAfterPlugins(GetGlobalContext(), uri.c_str()); | |
1295 } | |
1296 else | |
1297 { | |
1298 error = OrthancPluginRestApiDelete(GetGlobalContext(), uri.c_str()); | |
1299 } | |
1300 | |
1301 if (error == OrthancPluginErrorCode_Success) | |
1302 { | |
1303 return true; | |
1304 } | |
1305 else if (error == OrthancPluginErrorCode_UnknownResource || | |
1306 error == OrthancPluginErrorCode_InexistentItem) | |
1307 { | |
1308 return false; | |
1309 } | |
1310 else | |
1311 { | |
1312 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error); | |
1313 } | |
1314 } | |
1315 | |
1316 | |
1317 void ReportMinimalOrthancVersion(unsigned int major, | |
1318 unsigned int minor, | |
1319 unsigned int revision) | |
1320 { | |
1321 LogError("Your version of the Orthanc core (" + | |
1322 std::string(GetGlobalContext()->orthancVersion) + | |
1323 ") is too old to run this plugin (version " + | |
1324 boost::lexical_cast<std::string>(major) + "." + | |
1325 boost::lexical_cast<std::string>(minor) + "." + | |
1326 boost::lexical_cast<std::string>(revision) + | |
1327 " is required)"); | |
1328 } | |
1329 | |
1330 | |
1331 bool CheckMinimalOrthancVersion(unsigned int major, | |
1332 unsigned int minor, | |
1333 unsigned int revision) | |
1334 { | |
1335 if (!HasGlobalContext()) | |
1336 { | |
1337 LogError("Bad Orthanc context in the plugin"); | |
1338 return false; | |
1339 } | |
1340 | |
1341 if (!strcmp(GetGlobalContext()->orthancVersion, "mainline")) | |
1342 { | |
1343 // Assume compatibility with the mainline | |
1344 return true; | |
1345 } | |
1346 | |
1347 // Parse the version of the Orthanc core | |
1348 int aa, bb, cc; | |
1349 if ( | |
1350 #ifdef _MSC_VER | |
1351 sscanf_s | |
1352 #else | |
1353 sscanf | |
1354 #endif | |
1355 (GetGlobalContext()->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 || | |
1356 aa < 0 || | |
1357 bb < 0 || | |
1358 cc < 0) | |
1359 { | |
1360 return false; | |
1361 } | |
1362 | |
1363 unsigned int a = static_cast<unsigned int>(aa); | |
1364 unsigned int b = static_cast<unsigned int>(bb); | |
1365 unsigned int c = static_cast<unsigned int>(cc); | |
1366 | |
1367 // Check the major version number | |
1368 | |
1369 if (a > major) | |
3026 | 1370 { |
1371 return true; | |
1372 } | |
1373 | |
3028 | 1374 if (a < major) |
1375 { | |
1376 return false; | |
1377 } | |
1378 | |
2792
57e2d65d37ce
OrthancPlugins::MemoryBuffer::Release(), fixing openssl link
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
1379 |
3028 | 1380 // Check the minor version number |
1381 assert(a == major); | |
1382 | |
1383 if (b > minor) | |
3026 | 1384 { |
1385 return true; | |
1386 } | |
2792
57e2d65d37ce
OrthancPlugins::MemoryBuffer::Release(), fixing openssl link
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
1387 |
3028 | 1388 if (b < minor) |
3026 | 1389 { |
3028 | 1390 return false; |
3026 | 1391 } |
1392 | |
3028 | 1393 // Check the patch level version number |
1394 assert(a == major && b == minor); | |
3026 | 1395 |
3028 | 1396 if (c >= revision) |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1397 { |
3028 | 1398 return true; |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1399 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1400 else |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1401 { |
3028 | 1402 return false; |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1403 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1404 } |
2058
43cd2ab060c7
cont OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2048
diff
changeset
|
1405 |
43cd2ab060c7
cont OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2048
diff
changeset
|
1406 |
3028 | 1407 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) |
1408 const char* AutodetectMimeType(const std::string& path) | |
3026 | 1409 { |
3028 | 1410 const char* mime = OrthancPluginAutodetectMimeType(GetGlobalContext(), path.c_str()); |
2062
40ffd0e8676a
generation of etag in ServeFolders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2059
diff
changeset
|
1411 |
3028 | 1412 if (mime == NULL) |
1413 { | |
1414 // Should never happen, just for safety | |
1415 return "application/octet-stream"; | |
1416 } | |
1417 else | |
1418 { | |
1419 return mime; | |
1420 } | |
2848 | 1421 } |
3026 | 1422 #endif |
1423 | |
1424 | |
1425 #if HAS_ORTHANC_PLUGIN_PEERS == 1 | |
3028 | 1426 size_t OrthancPeers::GetPeerIndex(const std::string& name) const |
3026 | 1427 { |
3028 | 1428 size_t index; |
1429 if (LookupName(index, name)) | |
1430 { | |
1431 return index; | |
1432 } | |
1433 else | |
1434 { | |
1435 LogError("Inexistent peer: " + name); | |
1436 ORTHANC_PLUGINS_THROW_EXCEPTION(UnknownResource); | |
1437 } | |
3026 | 1438 } |
2059 | 1439 |
1440 | |
3028 | 1441 OrthancPeers::OrthancPeers() : |
1442 peers_(NULL), | |
1443 timeout_(0) | |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2177
diff
changeset
|
1444 { |
3028 | 1445 peers_ = OrthancPluginGetPeers(GetGlobalContext()); |
3026 | 1446 |
3028 | 1447 if (peers_ == NULL) |
3026 | 1448 { |
1449 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1450 } | |
1451 | |
3028 | 1452 uint32_t count = OrthancPluginGetPeersCount(GetGlobalContext(), peers_); |
1453 | |
1454 for (uint32_t i = 0; i < count; i++) | |
1455 { | |
1456 const char* name = OrthancPluginGetPeerName(GetGlobalContext(), peers_, i); | |
1457 if (name == NULL) | |
1458 { | |
1459 OrthancPluginFreePeers(GetGlobalContext(), peers_); | |
1460 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1461 } | |
1462 | |
1463 index_[name] = i; | |
1464 } | |
1465 } | |
1466 | |
1467 | |
1468 OrthancPeers::~OrthancPeers() | |
1469 { | |
1470 if (peers_ != NULL) | |
1471 { | |
1472 OrthancPluginFreePeers(GetGlobalContext(), peers_); | |
1473 } | |
1474 } | |
1475 | |
1476 | |
1477 bool OrthancPeers::LookupName(size_t& target, | |
1478 const std::string& name) const | |
1479 { | |
1480 Index::const_iterator found = index_.find(name); | |
1481 | |
1482 if (found == index_.end()) | |
1483 { | |
1484 return false; | |
1485 } | |
1486 else | |
1487 { | |
1488 target = found->second; | |
1489 return true; | |
1490 } | |
3026 | 1491 } |
3028 | 1492 |
1493 | |
1494 std::string OrthancPeers::GetPeerName(size_t index) const | |
1495 { | |
1496 if (index >= index_.size()) | |
1497 { | |
1498 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1499 } | |
1500 else | |
1501 { | |
1502 const char* s = OrthancPluginGetPeerName(GetGlobalContext(), peers_, static_cast<uint32_t>(index)); | |
1503 if (s == NULL) | |
1504 { | |
1505 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1506 } | |
1507 else | |
1508 { | |
1509 return s; | |
1510 } | |
1511 } | |
1512 } | |
1513 | |
1514 | |
1515 std::string OrthancPeers::GetPeerUrl(size_t index) const | |
1516 { | |
1517 if (index >= index_.size()) | |
1518 { | |
1519 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1520 } | |
1521 else | |
1522 { | |
1523 const char* s = OrthancPluginGetPeerUrl(GetGlobalContext(), peers_, static_cast<uint32_t>(index)); | |
1524 if (s == NULL) | |
1525 { | |
1526 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); | |
1527 } | |
1528 else | |
1529 { | |
1530 return s; | |
1531 } | |
1532 } | |
1533 } | |
1534 | |
1535 | |
1536 std::string OrthancPeers::GetPeerUrl(const std::string& name) const | |
1537 { | |
1538 return GetPeerUrl(GetPeerIndex(name)); | |
1539 } | |
1540 | |
1541 | |
1542 bool OrthancPeers::LookupUserProperty(std::string& value, | |
1543 size_t index, | |
1544 const std::string& key) const | |
1545 { | |
1546 if (index >= index_.size()) | |
1547 { | |
1548 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1549 } | |
1550 else | |
1551 { | |
1552 const char* s = OrthancPluginGetPeerUserProperty(GetGlobalContext(), peers_, static_cast<uint32_t>(index), key.c_str()); | |
1553 if (s == NULL) | |
1554 { | |
1555 return false; | |
1556 } | |
1557 else | |
1558 { | |
1559 value.assign(s); | |
1560 return true; | |
1561 } | |
1562 } | |
1563 } | |
1564 | |
1565 | |
1566 bool OrthancPeers::LookupUserProperty(std::string& value, | |
1567 const std::string& peer, | |
1568 const std::string& key) const | |
1569 { | |
1570 return LookupUserProperty(value, GetPeerIndex(peer), key); | |
1571 } | |
3026 | 1572 |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2177
diff
changeset
|
1573 |
3028 | 1574 bool OrthancPeers::DoGet(MemoryBuffer& target, |
1575 size_t index, | |
1576 const std::string& uri) const | |
1577 { | |
1578 if (index >= index_.size()) | |
1579 { | |
1580 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1581 } | |
1582 | |
1583 OrthancPluginMemoryBuffer answer; | |
1584 uint16_t status; | |
1585 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | |
1586 (GetGlobalContext(), &answer, NULL, &status, peers_, | |
1587 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(), | |
1588 0, NULL, NULL, NULL, 0, timeout_); | |
1589 | |
1590 if (code == OrthancPluginErrorCode_Success) | |
1591 { | |
1592 target.Assign(answer); | |
1593 return (status == 200); | |
1594 } | |
1595 else | |
1596 { | |
1597 return false; | |
1598 } | |
1599 } | |
1600 | |
1601 | |
1602 bool OrthancPeers::DoGet(MemoryBuffer& target, | |
1603 const std::string& name, | |
1604 const std::string& uri) const | |
1605 { | |
1606 size_t index; | |
1607 return (LookupName(index, name) && | |
1608 DoGet(target, index, uri)); | |
1609 } | |
1610 | |
1611 | |
1612 bool OrthancPeers::DoGet(Json::Value& target, | |
1613 size_t index, | |
1614 const std::string& uri) const | |
1615 { | |
1616 MemoryBuffer buffer; | |
1617 | |
1618 if (DoGet(buffer, index, uri)) | |
1619 { | |
1620 buffer.ToJson(target); | |
1621 return true; | |
1622 } | |
1623 else | |
1624 { | |
1625 return false; | |
1626 } | |
1627 } | |
1628 | |
1629 | |
1630 bool OrthancPeers::DoGet(Json::Value& target, | |
1631 const std::string& name, | |
1632 const std::string& uri) const | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1633 { |
3028 | 1634 MemoryBuffer buffer; |
1635 | |
1636 if (DoGet(buffer, name, uri)) | |
1637 { | |
1638 buffer.ToJson(target); | |
1639 return true; | |
1640 } | |
1641 else | |
1642 { | |
1643 return false; | |
1644 } | |
1645 } | |
1646 | |
1647 | |
1648 bool OrthancPeers::DoPost(MemoryBuffer& target, | |
1649 const std::string& name, | |
1650 const std::string& uri, | |
1651 const std::string& body) const | |
1652 { | |
1653 size_t index; | |
1654 return (LookupName(index, name) && | |
1655 DoPost(target, index, uri, body)); | |
1656 } | |
1657 | |
1658 | |
1659 bool OrthancPeers::DoPost(Json::Value& target, | |
1660 size_t index, | |
1661 const std::string& uri, | |
1662 const std::string& body) const | |
1663 { | |
1664 MemoryBuffer buffer; | |
1665 | |
1666 if (DoPost(buffer, index, uri, body)) | |
1667 { | |
1668 buffer.ToJson(target); | |
1669 return true; | |
1670 } | |
1671 else | |
1672 { | |
1673 return false; | |
1674 } | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1675 } |
3028 | 1676 |
1677 | |
1678 bool OrthancPeers::DoPost(Json::Value& target, | |
1679 const std::string& name, | |
1680 const std::string& uri, | |
1681 const std::string& body) const | |
1682 { | |
1683 MemoryBuffer buffer; | |
1684 | |
1685 if (DoPost(buffer, name, uri, body)) | |
1686 { | |
1687 buffer.ToJson(target); | |
1688 return true; | |
1689 } | |
1690 else | |
1691 { | |
1692 return false; | |
1693 } | |
1694 } | |
1695 | |
1696 | |
1697 bool OrthancPeers::DoPost(MemoryBuffer& target, | |
1698 size_t index, | |
1699 const std::string& uri, | |
1700 const std::string& body) const | |
1701 { | |
1702 if (index >= index_.size()) | |
1703 { | |
1704 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1705 } | |
1706 | |
1707 OrthancPluginMemoryBuffer answer; | |
1708 uint16_t status; | |
1709 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | |
1710 (GetGlobalContext(), &answer, NULL, &status, peers_, | |
1711 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), | |
1712 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); | |
1713 | |
1714 if (code == OrthancPluginErrorCode_Success) | |
1715 { | |
1716 target.Assign(answer); | |
1717 return (status == 200); | |
1718 } | |
1719 else | |
1720 { | |
1721 return false; | |
1722 } | |
1723 } | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1724 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1725 |
3028 | 1726 bool OrthancPeers::DoPut(size_t index, |
1727 const std::string& uri, | |
1728 const std::string& body) const | |
1729 { | |
1730 if (index >= index_.size()) | |
1731 { | |
1732 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1733 } | |
1734 | |
1735 OrthancPluginMemoryBuffer answer; | |
1736 uint16_t status; | |
1737 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | |
1738 (GetGlobalContext(), &answer, NULL, &status, peers_, | |
1739 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), | |
1740 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); | |
3026 | 1741 |
3028 | 1742 if (code == OrthancPluginErrorCode_Success) |
1743 { | |
1744 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &answer); | |
1745 return (status == 200); | |
1746 } | |
1747 else | |
1748 { | |
1749 return false; | |
1750 } | |
1751 } | |
1752 | |
1753 | |
1754 bool OrthancPeers::DoPut(const std::string& name, | |
1755 const std::string& uri, | |
1756 const std::string& body) const | |
1757 { | |
1758 size_t index; | |
1759 return (LookupName(index, name) && | |
1760 DoPut(index, uri, body)); | |
1761 } | |
1762 | |
1763 | |
1764 bool OrthancPeers::DoDelete(size_t index, | |
1765 const std::string& uri) const | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1766 { |
3028 | 1767 if (index >= index_.size()) |
1768 { | |
1769 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1770 } | |
1771 | |
1772 OrthancPluginMemoryBuffer answer; | |
1773 uint16_t status; | |
1774 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | |
1775 (GetGlobalContext(), &answer, NULL, &status, peers_, | |
1776 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), | |
1777 0, NULL, NULL, NULL, 0, timeout_); | |
1778 | |
1779 if (code == OrthancPluginErrorCode_Success) | |
1780 { | |
1781 OrthancPluginFreeMemoryBuffer(GetGlobalContext(), &answer); | |
1782 return (status == 200); | |
1783 } | |
1784 else | |
1785 { | |
1786 return false; | |
1787 } | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1788 } |
3028 | 1789 |
1790 | |
1791 bool OrthancPeers::DoDelete(const std::string& name, | |
1792 const std::string& uri) const | |
3026 | 1793 { |
3028 | 1794 size_t index; |
1795 return (LookupName(index, name) && | |
1796 DoDelete(index, uri)); | |
1797 } | |
1798 #endif | |
1799 | |
1800 | |
1801 | |
1802 #if HAS_ORTHANC_PLUGIN_JOB == 1 | |
1803 void OrthancJob::CallbackFinalize(void* job) | |
1804 { | |
1805 if (job != NULL) | |
1806 { | |
1807 delete reinterpret_cast<OrthancJob*>(job); | |
1808 } | |
3026 | 1809 } |
3028 | 1810 |
1811 | |
1812 float OrthancJob::CallbackGetProgress(void* job) | |
1813 { | |
1814 assert(job != NULL); | |
1815 | |
1816 try | |
1817 { | |
1818 return reinterpret_cast<OrthancJob*>(job)->progress_; | |
1819 } | |
1820 catch (...) | |
1821 { | |
1822 return 0; | |
1823 } | |
1824 } | |
1825 | |
1826 | |
1827 const char* OrthancJob::CallbackGetContent(void* job) | |
1828 { | |
1829 assert(job != NULL); | |
1830 | |
1831 try | |
1832 { | |
1833 return reinterpret_cast<OrthancJob*>(job)->content_.c_str(); | |
1834 } | |
1835 catch (...) | |
1836 { | |
1837 return 0; | |
1838 } | |
1839 } | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1840 |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1841 |
3028 | 1842 const char* OrthancJob::CallbackGetSerialized(void* job) |
1843 { | |
1844 assert(job != NULL); | |
1845 | |
1846 try | |
1847 { | |
1848 const OrthancJob& tmp = *reinterpret_cast<OrthancJob*>(job); | |
1849 | |
1850 if (tmp.hasSerialized_) | |
1851 { | |
1852 return tmp.serialized_.c_str(); | |
1853 } | |
1854 else | |
1855 { | |
1856 return NULL; | |
1857 } | |
1858 } | |
1859 catch (...) | |
1860 { | |
1861 return 0; | |
1862 } | |
1863 } | |
1864 | |
1865 | |
1866 OrthancPluginJobStepStatus OrthancJob::CallbackStep(void* job) | |
1867 { | |
1868 assert(job != NULL); | |
1869 | |
1870 try | |
1871 { | |
1872 return reinterpret_cast<OrthancJob*>(job)->Step(); | |
1873 } | |
1874 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS&) | |
1875 { | |
1876 return OrthancPluginJobStepStatus_Failure; | |
1877 } | |
1878 catch (...) | |
1879 { | |
1880 return OrthancPluginJobStepStatus_Failure; | |
1881 } | |
1882 } | |
1883 | |
1884 | |
1885 OrthancPluginErrorCode OrthancJob::CallbackStop(void* job, | |
1886 OrthancPluginJobStopReason reason) | |
1887 { | |
1888 assert(job != NULL); | |
1889 | |
1890 try | |
1891 { | |
1892 reinterpret_cast<OrthancJob*>(job)->Stop(reason); | |
1893 return OrthancPluginErrorCode_Success; | |
1894 } | |
1895 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
1896 { | |
1897 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
1898 } | |
1899 catch (...) | |
1900 { | |
1901 return OrthancPluginErrorCode_Plugin; | |
1902 } | |
1903 } | |
1904 | |
1905 | |
1906 OrthancPluginErrorCode OrthancJob::CallbackReset(void* job) | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1907 { |
3028 | 1908 assert(job != NULL); |
1909 | |
1910 try | |
1911 { | |
1912 reinterpret_cast<OrthancJob*>(job)->Reset(); | |
1913 return OrthancPluginErrorCode_Success; | |
1914 } | |
1915 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
1916 { | |
1917 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
1918 } | |
1919 catch (...) | |
1920 { | |
1921 return OrthancPluginErrorCode_Plugin; | |
1922 } | |
1923 } | |
1924 | |
1925 | |
1926 void OrthancJob::ClearContent() | |
1927 { | |
1928 Json::Value empty = Json::objectValue; | |
1929 UpdateContent(empty); | |
1930 } | |
1931 | |
1932 | |
1933 void OrthancJob::UpdateContent(const Json::Value& content) | |
1934 { | |
1935 if (content.type() != Json::objectValue) | |
1936 { | |
1937 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | |
1938 } | |
1939 else | |
1940 { | |
1941 Json::FastWriter writer; | |
1942 content_ = writer.write(content); | |
1943 } | |
1944 } | |
1945 | |
1946 | |
1947 void OrthancJob::ClearSerialized() | |
1948 { | |
1949 hasSerialized_ = false; | |
1950 serialized_.clear(); | |
3026 | 1951 } |
3028 | 1952 |
1953 | |
1954 void OrthancJob::UpdateSerialized(const Json::Value& serialized) | |
1955 { | |
1956 if (serialized.type() != Json::objectValue) | |
1957 { | |
1958 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | |
1959 } | |
1960 else | |
1961 { | |
1962 Json::FastWriter writer; | |
1963 serialized_ = writer.write(serialized); | |
1964 hasSerialized_ = true; | |
1965 } | |
1966 } | |
1967 | |
1968 | |
1969 void OrthancJob::UpdateProgress(float progress) | |
3026 | 1970 { |
3028 | 1971 if (progress < 0 || |
1972 progress > 1) | |
1973 { | |
1974 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | |
1975 } | |
1976 | |
1977 progress_ = progress; | |
1978 } | |
1979 | |
1980 | |
1981 OrthancJob::OrthancJob(const std::string& jobType) : | |
1982 jobType_(jobType), | |
1983 progress_(0) | |
1984 { | |
1985 ClearContent(); | |
1986 ClearSerialized(); | |
1987 } | |
1988 | |
1989 | |
1990 OrthancPluginJob* OrthancJob::Create(OrthancJob* job) | |
1991 { | |
1992 if (job == NULL) | |
1993 { | |
1994 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer); | |
1995 } | |
1996 | |
1997 OrthancPluginJob* orthanc = OrthancPluginCreateJob( | |
1998 GetGlobalContext(), job, CallbackFinalize, job->jobType_.c_str(), | |
1999 CallbackGetProgress, CallbackGetContent, CallbackGetSerialized, | |
2000 CallbackStep, CallbackStop, CallbackReset); | |
2001 | |
2002 if (orthanc == NULL) | |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2003 { |
3026 | 2004 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2005 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2006 else |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2007 { |
3028 | 2008 return orthanc; |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2009 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2010 } |
2205
395522e46b2b
refactoring of the worklist sample using OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2177
diff
changeset
|
2011 |
2264
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
2012 |
3028 | 2013 std::string OrthancJob::Submit(OrthancJob* job, |
2014 int priority) | |
2264
8e5e0de75839
primitives for HTTP client in plugin C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2263
diff
changeset
|
2015 { |
3028 | 2016 OrthancPluginJob* orthanc = Create(job); |
2017 | |
2018 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); | |
2019 | |
2020 if (id == NULL) | |
3026 | 2021 { |
3028 | 2022 LogError("Plugin cannot submit job"); |
2023 OrthancPluginFreeJob(GetGlobalContext(), orthanc); | |
3026 | 2024 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_Plugin); |
2025 } | |
2026 else | |
2027 { | |
3028 | 2028 std::string tmp(id); |
2029 tmp.assign(id); | |
2030 OrthancPluginFreeString(GetGlobalContext(), id); | |
3026 | 2031 |
3028 | 2032 return tmp; |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2033 } |
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2034 } |
2817
473bf302d629
C++ wrappers around OrthancPluginJob and OrthancPluginPeers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2803
diff
changeset
|
2035 #endif |
2047
438f86ee19fc
toolbox shared by all plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2036 } |