diff --git a/Modules/CppRestSdk/include/mitkIRESTServerMicroService.h b/Modules/CppRestSdk/include/mitkIRESTServerMicroService.h index 037def9ce2..3117e707cb 100644 --- a/Modules/CppRestSdk/include/mitkIRESTServerMicroService.h +++ b/Modules/CppRestSdk/include/mitkIRESTServerMicroService.h @@ -1,76 +1,85 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkIRESTServerMicroService_h #define mitkIRESTServerMicroService_h #include "cpprest/http_listener.h" #include #include #include #include #include +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4251) +#endif + typedef web::http::experimental::listener::http_listener MitkListener; typedef web::http::http_request MitkRequest; typedef web::http::http_response MitkResponse; typedef web::http::methods MitkRESTMethods; typedef web::http::status_codes MitkRestStatusCodes; typedef web::json::json_exception MitkJsonException; namespace mitk { class MITKCPPRESTSDK_EXPORT IRESTServerMicroService { public: //** // * @brief Creates an server listening to the given URI // * // * @param uri the URI at which the server is listening for requests // */ IRESTServerMicroService(); ~IRESTServerMicroService(); virtual web::uri GetUri() = 0; /** * @brief Opens the listener and starts the listening process */ virtual void OpenListener() = 0; /** * @brief Closes the listener and stops the listening process */ virtual void CloseListener() = 0; - protected: + private: /** * @brief Handle for incoming GET requests * * @param MitkRequest incoming request object */ virtual void HandleGet(MitkRequest request) = 0; + protected: MitkListener m_Listener; web::uri m_Uri; - - // public slots: }; } // namespace mitk + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + #endif \ No newline at end of file diff --git a/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h b/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h index 2600391f0f..48ec102207 100644 --- a/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h +++ b/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h @@ -1,68 +1,51 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkRESTServerMicroServiceQt_h #define mitkRESTServerMicroServiceQt_h #include -#include - -typedef web::http::experimental::listener::http_listener MitkListener; -typedef web::http::http_request MitkRequest; -typedef web::http::http_response MitkResponse; -typedef web::http::methods MitkRESTMethods; -typedef web::http::status_codes MitkRestStatusCodes; -typedef web::json::json_exception MitkJsonException; +#include namespace mitk { - class RESTServerMicroServiceQt : public QObject, public IRESTServerMicroService + class RESTServerMicroServiceQt : public QObject, public RESTServerMicroService { Q_OBJECT public: /** * @brief Creates an server listening to the given URI * * @param uri the URI at which the server is listening for requests */ RESTServerMicroServiceQt(web::uri uri); ~RESTServerMicroServiceQt(); - web::uri GetUri() override; - - private: - /** - * @brief Handle for incoming GET requests - * - * @param MitkRequest incoming request object - */ - void HandleGet(MitkRequest request) override; - public slots: /** * @brief Opens the listener and starts the listening process */ void OpenListener() override; /** * @brief Closes the listener and stops the listening process */ void CloseListener() override; }; } // namespace mitk #endif \ No newline at end of file diff --git a/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp b/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp index a492835561..78f1936511 100644 --- a/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp +++ b/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp @@ -1,34 +1,34 @@ #include "mitkCppRestSdkQtActivator.h" #include #include #include #include #include #include #include #include #include #include #include #include void MitkCppRestSdkQtActivator::Load(us::ModuleContext *context) { // Registration of the RESTManagerMicroservice m_RESTManagerQt.reset(new mitk::RESTManagerQt); us::ServiceProperties props; - if (QCoreApplication::instance()!=NULL) + if (QCoreApplication::instance()!=nullptr) { props[us::ServiceConstants::SERVICE_RANKING()] = 10; } else { props[us::ServiceConstants::SERVICE_RANKING()] = 0; } context->RegisterService(m_RESTManagerQt.get(),props); } void MitkCppRestSdkQtActivator::Unload(us::ModuleContext *) {} US_EXPORT_MODULE_ACTIVATOR(MitkCppRestSdkQtActivator) diff --git a/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp b/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp index 5e7fe5e64b..eb261b7e85 100644 --- a/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp +++ b/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp @@ -1,71 +1,18 @@ #include "mitkRESTServerMicroServiceQt.h" #include -mitk::RESTServerMicroServiceQt::RESTServerMicroServiceQt(web::uri uri) +mitk::RESTServerMicroServiceQt::RESTServerMicroServiceQt(web::uri uri) : RESTServerMicroService(uri) { - m_Uri = uri; } mitk::RESTServerMicroServiceQt::~RESTServerMicroServiceQt() {} void mitk::RESTServerMicroServiceQt::OpenListener() { - // create listener - m_Listener = MitkListener(m_Uri); - // Connect incoming get requests with HandleGet method - m_Listener.support(web::http::methods::GET, - std::bind(&mitk::RESTServerMicroServiceQt::HandleGet, this, std::placeholders::_1)); - // open listener - m_Listener.open().wait(); + mitk::RESTServerMicroService::OpenListener(); } void mitk::RESTServerMicroServiceQt::CloseListener() { - // close listener - m_Listener.close().wait(); + mitk::RESTServerMicroService::CloseListener(); } - -web::uri mitk::RESTServerMicroServiceQt::GetUri() -{ - return m_Uri; -} - -void mitk::RESTServerMicroServiceQt::HandleGet(MitkRequest request) -{ - int port = m_Listener.uri().port(); - // getting exact request uri has to be a parameter in handle function - web::uri_builder build(m_Listener.uri()); - build.append(request.absolute_uri()); - utility::string_t uriStringT = build.to_uri().to_string(); - - std::string uriString(uriStringT.begin(), uriStringT.end()); - MITK_INFO << "Get Request fot server at port " << port << " Exact request uri: " << uriString; - - web::json::value content; - // get RESTManager as microservice to call th Handle method of the manager - us::ModuleContext *context = us::GetModuleContext(); - - auto managerRef = context->GetServiceReference(); - if (managerRef) - { - auto managerService = context->GetService(managerRef); - if (managerService) - { - web::json::value data = request.extract_json().get(); - MITK_INFO << "Server: Data send to manager"; - // call the handle method - content = managerService->Handle(build.to_uri(), data); - MITK_INFO << "server: Data received from manager"; - } - } - if (content != NULL) - { - // content handled by observer - request.reply(MitkRestStatusCodes::OK, content); - } - else - { - // no observer to handle data - request.reply(MitkRestStatusCodes::NotFound); - } -} \ No newline at end of file