comparison Resources/Orthanc/Core/WebServiceParameters.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "PrecompiledHeaders.h"
35 #include "WebServiceParameters.h"
36
37 #include "../Core/Logging.h"
38 #include "../Core/OrthancException.h"
39
40 #if ORTHANC_SANDBOXED == 0
41 # include "../Core/SystemToolbox.h"
42 #endif
43
44 #include <cassert>
45
46 namespace Orthanc
47 {
48 WebServiceParameters::WebServiceParameters() :
49 advancedFormat_(false),
50 url_("http://127.0.0.1:8042/"),
51 pkcs11Enabled_(false)
52 {
53 }
54
55
56 void WebServiceParameters::ClearClientCertificate()
57 {
58 certificateFile_.clear();
59 certificateKeyFile_.clear();
60 certificateKeyPassword_.clear();
61 }
62
63
64 #if ORTHANC_SANDBOXED == 0
65 void WebServiceParameters::SetClientCertificate(const std::string& certificateFile,
66 const std::string& certificateKeyFile,
67 const std::string& certificateKeyPassword)
68 {
69 if (certificateFile.empty())
70 {
71 throw OrthancException(ErrorCode_ParameterOutOfRange);
72 }
73
74 if (!SystemToolbox::IsRegularFile(certificateFile))
75 {
76 LOG(ERROR) << "Cannot open certificate file: " << certificateFile;
77 throw OrthancException(ErrorCode_InexistentFile);
78 }
79
80 if (!certificateKeyFile.empty() &&
81 !SystemToolbox::IsRegularFile(certificateKeyFile))
82 {
83 LOG(ERROR) << "Cannot open key file: " << certificateKeyFile;
84 throw OrthancException(ErrorCode_InexistentFile);
85 }
86
87 advancedFormat_ = true;
88 certificateFile_ = certificateFile;
89 certificateKeyFile_ = certificateKeyFile;
90 certificateKeyPassword_ = certificateKeyPassword;
91 }
92 #endif
93
94
95 static void AddTrailingSlash(std::string& url)
96 {
97 if (url.size() != 0 &&
98 url[url.size() - 1] != '/')
99 {
100 url += '/';
101 }
102 }
103
104
105 void WebServiceParameters::FromJsonArray(const Json::Value& peer)
106 {
107 assert(peer.isArray());
108
109 advancedFormat_ = false;
110 pkcs11Enabled_ = false;
111
112 if (peer.size() != 1 &&
113 peer.size() != 3)
114 {
115 throw OrthancException(ErrorCode_BadFileFormat);
116 }
117
118 std::string url = peer.get(0u, "").asString();
119 if (url.empty())
120 {
121 throw OrthancException(ErrorCode_BadFileFormat);
122 }
123
124 AddTrailingSlash(url);
125 SetUrl(url);
126
127 if (peer.size() == 1)
128 {
129 SetUsername("");
130 SetPassword("");
131 }
132 else if (peer.size() == 3)
133 {
134 SetUsername(peer.get(1u, "").asString());
135 SetPassword(peer.get(2u, "").asString());
136 }
137 else
138 {
139 throw OrthancException(ErrorCode_BadFileFormat);
140 }
141 }
142
143
144 static std::string GetStringMember(const Json::Value& peer,
145 const std::string& key,
146 const std::string& defaultValue)
147 {
148 if (!peer.isMember(key))
149 {
150 return defaultValue;
151 }
152 else if (peer[key].type() != Json::stringValue)
153 {
154 throw OrthancException(ErrorCode_BadFileFormat);
155 }
156 else
157 {
158 return peer[key].asString();
159 }
160 }
161
162
163 void WebServiceParameters::FromJsonObject(const Json::Value& peer)
164 {
165 assert(peer.isObject());
166 advancedFormat_ = true;
167
168 std::string url = GetStringMember(peer, "Url", "");
169 if (url.empty())
170 {
171 throw OrthancException(ErrorCode_BadFileFormat);
172 }
173
174 AddTrailingSlash(url);
175 SetUrl(url);
176
177 SetUsername(GetStringMember(peer, "Username", ""));
178 SetPassword(GetStringMember(peer, "Password", ""));
179
180 #if ORTHANC_SANDBOXED == 0
181 if (peer.isMember("CertificateFile"))
182 {
183 SetClientCertificate(GetStringMember(peer, "CertificateFile", ""),
184 GetStringMember(peer, "CertificateKeyFile", ""),
185 GetStringMember(peer, "CertificateKeyPassword", ""));
186 }
187 #endif
188
189 if (peer.isMember("Pkcs11"))
190 {
191 if (peer["Pkcs11"].type() == Json::booleanValue)
192 {
193 pkcs11Enabled_ = peer["Pkcs11"].asBool();
194 }
195 else
196 {
197 throw OrthancException(ErrorCode_BadFileFormat);
198 }
199 }
200 }
201
202
203 void WebServiceParameters::FromJson(const Json::Value& peer)
204 {
205 try
206 {
207 if (peer.isArray())
208 {
209 FromJsonArray(peer);
210 }
211 else if (peer.isObject())
212 {
213 FromJsonObject(peer);
214 }
215 else
216 {
217 throw OrthancException(ErrorCode_BadFileFormat);
218 }
219 }
220 catch (OrthancException&)
221 {
222 throw;
223 }
224 catch (...)
225 {
226 throw OrthancException(ErrorCode_BadFileFormat);
227 }
228 }
229
230
231 void WebServiceParameters::ToJson(Json::Value& value) const
232 {
233 if (advancedFormat_)
234 {
235 value = Json::objectValue;
236 value["Url"] = url_;
237
238 if (!username_.empty() ||
239 !password_.empty())
240 {
241 value["Username"] = username_;
242 value["Password"] = password_;
243 }
244
245 if (!certificateFile_.empty())
246 {
247 value["CertificateFile"] = certificateFile_;
248 }
249
250 if (!certificateKeyFile_.empty())
251 {
252 value["CertificateKeyFile"] = certificateKeyFile_;
253 }
254
255 if (!certificateKeyPassword_.empty())
256 {
257 value["CertificateKeyPassword"] = certificateKeyPassword_;
258 }
259 }
260 else
261 {
262 value = Json::arrayValue;
263 value.append(url_);
264
265 if (!username_.empty() ||
266 !password_.empty())
267 {
268 value.append(username_);
269 value.append(password_);
270 }
271 }
272 }
273 }