# HG changeset patch # User Sebastien Jodogne # Date 1569581430 -7200 # Node ID 96dc376e4679a2f0c7790133ad2f3292e8b421f2 # Parent 062bbe766cc1fbf0bd4eafc67c49eecc5787ada9 slight optimization diff -r 062bbe766cc1 -r 96dc376e4679 Plugin/WadoRs.cpp --- a/Plugin/WadoRs.cpp Tue Sep 24 21:45:02 2019 +0200 +++ b/Plugin/WadoRs.cpp Fri Sep 27 12:50:30 2019 +0200 @@ -442,34 +442,90 @@ + static void AnswerMetadata(OrthancPluginRestOutput* output, const OrthancPluginHttpRequest* request, Orthanc::ResourceType level, const std::string& resource, - bool isInstance, bool isXml) { + static const char* const ID = "ID"; + static const char* const INSTANCES = "Instances"; + OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); std::list instances; - if (isInstance) - { - instances.push_back(resource); - } - else + + switch (level) { - Json::Value children; - if (!OrthancPlugins::RestApiGet(children, GetResourceUri(level, resource) + "/instances", false)) + case Orthanc::ResourceType_Instance: + instances.push_back(resource); + break; + + case Orthanc::ResourceType_Series: { - // Internal error - OrthancPluginSendHttpStatusCode(context, output, 400); - return; + Json::Value series; + if (!OrthancPlugins::RestApiGet(series, GetResourceUri(level, resource), false)) + { + // Internal error + OrthancPluginSendHttpStatusCode(context, output, 400); + return; + } + + if (series.type() != Json::objectValue || + !series.isMember(INSTANCES) || + series[INSTANCES].type() != Json::arrayValue) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); + } + + for (Json::Value::ArrayIndex i = 0; i < series[INSTANCES].size(); i++) + { + if (series[INSTANCES][i].type() != Json::stringValue) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); + } + else + { + instances.push_back(series[INSTANCES][i].asString()); + } + } + break; } - for (Json::Value::ArrayIndex i = 0; i < children.size(); i++) + case Orthanc::ResourceType_Study: { - instances.push_back(children[i]["ID"].asString()); + Json::Value children; + if (!OrthancPlugins::RestApiGet(children, GetResourceUri(level, resource) + "/instances", false)) + { + // Internal error + OrthancPluginSendHttpStatusCode(context, output, 400); + return; + } + + if (children.type() != Json::arrayValue) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); + } + + for (Json::Value::ArrayIndex i = 0; i < children.size(); i++) + { + if (children[i].type() != Json::objectValue || + !children[i].isMember(ID) || + children[i][ID].type() != Json::stringValue) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); + } + else + { + instances.push_back(children[i][ID].asString()); + } + } + break; } + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); } const std::string wadoBase = OrthancPlugins::Configuration::GetBaseUrl(request); @@ -708,7 +764,7 @@ std::string publicId; if (LocateStudy(output, publicId, request)) { - AnswerMetadata(output, request, Orthanc::ResourceType_Study, publicId, false, isXml); + AnswerMetadata(output, request, Orthanc::ResourceType_Study, publicId, isXml); } } } @@ -728,7 +784,7 @@ std::string publicId; if (LocateSeries(output, publicId, request)) { - AnswerMetadata(output, request, Orthanc::ResourceType_Series, publicId, false, isXml); + AnswerMetadata(output, request, Orthanc::ResourceType_Series, publicId, isXml); } } } @@ -748,7 +804,7 @@ std::string publicId; if (LocateInstance(output, publicId, request)) { - AnswerMetadata(output, request, Orthanc::ResourceType_Instance, publicId, true, isXml); + AnswerMetadata(output, request, Orthanc::ResourceType_Instance, publicId, isXml); } } }