comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 2234:a78d15509a1c

cleaner separation of PluginException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Dec 2016 14:35:35 +0100
parents 395522e46b2b
children a3a65de1840f
comparison
equal deleted inserted replaced
2232:3dd44baebc36 2234:a78d15509a1c
202 { 202 {
203 content.ReadFile(path); 203 content.ReadFile(path);
204 } 204 }
205 catch (...) 205 catch (...)
206 { 206 {
207 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile); 207 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
208 } 208 }
209 209
210 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); 210 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path));
211 std::string t = boost::posix_time::to_iso_string(lastModification); 211 std::string t = boost::posix_time::to_iso_string(lastModification);
212 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); 212 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str());
255 static void ConfigureFolders(const Json::Value& folders) 255 static void ConfigureFolders(const Json::Value& folders)
256 { 256 {
257 if (folders.type() != Json::objectValue) 257 if (folders.type() != Json::objectValue)
258 { 258 {
259 OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)"); 259 OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)");
260 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 260 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
261 } 261 }
262 262
263 Json::Value::Members members = folders.getMemberNames(); 263 Json::Value::Members members = folders.getMemberNames();
264 264
265 // Register the callback for each base URI 265 // Register the callback for each base URI
268 { 268 {
269 if (folders[*it].type() != Json::stringValue) 269 if (folders[*it].type() != Json::stringValue)
270 { 270 {
271 OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + 271 OrthancPlugins::LogError(context_, "The folder to be served \"" + *it +
272 "\" must be associated with a string value (its mapped URI)"); 272 "\" must be associated with a string value (its mapped URI)");
273 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 273 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
274 } 274 }
275 275
276 std::string baseUri = *it; 276 std::string baseUri = *it;
277 277
278 // Remove the heading and trailing slashes in the root URI, if any 278 // Remove the heading and trailing slashes in the root URI, if any
289 } 289 }
290 290
291 if (baseUri.empty()) 291 if (baseUri.empty())
292 { 292 {
293 OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty"); 293 OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty");
294 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 294 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
295 } 295 }
296 296
297 // Check whether the source folder exists and is indeed a directory 297 // Check whether the source folder exists and is indeed a directory
298 const std::string folder = folders[*it].asString(); 298 const std::string folder = folders[*it].asString();
299 if (!boost::filesystem::is_directory(folder)) 299 if (!boost::filesystem::is_directory(folder))
300 { 300 {
301 OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder); 301 OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder);
302 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile); 302 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile);
303 } 303 }
304 304
305 folders_[baseUri] = folder; 305 folders_[baseUri] = folder;
306 306
307 // Register the callback to serve the folder 307 // Register the callback to serve the folder
316 static void ConfigureExtensions(const Json::Value& extensions) 316 static void ConfigureExtensions(const Json::Value& extensions)
317 { 317 {
318 if (extensions.type() != Json::objectValue) 318 if (extensions.type() != Json::objectValue)
319 { 319 {
320 OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)"); 320 OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)");
321 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 321 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
322 } 322 }
323 323
324 Json::Value::Members members = extensions.getMemberNames(); 324 Json::Value::Members members = extensions.getMemberNames();
325 325
326 for (Json::Value::Members::const_iterator 326 for (Json::Value::Members::const_iterator
328 { 328 {
329 if (extensions[*it].type() != Json::stringValue) 329 if (extensions[*it].type() != Json::stringValue)
330 { 330 {
331 OrthancPlugins::LogError(context_, "The file extension \"" + *it + 331 OrthancPlugins::LogError(context_, "The file extension \"" + *it +
332 "\" must be associated with a string value (its MIME type)"); 332 "\" must be associated with a string value (its MIME type)");
333 ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat); 333 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
334 } 334 }
335 335
336 const std::string& mime = extensions[*it].asString(); 336 const std::string& mime = extensions[*it].asString();
337 337
338 std::string name = *it; 338 std::string name = *it;