diff --git a/Modules/CppRestSdk/CMakeLists.txt b/Modules/CppRestSdk/CMakeLists.txt index 24fe589848..de6047745c 100644 --- a/Modules/CppRestSdk/CMakeLists.txt +++ b/Modules/CppRestSdk/CMakeLists.txt @@ -1,15 +1,18 @@ if(MITK_USE_CppRestSdk) MITK_CREATE_MODULE( - DEPENDS MitkCore CppMicroServices + DEPENDS MitkCore #WARNINGS_NO_ERRORS AUTOLOAD_WITH MitkCore ) - find_package(OpenSSL REQUIRED) - - target_link_libraries(${MODULE_TARGET} PRIVATE cpprestsdk::cpprest) - target_link_libraries(${MODULE_TARGET} PUBLIC OpenSSL::SSL) -add_subdirectory(test) + if(TARGET ${MODULE_TARGET}) + find_package(OpenSSL REQUIRED) + + target_link_libraries(${MODULE_TARGET} PRIVATE cpprestsdk::cpprest) + target_link_libraries(${MODULE_TARGET} PUBLIC OpenSSL::SSL) + endif() + + add_subdirectory(test) endif(MITK_USE_CppRestSdk) diff --git a/Modules/CppRestSdk/files.cmake b/Modules/CppRestSdk/files.cmake index b84729c9a3..356b6d628d 100644 --- a/Modules/CppRestSdk/files.cmake +++ b/Modules/CppRestSdk/files.cmake @@ -1,10 +1,10 @@ file(GLOB_RECURSE H_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include/*") set(CPP_FILES - mitkRESTClientMicroService.cpp + mitkRESTClient.cpp mitkRESTServerMicroService.cpp mitkCppRestSdkActivator.cpp mitkIRESTManager.cpp mitkRESTManager.cpp mitkIRESTObserver.cpp ) \ No newline at end of file diff --git a/Modules/CppRestSdk/include/mitkIRESTManager.h b/Modules/CppRestSdk/include/mitkIRESTManager.h index a238e2720b..02f9c6b148 100644 --- a/Modules/CppRestSdk/include/mitkIRESTManager.h +++ b/Modules/CppRestSdk/include/mitkIRESTManager.h @@ -1,95 +1,96 @@ /*=================================================================== 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 mitkIRESTManager_h #define mitkIRESTManager_h #include "cpprest/json.h" #include "cpprest/uri.h" #include #include #include -#include +#include #include namespace mitk { class RESTServerMicroService; class MITKCPPRESTSDK_EXPORT IRESTManager { public: virtual ~IRESTManager(); /** * @brief request type for client requests by calling SendRequest */ - enum RequestType + //TODO: Get, Post, Put + enum class RequestType { get, post, put }; /** - * @brief Executes a HTTP request in the mitkRESTClientMicroService class + * @brief Executes a HTTP request in the mitkRESTClient class * * @param uri defines the URI the request is send to * @param type the RequestType of the HTTP request (optional) * @param body the body for the request (optional) * @return task to wait for */ virtual pplx::task SendRequest(const web::uri &uri, - const RequestType &type = get, + const RequestType &type = RequestType::get, const web::json::value &body = NULL, const utility::string_t &filePath = L"") = 0; /** * @brief starts listening for requests if there isn't another observer listening and the port is free * * @param uri defines the URI for which incoming requests should be send to the observer * @param observer the observer which handles the incoming requests */ virtual void ReceiveRequest(const web::uri &uri, IRESTObserver *observer) = 0; /** * @brief Handles incoming requests by notifying the observer which should receive it * * @param uri defines the URI of the request * @param body the body of the request * @return the data which is modified by the notified observer */ virtual web::json::value Handle(const web::uri &uri, web::json::value &body) = 0; /** * @brief Handles the deletion of an observer for all or a specific uri * * @param observer the observer which shouldn't receive requests anymore * @param uri the uri for which the observer doesn't handle requests anymore (optional) */ virtual void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri = L"") = 0; - virtual std::map GetM_ServerMap() = 0; - virtual std::map, IRESTObserver *> GetM_Observers() = 0; + virtual const std::map& GetM_ServerMap() = 0; + virtual const std::map, IRESTObserver *>& GetM_Observers() = 0; }; } // namespace mitk MITK_DECLARE_SERVICE_INTERFACE(mitk::IRESTManager, "org.mitk.IRESTManager") #endif diff --git a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h b/Modules/CppRestSdk/include/mitkRESTClient.h similarity index 84% rename from Modules/CppRestSdk/include/mitkRESTClientMicroService.h rename to Modules/CppRestSdk/include/mitkRESTClient.h index fc82b82baa..c3db296c48 100644 --- a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTClient.h @@ -1,86 +1,89 @@ /*=================================================================== 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 mitkRESTClientMicroService_h -#define mitkRESTClientMicroService_h +#ifndef mitkRESTClient_h +#define mitkRESTClient_h #include "cpprest/asyncrt_utils.h" #include "cpprest/containerstream.h" #include "cpprest/filestream.h" #include "cpprest/http_client.h" #include "cpprest/producerconsumerstream.h" //#include #include #include #include #include typedef web::http::client::http_client MitkClient; typedef web::http::http_request MitkRequest; typedef web::http::http_response MitkResponse; typedef web::http::methods MitkRESTMethods; typedef web::http::uri_builder MitkUriBuilder; typedef web::http::status_codes MitkRestStatusCodes; typedef web::json::json_exception MitkJsonException; namespace mitk { - class MITKCPPRESTSDK_EXPORT RESTClientMicroService + //TODO doku hinzufügen, wenn methode exception werfen kann + class MITKCPPRESTSDK_EXPORT RESTClient { public: - RESTClientMicroService(); - ~RESTClientMicroService(); + RESTClient(); + ~RESTClient(); /** - *@brief Executes a HTTP GET request with the given uri and returns a task waiting for a json object + * @brief Executes a HTTP GET request with the given uri and returns a task waiting for a json object * * @param uri the URI resulting the target of the HTTP request * @return task to wait for with resulting json object */ pplx::task Get(const web::uri &uri); /** - *@brief Executes a HTTP GET request with the given uri and and stores the byte stream in a file given by the - *filePath + * @brief Executes a HTTP GET request with the given uri and and stores the byte stream in a file given by the + * filePath * * @param uri the URI resulting the target of the HTTP request * @return task to wait for returning an empty json object */ pplx::task Get(const web::uri &uri, const utility::string_t &filePath); /** * @brief Executes a HTTP PUT request with given uri and the content given as json * * @param uri defines the URI resulting the target of the HTTP request * @param content the content as json value which should be the body of the request and thus the content of the * created resources * @return task to wait for with resulting json object */ + //TODO Put pplx::task PUT(const web::uri &uri, const web::json::value &content); /** * @brief Executes a HTTP POST request with given uri and the content given as json * * @param uri defines the URI resulting the target of the HTTP request * @param content the content as json value which should be the body of the request and thus the content of the * created resource * @return task to wait for with resulting json object */ + //TODO Post pplx::task POST(const web::uri &uri, const web::json::value &content); }; } // namespace mitk -#endif // !mitkRESTClientMicroService_h +#endif // !mitkRESTClient_h diff --git a/Modules/CppRestSdk/include/mitkRESTManager.h b/Modules/CppRestSdk/include/mitkRESTManager.h index 81335f0e14..8ec8912d0d 100644 --- a/Modules/CppRestSdk/include/mitkRESTManager.h +++ b/Modules/CppRestSdk/include/mitkRESTManager.h @@ -1,116 +1,122 @@ /*=================================================================== 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 mitkRESTManager_h #define mitkRESTManager_h #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4251) #endif #include #include namespace mitk { class MITKCPPRESTSDK_EXPORT RESTManager : public IRESTManager { public: RESTManager(); ~RESTManager() override; /** - * @brief Executes a HTTP request in the mitkRESTClientMicroService class + * @brief Executes a HTTP request in the mitkRESTClient class * * @param uri defines the URI the request is send to * @param type the RequestType of the HTTP request (optional) * @param body the body for the request (optional) * @param filePath the file path to store the request to * @return task to wait for */ + //TODO aus body pointer machen, nullptr pplx::task SendRequest(const web::uri &uri, - const RequestType &type = get, + const RequestType &type = RequestType::get, const web::json::value &body= NULL, const utility::string_t &filePath = L"") override; /** * @brief starts listening for requests if there isn't another observer listening and the port is free * * @param uri defines the URI for which incoming requests should be send to the observer * @param observer the observer which handles the incoming requests */ void ReceiveRequest(const web::uri &uri, IRESTObserver *observer) override; /** * @brief Handles incoming requests by notifying the observer which should receive it * * @param uri defines the URI of the request * @param body the body of the request * @return the data which is modified by the notified observer */ + //TODO body const web::json::value Handle(const web::uri &uri, web::json::value &body) override; /** * @brief Handles the deletion of an observer for all or a specific uri * * @param observer the observer which shouldn't receive requests anymore * @param uri the uri for which the observer doesn't handle requests anymore (optional) */ virtual void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri) override; - virtual std::map GetM_ServerMap() override; - virtual std::map, IRESTObserver *> GetM_Observers() override; + /** + * @brief internal use only + */ + //TODO alternative: aus interface rausnehmen und dynamic casten + virtual const std::map& GetM_ServerMap() override; + virtual const std::map, IRESTObserver *>& GetM_Observers() override; + protected: /** * @brief adds an observer if a port is free, called by ReceiveRequest method * * @param uri the uri which builds the key for the observer map * @param observer the observer which is added */ void AddObserver(const web::uri &uri, IRESTObserver *observer); /** * @brief handles server management if there is already a server under a port, called by ReceiveRequest method * * @param uri the uri which which is requested to be added * @param observer the observer which proceeds the request */ void ServerUnderPort(const web::uri &uri, IRESTObserver *observer); /** * @brief deletes an observer, called by HandleDeleteObserver method * * @param it the iterator comparing the observers in HandleDeleteObserver method * @param the uri for which the observer doesn't want to receive requests anymore * @return bool if there is another observer under the port */ bool DeleteObserver(std::map < std::pair, IRESTObserver *>::iterator &it, const web::uri &uri); - - protected: +//TODO Member immer private, zugriff über getter/setter std::map m_ServerMap; // Map with port server pairs std::map, IRESTObserver *> m_Observers; // Map with all observers }; } // namespace mitk #ifdef _MSC_VER #pragma warning(pop) #endif #endif // !mitkRESTManager_h diff --git a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h index 99af70060f..9fea58588d 100644 --- a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h @@ -1,80 +1,80 @@ /*=================================================================== 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 mitkRESTServerMicroService_h #define mitkRESTServerMicroService_h #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4251) #endif #include "cpprest/http_listener.h" #include #include #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; namespace mitk { class MITKCPPRESTSDK_EXPORT RESTServerMicroService { public: /** * @brief Creates an server listening to the given URI * * @param uri the URI at which the server is listening for requests */ - RESTServerMicroService(web::uri uri); + RESTServerMicroService(const web::uri &uri); ~RESTServerMicroService(); web::uri GetUri(); /** * @brief Opens the listener and starts the listening process */ void OpenListener(); /** * @brief Closes the listener and stops the listening process */ void CloseListener(); private: /** * @brief Handle for incoming GET requests * * @param MitkRequest incoming request object */ - void HandleGet(MitkRequest request); - + void HandleGet(const MitkRequest &request); + //TODO private machen protected: MitkListener m_Listener; web::uri m_Uri; }; } // namespace mitk #ifdef _MSC_VER #pragma warning(pop) #endif #endif \ No newline at end of file diff --git a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTClient.cpp similarity index 91% rename from Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp rename to Modules/CppRestSdk/src/mitkRESTClient.cpp index 0180092f07..d4cfee2a36 100644 --- a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClient.cpp @@ -1,210 +1,210 @@ -#include "mitkRESTClientMicroService.h" +#include "mitkRESTClient.h" #include "mitkRESTUtil.h" #include -mitk::RESTClientMicroService::RESTClientMicroService() {} +mitk::RESTClient::RESTClient() {} -mitk::RESTClientMicroService::~RESTClientMicroService() {} +mitk::RESTClient::~RESTClient() {} -pplx::task mitk::RESTClientMicroService::Get(const web::uri &uri) +pplx::task mitk::RESTClient::Get(const web::uri &uri) { //Create new HTTP client MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling GET with " << mitk::RESTUtil::convertToUtf8(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); //create get request MitkRequest getRequest(MitkRESTMethods::GET); //make request return client->request(getRequest).then([=](pplx::task responseTask) { try { //get response of the request MitkResponse response = responseTask.get(); auto status = response.status_code(); MITK_INFO << " status: " << status; if (status != MitkRestStatusCodes::OK) { //throw if something went wrong (e.g. invalid uri) //this exception can be handled by client mitkThrow() << "response was not OK"; } try { //parse content type to application/json if it isn't already //this is important if the content type is e.g. application/dicom+json utility::string_t requestContentType = response.headers().content_type(); if (requestContentType != L"application/json") { response.headers().set_content_type(L"application/json"); } //return json answer return response.extract_json().get(); } catch (...) { mitkThrow() << "extracting json went wrong"; } } catch (...) { mitkThrow() << "getting response went wrong"; } }); } -pplx::task mitk::RESTClientMicroService::Get(const web::uri &uri, const utility::string_t &filePath) +pplx::task mitk::RESTClient::Get(const web::uri &uri, const utility::string_t &filePath) { // Create new HTTP client MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling GET with " << mitk::RESTUtil::convertToUtf8(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()) << " save into " << mitk::RESTUtil::convertToUtf8(filePath); //create new file buffer auto fileBuffer = std::make_shared>(); // create get request MitkRequest getRequest(MitkRESTMethods::GET); //open file stream for the specified file path return concurrency::streams::file_buffer::open(filePath, std::ios::out) .then([=](concurrency::streams::streambuf outFile) -> pplx::task { *fileBuffer = outFile; //make the get request return client->request(MitkRESTMethods::GET); }) // Write the response body into the file buffer. .then([=](MitkResponse response) -> pplx::task { auto status = response.status_code(); MITK_DEBUG << "Status code: " << status; if (status != web::http::status_codes::OK) { // throw if something went wrong (e.g. invalid uri) // this exception can be handled by client mitkThrow() << "GET ended up with response " << mitk::RESTUtil::convertToUtf8(response.to_string()); } return response.body().read_to_end(*fileBuffer); }) // Close the file buffer. .then([=](size_t) { return fileBuffer->close(); }) .then([=]() { //return empty json object web::json::value data; return data; }); } -pplx::task mitk::RESTClientMicroService::PUT(const web::uri &uri, const web::json::value &content) +pplx::task mitk::RESTClient::PUT(const web::uri &uri, const web::json::value &content) { // Create new HTTP client MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling PUT with " << mitk::RESTUtil::convertToUtf8(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); // create put request MitkRequest putRequest(MitkRESTMethods::PUT); //set body of the put request with data given by client if (content != NULL) { putRequest.set_body(content); } //make put request return client->request(putRequest).then([=](pplx::task responseTask) { try { // get response of the request MitkResponse response = responseTask.get(); auto status = response.status_code(); MITK_INFO << " status: " << status; if (status != MitkRestStatusCodes::OK) { // throw if something went wrong (e.g. invalid uri) // this exception can be handled by client mitkThrow() << "response was not OK"; } try { // parse content type to application/json if it isn't already // this is important if the content type is e.g. application/dicom+json utility::string_t requestContentType = response.headers().content_type(); if (requestContentType != L"application/json") { response.headers().set_content_type(L"application/json"); } // return json answer return response.extract_json().get(); } catch (...) { mitkThrow() << "extracting json went wrong"; } } catch (...) { mitkThrow() << "getting response went wrong"; } }); } -pplx::task mitk::RESTClientMicroService::POST(const web::uri &uri, const web::json::value &content) +pplx::task mitk::RESTClient::POST(const web::uri &uri, const web::json::value &content) { // Create new HTTP client MitkClient *client = new MitkClient(uri); MITK_INFO << "Calling POST with " << mitk::RESTUtil::convertToUtf8(uri.path()) << " on client " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); // Create post request MitkRequest postRequest(MitkRESTMethods::POST); // set body of the put request with data given by client if (content != NULL) { postRequest.set_body(content); } //make post request return client->request(postRequest).then([=](pplx::task responseTask) { try { // get response of the request MitkResponse response = responseTask.get(); auto status = response.status_code(); MITK_INFO << " status: " << status; if (status != MitkRestStatusCodes::Created) { // throw if something went wrong (e.g. invalid uri) // this exception can be handled by client mitkThrow() << "response was not Created"; } try { // parse content type to application/json if it isn't already // this is important if the content type is e.g. application/dicom+json utility::string_t requestContentType = response.headers().content_type(); if (requestContentType != L"application/json") { response.headers().set_content_type(L"application/json"); } // return json answer return response.extract_json().get(); } catch (...) { mitkThrow() << "extracting json went wrong"; } } catch(...) { mitkThrow() << "getting response went wrong"; } }); } diff --git a/Modules/CppRestSdk/src/mitkRESTManager.cpp b/Modules/CppRestSdk/src/mitkRESTManager.cpp index ee5e1243c3..18b2bd8612 100644 --- a/Modules/CppRestSdk/src/mitkRESTManager.cpp +++ b/Modules/CppRestSdk/src/mitkRESTManager.cpp @@ -1,217 +1,212 @@ #include "mitkRESTManager.h" #include #include mitk::RESTManager::RESTManager() {} mitk::RESTManager::~RESTManager() {} pplx::task mitk::RESTManager::SendRequest(const web::uri &uri, const RequestType &type, const web::json::value &content, const utility::string_t &filePath) { pplx::task answer; - auto client = new RESTClientMicroService(); - //according to the RequestType, different HTTP requests are made + auto client = new RESTClient(); + // according to the RequestType, different HTTP requests are made switch (type) { - case get: - { - if (filePath == L"") + case RequestType::get: + + if (filePath.empty()) { - //no file path specified, starts a normal get request returning the normal json result + // no file path specified, starts a normal get request returning the normal json result answer = client->Get(uri); } else { - //file path ist specified, the result of the get request ist stored in this file - //and an empty json object is returned + // file path ist specified, the result of the get request ist stored in this file + // and an empty json object is returned answer = client->Get(uri, filePath); } break; - } - case post: - { + + case RequestType::post: + + //TODO fixen wert vorne bei vergleich if (content == NULL) { - //warning because normally you won't create an empty ressource + // warning because normally you won't create an empty ressource MITK_WARN << "Content for put is empty, this will create an empty ressource"; } answer = client->POST(uri, content); break; - } - break; - case put: - { + + case RequestType::put: + if (content == NULL) { - //warning because normally you won't empty a ressource + // warning because normally you won't empty a ressource MITK_WARN << "Content for put is empty, this will empty the ressource"; } - answer = client->PUT(uri,content); + answer = client->PUT(uri, content); break; - } + //TODO: default hinzufügen } return answer; } void mitk::RESTManager::ReceiveRequest(const web::uri &uri, mitk::IRESTObserver *observer) { // New instance of RESTServerMicroservice in m_ServerMap, key is port of the request int port = uri.port(); // Checking if port is free to add a new Server if (m_ServerMap.count(port) == 0) { this->AddObserver(uri, observer); // creating server instance - RESTServerMicroService *server = new RESTServerMicroService(uri.authority()); + auto server = new RESTServerMicroService(uri.authority()); // add reference to server instance to map m_ServerMap[port] = server; - //start Server + // start Server server->OpenListener(); MITK_INFO << "new server " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()) << " at port " << port; } // If there is already a server under this port else { + //TODO umbenennen this->ServerUnderPort(uri, observer); } } - web::json::value mitk::RESTManager::Handle(const web::uri &uri, web::json::value &body) { // Checking if there is an observer for the port and path std::pair key(uri.port(), uri.path()); if (m_Observers.count(key) != 0) { + //TODO Ausgaben minimieren MITK_INFO << "Manager: Data send to observer"; - return m_Observers[key]->Notify(body,uri); + return m_Observers[key]->Notify(body, uri); } - //No observer under this port, return null which results in status code 404 (s. RESTServerMicroService) + // No observer under this port, return null which results in status code 404 (s. RESTServerMicroService) else { MITK_WARN << "No Observer can handle the data"; return NULL; } } -void mitk::RESTManager::HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri= L"") +void mitk::RESTManager::HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri = L"") { for (auto it = m_Observers.begin(); it != m_Observers.end();) { mitk::IRESTObserver *obsMap = it->second; // Check wether observer is at this place in map if (obsMap == observer) { - //Check wether it is the right uri to be deleted - if (uri==L""||it->first.second == uri.path()) + // Check wether it is the right uri to be deleted + if (uri.is_empty() || it->first.second == uri.path()) { int port = it->first.first; bool noObserverForPort = this->DeleteObserver(it, uri); if (noObserverForPort) { // there isn't an observer at this port, delete m_ServerMap entry for this port // close listener m_ServerMap[port]->CloseListener(); - dynamic_cast(m_ServerMap[port])->~RESTServerMicroService(); + delete m_ServerMap[port]; // delete server from map m_ServerMap.erase(port); } - } + } else { ++it; } } else { ++it; } } } -std::map mitk::RESTManager::GetM_ServerMap() +const std::map &mitk::RESTManager::GetM_ServerMap() { return m_ServerMap; } -std::map, mitk::IRESTObserver *> mitk::RESTManager::GetM_Observers() +const std::map, mitk::IRESTObserver *> &mitk::RESTManager::GetM_Observers() { return m_Observers; } void mitk::RESTManager::AddObserver(const web::uri &uri, IRESTObserver *observer) { // new observer has to be added std::pair key(uri.port(), uri.path()); m_Observers[key] = observer; // testing if entry has been added to observer map MITK_INFO << "[" << uri.port() << ", " << mitk::RESTUtil::convertToUtf8(uri.path()) << "] : Number of elements in map: " << m_Observers.count(key); } -void mitk::RESTManager::ServerUnderPort(const web::uri &uri, IRESTObserver *observer) +void mitk::RESTManager::ServerUnderPort(const web::uri &uri, IRESTObserver *observer) { // Same host, means new observer but not a new server instance if (m_ServerMap[uri.port()]->GetUri() == uri.authority()) { // new observer has to be added std::pair key(uri.port(), uri.path()); // only add a new observer if there isn't already an observer for this uri if (m_Observers.count(key) == 0) { - m_Observers[key] = observer; - - // testing if entry has been added to map - MITK_INFO << "[" << uri.port() << ", " << mitk::RESTUtil::convertToUtf8(uri.path()) - << "] : Number of elements in map: " << m_Observers.count(key); + this->AddObserver(uri, observer); // info output MITK_INFO << "started listening, no new server instance has been created"; } else { MITK_ERROR << "Threre is already a observer handeling this data"; } } // Error, since another server can't be added under this port else { MITK_ERROR << "There is already another server listening under this port"; } } bool mitk::RESTManager::DeleteObserver(std::map, IRESTObserver *>::iterator &it, const web::uri &uri) { // if yes // 1. store port and path in a temporary variable // (path is only needed to create a key for info output) int port = it->first.first; utility::string_t path = it->first.second; std::pair key(port, path); MITK_INFO << "Number of elements at key [ " << port << ", " << mitk::RESTUtil::convertToUtf8(key.second) << "]: " << m_Observers.count(key); // 2. delete map entry it = m_Observers.erase(it); MITK_INFO << "Number of elements at key [ " << port << ", " << mitk::RESTUtil::convertToUtf8(key.second) << "]: " << m_Observers.count(key); // 3. check, if there is another observer under this port in observer map (with bool flag) - bool noObserverForPort = true; for (auto o : m_Observers) { if (o.first.first == port) { // there still exists an observer for this port - noObserverForPort = false; + return false; } } - return noObserverForPort; + return true; } - diff --git a/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp index 28e194d9e2..9bc9825aef 100644 --- a/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp @@ -1,74 +1,74 @@ #include "mitkRESTServerMicroService.h" #include #include -mitk::RESTServerMicroService::RESTServerMicroService(web::uri uri) +mitk::RESTServerMicroService::RESTServerMicroService(const web::uri &uri) { m_Uri = uri; } mitk::RESTServerMicroService::~RESTServerMicroService() { } void mitk::RESTServerMicroService::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::RESTServerMicroService::HandleGet, this, std::placeholders::_1)); //open listener m_Listener.open().wait(); } void mitk::RESTServerMicroService::CloseListener() { //close listener m_Listener.close().wait(); } web::uri mitk::RESTServerMicroService::GetUri() { return m_Uri; } -void mitk::RESTServerMicroService::HandleGet(MitkRequest request) +void mitk::RESTServerMicroService::HandleGet(const 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(); + auto uriStringT = build.to_uri().to_string(); MITK_INFO << "Get Request for server at port " << port << " Exact request uri: " << mitk::RESTUtil::convertToUtf8(uriStringT); web::json::value content; //get RESTManager as microservice to call th Handle method of the manager - us::ModuleContext *context = us::GetModuleContext(); + auto 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 diff --git a/Modules/CppRestSdk/test/mitkRESTClientTest.cpp b/Modules/CppRestSdk/test/mitkRESTClientTest.cpp index 6596eea8b9..63c49ca3bc 100644 --- a/Modules/CppRestSdk/test/mitkRESTClientTest.cpp +++ b/Modules/CppRestSdk/test/mitkRESTClientTest.cpp @@ -1,279 +1,281 @@ /*=================================================================== 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. ===================================================================*/ // Testing #include "mitkTestFixture.h" #include "mitkTestingMacros.h" // MITK includes -#include "mitkRESTClientMicroService.h" +#include "mitkRESTClient.h" // VTK includes #include #include "mitkIRESTManager.h" #include #include #include #include #include #include class mitkRESTClientTestSuite : public mitk::TestFixture, mitk::IRESTObserver { CPPUNIT_TEST_SUITE(mitkRESTClientTestSuite); MITK_TEST(GetRequestValidURI_ReturnsExpectedJSON); MITK_TEST(MultipleGetRequestValidURI_AllTasksFinish); MITK_TEST(PutRequestValidURI_ReturnsExpectedJSON); MITK_TEST(PostRequestValidURI_ReturnsExpectedJSON); MITK_TEST(GetRequestInvalidURI_ThrowsException); MITK_TEST(PutRequestInvalidURI_ThrowsException); MITK_TEST(PostRequestInvalidURI_ThrowsException); CPPUNIT_TEST_SUITE_END(); public: mitk::IRESTManager *m_Service; web::json::value Notify(web::json::value &data, const web::uri &uri) override { MITK_INFO << "Observer: Data in observer"; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); return data; } /** * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used * members for a new test case. (If the members are not used in a test, the method does not need to be called). */ void setUp() override { us::ServiceReference serviceRef = us::GetModuleContext()->GetServiceReference(); if (serviceRef) { m_Service = us::GetModuleContext()->GetService(serviceRef); } if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } } void tearDown() override { if (m_Service) { m_Service->HandleDeleteObserver(this); } } void GetRequestValidURI_ReturnsExpectedJSON() { web::json::value *result = new web::json::value(); web::json::value data; + //TODO: data als memebrvariable/ in setup/ in methode data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); if (m_Service) { m_Service->SendRequest(L"http://localhost:8080/test") .then([=](pplx::task resultTask) { try { *result = resultTask.get(); } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }) .wait(); } CPPUNIT_ASSERT_MESSAGE("Result is the expected JSON value", *result == data); } void MultipleGetRequestValidURI_AllTasksFinish() { int *count = new int(0); if (m_Service) { // Create multiple tasks e.g. as shown below + //TODO: vector konstruktoren anschauen / emplace_back std::vector> tasks; - for (int i = 0; i < 20; i++) + for (int i = 0; i < 20; ++i) { pplx::task singleTask = m_Service->SendRequest(L"http://localhost:8080/test") .then([=](pplx::task resultTask) { // Do something when a single task is done try { resultTask.get(); *count +=1; } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }); tasks.push_back(singleTask); } // Create a joinTask which includes all tasks you've created auto joinTask = pplx::when_all(begin(tasks), end(tasks)); // Run asynchonously joinTask.then([=](pplx::task resultTask) { // Do something when all tasks are finished try { resultTask.get(); *count += 1; } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }).wait(); } CPPUNIT_ASSERT_MESSAGE("Multiple Requests", *count ==21); } void PutRequestValidURI_ReturnsExpectedJSON() { // optional: link might get invalid or content is changed web::json::value *result = new web::json::value(); web::json::value data; if (m_Service) { data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a changed title")); data[L"body"] = web::json::value(U("and the body is changed as well")); m_Service ->SendRequest(L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::put, data) .then([=](pplx::task resultTask) { try { *result = resultTask.get(); } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }) .wait(); } CPPUNIT_ASSERT_MESSAGE( "Result is the expected JSON value, check if the link is still valid since this is an optional test", *result == data); } void PostRequestValidURI_ReturnsExpectedJSON() { // optional: link might get invalid or content is changed web::json::value *result = new web::json::value(); web::json::value data; if (m_Service) { data[L"userId"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a new title")); data[L"body"] = web::json::value(U("this is a new body")); m_Service->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::post, data) .then([=](pplx::task resultTask) { try { *result = resultTask.get(); } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }) .wait(); } data[L"id"] = web::json::value(101); CPPUNIT_ASSERT_MESSAGE( "Result is the expected JSON value, check if the link is still valid since this is an optional test", *result == data); } void GetException() { //Method which makes a get request to an invalid uri web::json::value *result = new web::json::value(); if (m_Service) { m_Service->SendRequest(L"http://localhost:1234/invalid") .then([=](pplx::task resultTask) { *result = resultTask.get(); }) .wait(); } } void GetRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(GetException(), mitk::Exception); } void PutException() { //Method which makes a put request to an invalid uri web::json::value *result = new web::json::value(); web::json::value data; if (m_Service) { data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a changed title")); data[L"body"] = web::json::value(U("and the body is changed as well")); m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::put, data) .then([=](pplx::task resultTask) { *result = resultTask.get();}) .wait(); } } void PutRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PutException(), mitk::Exception); } void PostException() { //Method which makes a post request to an invalid uri web::json::value *result = new web::json::value(); web::json::value data; if (m_Service) { data[L"userId"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a new title")); data[L"body"] = web::json::value(U("this is a new body")); m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::post, data) .then([=](pplx::task resultTask) { *result = resultTask.get(); }) .wait(); } } void PostRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PostException(), mitk::Exception); } }; MITK_TEST_SUITE_REGISTRATION(mitkRESTClient) \ No newline at end of file diff --git a/Modules/CppRestSdk/test/mitkRESTServerTest.cpp b/Modules/CppRestSdk/test/mitkRESTServerTest.cpp index e7f6e43cc3..08e74c5569 100644 --- a/Modules/CppRestSdk/test/mitkRESTServerTest.cpp +++ b/Modules/CppRestSdk/test/mitkRESTServerTest.cpp @@ -1,259 +1,262 @@ /*=================================================================== 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. ===================================================================*/ // Testing #include "mitkTestFixture.h" #include "mitkTestingMacros.h" // MITK includes #include "mitkRESTServerMicroService.h" // VTK includes #include class mitkRESTServerTestSuite : public mitk::TestFixture, mitk::IRESTObserver { CPPUNIT_TEST_SUITE(mitkRESTServerTestSuite); MITK_TEST(OpenListener_Succeed); MITK_TEST(TwoListenerSameHostSamePort_OnlyOneOpened); MITK_TEST(CloseListener_Succeed); MITK_TEST(OpenMultipleListenerCloseOne_Succeed); MITK_TEST(OpenMultipleListenerCloseAll_Succeed); MITK_TEST(OpenListenerGetRequestSamePath_ReturnExpectedJSON); MITK_TEST(CloseListener_NoRequestPossible); MITK_TEST(OpenListenerGetRequestDifferentPath_ReturnNotFound); MITK_TEST(OpenListenerCloseAndReopen_Succeed); CPPUNIT_TEST_SUITE_END(); public: mitk::IRESTManager *m_Service; web::json::value m_Data; + //TODO: bei Notify uri parameter zuerst + //TODO: data als const ref web::json::value Notify(web::json::value &data, const web::uri &uri) override { MITK_INFO << "Observer: Data in observer"; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); return data; } /** * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used * members for a new test case. (If the members are not used in a test, the method does not need to be called). */ void setUp() override { m_Data[L"userId"] = web::json::value(1); m_Data[L"id"] = web::json::value(1); m_Data[L"title"] = web::json::value(U("this is a title")); m_Data[L"body"] = web::json::value(U("this is a body")); us::ServiceReference serviceRef = us::GetModuleContext()->GetServiceReference(); if (serviceRef) { m_Service = us::GetModuleContext()->GetService(serviceRef); } + //TODO: if(m_Service) überprüfen, exception wenn nicht } void tearDown() override { m_Service->HandleDeleteObserver(this); } void OpenListener_Succeed() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); } void TwoListenerSameHostSamePort_OnlyOneOpened() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); m_Service->ReceiveRequest(L"http://localhost:8080/example", this); } CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, observer map size is two", m_Service->GetM_Observers().size() == 2); CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, server map size is one", m_Service->GetM_ServerMap().size() == 1); } void CloseListener_Succeed() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); if (m_Service) { m_Service->HandleDeleteObserver(this); } CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", m_Service->GetM_Observers().size() == 0); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", m_Service->GetM_ServerMap().size() == 0); } void OpenMultipleListenerCloseOne_Succeed() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); m_Service->ReceiveRequest(L"http://localhost:8090/example", this); } CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", m_Service->GetM_Observers().size() == 2); CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", m_Service->GetM_ServerMap().size() == 2); if (m_Service) { m_Service->HandleDeleteObserver(this, L"http://localhost:8080/test"); } CPPUNIT_ASSERT_MESSAGE("Closed one of two listeners, observer map is size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Closed one of two listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); } void OpenMultipleListenerCloseAll_Succeed() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); m_Service->ReceiveRequest(L"http://localhost:8090/example", this); } CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", m_Service->GetM_Observers().size() == 2); CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", m_Service->GetM_ServerMap().size() == 2); if (m_Service) { m_Service->HandleDeleteObserver(this); } CPPUNIT_ASSERT_MESSAGE("Closed all listeners, observer map is empty", m_Service->GetM_Observers().size() == 0); CPPUNIT_ASSERT_MESSAGE("Closed all listeners, server map is empty", m_Service->GetM_ServerMap().size() == 0); } void OpenListenerGetRequestSamePath_ReturnExpectedJSON() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } web::json::value *result = new web::json::value(); web::json::value data; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); if (m_Service) { m_Service->SendRequest(L"http://localhost:8080/test") .then([=](pplx::task resultTask) { try { *result = resultTask.get(); } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }) .wait(); } CPPUNIT_ASSERT_MESSAGE("Opened listener and send request to same uri, returned expected JSON", *result == data); } void RequestToClosedListener() { web::json::value *result = new web::json::value(); if (m_Service) { m_Service->SendRequest(L"http://localhost:8080/test") .then([=](pplx::task resultTask) { *result = resultTask.get(); }) .wait(); } } void CloseListener_NoRequestPossible() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); if (m_Service) { m_Service->HandleDeleteObserver(this); } CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", m_Service->GetM_Observers().size() == 0); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", m_Service->GetM_ServerMap().size() == 0); CPPUNIT_ASSERT_THROW(RequestToClosedListener(), mitk::Exception); } void RequestToDifferentPathNotFound() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } web::json::value *result = new web::json::value(); web::json::value data; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); if (m_Service) { m_Service->SendRequest(L"http://localhost:8080/example") .then([=](pplx::task resultTask) { *result = resultTask.get(); }) .wait(); } } void OpenListenerGetRequestDifferentPath_ReturnNotFound() { CPPUNIT_ASSERT_THROW(RequestToDifferentPathNotFound(), mitk::Exception); } void OpenListenerCloseAndReopen_Succeed() { if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); if (m_Service) { m_Service->HandleDeleteObserver(this); } CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", m_Service->GetM_Observers().size() == 0); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", m_Service->GetM_ServerMap().size() == 0); if (m_Service) { m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } CPPUNIT_ASSERT_MESSAGE("Reopened listener, observer map size is one", m_Service->GetM_Observers().size() == 1); CPPUNIT_ASSERT_MESSAGE("Reopened listener, server map size is one", m_Service->GetM_ServerMap().size() == 1); } }; MITK_TEST_SUITE_REGISTRATION(mitkRESTServer) \ No newline at end of file diff --git a/Modules/CppRestSdkQt/CMakeLists.txt b/Modules/CppRestSdkQt/CMakeLists.txt index 0874d9739d..f5edd1b638 100644 --- a/Modules/CppRestSdkQt/CMakeLists.txt +++ b/Modules/CppRestSdkQt/CMakeLists.txt @@ -1,15 +1,17 @@ if(MITK_USE_CppRestSdk) MITK_CREATE_MODULE( - DEPENDS MitkCore MitkQtWidgetsExt CppMicroServices MitkCppRestSdk + DEPENDS MitkCore MitkCppRestSdk PACKAGE_DEPENDS Qt5|Core #WARNINGS_NO_ERRORS AUTOLOAD_WITH MitkQtWidgets ) - find_package(OpenSSL REQUIRED) - - target_link_libraries(${MODULE_TARGET} PRIVATE cpprestsdk::cpprest) - target_link_libraries(${MODULE_TARGET} PUBLIC OpenSSL::SSL) - + if(TARGET ${MODULE_TARGET}) + find_package(OpenSSL REQUIRED) + + target_link_libraries(${MODULE_TARGET} PRIVATE cpprestsdk::cpprest) + target_link_libraries(${MODULE_TARGET} PUBLIC OpenSSL::SSL) + endif() + endif() diff --git a/Modules/CppRestSdkQt/include/mitkRESTManagerQt.h b/Modules/CppRestSdkQt/include/mitkRESTManagerQt.h index 3e7078ff2a..89b7a1bef1 100644 --- a/Modules/CppRestSdkQt/include/mitkRESTManagerQt.h +++ b/Modules/CppRestSdkQt/include/mitkRESTManagerQt.h @@ -1,61 +1,61 @@ /*=================================================================== 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 mitkRESTManagerQt_h #define mitkRESTManagerQt_h #include #include #include #include -#include +#include #include #include namespace mitk { class MITKCPPRESTSDKQT_EXPORT RESTManagerQt : public QObject, public RESTManager { Q_OBJECT public: RESTManagerQt(); ~RESTManagerQt() override; /** * @brief starts listening for requests if there isn't another observer listening and the port is free * * @param uri defines the URI for which incoming requests should be send to the observer * @param observer the observer which handles the incoming requests */ void ReceiveRequest(const web::uri &uri, IRESTObserver *observer) override; /** * @brief Handles the deletion of an observer for all or a specific uri * * @param observer the observer which shouldn't receive requests anymore * @param uri the uri for which the observer doesn't handle requests anymore (optional) */ - virtual void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri) override; + void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri) override; private: - std::map m_ServerThreadMap; // Map with threads for servers + std::map m_ServerThreadMap; // Map with threads for servers }; } // namespace mitk #endif // !mitkRESTManager_h \ No newline at end of file diff --git a/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h b/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h index 2cf397e0c0..50aca370e1 100644 --- a/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h +++ b/Modules/CppRestSdkQt/include/mitkRESTServerMicroServiceQt.h @@ -1,51 +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 namespace mitk { 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(const web::uri &uri); ~RESTServerMicroServiceQt(); public slots: /** * @brief Opens the listener and starts the listening process */ void OpenListener(); /** * @brief Closes the listener and stops the listening process */ void CloseListener(); }; } // namespace mitk #endif \ No newline at end of file diff --git a/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp b/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp index 57ddc4d2a3..f7ebbcdf7f 100644 --- a/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp +++ b/Modules/CppRestSdkQt/src/mitkCppRestSdkQtActivator.cpp @@ -1,31 +1,31 @@ #include "mitkCppRestSdkQtActivator.h" #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()!=nullptr) + 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/mitkRESTManagerQt.cpp b/Modules/CppRestSdkQt/src/mitkRESTManagerQt.cpp index 892ad8b8e2..5441cbd4f2 100644 --- a/Modules/CppRestSdkQt/src/mitkRESTManagerQt.cpp +++ b/Modules/CppRestSdkQt/src/mitkRESTManagerQt.cpp @@ -1,79 +1,79 @@ #include "mitkRESTManagerQt.h" #include #include mitk::RESTManagerQt::RESTManagerQt() {} mitk::RESTManagerQt::~RESTManagerQt() {} void mitk::RESTManagerQt::ReceiveRequest(const web::uri &uri, mitk::IRESTObserver *observer) { // New instance of RESTServerMicroservice in m_ServerMap, key is port of the request int port = uri.port(); // Checking if port is free to add a new Server if (m_ServerMap.count(port) == 0) { mitk::RESTManager::AddObserver(uri, observer); // creating server instance - RESTServerMicroServiceQt *server = new RESTServerMicroServiceQt(uri.authority()); + auto server = new RESTServerMicroServiceQt(uri.authority()); // add reference to server instance to map m_ServerMap[port] = server; // Move server to seperate Thread and create connections between threads m_ServerThreadMap[port] = new QThread; server->moveToThread(m_ServerThreadMap[port]); connect(m_ServerThreadMap[port], &QThread::finished, server, &QObject::deleteLater); // starting Server m_ServerThreadMap[port]->start(); QMetaObject::invokeMethod(server, "OpenListener"); MITK_INFO << "new server " << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()) << " at port " << port; } // If there is already a server under this port else { mitk::RESTManager::ServerUnderPort(uri, observer); } } void mitk::RESTManagerQt::HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri = L"") { for (auto it = m_Observers.begin(); it != m_Observers.end();) { mitk::IRESTObserver *obsMap = it->second; // Check wether observer is at this place in map if (obsMap == observer) { // Check wether it is the right uri to be deleted - if (uri == L"" || it->first.second == uri.path()) + if (uri.is_empty() || it->first.second == uri.path()) { int port = it->first.first; bool noObserverForPort = mitk::RESTManager::DeleteObserver(it, uri); if (noObserverForPort) { // there isn't an observer at this port, delete m_ServerMap entry for this port // close listener QMetaObject::invokeMethod(static_cast(m_ServerMap[port]), "CloseListener"); // end thread m_ServerThreadMap[port]->quit(); m_ServerThreadMap[port]->wait(); // delete server from map m_ServerMap.erase(port); } } else { ++it; } } else { ++it; } } } diff --git a/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp b/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp index 8dc709951c..03ce12991a 100644 --- a/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp +++ b/Modules/CppRestSdkQt/src/mitkRESTServerMicroServiceQt.cpp @@ -1,17 +1,17 @@ #include "mitkRESTServerMicroServiceQt.h" -mitk::RESTServerMicroServiceQt::RESTServerMicroServiceQt(web::uri uri) : RESTServerMicroService(uri) +mitk::RESTServerMicroServiceQt::RESTServerMicroServiceQt(const web::uri &uri) : RESTServerMicroService(uri) { } mitk::RESTServerMicroServiceQt::~RESTServerMicroServiceQt() {} void mitk::RESTServerMicroServiceQt::OpenListener() { mitk::RESTServerMicroService::OpenListener(); } void mitk::RESTServerMicroServiceQt::CloseListener() { mitk::RESTServerMicroService::CloseListener(); } diff --git a/Plugins/PluginList.cmake b/Plugins/PluginList.cmake index e3b4460cba..a7f1255c6f 100644 --- a/Plugins/PluginList.cmake +++ b/Plugins/PluginList.cmake @@ -1,116 +1,116 @@ # Plug-ins must be ordered according to their dependencies set(MITK_PLUGINS org.blueberry.core.runtime:ON org.blueberry.core.expressions:OFF org.blueberry.core.commands:OFF org.blueberry.core.jobs:OFF org.blueberry.ui.qt:OFF org.blueberry.ui.qt.help:ON org.blueberry.ui.qt.log:ON org.blueberry.ui.qt.objectinspector:OFF #org.blueberry.test:ON #org.blueberry.uitest:ON #Testing/org.blueberry.core.runtime.tests:ON #Testing/org.blueberry.osgi.tests:ON org.mitk.core.services:ON org.mitk.gui.common:ON org.mitk.planarfigure:ON org.mitk.core.ext:OFF org.mitk.core.jobs:OFF org.mitk.gui.qt.application:ON org.mitk.gui.qt.coreapplication:OFF org.mitk.gui.qt.ext:OFF org.mitk.gui.qt.extapplication:OFF org.mitk.gui.qt.common:ON org.mitk.gui.qt.stdmultiwidgeteditor:ON org.mitk.gui.qt.common.legacy:OFF org.mitk.gui.qt.cmdlinemodules:OFF org.mitk.gui.qt.diffusionimagingapp:OFF org.mitk.gui.qt.datamanager:ON org.mitk.gui.qt.datamanagerlight:OFF org.mitk.gui.qt.datastorageviewertest:OFF org.mitk.gui.qt.properties:ON org.mitk.gui.qt.basicimageprocessing:OFF org.mitk.gui.qt.dicom:OFF org.mitk.gui.qt.dicominspector:OFF org.mitk.gui.qt.diffusionimaging:OFF org.mitk.gui.qt.diffusionimaging.connectomics:OFF org.mitk.gui.qt.diffusionimaging.denoising:OFF org.mitk.gui.qt.diffusionimaging.fiberfox:OFF org.mitk.gui.qt.diffusionimaging.fiberprocessing:OFF org.mitk.gui.qt.diffusionimaging.ivim:OFF org.mitk.gui.qt.diffusionimaging.odfpeaks:OFF org.mitk.gui.qt.diffusionimaging.partialvolume:OFF org.mitk.gui.qt.diffusionimaging.preprocessing:OFF org.mitk.gui.qt.diffusionimaging.reconstruction:OFF org.mitk.gui.qt.diffusionimaging.registration:OFF org.mitk.gui.qt.diffusionimaging.tbss:OFF org.mitk.gui.qt.diffusionimaging.tractography:OFF org.mitk.gui.qt.diffusionimaging.python:OFF org.mitk.gui.qt.dosevisualization:OFF org.mitk.gui.qt.geometrytools:OFF org.mitk.gui.qt.igtexamples:OFF org.mitk.gui.qt.igttracking:OFF org.mitk.gui.qt.lasercontrol:OFF org.mitk.gui.qt.openigtlink:OFF org.mitk.gui.qt.imagecropper:OFF org.mitk.gui.qt.imagenavigator:ON org.mitk.gui.qt.viewnavigator:OFF org.mitk.gui.qt.materialeditor:OFF org.mitk.gui.qt.measurementtoolbox:OFF org.mitk.gui.qt.moviemaker:OFF org.mitk.gui.qt.pointsetinteraction:OFF org.mitk.gui.qt.pointsetinteractionmultispectrum:OFF org.mitk.gui.qt.python:OFF org.mitk.gui.qt.remeshing:OFF org.mitk.gui.qt.segmentation:OFF org.mitk.gui.qt.aicpregistration:OFF org.mitk.gui.qt.renderwindowmanager:OFF org.mitk.gui.qt.toftutorial:OFF org.mitk.gui.qt.tofutil:OFF org.mitk.gui.qt.tubegraph:OFF org.mitk.gui.qt.ugvisualization:OFF org.mitk.gui.qt.photoacoustics.pausviewer:OFF org.mitk.gui.qt.photoacoustics.pausmotioncompensation:OFF org.mitk.gui.qt.photoacoustics.imageprocessing:OFF org.mitk.gui.qt.photoacoustics.simulation:OFF org.mitk.gui.qt.photoacoustics.spectralunmixing:OFF org.mitk.gui.qt.ultrasound:OFF org.mitk.gui.qt.volumevisualization:OFF org.mitk.gui.qt.eventrecorder:OFF org.mitk.gui.qt.xnat:OFF org.mitk.gui.qt.igt.app.echotrack:OFF org.mitk.gui.qt.spectrocamrecorder:OFF org.mitk.gui.qt.classificationsegmentation:OFF org.mitk.gui.qt.overlaymanager:OFF org.mitk.gui.qt.igt.app.hummelprotocolmeasurements:OFF org.mitk.gui.qt.multilabelsegmentation:OFF org.mitk.matchpoint.core.helper:OFF org.mitk.gui.qt.matchpoint.algorithm.browser:OFF org.mitk.gui.qt.matchpoint.algorithm.control:OFF org.mitk.gui.qt.matchpoint.algorithm.batch:OFF org.mitk.gui.qt.matchpoint.mapper:OFF org.mitk.gui.qt.matchpoint.framereg:OFF org.mitk.gui.qt.matchpoint.visualizer:OFF org.mitk.gui.qt.matchpoint.evaluator:OFF org.mitk.gui.qt.matchpoint.manipulator:OFF org.mitk.gui.qt.preprocessing.resampling:OFF org.mitk.gui.qt.radiomics:OFF org.mitk.gui.qt.cest:OFF org.mitk.gui.qt.fit.demo:OFF org.mitk.gui.qt.fit.inspector:OFF org.mitk.gui.qt.fit.genericfitting:OFF org.mitk.gui.qt.pharmacokinetics.mri:OFF org.mitk.gui.qt.pharmacokinetics.pet:OFF org.mitk.gui.qt.pharmacokinetics.simulation:OFF org.mitk.gui.qt.pharmacokinetics.curvedescriptor:OFF org.mitk.gui.qt.pharmacokinetics.concentration.mri:OFF - org.mitk.gui.qt.thread:ON - org.mitk.gui.qt.client:ON + org.mitk.gui.qt.thread:OFF + org.mitk.gui.qt.client:OFF )