Mercurial > hg > orthanc
comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3397:9019279dbfd7
new plugin c++ wrapper: MultipartRestCallback
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 07 Jun 2019 11:26:34 +0200 |
parents | 2f82ef41bf5a |
children | 4e8205871967 |
comparison
equal
deleted
inserted
replaced
3396:4981405e6c5c | 3397:9019279dbfd7 |
---|---|
1803 } | 1803 } |
1804 #endif | 1804 #endif |
1805 | 1805 |
1806 | 1806 |
1807 | 1807 |
1808 | |
1809 | |
1810 /****************************************************************** | |
1811 ** JOBS | |
1812 ******************************************************************/ | |
1813 | |
1808 #if HAS_ORTHANC_PLUGIN_JOB == 1 | 1814 #if HAS_ORTHANC_PLUGIN_JOB == 1 |
1809 void OrthancJob::CallbackFinalize(void* job) | 1815 void OrthancJob::CallbackFinalize(void* job) |
1810 { | 1816 { |
1811 if (job != NULL) | 1817 if (job != NULL) |
1812 { | 1818 { |
2039 } | 2045 } |
2040 } | 2046 } |
2041 #endif | 2047 #endif |
2042 | 2048 |
2043 | 2049 |
2050 | |
2051 | |
2052 /****************************************************************** | |
2053 ** METRICS | |
2054 ******************************************************************/ | |
2055 | |
2044 #if HAS_ORTHANC_PLUGIN_METRICS == 1 | 2056 #if HAS_ORTHANC_PLUGIN_METRICS == 1 |
2045 MetricsTimer::MetricsTimer(const char* name) : | 2057 MetricsTimer::MetricsTimer(const char* name) : |
2046 name_(name) | 2058 name_(name) |
2047 { | 2059 { |
2048 start_ = boost::posix_time::microsec_clock::universal_time(); | 2060 start_ = boost::posix_time::microsec_clock::universal_time(); |
2056 OrthancPluginMetricsType_Timer); | 2068 OrthancPluginMetricsType_Timer); |
2057 } | 2069 } |
2058 #endif | 2070 #endif |
2059 | 2071 |
2060 | 2072 |
2073 | |
2074 | |
2075 /****************************************************************** | |
2076 ** HTTP CLIENT | |
2077 ******************************************************************/ | |
2061 | 2078 |
2062 #if HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 | 2079 #if HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 |
2063 class HttpClient::RequestBodyWrapper : public boost::noncopyable | 2080 class HttpClient::RequestBodyWrapper : public boost::noncopyable |
2064 { | 2081 { |
2065 private: | 2082 private: |
2579 } | 2596 } |
2580 #endif | 2597 #endif |
2581 } | 2598 } |
2582 | 2599 |
2583 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ | 2600 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ |
2601 | |
2602 | |
2603 | |
2604 | |
2605 | |
2606 /****************************************************************** | |
2607 ** MULTIPART REST SERVER | |
2608 ******************************************************************/ | |
2609 | |
2610 #if HAS_ORTHANC_PLUGIN_HTTP_MULTIPART_SERVER == 1 | |
2611 static OrthancPluginMultipartRestHandler* MultipartRestFactory( | |
2612 OrthancPluginMultipartRestFactory* factory, | |
2613 OrthancPluginHttpMethod method, | |
2614 const char* url, | |
2615 const char* contentType, | |
2616 uint32_t groupsCount, | |
2617 const char* const* groups, | |
2618 uint32_t headersCount, | |
2619 const char* const* headersKeys, | |
2620 const char* const* headersValues) | |
2621 { | |
2622 try | |
2623 { | |
2624 assert(factory != NULL); | |
2625 MultipartRestCallback& that = *reinterpret_cast<MultipartRestCallback*>(factory); | |
2626 | |
2627 std::vector<std::string> g; | |
2628 g.resize(groupsCount); | |
2629 | |
2630 for (uint32_t i = 0; i < groupsCount; i++) | |
2631 { | |
2632 g[i] = groups[i]; | |
2633 } | |
2634 | |
2635 std::map<std::string, std::string> headers; | |
2636 for (uint32_t i = 0; i < headersCount; i++) | |
2637 { | |
2638 headers[headersKeys[i]] = headersValues[i]; | |
2639 } | |
2640 | |
2641 return reinterpret_cast<OrthancPluginMultipartRestHandler*>( | |
2642 that.CreateHandler(method, url, contentType, g, headers)); | |
2643 } | |
2644 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2645 { | |
2646 LogError("Exception while creating a multipart handler"); | |
2647 return NULL; | |
2648 } | |
2649 catch (...) | |
2650 { | |
2651 LogError("Native exception while creating a multipart handler"); | |
2652 return NULL; | |
2653 } | |
2654 } | |
2655 | |
2656 static OrthancPluginErrorCode MultipartRestAddPart( | |
2657 OrthancPluginMultipartRestHandler* handler, | |
2658 const char* contentType, | |
2659 uint32_t headersCount, | |
2660 const char* const* headersKeys, | |
2661 const char* const* headersValues, | |
2662 const void* data, | |
2663 uint32_t size) | |
2664 { | |
2665 try | |
2666 { | |
2667 assert(handler != NULL); | |
2668 std::map<std::string, std::string> headers; | |
2669 for (uint32_t i = 0; i < headersCount; i++) | |
2670 { | |
2671 headers[headersKeys[i]] = headersValues[i]; | |
2672 } | |
2673 | |
2674 return reinterpret_cast<MultipartRestCallback::IHandler*>(handler)-> | |
2675 AddPart(contentType, headers, data, size); | |
2676 } | |
2677 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2678 { | |
2679 LogError("Exception while add a part to a multipart handler"); | |
2680 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2681 } | |
2682 catch (...) | |
2683 { | |
2684 LogError("Native exception while add a part to a multipart handler"); | |
2685 return OrthancPluginErrorCode_Plugin; | |
2686 } | |
2687 } | |
2688 | |
2689 static OrthancPluginErrorCode MultipartRestExecute( | |
2690 OrthancPluginMultipartRestHandler* handler, | |
2691 OrthancPluginRestOutput* output) | |
2692 { | |
2693 try | |
2694 { | |
2695 assert(handler != NULL); | |
2696 return reinterpret_cast<MultipartRestCallback::IHandler*>(handler)->Execute(output); | |
2697 } | |
2698 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2699 { | |
2700 LogError("Exception while executing a multipart handler"); | |
2701 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2702 } | |
2703 catch (...) | |
2704 { | |
2705 LogError("Native exception while executing a multipart handler"); | |
2706 return OrthancPluginErrorCode_Plugin; | |
2707 } | |
2708 } | |
2709 | |
2710 static void MultipartRestFinalize(OrthancPluginMultipartRestHandler* handler) | |
2711 { | |
2712 try | |
2713 { | |
2714 assert(handler != NULL); | |
2715 delete reinterpret_cast<MultipartRestCallback::IHandler*>(handler); | |
2716 } | |
2717 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2718 { | |
2719 LogError("Exception while finalizing a multipart handler"); | |
2720 } | |
2721 catch (...) | |
2722 { | |
2723 LogError("Native exception while finalizing a multipart handler"); | |
2724 } | |
2725 } | |
2726 | |
2727 void MultipartRestCallback::Register(const std::string& regularExpression) | |
2728 { | |
2729 OrthancPluginRegisterMultipartRestCallback( | |
2730 GetGlobalContext(), regularExpression.c_str(), | |
2731 reinterpret_cast<OrthancPluginMultipartRestFactory*>(this), | |
2732 MultipartRestFactory, MultipartRestAddPart, MultipartRestExecute, MultipartRestFinalize); | |
2733 } | |
2734 #endif | |
2735 | |
2584 } | 2736 } |