Mercurial > hg > orthanc
comparison Core/WebServiceParameters.cpp @ 2020:a0bd8cd55da7
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 15 Jun 2016 17:19:10 +0200 |
parents | OrthancServer/OrthancPeerParameters.cpp@9c9332e486ca |
children | bd143a77eb7a |
comparison
equal
deleted
inserted
replaced
2019:9c9332e486ca | 2020:a0bd8cd55da7 |
---|---|
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 namespace Orthanc | |
41 { | |
42 WebServiceParameters::WebServiceParameters() : | |
43 advancedFormat_(false), | |
44 url_("http://localhost:8042/") | |
45 { | |
46 } | |
47 | |
48 | |
49 void WebServiceParameters::SetClientCertificate(const std::string& certificateFile, | |
50 const std::string& certificateKeyFile, | |
51 const std::string& certificateKeyPassword) | |
52 { | |
53 if (certificateFile.empty()) | |
54 { | |
55 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
56 } | |
57 | |
58 if (!Toolbox::IsRegularFile(certificateFile)) | |
59 { | |
60 LOG(ERROR) << "Cannot open certificate file: " << certificateFile; | |
61 throw OrthancException(ErrorCode_InexistentFile); | |
62 } | |
63 | |
64 if (!certificateKeyFile.empty() && | |
65 !Toolbox::IsRegularFile(certificateKeyFile)) | |
66 { | |
67 LOG(ERROR) << "Cannot open key file: " << certificateKeyFile; | |
68 throw OrthancException(ErrorCode_InexistentFile); | |
69 } | |
70 | |
71 advancedFormat_ = true; | |
72 certificateFile_ = certificateFile; | |
73 certificateKeyFile_ = certificateKeyFile; | |
74 certificateKeyPassword_ = certificateKeyPassword; | |
75 } | |
76 | |
77 | |
78 static void AddTrailingSlash(std::string& url) | |
79 { | |
80 if (url.size() != 0 && | |
81 url[url.size() - 1] != '/') | |
82 { | |
83 url += '/'; | |
84 } | |
85 } | |
86 | |
87 | |
88 void WebServiceParameters::FromJsonArray(const Json::Value& peer) | |
89 { | |
90 assert(peer.isArray()); | |
91 | |
92 advancedFormat_ = false; | |
93 | |
94 if (peer.size() != 1 && | |
95 peer.size() != 3) | |
96 { | |
97 throw OrthancException(ErrorCode_BadFileFormat); | |
98 } | |
99 | |
100 std::string url = peer.get(0u, "").asString(); | |
101 if (url.empty()) | |
102 { | |
103 throw OrthancException(ErrorCode_BadFileFormat); | |
104 } | |
105 | |
106 AddTrailingSlash(url); | |
107 SetUrl(url); | |
108 | |
109 if (peer.size() == 1) | |
110 { | |
111 SetUsername(""); | |
112 SetPassword(""); | |
113 } | |
114 else if (peer.size() == 3) | |
115 { | |
116 SetUsername(peer.get(1u, "").asString()); | |
117 SetPassword(peer.get(2u, "").asString()); | |
118 } | |
119 else | |
120 { | |
121 throw OrthancException(ErrorCode_BadFileFormat); | |
122 } | |
123 } | |
124 | |
125 | |
126 static std::string GetStringMember(const Json::Value& peer, | |
127 const std::string& key, | |
128 const std::string& defaultValue) | |
129 { | |
130 if (!peer.isMember(key)) | |
131 { | |
132 return defaultValue; | |
133 } | |
134 else if (peer[key].type() != Json::stringValue) | |
135 { | |
136 throw OrthancException(ErrorCode_BadFileFormat); | |
137 } | |
138 else | |
139 { | |
140 return peer[key].asString(); | |
141 } | |
142 } | |
143 | |
144 | |
145 void WebServiceParameters::FromJsonObject(const Json::Value& peer) | |
146 { | |
147 assert(peer.isObject()); | |
148 advancedFormat_ = true; | |
149 | |
150 std::string url = GetStringMember(peer, "Url", ""); | |
151 if (url.empty()) | |
152 { | |
153 throw OrthancException(ErrorCode_BadFileFormat); | |
154 } | |
155 | |
156 AddTrailingSlash(url); | |
157 SetUrl(url); | |
158 | |
159 SetUsername(GetStringMember(peer, "Username", "")); | |
160 SetPassword(GetStringMember(peer, "Password", "")); | |
161 | |
162 if (peer.isMember("CertificateFile")) | |
163 { | |
164 SetClientCertificate(GetStringMember(peer, "CertificateFile", ""), | |
165 GetStringMember(peer, "CertificateKeyFile", ""), | |
166 GetStringMember(peer, "CertificateKeyPassword", "")); | |
167 } | |
168 } | |
169 | |
170 | |
171 void WebServiceParameters::FromJson(const Json::Value& peer) | |
172 { | |
173 try | |
174 { | |
175 if (peer.isArray()) | |
176 { | |
177 FromJsonArray(peer); | |
178 } | |
179 else if (peer.isObject()) | |
180 { | |
181 FromJsonObject(peer); | |
182 } | |
183 else | |
184 { | |
185 throw OrthancException(ErrorCode_BadFileFormat); | |
186 } | |
187 } | |
188 catch (...) | |
189 { | |
190 throw OrthancException(ErrorCode_BadFileFormat); | |
191 } | |
192 } | |
193 | |
194 | |
195 void WebServiceParameters::ToJson(Json::Value& value) const | |
196 { | |
197 if (advancedFormat_) | |
198 { | |
199 value = Json::objectValue; | |
200 value["Url"] = url_; | |
201 | |
202 if (!username_.empty() || | |
203 !password_.empty()) | |
204 { | |
205 value["Username"] = username_; | |
206 value["Password"] = password_; | |
207 } | |
208 | |
209 if (!certificateFile_.empty()) | |
210 { | |
211 value["CertificateFile"] = certificateFile_; | |
212 } | |
213 | |
214 if (!certificateKeyFile_.empty()) | |
215 { | |
216 value["CertificateKeyFile"] = certificateKeyFile_; | |
217 } | |
218 | |
219 if (!certificateKeyPassword_.empty()) | |
220 { | |
221 value["CertificateKeyPassword"] = certificateKeyPassword_; | |
222 } | |
223 } | |
224 else | |
225 { | |
226 value = Json::arrayValue; | |
227 value.append(url_); | |
228 | |
229 if (!username_.empty() || | |
230 !password_.empty()) | |
231 { | |
232 value.append(username_); | |
233 value.append(password_); | |
234 } | |
235 } | |
236 } | |
237 } |