Mercurial > hg > orthanc-transfers
comparison Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 40:1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 12 Jul 2022 12:43:43 +0200 |
parents | 81262707d68e |
children | f4e828607f02 |
comparison
equal
deleted
inserted
replaced
39:f627882388dc | 40:1256194e1c08 |
---|---|
1 /** | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
4 * Department, University Hospital of Liege, Belgium | 4 * Department, University Hospital of Liege, Belgium |
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium | 5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
6 * | 7 * |
7 * This program is free software: you can redistribute it and/or | 8 * This program is free software: you can redistribute it and/or |
8 * modify it under the terms of the GNU General Public License as | 9 * modify it under the terms of the GNU General Public License as |
9 * published by the Free Software Foundation, either version 3 of the | 10 * published by the Free Software Foundation, either version 3 of the |
10 * License, or (at your option) any later version. | 11 * License, or (at your option) any later version. |
247 { | 248 { |
248 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str())); | 249 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str())); |
249 } | 250 } |
250 } | 251 } |
251 | 252 |
253 // helper class to convert std::map of headers to the plugin SDK C structure | |
254 class PluginHttpHeaders | |
255 { | |
256 std::vector<const char*> headersKeys_; | |
257 std::vector<const char*> headersValues_; | |
258 public: | |
259 | |
260 PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders) | |
261 { | |
262 for (std::map<std::string, std::string>::const_iterator | |
263 it = httpHeaders.begin(); it != httpHeaders.end(); it++) | |
264 { | |
265 headersKeys_.push_back(it->first.c_str()); | |
266 headersValues_.push_back(it->second.c_str()); | |
267 } | |
268 } | |
269 | |
270 const char* const* GetKeys() | |
271 { | |
272 return (headersKeys_.empty() ? NULL : &headersKeys_[0]); | |
273 } | |
274 | |
275 const char* const* GetValues() | |
276 { | |
277 return (headersValues_.empty() ? NULL : &headersValues_[0]); | |
278 } | |
279 | |
280 uint32_t GetSize() | |
281 { | |
282 return static_cast<uint32_t>(headersKeys_.size()); | |
283 } | |
284 }; | |
285 | |
252 bool MemoryBuffer::RestApiGet(const std::string& uri, | 286 bool MemoryBuffer::RestApiGet(const std::string& uri, |
253 const std::map<std::string, std::string>& httpHeaders, | 287 const std::map<std::string, std::string>& httpHeaders, |
254 bool applyPlugins) | 288 bool applyPlugins) |
255 { | 289 { |
256 Clear(); | 290 Clear(); |
257 | 291 |
258 std::vector<const char*> headersKeys; | 292 PluginHttpHeaders headers(httpHeaders); |
259 std::vector<const char*> headersValues; | |
260 | |
261 for (std::map<std::string, std::string>::const_iterator | |
262 it = httpHeaders.begin(); it != httpHeaders.end(); it++) | |
263 { | |
264 headersKeys.push_back(it->first.c_str()); | |
265 headersValues.push_back(it->second.c_str()); | |
266 } | |
267 | 293 |
268 return CheckHttp(OrthancPluginRestApiGet2( | 294 return CheckHttp(OrthancPluginRestApiGet2( |
269 GetGlobalContext(), &buffer_, uri.c_str(), httpHeaders.size(), | 295 GetGlobalContext(), &buffer_, uri.c_str(), |
270 (headersKeys.empty() ? NULL : &headersKeys[0]), | 296 headers.GetSize(), |
271 (headersValues.empty() ? NULL : &headersValues[0]), applyPlugins)); | 297 headers.GetKeys(), |
298 headers.GetValues(), applyPlugins)); | |
272 } | 299 } |
273 | 300 |
274 bool MemoryBuffer::RestApiPost(const std::string& uri, | 301 bool MemoryBuffer::RestApiPost(const std::string& uri, |
275 const void* body, | 302 const void* body, |
276 size_t bodySize, | 303 size_t bodySize, |
289 { | 316 { |
290 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | 317 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); |
291 } | 318 } |
292 } | 319 } |
293 | 320 |
321 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1 | |
322 | |
323 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
324 const void* body, | |
325 size_t bodySize, | |
326 const std::map<std::string, std::string>& httpHeaders, | |
327 bool applyPlugins) | |
328 { | |
329 MemoryBuffer answerHeaders; | |
330 uint16_t httpStatus; | |
331 | |
332 PluginHttpHeaders headers(httpHeaders); | |
333 | |
334 return CheckHttp(OrthancPluginCallRestApi(GetGlobalContext(), | |
335 &buffer_, | |
336 *answerHeaders, | |
337 &httpStatus, | |
338 OrthancPluginHttpMethod_Post, | |
339 uri.c_str(), | |
340 headers.GetSize(), headers.GetKeys(), headers.GetValues(), | |
341 body, bodySize, | |
342 applyPlugins)); | |
343 } | |
344 | |
345 | |
346 bool MemoryBuffer::RestApiPost(const std::string& uri, | |
347 const Json::Value& body, | |
348 const std::map<std::string, std::string>& httpHeaders, | |
349 bool applyPlugins) | |
350 { | |
351 std::string s; | |
352 WriteFastJson(s, body); | |
353 return RestApiPost(uri, s.c_str(), s.size(), httpHeaders, applyPlugins); | |
354 } | |
355 #endif | |
294 | 356 |
295 bool MemoryBuffer::RestApiPut(const std::string& uri, | 357 bool MemoryBuffer::RestApiPut(const std::string& uri, |
296 const void* body, | 358 const void* body, |
297 size_t bodySize, | 359 size_t bodySize, |
298 bool applyPlugins) | 360 bool applyPlugins) |
508 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | 570 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
509 } | 571 } |
510 } | 572 } |
511 | 573 |
512 | 574 |
575 void OrthancString::ToJsonWithoutComments(Json::Value& target) const | |
576 { | |
577 if (str_ == NULL) | |
578 { | |
579 LogError("Cannot convert an empty memory buffer to JSON"); | |
580 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
581 } | |
582 | |
583 if (!ReadJsonWithoutComments(target, str_)) | |
584 { | |
585 LogError("Cannot convert some memory buffer to JSON"); | |
586 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
587 } | |
588 } | |
589 | |
590 | |
513 void MemoryBuffer::DicomToJson(Json::Value& target, | 591 void MemoryBuffer::DicomToJson(Json::Value& target, |
514 OrthancPluginDicomToJsonFormat format, | 592 OrthancPluginDicomToJsonFormat format, |
515 OrthancPluginDicomToJsonFlags flags, | 593 OrthancPluginDicomToJsonFlags flags, |
516 uint32_t maxStringLength) | 594 uint32_t maxStringLength) |
517 { | 595 { |
537 const std::string& body, | 615 const std::string& body, |
538 const std::string& username, | 616 const std::string& username, |
539 const std::string& password) | 617 const std::string& password) |
540 { | 618 { |
541 Clear(); | 619 Clear(); |
620 | |
621 if (body.size() > 0xffffffffu) | |
622 { | |
623 LogError("Cannot handle body size > 4GB"); | |
624 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
625 } | |
626 | |
542 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), | 627 return CheckHttp(OrthancPluginHttpPost(GetGlobalContext(), &buffer_, url.c_str(), |
543 body.c_str(), body.size(), | 628 body.c_str(), body.size(), |
544 username.empty() ? NULL : username.c_str(), | 629 username.empty() ? NULL : username.c_str(), |
545 password.empty() ? NULL : password.c_str())); | 630 password.empty() ? NULL : password.c_str())); |
546 } | 631 } |
550 const std::string& body, | 635 const std::string& body, |
551 const std::string& username, | 636 const std::string& username, |
552 const std::string& password) | 637 const std::string& password) |
553 { | 638 { |
554 Clear(); | 639 Clear(); |
640 | |
641 if (body.size() > 0xffffffffu) | |
642 { | |
643 LogError("Cannot handle body size > 4GB"); | |
644 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
645 } | |
646 | |
555 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), | 647 return CheckHttp(OrthancPluginHttpPut(GetGlobalContext(), &buffer_, url.c_str(), |
556 body.empty() ? NULL : body.c_str(), | 648 body.empty() ? NULL : body.c_str(), |
557 body.size(), | 649 body.size(), |
558 username.empty() ? NULL : username.c_str(), | 650 username.empty() ? NULL : username.c_str(), |
559 password.empty() ? NULL : password.c_str())); | 651 password.empty() ? NULL : password.c_str())); |
628 { | 720 { |
629 LogError("Cannot access the Orthanc configuration"); | 721 LogError("Cannot access the Orthanc configuration"); |
630 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 722 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
631 } | 723 } |
632 | 724 |
633 str.ToJson(configuration_); | 725 str.ToJsonWithoutComments(configuration_); |
634 | 726 |
635 if (configuration_.type() != Json::objectValue) | 727 if (configuration_.type() != Json::objectValue) |
636 { | 728 { |
637 LogError("Unable to read the Orthanc configuration"); | 729 LogError("Unable to read the Orthanc configuration"); |
638 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 730 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1424 } | 1516 } |
1425 return true; | 1517 return true; |
1426 } | 1518 } |
1427 } | 1519 } |
1428 | 1520 |
1521 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1 | |
1522 bool RestApiPost(Json::Value& result, | |
1523 const std::string& uri, | |
1524 const Json::Value& body, | |
1525 const std::map<std::string, std::string>& httpHeaders, | |
1526 bool applyPlugins) | |
1527 { | |
1528 MemoryBuffer answer; | |
1529 | |
1530 if (!answer.RestApiPost(uri, body, httpHeaders, applyPlugins)) | |
1531 { | |
1532 return false; | |
1533 } | |
1534 else | |
1535 { | |
1536 if (!answer.IsEmpty()) | |
1537 { | |
1538 answer.ToJson(result); | |
1539 } | |
1540 return true; | |
1541 } | |
1542 } | |
1543 #endif | |
1544 | |
1429 | 1545 |
1430 bool RestApiPost(Json::Value& result, | 1546 bool RestApiPost(Json::Value& result, |
1431 const std::string& uri, | 1547 const std::string& uri, |
1432 const Json::Value& body, | 1548 const Json::Value& body, |
1433 bool applyPlugins) | 1549 bool applyPlugins) |
1513 boost::lexical_cast<std::string>(minor) + "." + | 1629 boost::lexical_cast<std::string>(minor) + "." + |
1514 boost::lexical_cast<std::string>(revision) + | 1630 boost::lexical_cast<std::string>(revision) + |
1515 " is required)"); | 1631 " is required)"); |
1516 } | 1632 } |
1517 | 1633 |
1518 | 1634 bool CheckMinimalVersion(const char* version, |
1519 bool CheckMinimalOrthancVersion(unsigned int major, | 1635 unsigned int major, |
1520 unsigned int minor, | 1636 unsigned int minor, |
1521 unsigned int revision) | 1637 unsigned int revision) |
1522 { | 1638 { |
1523 if (!HasGlobalContext()) | 1639 if (!strcmp(version, "mainline")) |
1524 { | |
1525 LogError("Bad Orthanc context in the plugin"); | |
1526 return false; | |
1527 } | |
1528 | |
1529 if (!strcmp(GetGlobalContext()->orthancVersion, "mainline")) | |
1530 { | 1640 { |
1531 // Assume compatibility with the mainline | 1641 // Assume compatibility with the mainline |
1532 return true; | 1642 return true; |
1533 } | 1643 } |
1534 | 1644 |
1535 // Parse the version of the Orthanc core | 1645 // Parse the version |
1536 int aa, bb, cc; | 1646 int aa, bb, cc; |
1537 if ( | 1647 if ( |
1538 #ifdef _MSC_VER | 1648 #ifdef _MSC_VER |
1539 sscanf_s | 1649 sscanf_s |
1540 #else | 1650 #else |
1541 sscanf | 1651 sscanf |
1542 #endif | 1652 #endif |
1543 (GetGlobalContext()->orthancVersion, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 || | 1653 (version, "%4d.%4d.%4d", &aa, &bb, &cc) != 3 || |
1544 aa < 0 || | 1654 aa < 0 || |
1545 bb < 0 || | 1655 bb < 0 || |
1546 cc < 0) | 1656 cc < 0) |
1547 { | 1657 { |
1548 return false; | 1658 return false; |
1562 if (a < major) | 1672 if (a < major) |
1563 { | 1673 { |
1564 return false; | 1674 return false; |
1565 } | 1675 } |
1566 | 1676 |
1567 | |
1568 // Check the minor version number | 1677 // Check the minor version number |
1569 assert(a == major); | 1678 assert(a == major); |
1570 | 1679 |
1571 if (b > minor) | 1680 if (b > minor) |
1572 { | 1681 { |
1587 } | 1696 } |
1588 else | 1697 else |
1589 { | 1698 { |
1590 return false; | 1699 return false; |
1591 } | 1700 } |
1701 } | |
1702 | |
1703 | |
1704 bool CheckMinimalOrthancVersion(unsigned int major, | |
1705 unsigned int minor, | |
1706 unsigned int revision) | |
1707 { | |
1708 if (!HasGlobalContext()) | |
1709 { | |
1710 LogError("Bad Orthanc context in the plugin"); | |
1711 return false; | |
1712 } | |
1713 | |
1714 return CheckMinimalVersion(GetGlobalContext()->orthancVersion, | |
1715 major, minor, revision); | |
1592 } | 1716 } |
1593 | 1717 |
1594 | 1718 |
1595 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) | 1719 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 5, 0) |
1596 const char* AutodetectMimeType(const std::string& path) | 1720 const char* AutodetectMimeType(const std::string& path) |
1890 if (index >= index_.size()) | 2014 if (index >= index_.size()) |
1891 { | 2015 { |
1892 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 2016 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
1893 } | 2017 } |
1894 | 2018 |
2019 if (body.size() > 0xffffffffu) | |
2020 { | |
2021 LogError("Cannot handle body size > 4GB"); | |
2022 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
2023 } | |
2024 | |
1895 OrthancPlugins::MemoryBuffer answer; | 2025 OrthancPlugins::MemoryBuffer answer; |
1896 uint16_t status; | 2026 uint16_t status; |
1897 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 2027 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
1898 (GetGlobalContext(), *answer, NULL, &status, peers_, | 2028 (GetGlobalContext(), *answer, NULL, &status, peers_, |
1899 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), | 2029 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), |
1916 const std::string& body) const | 2046 const std::string& body) const |
1917 { | 2047 { |
1918 if (index >= index_.size()) | 2048 if (index >= index_.size()) |
1919 { | 2049 { |
1920 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 2050 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
2051 } | |
2052 | |
2053 if (body.size() > 0xffffffffu) | |
2054 { | |
2055 LogError("Cannot handle body size > 4GB"); | |
2056 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
1921 } | 2057 } |
1922 | 2058 |
1923 OrthancPlugins::MemoryBuffer answer; | 2059 OrthancPlugins::MemoryBuffer answer; |
1924 uint16_t status; | 2060 uint16_t status; |
1925 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 2061 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
2481 { | 2617 { |
2482 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | 2618 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); |
2483 } | 2619 } |
2484 catch (...) | 2620 catch (...) |
2485 { | 2621 { |
2486 return OrthancPluginErrorCode_InternalError; | 2622 return OrthancPluginErrorCode_Plugin; |
2487 } | 2623 } |
2488 } | 2624 } |
2489 } | 2625 } |
2490 }; | 2626 }; |
2491 | 2627 |
2567 } | 2703 } |
2568 | 2704 |
2569 | 2705 |
2570 void HttpClient::ClearCredentials() | 2706 void HttpClient::ClearCredentials() |
2571 { | 2707 { |
2572 username_.empty(); | 2708 username_.clear(); |
2573 password_.empty(); | 2709 password_.clear(); |
2574 } | 2710 } |
2575 | 2711 |
2576 | 2712 |
2577 void HttpClient::SetCertificate(const std::string& certificateFile, | 2713 void HttpClient::SetCertificate(const std::string& certificateFile, |
2578 const std::string& keyFile, | 2714 const std::string& keyFile, |
2881 { | 3017 { |
2882 HeadersWrapper headers(headers_); | 3018 HeadersWrapper headers(headers_); |
2883 | 3019 |
2884 MemoryBuffer answerBodyBuffer, answerHeadersBuffer; | 3020 MemoryBuffer answerBodyBuffer, answerHeadersBuffer; |
2885 | 3021 |
3022 if (body.size() > 0xffffffffu) | |
3023 { | |
3024 LogError("Cannot handle body size > 4GB"); | |
3025 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
3026 } | |
3027 | |
2886 OrthancPluginErrorCode error = OrthancPluginHttpClient( | 3028 OrthancPluginErrorCode error = OrthancPluginHttpClient( |
2887 GetGlobalContext(), | 3029 GetGlobalContext(), |
2888 *answerBodyBuffer, | 3030 *answerBodyBuffer, |
2889 *answerHeadersBuffer, | 3031 *answerHeadersBuffer, |
2890 &httpStatus, | 3032 &httpStatus, |
3497 result->toFree_ = true; | 3639 result->toFree_ = true; |
3498 return result.release(); | 3640 return result.release(); |
3499 } | 3641 } |
3500 } | 3642 } |
3501 #endif | 3643 #endif |
3644 | |
3645 | |
3646 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3647 static std::vector<std::string> WebDavConvertPath(uint32_t pathSize, | |
3648 const char* const* pathItems) | |
3649 { | |
3650 std::vector<std::string> result(pathSize); | |
3651 | |
3652 for (uint32_t i = 0; i < pathSize; i++) | |
3653 { | |
3654 result[i] = pathItems[i]; | |
3655 } | |
3656 | |
3657 return result; | |
3658 } | |
3659 #endif | |
3660 | |
3661 | |
3662 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3663 static OrthancPluginErrorCode WebDavIsExistingFolder(uint8_t* isExisting, | |
3664 uint32_t pathSize, | |
3665 const char* const* pathItems, | |
3666 void* payload) | |
3667 { | |
3668 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3669 | |
3670 try | |
3671 { | |
3672 *isExisting = (that.IsExistingFolder(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3673 return OrthancPluginErrorCode_Success; | |
3674 } | |
3675 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3676 { | |
3677 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3678 } | |
3679 catch (...) | |
3680 { | |
3681 return OrthancPluginErrorCode_Plugin; | |
3682 } | |
3683 } | |
3684 #endif | |
3685 | |
3686 | |
3687 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3688 static OrthancPluginErrorCode WebDavListFolder(uint8_t* isExisting, | |
3689 OrthancPluginWebDavCollection* collection, | |
3690 OrthancPluginWebDavAddFile addFile, | |
3691 OrthancPluginWebDavAddFolder addFolder, | |
3692 uint32_t pathSize, | |
3693 const char* const* pathItems, | |
3694 void* payload) | |
3695 { | |
3696 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3697 | |
3698 try | |
3699 { | |
3700 std::list<IWebDavCollection::FileInfo> files; | |
3701 std::list<IWebDavCollection::FolderInfo> subfolders; | |
3702 | |
3703 if (!that.ListFolder(files, subfolders, WebDavConvertPath(pathSize, pathItems))) | |
3704 { | |
3705 *isExisting = 0; | |
3706 } | |
3707 else | |
3708 { | |
3709 *isExisting = 1; | |
3710 | |
3711 for (std::list<IWebDavCollection::FileInfo>::const_iterator | |
3712 it = files.begin(); it != files.end(); ++it) | |
3713 { | |
3714 OrthancPluginErrorCode code = addFile( | |
3715 collection, it->GetName().c_str(), it->GetContentSize(), | |
3716 it->GetMimeType().c_str(), it->GetDateTime().c_str()); | |
3717 | |
3718 if (code != OrthancPluginErrorCode_Success) | |
3719 { | |
3720 return code; | |
3721 } | |
3722 } | |
3723 | |
3724 for (std::list<IWebDavCollection::FolderInfo>::const_iterator it = | |
3725 subfolders.begin(); it != subfolders.end(); ++it) | |
3726 { | |
3727 OrthancPluginErrorCode code = addFolder( | |
3728 collection, it->GetName().c_str(), it->GetDateTime().c_str()); | |
3729 | |
3730 if (code != OrthancPluginErrorCode_Success) | |
3731 { | |
3732 return code; | |
3733 } | |
3734 } | |
3735 } | |
3736 | |
3737 return OrthancPluginErrorCode_Success; | |
3738 } | |
3739 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3740 { | |
3741 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3742 } | |
3743 catch (...) | |
3744 { | |
3745 return OrthancPluginErrorCode_Plugin; | |
3746 } | |
3747 } | |
3748 #endif | |
3749 | |
3750 | |
3751 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3752 static OrthancPluginErrorCode WebDavRetrieveFile(OrthancPluginWebDavCollection* collection, | |
3753 OrthancPluginWebDavRetrieveFile retrieveFile, | |
3754 uint32_t pathSize, | |
3755 const char* const* pathItems, | |
3756 void* payload) | |
3757 { | |
3758 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3759 | |
3760 try | |
3761 { | |
3762 std::string content, mime, dateTime; | |
3763 | |
3764 if (that.GetFile(content, mime, dateTime, WebDavConvertPath(pathSize, pathItems))) | |
3765 { | |
3766 return retrieveFile(collection, content.empty() ? NULL : content.c_str(), | |
3767 content.size(), mime.c_str(), dateTime.c_str()); | |
3768 } | |
3769 else | |
3770 { | |
3771 // Inexisting file | |
3772 return OrthancPluginErrorCode_Success; | |
3773 } | |
3774 } | |
3775 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3776 { | |
3777 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3778 } | |
3779 catch (...) | |
3780 { | |
3781 return OrthancPluginErrorCode_InternalError; | |
3782 } | |
3783 } | |
3784 #endif | |
3785 | |
3786 | |
3787 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3788 static OrthancPluginErrorCode WebDavStoreFileCallback(uint8_t* isReadOnly, /* out */ | |
3789 uint32_t pathSize, | |
3790 const char* const* pathItems, | |
3791 const void* data, | |
3792 uint64_t size, | |
3793 void* payload) | |
3794 { | |
3795 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3796 | |
3797 try | |
3798 { | |
3799 if (static_cast<uint64_t>(static_cast<size_t>(size)) != size) | |
3800 { | |
3801 ORTHANC_PLUGINS_THROW_EXCEPTION(NotEnoughMemory); | |
3802 } | |
3803 | |
3804 *isReadOnly = (that.StoreFile(WebDavConvertPath(pathSize, pathItems), data, | |
3805 static_cast<size_t>(size)) ? 1 : 0); | |
3806 return OrthancPluginErrorCode_Success; | |
3807 } | |
3808 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3809 { | |
3810 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3811 } | |
3812 catch (...) | |
3813 { | |
3814 return OrthancPluginErrorCode_InternalError; | |
3815 } | |
3816 } | |
3817 #endif | |
3818 | |
3819 | |
3820 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3821 static OrthancPluginErrorCode WebDavCreateFolderCallback(uint8_t* isReadOnly, /* out */ | |
3822 uint32_t pathSize, | |
3823 const char* const* pathItems, | |
3824 void* payload) | |
3825 { | |
3826 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3827 | |
3828 try | |
3829 { | |
3830 *isReadOnly = (that.CreateFolder(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3831 return OrthancPluginErrorCode_Success; | |
3832 } | |
3833 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3834 { | |
3835 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3836 } | |
3837 catch (...) | |
3838 { | |
3839 return OrthancPluginErrorCode_InternalError; | |
3840 } | |
3841 } | |
3842 #endif | |
3843 | |
3844 | |
3845 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3846 static OrthancPluginErrorCode WebDavDeleteItemCallback(uint8_t* isReadOnly, /* out */ | |
3847 uint32_t pathSize, | |
3848 const char* const* pathItems, | |
3849 void* payload) | |
3850 { | |
3851 IWebDavCollection& that = *reinterpret_cast<IWebDavCollection*>(payload); | |
3852 | |
3853 try | |
3854 { | |
3855 *isReadOnly = (that.DeleteItem(WebDavConvertPath(pathSize, pathItems)) ? 1 : 0); | |
3856 return OrthancPluginErrorCode_Success; | |
3857 } | |
3858 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
3859 { | |
3860 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
3861 } | |
3862 catch (...) | |
3863 { | |
3864 return OrthancPluginErrorCode_InternalError; | |
3865 } | |
3866 } | |
3867 #endif | |
3868 | |
3869 | |
3870 #if HAS_ORTHANC_PLUGIN_WEBDAV == 1 | |
3871 void IWebDavCollection::Register(const std::string& uri, | |
3872 IWebDavCollection& collection) | |
3873 { | |
3874 OrthancPluginErrorCode code = OrthancPluginRegisterWebDavCollection( | |
3875 GetGlobalContext(), uri.c_str(), WebDavIsExistingFolder, WebDavListFolder, WebDavRetrieveFile, | |
3876 WebDavStoreFileCallback, WebDavCreateFolderCallback, WebDavDeleteItemCallback, &collection); | |
3877 | |
3878 if (code != OrthancPluginErrorCode_Success) | |
3879 { | |
3880 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | |
3881 } | |
3882 } | |
3883 #endif | |
3884 | |
3885 void GetHttpHeaders(std::map<std::string, std::string>& result, const OrthancPluginHttpRequest* request) | |
3886 { | |
3887 result.clear(); | |
3888 | |
3889 for (uint32_t i = 0; i < request->headersCount; ++i) | |
3890 { | |
3891 result[request->headersKeys[i]] = request->headersValues[i]; | |
3892 } | |
3893 } | |
3502 } | 3894 } |