comparison OrthancServer/OrthancInitialization.cpp @ 58:6da7fc87efaa orthanc-renaming

renaming cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 16 Sep 2012 09:24:13 +0200
parents OrthancServer/PalanthirInitialization.cpp@4bc019d2f969
children a70bb32802ae
comparison
equal deleted inserted replaced
57:4bc019d2f969 58:6da7fc87efaa
1 /**
2 * Palanthir - A Lightweight, RESTful DICOM Store
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
21 #include "PalanthirInitialization.h"
22
23 #include "../Core/PalanthirException.h"
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
31 namespace Palanthir
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);
48 }
49 else
50 {
51 try
52 {
53 #if PALANTHIR_STANDALONE == 0
54 boost::filesystem::path p = PALANTHIR_PATH;
55 p /= "Resources";
56 p /= CONFIGURATION_FILE;
57 Toolbox::ReadFile(content, p.string());
58 #else
59 Toolbox::ReadFile(content, CONFIGURATION_FILE);
60 #endif
61 }
62 catch (PalanthirException&)
63 {
64 // No configuration file found, give up with empty configuration
65 return;
66 }
67 }
68
69 Json::Reader reader;
70 if (!reader.parse(content, *configuration_))
71 {
72 throw PalanthirException("Unable to read the configuration file");
73 }
74 }
75
76
77 void PalanthirInitialize(const char* configurationFile)
78 {
79 boost::mutex::scoped_lock lock(globalMutex_);
80 ReadGlobalConfiguration(configurationFile);
81 curl_global_init(CURL_GLOBAL_ALL);
82 }
83
84
85
86 void PalanthirFinalize()
87 {
88 boost::mutex::scoped_lock lock(globalMutex_);
89 curl_global_cleanup();
90 configuration_.reset(NULL);
91 }
92
93
94
95 std::string GetGlobalStringParameter(const std::string& parameter,
96 const std::string& defaultValue)
97 {
98 boost::mutex::scoped_lock lock(globalMutex_);
99
100 if (configuration_->isMember(parameter))
101 {
102 return (*configuration_) [parameter].asString();
103 }
104 else
105 {
106 return defaultValue;
107 }
108 }
109
110
111 int GetGlobalIntegerParameter(const std::string& parameter,
112 int defaultValue)
113 {
114 boost::mutex::scoped_lock lock(globalMutex_);
115
116 if (configuration_->isMember(parameter))
117 {
118 return (*configuration_) [parameter].asInt();
119 }
120 else
121 {
122 return defaultValue;
123 }
124 }
125
126 bool GetGlobalBoolParameter(const std::string& parameter,
127 bool defaultValue)
128 {
129 boost::mutex::scoped_lock lock(globalMutex_);
130
131 if (configuration_->isMember(parameter))
132 {
133 return (*configuration_) [parameter].asBool();
134 }
135 else
136 {
137 return defaultValue;
138 }
139 }
140
141
142
143
144 void GetDicomModality(const std::string& name,
145 std::string& aet,
146 std::string& address,
147 int& port)
148 {
149 boost::mutex::scoped_lock lock(globalMutex_);
150
151 if (!configuration_->isMember("DicomModalities"))
152 {
153 throw PalanthirException("");
154 }
155
156 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
157 if (modalities.type() != Json::objectValue ||
158 !modalities.isMember(name))
159 {
160 throw PalanthirException("");
161 }
162
163 try
164 {
165 aet = modalities[name].get(0u, "").asString();
166 address = modalities[name].get(1u, "").asString();
167 port = modalities[name].get(2u, "").asInt();
168 }
169 catch (...)
170 {
171 throw PalanthirException("Badly formatted DICOM modality");
172 }
173 }
174
175
176
177 void GetListOfDicomModalities(std::set<std::string>& target)
178 {
179 boost::mutex::scoped_lock lock(globalMutex_);
180
181 target.clear();
182
183 if (!configuration_->isMember("DicomModalities"))
184 {
185 return;
186 }
187
188 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
189 if (modalities.type() != Json::objectValue)
190 {
191 throw PalanthirException("Badly formatted list of DICOM modalities");
192 }
193
194 Json::Value::Members members = modalities.getMemberNames();
195 for (size_t i = 0; i < members.size(); i++)
196 {
197 for (size_t j = 0; j < members[i].size(); j++)
198 {
199 if (!isalnum(members[i][j]) && members[i][j] != '-')
200 {
201 throw PalanthirException("Only alphanumeric and dash characters are allowed in the names of the modalities");
202 }
203 }
204
205 target.insert(members[i]);
206 }
207 }
208
209
210
211 void SetupRegisteredUsers(MongooseServer& httpServer)
212 {
213 boost::mutex::scoped_lock lock(globalMutex_);
214
215 httpServer.ClearUsers();
216
217 if (!configuration_->isMember("RegisteredUsers"))
218 {
219 return;
220 }
221
222 const Json::Value& users = (*configuration_) ["RegisteredUsers"];
223 if (users.type() != Json::objectValue)
224 {
225 throw PalanthirException("Badly formatted list of users");
226 }
227
228 Json::Value::Members usernames = users.getMemberNames();
229 for (size_t i = 0; i < usernames.size(); i++)
230 {
231 const std::string& username = usernames[i];
232 std::string password = users[username].asString();
233 httpServer.RegisterUser(username.c_str(), password.c_str());
234 }
235 }
236 }