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