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