Mercurial > hg > orthanc
annotate OrthancServer/OrthancInitialization.cpp @ 136:fe180eae201d
openssl exception
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Oct 2012 18:21:01 +0200 |
parents | a6e41de88a53 |
children | 9784f19f7e1b 4d5f0857ec9c |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * 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. | |
136 | 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. | |
0 | 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 | |
62 | 33 #include "OrthancInitialization.h" |
0 | 34 |
62 | 35 #include "../Core/OrthancException.h" |
0 | 36 #include "../Core/Toolbox.h" |
37 | |
38 #include <boost/lexical_cast.hpp> | |
39 #include <boost/filesystem.hpp> | |
40 #include <curl/curl.h> | |
41 #include <boost/thread.hpp> | |
108 | 42 #include <glog/logging.h> |
0 | 43 |
62 | 44 namespace Orthanc |
0 | 45 { |
46 static const char* CONFIGURATION_FILE = "Configuration.json"; | |
47 | |
48 static boost::mutex globalMutex_; | |
49 static std::auto_ptr<Json::Value> configuration_; | |
50 | |
51 | |
52 static void ReadGlobalConfiguration(const char* configurationFile) | |
53 { | |
54 configuration_.reset(new Json::Value); | |
55 | |
56 std::string content; | |
57 | |
58 if (configurationFile) | |
59 { | |
60 Toolbox::ReadFile(content, configurationFile); | |
108 | 61 LOG(INFO) << "Using the configuration from: " << configurationFile; |
0 | 62 } |
63 else | |
64 { | |
91 | 65 #if 0 && ORTHANC_STANDALONE == 1 && defined(__linux) |
66 // Unused anymore | |
88 | 67 // Under Linux, try and open "../../etc/orthanc/Configuration.json" |
19 | 68 try |
69 { | |
88 | 70 boost::filesystem::path p = Toolbox::GetDirectoryOfExecutable(); |
71 p = p.parent_path().parent_path(); | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
72 p /= "etc"; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
73 p /= "orthanc"; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
74 p /= CONFIGURATION_FILE; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
75 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
76 Toolbox::ReadFile(content, p.string()); |
108 | 77 LOG(INFO) << "Using the configuration from: " << p.string(); |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
78 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
79 catch (OrthancException&) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
80 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
81 // No configuration file found, give up with empty configuration |
108 | 82 LOG(INFO) << "Using the default Orthanc configuration"; |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
83 return; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
84 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
85 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
86 #elif ORTHANC_STANDALONE == 1 |
91 | 87 // No default path for the standalone configuration |
108 | 88 LOG(INFO) << "Using the default Orthanc configuration"; |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
89 return; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
90 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
91 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
92 // In a non-standalone build, we use the |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
93 // "Resources/Configuration.json" from the Orthanc distribution |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
94 try |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
95 { |
62 | 96 boost::filesystem::path p = ORTHANC_PATH; |
19 | 97 p /= "Resources"; |
98 p /= CONFIGURATION_FILE; | |
99 Toolbox::ReadFile(content, p.string()); | |
108 | 100 LOG(INFO) << "Using the configuration from: " << p.string(); |
19 | 101 } |
62 | 102 catch (OrthancException&) |
19 | 103 { |
104 // No configuration file found, give up with empty configuration | |
108 | 105 LOG(INFO) << "Using the default Orthanc configuration"; |
19 | 106 return; |
107 } | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
108 #endif |
0 | 109 } |
110 | |
111 Json::Reader reader; | |
112 if (!reader.parse(content, *configuration_)) | |
113 { | |
62 | 114 throw OrthancException("Unable to read the configuration file"); |
0 | 115 } |
116 } | |
117 | |
118 | |
62 | 119 void OrthancInitialize(const char* configurationFile) |
0 | 120 { |
121 boost::mutex::scoped_lock lock(globalMutex_); | |
122 ReadGlobalConfiguration(configurationFile); | |
123 curl_global_init(CURL_GLOBAL_ALL); | |
124 } | |
125 | |
126 | |
127 | |
62 | 128 void OrthancFinalize() |
0 | 129 { |
130 boost::mutex::scoped_lock lock(globalMutex_); | |
131 curl_global_cleanup(); | |
132 configuration_.reset(NULL); | |
133 } | |
134 | |
135 | |
136 | |
137 std::string GetGlobalStringParameter(const std::string& parameter, | |
138 const std::string& defaultValue) | |
139 { | |
140 boost::mutex::scoped_lock lock(globalMutex_); | |
141 | |
142 if (configuration_->isMember(parameter)) | |
143 { | |
144 return (*configuration_) [parameter].asString(); | |
145 } | |
146 else | |
147 { | |
148 return defaultValue; | |
149 } | |
150 } | |
151 | |
152 | |
153 int GetGlobalIntegerParameter(const std::string& parameter, | |
154 int defaultValue) | |
155 { | |
156 boost::mutex::scoped_lock lock(globalMutex_); | |
157 | |
158 if (configuration_->isMember(parameter)) | |
159 { | |
160 return (*configuration_) [parameter].asInt(); | |
161 } | |
162 else | |
163 { | |
164 return defaultValue; | |
165 } | |
166 } | |
167 | |
23 | 168 bool GetGlobalBoolParameter(const std::string& parameter, |
169 bool defaultValue) | |
170 { | |
171 boost::mutex::scoped_lock lock(globalMutex_); | |
172 | |
173 if (configuration_->isMember(parameter)) | |
174 { | |
175 return (*configuration_) [parameter].asBool(); | |
176 } | |
177 else | |
178 { | |
179 return defaultValue; | |
180 } | |
181 } | |
182 | |
183 | |
0 | 184 |
185 | |
186 void GetDicomModality(const std::string& name, | |
187 std::string& aet, | |
188 std::string& address, | |
189 int& port) | |
190 { | |
191 boost::mutex::scoped_lock lock(globalMutex_); | |
192 | |
193 if (!configuration_->isMember("DicomModalities")) | |
194 { | |
62 | 195 throw OrthancException(""); |
0 | 196 } |
197 | |
198 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
199 if (modalities.type() != Json::objectValue || | |
200 !modalities.isMember(name)) | |
201 { | |
62 | 202 throw OrthancException(""); |
0 | 203 } |
204 | |
205 try | |
206 { | |
207 aet = modalities[name].get(0u, "").asString(); | |
208 address = modalities[name].get(1u, "").asString(); | |
209 port = modalities[name].get(2u, "").asInt(); | |
210 } | |
211 catch (...) | |
212 { | |
62 | 213 throw OrthancException("Badly formatted DICOM modality"); |
0 | 214 } |
215 } | |
216 | |
217 | |
218 | |
219 void GetListOfDicomModalities(std::set<std::string>& target) | |
220 { | |
221 boost::mutex::scoped_lock lock(globalMutex_); | |
222 | |
223 target.clear(); | |
224 | |
225 if (!configuration_->isMember("DicomModalities")) | |
226 { | |
227 return; | |
228 } | |
229 | |
230 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
231 if (modalities.type() != Json::objectValue) | |
232 { | |
62 | 233 throw OrthancException("Badly formatted list of DICOM modalities"); |
0 | 234 } |
235 | |
236 Json::Value::Members members = modalities.getMemberNames(); | |
237 for (size_t i = 0; i < members.size(); i++) | |
238 { | |
239 for (size_t j = 0; j < members[i].size(); j++) | |
240 { | |
241 if (!isalnum(members[i][j]) && members[i][j] != '-') | |
242 { | |
62 | 243 throw OrthancException("Only alphanumeric and dash characters are allowed in the names of the modalities"); |
0 | 244 } |
245 } | |
246 | |
247 target.insert(members[i]); | |
248 } | |
249 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
250 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
251 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
252 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
253 void SetupRegisteredUsers(MongooseServer& httpServer) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
254 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
255 boost::mutex::scoped_lock lock(globalMutex_); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
256 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
257 httpServer.ClearUsers(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
258 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
259 if (!configuration_->isMember("RegisteredUsers")) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
260 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
261 return; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
262 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
263 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
264 const Json::Value& users = (*configuration_) ["RegisteredUsers"]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
265 if (users.type() != Json::objectValue) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
266 { |
62 | 267 throw OrthancException("Badly formatted list of users"); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
268 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
269 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
270 Json::Value::Members usernames = users.getMemberNames(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
271 for (size_t i = 0; i < usernames.size(); i++) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
272 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
273 const std::string& username = usernames[i]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
274 std::string password = users[username].asString(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
275 httpServer.RegisterUser(username.c_str(), password.c_str()); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
276 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
277 } |
0 | 278 } |