annotate StoneWebViewer/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 1510:1005c1cbe4dd

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