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