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