Mercurial > hg > orthanc
annotate OrthancServer/OrthancInitialization.cpp @ 394:9784f19f7e1b lua-scripting
path relative to configuration path, list of lua scripts
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 02 May 2013 11:02:15 +0200 |
parents | fe180eae201d |
children | 2d269089078f |
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_; | |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
50 static boost::filesystem::path defaultDirectory_; |
0 | 51 |
52 | |
53 static void ReadGlobalConfiguration(const char* configurationFile) | |
54 { | |
55 configuration_.reset(new Json::Value); | |
56 | |
57 std::string content; | |
58 | |
59 if (configurationFile) | |
60 { | |
61 Toolbox::ReadFile(content, configurationFile); | |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
62 defaultDirectory_ = boost::filesystem::path(configurationFile).parent_path(); |
108 | 63 LOG(INFO) << "Using the configuration from: " << configurationFile; |
0 | 64 } |
65 else | |
66 { | |
91 | 67 #if 0 && ORTHANC_STANDALONE == 1 && defined(__linux) |
68 // Unused anymore | |
88 | 69 // Under Linux, try and open "../../etc/orthanc/Configuration.json" |
19 | 70 try |
71 { | |
88 | 72 boost::filesystem::path p = Toolbox::GetDirectoryOfExecutable(); |
73 p = p.parent_path().parent_path(); | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
74 p /= "etc"; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
75 p /= "orthanc"; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
76 p /= CONFIGURATION_FILE; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
77 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
78 Toolbox::ReadFile(content, p.string()); |
108 | 79 LOG(INFO) << "Using the configuration from: " << p.string(); |
87
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 catch (OrthancException&) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
82 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
83 // No configuration file found, give up with empty configuration |
108 | 84 LOG(INFO) << "Using the default Orthanc configuration"; |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
85 return; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
86 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
87 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
88 #elif ORTHANC_STANDALONE == 1 |
91 | 89 // No default path for the standalone configuration |
108 | 90 LOG(INFO) << "Using the default Orthanc configuration"; |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
91 return; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
92 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
93 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
94 // In a non-standalone build, we use the |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
95 // "Resources/Configuration.json" from the Orthanc distribution |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
96 try |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
97 { |
62 | 98 boost::filesystem::path p = ORTHANC_PATH; |
19 | 99 p /= "Resources"; |
100 p /= CONFIGURATION_FILE; | |
101 Toolbox::ReadFile(content, p.string()); | |
108 | 102 LOG(INFO) << "Using the configuration from: " << p.string(); |
19 | 103 } |
62 | 104 catch (OrthancException&) |
19 | 105 { |
106 // No configuration file found, give up with empty configuration | |
108 | 107 LOG(INFO) << "Using the default Orthanc configuration"; |
19 | 108 return; |
109 } | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
110 #endif |
0 | 111 } |
112 | |
113 Json::Reader reader; | |
114 if (!reader.parse(content, *configuration_)) | |
115 { | |
62 | 116 throw OrthancException("Unable to read the configuration file"); |
0 | 117 } |
118 } | |
119 | |
120 | |
62 | 121 void OrthancInitialize(const char* configurationFile) |
0 | 122 { |
123 boost::mutex::scoped_lock lock(globalMutex_); | |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
124 defaultDirectory_ = boost::filesystem::current_path(); |
0 | 125 ReadGlobalConfiguration(configurationFile); |
126 curl_global_init(CURL_GLOBAL_ALL); | |
127 } | |
128 | |
129 | |
130 | |
62 | 131 void OrthancFinalize() |
0 | 132 { |
133 boost::mutex::scoped_lock lock(globalMutex_); | |
134 curl_global_cleanup(); | |
135 configuration_.reset(NULL); | |
136 } | |
137 | |
138 | |
139 | |
140 std::string GetGlobalStringParameter(const std::string& parameter, | |
141 const std::string& defaultValue) | |
142 { | |
143 boost::mutex::scoped_lock lock(globalMutex_); | |
144 | |
145 if (configuration_->isMember(parameter)) | |
146 { | |
147 return (*configuration_) [parameter].asString(); | |
148 } | |
149 else | |
150 { | |
151 return defaultValue; | |
152 } | |
153 } | |
154 | |
155 | |
156 int GetGlobalIntegerParameter(const std::string& parameter, | |
157 int defaultValue) | |
158 { | |
159 boost::mutex::scoped_lock lock(globalMutex_); | |
160 | |
161 if (configuration_->isMember(parameter)) | |
162 { | |
163 return (*configuration_) [parameter].asInt(); | |
164 } | |
165 else | |
166 { | |
167 return defaultValue; | |
168 } | |
169 } | |
170 | |
23 | 171 bool GetGlobalBoolParameter(const std::string& parameter, |
172 bool defaultValue) | |
173 { | |
174 boost::mutex::scoped_lock lock(globalMutex_); | |
175 | |
176 if (configuration_->isMember(parameter)) | |
177 { | |
178 return (*configuration_) [parameter].asBool(); | |
179 } | |
180 else | |
181 { | |
182 return defaultValue; | |
183 } | |
184 } | |
185 | |
186 | |
0 | 187 |
188 | |
189 void GetDicomModality(const std::string& name, | |
190 std::string& aet, | |
191 std::string& address, | |
192 int& port) | |
193 { | |
194 boost::mutex::scoped_lock lock(globalMutex_); | |
195 | |
196 if (!configuration_->isMember("DicomModalities")) | |
197 { | |
62 | 198 throw OrthancException(""); |
0 | 199 } |
200 | |
201 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
202 if (modalities.type() != Json::objectValue || | |
203 !modalities.isMember(name)) | |
204 { | |
62 | 205 throw OrthancException(""); |
0 | 206 } |
207 | |
208 try | |
209 { | |
210 aet = modalities[name].get(0u, "").asString(); | |
211 address = modalities[name].get(1u, "").asString(); | |
212 port = modalities[name].get(2u, "").asInt(); | |
213 } | |
214 catch (...) | |
215 { | |
62 | 216 throw OrthancException("Badly formatted DICOM modality"); |
0 | 217 } |
218 } | |
219 | |
220 | |
221 | |
222 void GetListOfDicomModalities(std::set<std::string>& target) | |
223 { | |
224 boost::mutex::scoped_lock lock(globalMutex_); | |
225 | |
226 target.clear(); | |
227 | |
228 if (!configuration_->isMember("DicomModalities")) | |
229 { | |
230 return; | |
231 } | |
232 | |
233 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
234 if (modalities.type() != Json::objectValue) | |
235 { | |
62 | 236 throw OrthancException("Badly formatted list of DICOM modalities"); |
0 | 237 } |
238 | |
239 Json::Value::Members members = modalities.getMemberNames(); | |
240 for (size_t i = 0; i < members.size(); i++) | |
241 { | |
242 for (size_t j = 0; j < members[i].size(); j++) | |
243 { | |
244 if (!isalnum(members[i][j]) && members[i][j] != '-') | |
245 { | |
62 | 246 throw OrthancException("Only alphanumeric and dash characters are allowed in the names of the modalities"); |
0 | 247 } |
248 } | |
249 | |
250 target.insert(members[i]); | |
251 } | |
252 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
253 |
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 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
256 void SetupRegisteredUsers(MongooseServer& httpServer) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
257 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
258 boost::mutex::scoped_lock lock(globalMutex_); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
259 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
260 httpServer.ClearUsers(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
261 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
262 if (!configuration_->isMember("RegisteredUsers")) |
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 return; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
265 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
266 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
267 const Json::Value& users = (*configuration_) ["RegisteredUsers"]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
268 if (users.type() != Json::objectValue) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
269 { |
62 | 270 throw OrthancException("Badly formatted list of users"); |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
271 } |
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 Json::Value::Members usernames = users.getMemberNames(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
274 for (size_t i = 0; i < usernames.size(); i++) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
275 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
276 const std::string& username = usernames[i]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
277 std::string password = users[username].asString(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
278 httpServer.RegisterUser(username.c_str(), password.c_str()); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
279 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
280 } |
394
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
281 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
282 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
283 std::string InterpretStringParameterAsPath(const std::string& parameter) |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
284 { |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
285 boost::mutex::scoped_lock lock(globalMutex_); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
286 return (defaultDirectory_ / parameter).string(); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
287 } |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
288 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
289 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
290 void GetGlobalListOfStringsParameter(std::list<std::string>& target, |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
291 const std::string& key) |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
292 { |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
293 boost::mutex::scoped_lock lock(globalMutex_); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
294 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
295 target.clear(); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
296 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
297 if (!configuration_->isMember(key)) |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
298 { |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
299 return; |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
300 } |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
301 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
302 const Json::Value& lst = (*configuration_) [key]; |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
303 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
304 if (lst.type() != Json::arrayValue) |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
305 { |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
306 throw OrthancException("Badly formatted list of strings"); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
307 } |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
308 |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
309 for (Json::Value::ArrayIndex i = 0; i < lst.size(); i++) |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
310 { |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
311 target.push_back(lst[i].asString()); |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
312 } |
9784f19f7e1b
path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
313 } |
0 | 314 } |