comparison Applications/StoneWebViewer/Plugin/Plugin.cpp @ 1727:c6139e08132b

the plugin sets the default value for all the configuration options
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 01 Dec 2020 19:16:19 +0100
parents aec45e0b2528
children ef2f56c0311c
comparison
equal deleted inserted replaced
1726:0257339b0884 1727:c6139e08132b
26 #include <Logging.h> 26 #include <Logging.h>
27 #include <SystemToolbox.h> 27 #include <SystemToolbox.h>
28 #include <Toolbox.h> 28 #include <Toolbox.h>
29 29
30 30
31 static const std::string STONE_WEB_VIEWER_ROOT = "/stone-webviewer"; 31 static const std::string STONE_WEB_VIEWER_ROOT = "/stone-webviewer"; // (*)
32 static const char* CONFIG_SECTION = "StoneWebViewer";
32 33
33 34
34 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, 35 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
35 OrthancPluginResourceType resourceType, 36 OrthancPluginResourceType resourceType,
36 const char* resourceId) 37 const char* resourceId)
162 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime); 163 OrthancPluginAnswerBuffer(context, output, resource, s.size(), mime);
163 } 164 }
164 } 165 }
165 166
166 167
168 static void GetDefaultConfiguration(Json::Value& target)
169 {
170 std::string s;
171 Orthanc::EmbeddedResources::GetDirectoryResource(
172 s, Orthanc::EmbeddedResources::WEB_APPLICATION, "/configuration.json");
173
174 Json::Reader reader;
175 Json::Value full;
176 if (!reader.parse(s, full) ||
177 full.type() != Json::objectValue ||
178 !full.isMember(CONFIG_SECTION) ||
179 full[CONFIG_SECTION].type() != Json::objectValue)
180 {
181 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat,
182 "Cannot read the default configuration");
183 }
184
185 Orthanc::Toolbox::CopyJsonWithoutComments(target, full);
186 }
187
188
167 void ServeConfiguration(OrthancPluginRestOutput* output, 189 void ServeConfiguration(OrthancPluginRestOutput* output,
168 const char* url, 190 const char* url,
169 const OrthancPluginHttpRequest* request) 191 const OrthancPluginHttpRequest* request)
170 { 192 {
171 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); 193 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
174 { 196 {
175 OrthancPluginSendMethodNotAllowed(context, output, "GET"); 197 OrthancPluginSendMethodNotAllowed(context, output, "GET");
176 } 198 }
177 else 199 else
178 { 200 {
179 static const char* CONFIG_SECTION = "StoneWebViewer";
180 static const char* ORTHANC_API_ROOT = "OrthancApiRoot"; 201 static const char* ORTHANC_API_ROOT = "OrthancApiRoot";
181 static const char* DICOM_WEB_ROOT = "DicomWebRoot"; 202 static const char* DICOM_WEB_ROOT = "DicomWebRoot";
182 203 static const char* EXPECTED_MESSAGE_ORIGIN = "ExpectedMessageOrigin";
204
205 Json::Value defaultConfig;
206 GetDefaultConfiguration(defaultConfig);
207 defaultConfig[CONFIG_SECTION][EXPECTED_MESSAGE_ORIGIN] = ""; // By default, disable messages for security
208
183 Json::Value config = Json::objectValue; 209 Json::Value config = Json::objectValue;
184 210
185 OrthancPlugins::OrthancConfiguration orthanc; 211 OrthancPlugins::OrthancConfiguration orthanc;
186 if (orthanc.IsSection(CONFIG_SECTION)) 212 if (orthanc.IsSection(CONFIG_SECTION))
187 { 213 {
192 else 218 else
193 { 219 {
194 LOG(WARNING) << "The Orthanc configuration file doesn't contain a section \"" 220 LOG(WARNING) << "The Orthanc configuration file doesn't contain a section \""
195 << CONFIG_SECTION << "\" to configure the Stone Web viewer: " 221 << CONFIG_SECTION << "\" to configure the Stone Web viewer: "
196 << "Will use default settings"; 222 << "Will use default settings";
197 223 config = defaultConfig;
198 std::string s; 224 }
199 Orthanc::EmbeddedResources::GetDirectoryResource( 225
200 s, Orthanc::EmbeddedResources::WEB_APPLICATION, "/configuration.json"); 226 assert(config[CONFIG_SECTION].type() == Json::objectValue);
201 227
202 Json::Reader reader; 228 // Assume that the Stone Web viewer is mapped at "/stone-webviewer" in the REST API (*)
203 if (!reader.parse(s, config) || 229 config[CONFIG_SECTION][ORTHANC_API_ROOT] = "..";
204 config.type() != Json::objectValue) 230
231 if (!config[CONFIG_SECTION].isMember(DICOM_WEB_ROOT))
232 {
233 config[CONFIG_SECTION][DICOM_WEB_ROOT] = "../dicom-web"; // (*)
234 }
235
236
237 // Copy the default values for the missing options
238 std::vector<std::string> defaultOptions = defaultConfig[CONFIG_SECTION].getMemberNames();
239
240 for (size_t i = 0; i < defaultOptions.size(); i++)
241 {
242 if (!config[CONFIG_SECTION].isMember(defaultOptions[i]))
205 { 243 {
206 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, 244 config[CONFIG_SECTION][defaultOptions[i]] = defaultConfig[CONFIG_SECTION][defaultOptions[i]];
207 "Cannot read the default configuration");
208 } 245 }
209 }
210
211 assert(config[CONFIG_SECTION].type() == Json::objectValue);
212
213 // Assume that the Stone Web viewer is mapped at "/stone-webviewer" in the REST API
214 config[CONFIG_SECTION][ORTHANC_API_ROOT] = "..";
215
216 if (!config[CONFIG_SECTION].isMember(DICOM_WEB_ROOT))
217 {
218 config[CONFIG_SECTION][DICOM_WEB_ROOT] = "../dicom-web";
219 } 246 }
220 247
221 const std::string s = config.toStyledString(); 248 const std::string s = config.toStyledString();
222 OrthancPluginAnswerBuffer(context, output, s.c_str(), s.size(), "application/json"); 249 OrthancPluginAnswerBuffer(context, output, s.c_str(), s.size(), "application/json");
223 } 250 }