diff --git a/Modules/CppRestSdk/include/mitkRESTClient.h b/Modules/CppRestSdk/include/mitkRESTClient.h index c3db296c48..bcce92f7c6 100644 --- a/Modules/CppRestSdk/include/mitkRESTClient.h +++ b/Modules/CppRestSdk/include/mitkRESTClient.h @@ -1,89 +1,87 @@ /*=================================================================== 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 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 { //TODO doku hinzufügen, wenn methode exception werfen kann class MITKCPPRESTSDK_EXPORT RESTClient { public: RESTClient(); ~RESTClient(); /** * @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 */ - //TODO Put - pplx::task PUT(const web::uri &uri, const web::json::value &content); + 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); + pplx::task Post(const web::uri &uri, const web::json::value &content); }; } // namespace mitk #endif // !mitkRESTClient_h diff --git a/Modules/CppRestSdk/src/mitkRESTClient.cpp b/Modules/CppRestSdk/src/mitkRESTClient.cpp index d4cfee2a36..eb8d23152b 100644 --- a/Modules/CppRestSdk/src/mitkRESTClient.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClient.cpp @@ -1,210 +1,210 @@ #include "mitkRESTClient.h" #include "mitkRESTUtil.h" #include mitk::RESTClient::RESTClient() {} mitk::RESTClient::~RESTClient() {} 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::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::RESTClient::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::RESTClient::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 b8c55b0928..2c7073e071 100644 --- a/Modules/CppRestSdk/src/mitkRESTManager.cpp +++ b/Modules/CppRestSdk/src/mitkRESTManager.cpp @@ -1,212 +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 RESTClient(); // according to the RequestType, different HTTP requests are made switch (type) { case RequestType::Get: if (filePath.empty()) { // 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 answer = client->Get(uri, filePath); } break; case RequestType::Post: //TODO fixen wert vorne bei vergleich if (content == NULL) { // 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); + answer = client->Post(uri, content); break; case RequestType::Put: if (content == NULL) { // 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 RESTServer 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 auto server = new RESTServer(uri.authority()); // add reference to server instance to map m_ServerMap[port] = 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); } // No observer under this port, return null which results in status code 404 (s. RESTServer) else { MITK_WARN << "No Observer can handle the data"; return NULL; } } 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.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(); delete m_ServerMap[port]; // delete server from map m_ServerMap.erase(port); } } else { ++it; } } else { ++it; } } } const std::map &mitk::RESTManager::GetM_ServerMap() { return m_ServerMap; } 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) { // 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) { 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) for (auto o : m_Observers) { if (o.first.first == port) { // there still exists an observer for this port return false; } } return true; }