Mercurial > hg > orthanc
annotate 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 |
rev | line source |
---|---|
0 | 1 /** |
50 | 2 * Palanthir - 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 | |
50 | 21 #include "PalanthirInitialization.h" |
0 | 22 |
50 | 23 #include "../Core/PalanthirException.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 | |
50 | 31 namespace Palanthir |
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); | |
48 } | |
49 else | |
50 { | |
19 | 51 try |
52 { | |
50 | 53 #if PALANTHIR_STANDALONE == 0 |
54 boost::filesystem::path p = PALANTHIR_PATH; | |
19 | 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 } |
50 | 62 catch (PalanthirException&) |
19 | 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 { | |
50 | 72 throw PalanthirException("Unable to read the configuration file"); |
0 | 73 } |
74 } | |
75 | |
76 | |
50 | 77 void PalanthirInitialize(const char* configurationFile) |
0 | 78 { |
79 boost::mutex::scoped_lock lock(globalMutex_); | |
80 ReadGlobalConfiguration(configurationFile); | |
81 curl_global_init(CURL_GLOBAL_ALL); | |
82 } | |
83 | |
84 | |
85 | |
50 | 86 void PalanthirFinalize() |
0 | 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 { | |
50 | 153 throw PalanthirException(""); |
0 | 154 } |
155 | |
156 const Json::Value& modalities = (*configuration_) ["DicomModalities"]; | |
157 if (modalities.type() != Json::objectValue || | |
158 !modalities.isMember(name)) | |
159 { | |
50 | 160 throw PalanthirException(""); |
0 | 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 { | |
50 | 171 throw PalanthirException("Badly formatted DICOM modality"); |
0 | 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 { | |
50 | 191 throw PalanthirException("Badly formatted list of DICOM modalities"); |
0 | 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 { | |
50 | 201 throw PalanthirException("Only alphanumeric and dash characters are allowed in the names of the modalities"); |
0 | 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 { |
50 | 225 throw PalanthirException("Badly formatted list of users"); |
25
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 } |