diff --git a/Modules/CppRestSdk/include/mitkRESTClient.h b/Modules/CppRestSdk/include/mitkRESTClient.h index 9150e9fdb1..60df1f8296 100644 --- a/Modules/CppRestSdk/include/mitkRESTClient.h +++ b/Modules/CppRestSdk/include/mitkRESTClient.h @@ -1,59 +1,59 @@ /*=================================================================== 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/json.h" #include "cpprest/producerconsumerstream.h" #include "cpprest/uri.h" #include "MitkCppRestSdkExports.h" 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 RESTClient { public:; RESTClient(utility::string_t url); virtual ~RESTClient(); pplx::task Post(utility::string_t uri, utility::string_t contentType, concurrency::streams::basic_istream fileStream); - pplx::task Post(utility::string_t uri, utility::string_t contentType, utility::string_t filePath); + pplx::task Post(utility::string_t uri, utility::string_t filePath); pplx::task Get(const utility::string_t filePath, utility::string_t uri); protected: MitkClient* m_Client; }; }; #endif // MITKRESTCLIENT_H diff --git a/Modules/CppRestSdk/src/mitkDICOMWeb.cpp b/Modules/CppRestSdk/src/mitkDICOMWeb.cpp index dcf668713b..4691d8dc37 100644 --- a/Modules/CppRestSdk/src/mitkDICOMWeb.cpp +++ b/Modules/CppRestSdk/src/mitkDICOMWeb.cpp @@ -1,134 +1,134 @@ /*=================================================================== 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. ===================================================================*/ #include "mitkDICOMWeb.h" #include "mitkRESTUtil.h" #include mitk::DICOMWeb::DICOMWeb(utility::string_t url) : mitk::RESTClient(url) { } mitk::DICOMWeb::~DICOMWeb() { } pplx::task mitk::DICOMWeb::QuidoRSInstances(std::map params) { MitkUriBuilder queryBuilder(U("rs/instances")); for (auto const& element : params) { queryBuilder.append_query(mitk::RESTUtil::convertToTString(element.first), mitk::RESTUtil::convertToTString(element.second)); } MITK_INFO << utility::conversions::to_utf8string(queryBuilder.to_string()); MitkRequest instances(MitkRESTMethods::GET); instances.set_request_uri(queryBuilder.to_string()); instances.headers().add(U("Accept"), U("application/json")); return m_Client->request(instances).then([=](MitkResponse response) { auto status = response.status_code(); MITK_INFO << " status: " << status; if (status != web::http::status_codes::OK) { mitkThrow() << "QUIDO-RS ended up with response " << mitk::RESTUtil::convertToUtf8(response.to_string()); } return response.extract_json().get(); }); } pplx::task mitk::DICOMWeb::WadoRS(utility::string_t filePath, std::string studyUID, std::string seriesUID, std::string instanceUID) { MitkUriBuilder builder(U("wado")); builder.append_query(U("requestType"), U("WADO")); builder.append_query(U("studyUID"), mitk::RESTUtil::convertToTString(studyUID)); builder.append_query(U("seriesUID"), mitk::RESTUtil::convertToTString(seriesUID)); builder.append_query(U("objectUID"), mitk::RESTUtil::convertToTString(instanceUID)); builder.append_query(U("contentType"), U("application/dicom")); return Get(filePath, builder.to_string()); } pplx::task mitk::DICOMWeb::WadoRS(const utility::string_t folderPath, std::string studyUID, std::string seriesUID) { typedef std::map ParamMap; ParamMap seriesInstancesParams; seriesInstancesParams.insert(ParamMap::value_type({"StudyInstanceUID"}, studyUID)); seriesInstancesParams.insert(ParamMap::value_type({"SeriesInstanceUID"}, seriesUID)); return QuidoRSInstances(seriesInstancesParams).then([=](web::json::value jsonResult) -> pplx::task { auto jsonListResult = jsonResult; auto resultArray = jsonListResult.as_array(); auto firstFileName = std::string(); std::vector> tasks; for (unsigned short i = 0; i < resultArray.size(); i++) { try { auto firstResult = resultArray[i]; auto sopInstanceUIDKey = firstResult.at(U("00080018")); auto sopInstanceObject = sopInstanceUIDKey.as_object(); auto valueKey = sopInstanceObject.at(U("Value")); auto valueArray = valueKey.as_array(); auto sopInstanceUID = valueArray[0].as_string(); auto fileName = utility::string_t(sopInstanceUID).append(U(".dcm")); // save first file name as result to load series if (i == 0) { firstFileName = utility::conversions::to_utf8string(fileName); } auto filePath = utility::string_t(folderPath).append(fileName); auto task = WadoRS(filePath, studyUID, seriesUID, mitk::RESTUtil::convertToUtf8(sopInstanceUID)); tasks.push_back(task); } catch (const web::json::json_exception& e) { MITK_ERROR << e.what(); } } auto joinTask = pplx::when_all(begin(tasks), end(tasks)); auto returnTask = joinTask.then([=](void) -> std::string { auto folderPathUtf8 = utility::conversions::to_utf8string(folderPath); auto result = folderPathUtf8 + firstFileName; return result; }); return returnTask; }); } pplx::task mitk::DICOMWeb::StowRS(utility::string_t filePath, std::string studyUID) { // TODO: add data MitkUriBuilder builder(U("rs/studies")); builder.append_path(mitk::RESTUtil::convertToTString(studyUID)); - return Post(builder.to_string(), U("multipart/related; type='application/dicom'; boundary='boundary'"), filePath); + return Post(builder.to_string(), filePath); } \ No newline at end of file diff --git a/Modules/CppRestSdk/src/mitkRESTClient.cpp b/Modules/CppRestSdk/src/mitkRESTClient.cpp index d815f395b1..cb9d9e760b 100644 --- a/Modules/CppRestSdk/src/mitkRESTClient.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClient.cpp @@ -1,104 +1,104 @@ /*=================================================================== 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. ===================================================================*/ #include "mitkRESTClient.h" #include "mitkRESTUtil.h" #include #include mitk::RESTClient::RESTClient(utility::string_t url) { m_Client = new MitkClient(url); } mitk::RESTClient::~RESTClient() { delete m_Client; } pplx::task mitk::RESTClient::Get(utility::string_t filePath, utility::string_t uri) { MITK_DEBUG << "Calling GET with " << utility::conversions::to_utf8string(uri) << " on client " << mitk::RESTUtil::convertToUtf8(m_Client->base_uri().to_string()) << " save into " << mitk::RESTUtil::convertToUtf8(filePath); auto fileBuffer = std::make_shared>(); return concurrency::streams::file_buffer::open(filePath, std::ios::out).then([=](concurrency::streams::streambuf outFile) -> pplx::task { *fileBuffer = outFile; return m_Client->request(MitkRESTMethods::GET, uri); }) // 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) { 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(); }); } -pplx::task mitk::RESTClient::Post(utility::string_t uri, utility::string_t contentType, utility::string_t filePath) +pplx::task mitk::RESTClient::Post(utility::string_t uri, utility::string_t filePath) { // this is the working stow-rs request which supports just one dicom file packed into a multipart message std::basic_ifstream input(filePath, std::ios::binary); std::vector result; std::vector buffer((std::istreambuf_iterator(input)), (std::istreambuf_iterator())); // reuse 'content-type' variable or struct to be more flexible, in future more than one file should also be supported.. std::string head = ""; head += "\r\n--boundary"; head += "\r\nContent-Type: " + mitk::RESTUtil::convertToUtf8(U("application/dicom")) + "\r\n\r\n"; std::vector bodyVector(head.begin(), head.end()); std::string tail = ""; tail += "\r\n--boundary--"; result.insert(result.end(), bodyVector.begin(), bodyVector.end()); result.insert(result.end(), buffer.begin(), buffer.end()); result.insert(result.end(), tail.begin(), tail.end()); MitkRequest postRequest(MitkRESTMethods::POST); postRequest.set_request_uri(uri); postRequest.headers().add(U("Content-Type"), "multipart/related; type=\"application/dicom\"; boundary=boundary"); postRequest.set_body(result); MITK_INFO << "Request: " << mitk::RESTUtil::convertToUtf8(postRequest.to_string()); return m_Client->request(postRequest).then([](MitkResponse response) { auto status = response.status_code(); if (status != web::http::status_codes::OK) { mitkThrow() << "POST ended up with response " << mitk::RESTUtil::convertToUtf8(response.to_string()); } MITK_INFO << "Response: " << mitk::RESTUtil::convertToUtf8(response.to_string()); }); } \ No newline at end of file