Mercurial > hg > orthanc
annotate PalantirServer/PalantirInitialization.cpp @ 32:0c3e317f35e8
fix for linux32
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 Aug 2012 09:35:38 +0200 |
parents | dd1489098265 |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * Palantir - 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 "PalantirInitialization.h" | |
22 | |
23 #include "../Core/PalantirException.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 Palantir | |
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 { | |
19 | 51 try |
52 { | |
53 #if PALANTIR_STANDALONE == 0 | |
54 boost::filesystem::path p = PALANTIR_PATH; | |
55 p /= "Resources"; | |
56 p /= CONFIGURATION_FILE; | |
57 Toolbox::ReadFile(content, p.string()); | |
0 | 58 #else |
19 | 59 Toolbox::ReadFile(content, CONFIGURATION_FILE); |
0 | 60 #endif |
19 | 61 } |
62 catch (PalantirException&) | |
63 { | |
64 // No configuration file found, give up with empty configuration | |
65 return; | |
66 } | |
0 | 67 } |
68 | |
69 Json::Reader reader; | |
70 if (!reader.parse(content, *configuration_)) | |
71 { | |
72 throw PalantirException("Unable to read the configuration file"); | |
73 } | |
74 } | |
75 | |
76 | |
77 void PalantirInitialize(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 PalantirFinalize() | |
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 | |
23 | 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 | |
0 | 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 PalantirException(""); | |
154 } | |
155 | |
156 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
157 if (modalities.type() != Json::objectValue || | |
158 !modalities.isMember(name)) | |
159 { | |
160 throw PalantirException(""); | |
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 PalantirException("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 PalantirException("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 PalantirException("Only alphanumeric and dash characters are allowed in the names of the modalities"); | |
202 } | |
203 } | |
204 | |
205 target.insert(members[i]); | |
206 } | |
207 } | |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
208 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
209 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
210 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
211 void SetupRegisteredUsers(MongooseServer& httpServer) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
212 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
213 boost::mutex::scoped_lock lock(globalMutex_); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
214 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
215 httpServer.ClearUsers(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
216 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
217 if (!configuration_->isMember("RegisteredUsers")) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
218 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
219 return; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
220 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
221 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
222 const Json::Value& users = (*configuration_) ["RegisteredUsers"]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
223 if (users.type() != Json::objectValue) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
224 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
225 throw PalantirException("Badly formatted list of users"); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
226 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
227 |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
228 Json::Value::Members usernames = users.getMemberNames(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
229 for (size_t i = 0; i < usernames.size(); i++) |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
230 { |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
231 const std::string& username = usernames[i]; |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
232 std::string password = users[username].asString(); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
233 httpServer.RegisterUser(username.c_str(), password.c_str()); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
234 } |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
235 } |
0 | 236 } |