comparison OrthancServer/Plugins/Samples/ServeFolders/Plugin.cpp @ 5572:f0dc99bc811c

removed circular dependency of OrthancPluginsCppWrapper with Orthanc::Logging
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Apr 2024 15:50:01 +0200
parents e02cdf358905
children f7adfb22e20e
comparison
equal deleted inserted replaced
5571:addccb1590d2 5572:f0dc99bc811c
21 21
22 22
23 #define SERVE_FOLDERS_NAME "serve-folders" 23 #define SERVE_FOLDERS_NAME "serve-folders"
24 24
25 #include "../Common/OrthancPluginCppWrapper.h" 25 #include "../Common/OrthancPluginCppWrapper.h"
26 #include "../../../OrthancFramework/Sources/Logging.h"
27 26
28 #include <json/value.h> 27 #include <json/value.h>
29 #include <boost/filesystem.hpp> 28 #include <boost/filesystem.hpp>
30 #include <boost/date_time/posix_time/posix_time.hpp> 29 #include <boost/date_time/posix_time/posix_time.hpp>
31 30
93 { 92 {
94 return found->second; 93 return found->second;
95 } 94 }
96 else 95 else
97 { 96 {
98 LOG(WARNING) << "ServeFolders: Unknown MIME type for extension \"" << extension << "\""; 97 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: Unknown MIME type for extension \"" + extension + "\"");
99 return "application/octet-stream"; 98 return "application/octet-stream";
100 } 99 }
101 } 100 }
102 101
103 102
108 const std::string uri = request->groups[0]; 107 const std::string uri = request->groups[0];
109 108
110 std::map<std::string, std::string>::const_iterator found = folders_.find(uri); 109 std::map<std::string, std::string>::const_iterator found = folders_.find(uri);
111 if (found == folders_.end()) 110 if (found == folders_.end())
112 { 111 {
113 LOG(ERROR) << "Unknown URI in plugin server-folders: " << uri; 112 ORTHANC_PLUGINS_LOG_ERROR("Unknown URI in plugin server-folders: " + uri);
114 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); 113 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404);
115 return false; 114 return false;
116 } 115 }
117 else 116 else
118 { 117 {
264 263
265 static void ConfigureFolders(const Json::Value& folders) 264 static void ConfigureFolders(const Json::Value& folders)
266 { 265 {
267 if (folders.type() != Json::objectValue) 266 if (folders.type() != Json::objectValue)
268 { 267 {
269 LOG(ERROR) << "The list of folders to be served is badly formatted (must be a JSON object)"; 268 ORTHANC_PLUGINS_LOG_ERROR("The list of folders to be served is badly formatted (must be a JSON object)");
270 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 269 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
271 } 270 }
272 271
273 Json::Value::Members members = folders.getMemberNames(); 272 Json::Value::Members members = folders.getMemberNames();
274 273
276 for (Json::Value::Members::const_iterator 275 for (Json::Value::Members::const_iterator
277 it = members.begin(); it != members.end(); ++it) 276 it = members.begin(); it != members.end(); ++it)
278 { 277 {
279 if (folders[*it].type() != Json::stringValue) 278 if (folders[*it].type() != Json::stringValue)
280 { 279 {
281 LOG(ERROR) << "The folder to be served \"" << *it << 280 ORTHANC_PLUGINS_LOG_ERROR("The folder to be served \"" + *it +
282 "\" must be associated with a string value (its mapped URI)"; 281 "\" must be associated with a string value (its mapped URI)");
283 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 282 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
284 } 283 }
285 284
286 std::string baseUri = *it; 285 std::string baseUri = *it;
287 286
298 baseUri.resize(baseUri.size() - 1); 297 baseUri.resize(baseUri.size() - 1);
299 } 298 }
300 299
301 if (baseUri.empty()) 300 if (baseUri.empty())
302 { 301 {
303 LOG(ERROR) << "The URI of a folder to be served cannot be empty"; 302 ORTHANC_PLUGINS_LOG_ERROR("The URI of a folder to be served cannot be empty");
304 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 303 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
305 } 304 }
306 305
307 // Check whether the source folder exists and is indeed a directory 306 // Check whether the source folder exists and is indeed a directory
308 const std::string folder = folders[*it].asString(); 307 const std::string folder = folders[*it].asString();
309 if (!boost::filesystem::is_directory(folder)) 308 if (!boost::filesystem::is_directory(folder))
310 { 309 {
311 LOG(ERROR) << "Trying to serve an inexistent folder: " + folder; 310 ORTHANC_PLUGINS_LOG_ERROR("Trying to serve an inexistent folder: " + folder);
312 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); 311 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
313 } 312 }
314 313
315 folders_[baseUri] = folder; 314 folders_[baseUri] = folder;
316 315
325 324
326 static void ConfigureExtensions(const Json::Value& extensions) 325 static void ConfigureExtensions(const Json::Value& extensions)
327 { 326 {
328 if (extensions.type() != Json::objectValue) 327 if (extensions.type() != Json::objectValue)
329 { 328 {
330 LOG(ERROR) << "The list of extensions is badly formatted (must be a JSON object)"; 329 ORTHANC_PLUGINS_LOG_ERROR("The list of extensions is badly formatted (must be a JSON object)");
331 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 330 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
332 } 331 }
333 332
334 Json::Value::Members members = extensions.getMemberNames(); 333 Json::Value::Members members = extensions.getMemberNames();
335 334
336 for (Json::Value::Members::const_iterator 335 for (Json::Value::Members::const_iterator
337 it = members.begin(); it != members.end(); ++it) 336 it = members.begin(); it != members.end(); ++it)
338 { 337 {
339 if (extensions[*it].type() != Json::stringValue) 338 if (extensions[*it].type() != Json::stringValue)
340 { 339 {
341 LOG(ERROR) << "The file extension \"" << *it << 340 ORTHANC_PLUGINS_LOG_ERROR("The file extension \"" + *it +
342 "\" must be associated with a string value (its MIME type)"; 341 "\" must be associated with a string value (its MIME type)");
343 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 342 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
344 } 343 }
345 344
346 const std::string& mime = extensions[*it].asString(); 345 const std::string& mime = extensions[*it].asString();
347 346
355 354
356 extensions_[name] = mime; 355 extensions_[name] = mime;
357 356
358 if (mime.empty()) 357 if (mime.empty())
359 { 358 {
360 LOG(WARNING) << "ServeFolders: Removing MIME type for file extension \"." << name << "\""; 359 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: Removing MIME type for file extension \"." +
360 name + "\"");
361 } 361 }
362 else 362 else
363 { 363 {
364 LOG(WARNING) << "ServeFolders: Associating file extension \"." << name << "\" with MIME type \"" << mime << "\""; 364 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: Associating file extension \"." + name +
365 "\" with MIME type \"" + mime + "\"");
365 } 366 }
366 } 367 }
367 } 368 }
368 369
369 370
389 bool tmp; 390 bool tmp;
390 391
391 if (configuration.LookupBooleanValue(tmp, "AllowCache")) 392 if (configuration.LookupBooleanValue(tmp, "AllowCache"))
392 { 393 {
393 allowCache_ = tmp; 394 allowCache_ = tmp;
394 LOG(WARNING) << "ServeFolders: Requesting the HTTP client to " << (tmp ? "enable" : "disable") << " its caching mechanism"; 395 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: Requesting the HTTP client to " +
396 std::string(tmp ? "enable" : "disable") +
397 " its caching mechanism");
395 } 398 }
396 399
397 if (configuration.LookupBooleanValue(tmp, "GenerateETag")) 400 if (configuration.LookupBooleanValue(tmp, "GenerateETag"))
398 { 401 {
399 generateETag_ = tmp; 402 generateETag_ = tmp;
400 LOG(WARNING) << "ServeFolders: The computation of an ETag for the served resources is " << (tmp ? "enabled" : "disabled"); 403 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: The computation of an ETag for the served resources is " +
404 std::string(tmp ? "enabled" : "disabled"));
401 } 405 }
402 406
403 OrthancPlugins::OrthancConfiguration extensions; 407 OrthancPlugins::OrthancConfiguration extensions;
404 configuration.GetSection(extensions, "Extensions"); 408 configuration.GetSection(extensions, "Extensions");
405 ConfigureExtensions(extensions.GetJson()); 409 ConfigureExtensions(extensions.GetJson());
406 } 410 }
407 411
408 if (folders_.empty()) 412 if (folders_.empty())
409 { 413 {
410 LOG(WARNING) << "ServeFolders: Empty configuration file: No additional folder will be served!"; 414 ORTHANC_PLUGINS_LOG_WARNING("ServeFolders: Empty configuration file: No additional folder will be served!");
411 } 415 }
412 } 416 }
413 417
414 418
415 extern "C" 419 extern "C"
416 { 420 {
417 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) 421 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
418 { 422 {
419 OrthancPlugins::SetGlobalContext(context, SERVE_FOLDERS_NAME); 423 OrthancPlugins::SetGlobalContext(context, SERVE_FOLDERS_NAME);
420 Orthanc::Logging::InitializePluginContext(context, SERVE_FOLDERS_NAME);
421 424
422 /* Check the version of the Orthanc core */ 425 /* Check the version of the Orthanc core */
423 if (OrthancPluginCheckVersion(context) == 0) 426 if (OrthancPluginCheckVersion(context) == 0)
424 { 427 {
425 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, 428 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
437 { 440 {
438 ReadConfiguration(); 441 ReadConfiguration();
439 } 442 }
440 catch (OrthancPlugins::PluginException& e) 443 catch (OrthancPlugins::PluginException& e)
441 { 444 {
442 LOG(ERROR) << "Error while initializing the ServeFolders plugin: " << e.What(context); 445 ORTHANC_PLUGINS_LOG_ERROR("Error while initializing the ServeFolders plugin: " +
446 std::string(e.What(context)));
443 } 447 }
444 448
445 return 0; 449 return 0;
446 } 450 }
447 451