Mercurial > hg > orthanc
comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 2176:fead5549aaa7
introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Nov 2016 12:07:36 +0100 |
parents | d15de5685ad8 |
children | 11420238f337 |
comparison
equal
deleted
inserted
replaced
2175:bed8e7ad8bab | 2176:fead5549aaa7 |
---|---|
24 #include <json/value.h> | 24 #include <json/value.h> |
25 #include <boost/filesystem.hpp> | 25 #include <boost/filesystem.hpp> |
26 #include <boost/date_time/posix_time/posix_time.hpp> | 26 #include <boost/date_time/posix_time/posix_time.hpp> |
27 | 27 |
28 | 28 |
29 #if HAS_ORTHANC_EXCEPTION == 1 | |
30 # error The macro HAS_ORTHANC_EXCEPTION must be set to 0 to compile this plugin | |
31 #endif | |
32 | |
33 | |
34 | |
29 static OrthancPluginContext* context_ = NULL; | 35 static OrthancPluginContext* context_ = NULL; |
30 static std::map<std::string, std::string> extensions_; | 36 static std::map<std::string, std::string> extensions_; |
31 static std::map<std::string, std::string> folders_; | 37 static std::map<std::string, std::string> folders_; |
32 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; | 38 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; |
33 static bool allowCache_ = false; | 39 static bool allowCache_ = false; |
192 | 198 |
193 try | 199 try |
194 { | 200 { |
195 content.ReadFile(path); | 201 content.ReadFile(path); |
196 } | 202 } |
197 catch (OrthancPlugins::PluginException&) | 203 catch (...) |
198 { | 204 { |
199 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile); | 205 OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile); |
200 } | 206 } |
201 | 207 |
202 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); | 208 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); |
203 std::string t = boost::posix_time::to_iso_string(lastModification); | 209 std::string t = boost::posix_time::to_iso_string(lastModification); |
204 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); | 210 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); |
247 static void ConfigureFolders(const Json::Value& folders) | 253 static void ConfigureFolders(const Json::Value& folders) |
248 { | 254 { |
249 if (folders.type() != Json::objectValue) | 255 if (folders.type() != Json::objectValue) |
250 { | 256 { |
251 OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)"); | 257 OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)"); |
252 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 258 OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat); |
253 } | 259 } |
254 | 260 |
255 Json::Value::Members members = folders.getMemberNames(); | 261 Json::Value::Members members = folders.getMemberNames(); |
256 | 262 |
257 // Register the callback for each base URI | 263 // Register the callback for each base URI |
260 { | 266 { |
261 if (folders[*it].type() != Json::stringValue) | 267 if (folders[*it].type() != Json::stringValue) |
262 { | 268 { |
263 OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + | 269 OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + |
264 "\" must be associated with a string value (its mapped URI)"); | 270 "\" must be associated with a string value (its mapped URI)"); |
265 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 271 OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat); |
266 } | 272 } |
267 | 273 |
268 std::string baseUri = *it; | 274 std::string baseUri = *it; |
269 | 275 |
270 // Remove the heading and trailing slashes in the root URI, if any | 276 // Remove the heading and trailing slashes in the root URI, if any |
281 } | 287 } |
282 | 288 |
283 if (baseUri.empty()) | 289 if (baseUri.empty()) |
284 { | 290 { |
285 OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty"); | 291 OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty"); |
286 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 292 OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat); |
287 } | 293 } |
288 | 294 |
289 // Check whether the source folder exists and is indeed a directory | 295 // Check whether the source folder exists and is indeed a directory |
290 const std::string folder = folders[*it].asString(); | 296 const std::string folder = folders[*it].asString(); |
291 if (!boost::filesystem::is_directory(folder)) | 297 if (!boost::filesystem::is_directory(folder)) |
292 { | 298 { |
293 OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder); | 299 OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder); |
294 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile); | 300 OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile); |
295 } | 301 } |
296 | 302 |
297 folders_[baseUri] = folder; | 303 folders_[baseUri] = folder; |
298 | 304 |
299 // Register the callback to serve the folder | 305 // Register the callback to serve the folder |
308 static void ConfigureExtensions(const Json::Value& extensions) | 314 static void ConfigureExtensions(const Json::Value& extensions) |
309 { | 315 { |
310 if (extensions.type() != Json::objectValue) | 316 if (extensions.type() != Json::objectValue) |
311 { | 317 { |
312 OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)"); | 318 OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)"); |
313 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 319 OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat); |
314 } | 320 } |
315 | 321 |
316 Json::Value::Members members = extensions.getMemberNames(); | 322 Json::Value::Members members = extensions.getMemberNames(); |
317 | 323 |
318 for (Json::Value::Members::const_iterator | 324 for (Json::Value::Members::const_iterator |
320 { | 326 { |
321 if (extensions[*it].type() != Json::stringValue) | 327 if (extensions[*it].type() != Json::stringValue) |
322 { | 328 { |
323 OrthancPlugins::LogError(context_, "The file extension \"" + *it + | 329 OrthancPlugins::LogError(context_, "The file extension \"" + *it + |
324 "\" must be associated with a string value (its MIME type)"); | 330 "\" must be associated with a string value (its MIME type)"); |
325 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 331 OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat); |
326 } | 332 } |
327 | 333 |
328 const std::string& mime = extensions[*it].asString(); | 334 const std::string& mime = extensions[*it].asString(); |
329 | 335 |
330 std::string name = *it; | 336 std::string name = *it; |
426 { | 432 { |
427 ReadConfiguration(); | 433 ReadConfiguration(); |
428 } | 434 } |
429 catch (OrthancPlugins::PluginException& e) | 435 catch (OrthancPlugins::PluginException& e) |
430 { | 436 { |
431 OrthancPlugins::LogError(context, "Error while initializing the ServeFolders plugin: " + | 437 OrthancPlugins::LogError(context_, "Error while initializing the ServeFolders plugin: " + |
432 std::string(e.GetErrorDescription(context))); | 438 std::string(e.What(context_))); |
433 return -1; | |
434 } | 439 } |
435 | 440 |
436 return 0; | 441 return 0; |
437 } | 442 } |
438 | 443 |