diff --git a/Modules/CppRestSdk/include/mitkRESTServer.h b/Modules/CppRestSdk/include/mitkRESTServer.h index adeb925f69..862151aa5c 100644 --- a/Modules/CppRestSdk/include/mitkRESTServer.h +++ b/Modules/CppRestSdk/include/mitkRESTServer.h @@ -1,66 +1,63 @@ /*=================================================================== 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 MITKRESTSERVER_H #define MITKRESTSERVER_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 -#include - #include "MitkCppRestSdkExports.h" 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 RESTServer : public QObject { public: RESTServer(); RESTServer(utility::string_t url); virtual ~RESTServer(); pplx::task Open() { return m_Listener.open(); } pplx::task Close() { return m_Listener.close(); } protected: virtual void HandleGet(MitkRequest){}; virtual void HandlePut(MitkRequest){}; virtual void HandlePost(MitkRequest){}; virtual void HandleDelete(MitkRequest){}; void HandleError(pplx::task &t); MitkListener m_Listener; }; }; #endif // MITKRESTSERVER_H diff --git a/Modules/CppRestSdk/src/mitkDICOMWeb.cpp b/Modules/CppRestSdk/src/mitkDICOMWeb.cpp index fba477e8d3..cbfb84248a 100644 --- a/Modules/CppRestSdk/src/mitkDICOMWeb.cpp +++ b/Modules/CppRestSdk/src/mitkDICOMWeb.cpp @@ -1,128 +1,128 @@ /*=================================================================== 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 "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)); return joinTask.then([=](void) { return utility::conversions::to_utf8string(folderPath).append(firstFileName); }); }); } 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); } \ No newline at end of file diff --git a/Modules/CppRestSdk/src/mitkRESTClient.cpp b/Modules/CppRestSdk/src/mitkRESTClient.cpp index 34262c3478..53b74ad845 100644 --- a/Modules/CppRestSdk/src/mitkRESTClient.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClient.cpp @@ -1,144 +1,144 @@ /*=================================================================== 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 "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, concurrency::streams::basic_istream fileStream) { MITK_INFO << "Calling POST with " << mitk::RESTUtil::convertToUtf8(uri) << " on client " << mitk::RESTUtil::convertToUtf8(m_Client->base_uri().to_string()); // currently not working, but stream approach may be useful for later.. don't use string streams for dcm files... concurrency::streams::container_buffer inStringBuffer; return fileStream.read(inStringBuffer, fileStream.streambuf().size()).then([=](size_t bytesRead) -> pplx::task { const std::string &text = inStringBuffer.collection(); std::string body = ""; body += "\r\n--boundary"; body += "\r\nContentType: " + mitk::RESTUtil::convertToUtf8(L"application/dicom") + "\r\n\r\n"; body += text; body += "\r\n--boundary--"; auto utf8String = utility::conversions::to_utf8string(body); auto binaryVector = std::vector(utf8String.begin(), utf8String.end()); MitkRequest postRequest(MitkRESTMethods::POST); postRequest.set_request_uri(uri); postRequest.headers().add(U("Content-Type"), contentType); postRequest.set_body(binaryVector); MITK_INFO << "Request: " << mitk::RESTUtil::convertToUtf8(postRequest.to_string()); return m_Client->request(postRequest).then([fileStream](MitkResponse response) { fileStream.close(); auto status = response.status_code(); if (status != web::http::status_codes::OK) { mitkThrow() << "POST ended up with response " << mitk::RESTUtil::convertToUtf8(response.to_string()); } }); }); } pplx::task mitk::RESTClient::Post(utility::string_t uri, utility::string_t contentType, 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(L"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 diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h index ede31e8ad0..2ca3f0f79c 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h @@ -1,72 +1,72 @@ /*=================================================================== 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 SegmentationReworkREST_h #define SegmentationReworkREST_h -#include +#include class SegmentationReworkREST : public mitk::RESTServer { Q_OBJECT public: struct DicomDTO { std::string segSeriesUIDA; std::string segSeriesUIDB; std::string imageSeriesUID; std::string studyUID; std::string segInstanceUIDA; std::string segInstanceUIDB; std::string srSeriesUID; std::vector simScoreArray; int minSliceStart; std::string groundTruth; }; SegmentationReworkREST(); SegmentationReworkREST(utility::string_t url); ~SegmentationReworkREST(); void HandlePut(MitkRequest message); void HandleGet(MitkRequest message); void SetPutCallback(std::function callback) { m_PutCallback = callback; } void SetGetImageSegCallback(std::function callback) { m_GetImageSegCallback = callback; } void SetGetEvalCallback(std::function callback) { m_GetEvalCallback = callback; } signals: void InvokeUpdateChartWidget(); private: std::function m_PutCallback; std::function m_GetImageSegCallback; std::function m_GetEvalCallback; }; #endif // SegmentationReworkREST_h