diff --git a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h index c34a9f7ac4..8c343ed697 100644 --- a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h @@ -1,87 +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 mitkRESTClientMicroService_h #define mitkRESTClientMicroService_h #include "cpprest/asyncrt_utils.h" #include "cpprest/containerstream.h" #include "cpprest/filestream.h" #include "cpprest/http_client.h" -#include "cpprest/json.h" #include "cpprest/producerconsumerstream.h" -#include "cpprest/uri.h" #include "MitkCppRestSdkExports.h" #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 RESTClientMicroService { public: RESTClientMicroService(); ~RESTClientMicroService(); /** *@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 * * @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 */ 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 */ pplx::task POST(const web::uri &uri, const web::json::value &content); }; } // namespace mitk #endif // !mitkRESTClientMicroService_h diff --git a/Modules/CppRestSdk/include/mitkRESTManager.h b/Modules/CppRestSdk/include/mitkRESTManager.h index 272928f8a3..7c9af10cdf 100644 --- a/Modules/CppRestSdk/include/mitkRESTManager.h +++ b/Modules/CppRestSdk/include/mitkRESTManager.h @@ -1,85 +1,84 @@ /*=================================================================== 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 #include #include #include -#include #include #include #include namespace mitk { class MITKCPPRESTSDK_EXPORT RESTManager : public QObject, public IRESTManager { Q_OBJECT public: RESTManager(); ~RESTManager() override; /** * @brief Executes a HTTP request in the mitkRESTClientMicroService 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 */ pplx::task SendRequest(const web::uri &uri, const RequestType &type = 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 */ 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; private: std::map m_ServerMap; // Map with port server pairs std::map m_ServerThreadMap; // Map with threads for servers std::map, IRESTObserver *> m_Observers; // Map with all observers }; } // namespace mitk #endif // !mitkRESTManager_h diff --git a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h index ca3673ba14..26ad46b9f0 100644 --- a/Modules/CppRestSdk/include/mitkRESTServerMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTServerMicroService.h @@ -1,82 +1,77 @@ /*=================================================================== 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 #include -#include "cpprest/asyncrt_utils.h" -#include "cpprest/containerstream.h" -#include "cpprest/filestream.h" #include "cpprest/http_listener.h" -#include "cpprest/json.h" -#include "cpprest/producerconsumerstream.h" -#include "cpprest/uri.h" #include "MitkCppRestSdkExports.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 RESTServerMicroService : public QObject { Q_OBJECT 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(); web::uri GetUri(); private: /** * @brief Handle for incoming GET requests * * @param MitkRequest incoming request object */ void HandleGet(MitkRequest request); MitkListener m_Listener; web::uri m_Uri; 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/CppRestSdk/src/mitkRESTServerMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp index 84342e7621..1332e2d3c7 100644 --- a/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp @@ -1,75 +1,73 @@ #include "mitkRESTServerMicroService.h" #include -#include -#include mitk::RESTServerMicroService::RESTServerMicroService(web::uri uri) : m_Listener(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) { 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 diff --git a/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h index dd523c1e38..15ccb1c4aa 100644 --- a/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h +++ b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h @@ -1,64 +1,62 @@ /*=================================================================== 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 QmitkClientView_h #define QmitkClientView_h #include #include "cpprest/uri.h" -#include -#include #include namespace Ui { class QmitkClientView; } namespace mitk { class RESTManager; } class QmitkClientView : public QmitkAbstractView { Q_OBJECT public: static const std::string VIEW_ID; QmitkClientView(); ~QmitkClientView() override; void CreateQtPartControl(QWidget *parent) override; signals: void UpdateProgressBar(); void UpdateLabel(QString); private slots: void OnGetMultipleButtonClicked(); void OnGetSingleButtonClicked(); void OnGetSaveButtonClicked(); void OnPutButtonClicked(); void OnPostButtonClicked(); void OnUpdateProgressBar(); void OnUpdateLabel(QString text); private: void SetFocus() override; Ui::QmitkClientView *m_Ui; }; #endif