diff --git a/Modules/CppRestSdk/src/mitkRESTClient.cpp b/Modules/CppRestSdk/src/mitkRESTClient.cpp index 8d5f219694..1a97faf427 100644 --- a/Modules/CppRestSdk/src/mitkRESTClient.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClient.cpp @@ -1,197 +1,202 @@ /*=================================================================== 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(url) { } mitk::RESTClient::~RESTClient() {} 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 " << utility::conversions::to_utf8string(m_Client.base_uri().to_string()) << " save into " << utility::conversions::to_utf8string(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 { MITK_DEBUG << "Status code: " << response.status_code(); 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 " << utility::conversions::to_utf8string(uri) << " on client " << utility::conversions::to_utf8string(m_Client.base_uri().to_string()); 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: " + utility::conversions::to_utf8string("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: " << utility::conversions::to_utf8string(postRequest.to_string()); return m_Client.request(postRequest).then([fileStream](MitkResponse response) { fileStream.close(); MITK_INFO << "Response: " << utility::conversions::to_utf8string(response.to_string()); }); }); } pplx::task mitk::RESTClient::Post(utility::string_t uri, utility::string_t contentType, utility::string_t filePath) { mitk::RESTUtil util; util.AddParameter("dicomObject", std::experimental::filesystem::path(utility::conversions::to_utf8string(filePath)).filename().string()); util.AddFile("file", utility::conversions::to_utf8string(filePath)); std::string boundary = util.boundary(); std::string body = util.GenBodyContent(); + 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.set_body(body, utility::conversions::to_utf8string(contentType)); + postRequest.headers().add(U("Content-Type"), contentType); + postRequest.set_body(binaryVector); MITK_INFO << "Request: " << utility::conversions::to_utf8string(postRequest.to_string()); return m_Client.request(postRequest).then([](MitkResponse response) { MITK_INFO << "Response: " << utility::conversions::to_utf8string(response.to_string()); }); } pplx::task mitk::RESTClient::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"), utility::conversions::to_string_t(studyUID)); builder.append_query(U("seriesUID"), utility::conversions::to_string_t(seriesUID)); builder.append_query(U("objectUID"), utility::conversions::to_string_t(instanceUID)); builder.append_query(U("contentType"), U("application/dicom")); return Get(filePath, builder.to_string()); } pplx::task mitk::RESTClient::WadoRS(const utility::string_t folderPath, std::string studyUID, std::string seriesUID) { // this is actually a quido-rs request, should be packed into a seperate method.. at some time.. //but there are many possible requests to support: http://dicom.nema.org/medical/dicom/current/output/chtml/part18/sect_6.7.html MitkUriBuilder builder(U("rs/instances")); builder.append_query(U("StudyInstanceUID"), utility::conversions::to_string_t(studyUID)); builder.append_query(U("SeriesInstanceUID"), utility::conversions::to_string_t(seriesUID)); MITK_INFO << utility::conversions::to_utf8string(builder.to_string()); MitkRequest getSeries(MitkRESTMethods::GET); getSeries.set_request_uri(builder.to_string()); getSeries.headers().add(U("Accept"), U("application/json")); return m_Client.request(getSeries).then([=](MitkResponse response) -> pplx::task { MITK_INFO << "search for instances in series with uid " << seriesUID << " status: " << response.status_code(); auto jsonListResult = response.extract_json().get(); 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, utility::conversions::to_utf8string(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::RESTClient::StowRS(utility::string_t filePath, std::string studyUID) { // TODO: add data MitkUriBuilder builder(U("rs/studies")); builder.append_path(utility::conversions::to_string_t(studyUID)); - return concurrency::streams::file_stream::open_istream(filePath).then([=](concurrency::streams::basic_istream fileStream) { - return Post(builder.to_string(), U("multipart/related; type='application/dicom'; boundary='boundary'"), fileStream); - }); + //return concurrency::streams::file_stream::open_istream(filePath).then([=](concurrency::streams::basic_istream fileStream) { + return Post(builder.to_string(), U("multipart/related; type='application/dicom'; boundary='boundary'"), filePath); + //}); } \ No newline at end of file diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp index 2341c17577..c7edc3fa09 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp @@ -1,113 +1,126 @@ /*=================================================================== 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 "SegmentationReworkREST.h" #include SegmentationReworkREST::SegmentationReworkREST() {} SegmentationReworkREST::SegmentationReworkREST(utility::string_t url) : mitk::RESTServer(url) { m_Listener.support(MitkRESTMethods::PUT, std::bind(&SegmentationReworkREST::HandlePut, this, std::placeholders::_1)); m_Listener.support(MitkRESTMethods::GET, std::bind(&SegmentationReworkREST::HandleGet, this, std::placeholders::_1)); } SegmentationReworkREST::~SegmentationReworkREST() {} void SegmentationReworkREST::HandleGet(MitkRequest message) { auto messageString = message.to_string(); MITK_INFO << "Message GET incoming..."; MITK_INFO << convertToUtf8(messageString); - auto http_get_vars = web::uri::split_query(message.request_uri().query()); + auto httpParams = web::uri::split_query(message.request_uri().query()); // IHE Invoke Image Display style - auto requestType = http_get_vars.at(L"requestType"); + auto requestType = httpParams.find(L"requestType"); - if (requestType == L"IMAGE_SEG") + if (requestType != httpParams.end() && requestType->second == L"IMAGE_SEG") { - auto studyUID = http_get_vars.at(L"studyUID"); - auto imageSeriesUID = http_get_vars.at(L"imageSeriesUID"); - auto segSeriesUID = http_get_vars.at(L"segSeriesUID"); + try + { + auto studyUID = httpParams.at(L"studyUID"); + auto imageSeriesUID = httpParams.at(L"imageSeriesUID"); + auto segSeriesUID = httpParams.at(L"segSeriesUID"); + + DicomDTO dto; + dto.imageSeriesUID = convertToUtf8(imageSeriesUID); + dto.studyUID = convertToUtf8(studyUID); + dto.segSeriesUIDA = convertToUtf8(segSeriesUID); - DicomDTO dto; - dto.imageSeriesUID = convertToUtf8(imageSeriesUID); - dto.studyUID = convertToUtf8(studyUID); - dto.segSeriesUIDA = convertToUtf8(segSeriesUID); + m_GetCallback(dto); - m_GetCallback(dto); + MitkResponse response(MitkRestStatusCodes::OK); + response.set_body("Sure, i got you.. have an awesome day"); + message.reply(response); + } + catch (std::out_of_range& e) { + message.reply(MitkRestStatusCodes::BadRequest, "oh man, that was not expected: " + std::string(e.what())); + } + } + else { + message.reply(MitkRestStatusCodes::BadRequest, "Oh man, i can only deal with 'requestType' = 'IMAGE+SEG'..."); } } void SegmentationReworkREST::HandlePut(MitkRequest message) { auto messageString = message.to_string(); MITK_INFO << "Message PUT incoming..."; MITK_INFO << convertToUtf8(messageString); try { auto jsonMessage = message.extract_json().get(); auto messageTypeKey = jsonMessage.at(U("messageType")); if (messageTypeKey.as_string() == U("downloadData")) { auto imageStudyUIDKey = jsonMessage.at(U("studyUID")); auto imageSeriesUIDKey = jsonMessage.at(U("imageSeriesUID")); auto segSeriesUIDAKey = jsonMessage.at(U("segSeriesUIDA")); auto segSeriesUIDBKey = jsonMessage.at(U("segSeriesUIDB")); auto simScoreKey = jsonMessage.at(U("simScoreArray")); auto minSliceStartKey = jsonMessage.at(U("minSliceStart")); DicomDTO dto; dto.segSeriesUIDA = convertToUtf8(segSeriesUIDAKey.as_string()); dto.segSeriesUIDB = convertToUtf8(segSeriesUIDBKey.as_string()); dto.imageSeriesUID = convertToUtf8(imageSeriesUIDKey.as_string()); dto.studyUID = convertToUtf8(imageStudyUIDKey.as_string()); dto.minSliceStart = minSliceStartKey.as_integer(); std::vector vec; web::json::array simArray = simScoreKey.as_array(); for (web::json::value score : simArray) { vec.push_back(score.as_double()); } dto.simScoreArray = vec; m_PutCallback(dto); emit InvokeUpdateChartWidget(); } else { message.reply(MitkRestStatusCodes::BadRequest, "Oh man, i can only deal with 'messageType' = 'downloadData'..."); } } catch (MitkJsonException &e) { MITK_ERROR << e.what() << ".. oh man, that was not expected"; message.reply(MitkRestStatusCodes::BadRequest, "oh man, that was not expected"); return; } MitkResponse response(MitkRestStatusCodes::OK); response.headers().add(U("Access-Control-Allow-Origin"), U("localhost:9000/*")); response.set_body("Sure, i got you.. have an awesome day"); message.reply(response); return; } \ No newline at end of file