changeset 579:9fa6f5098127

improved bulk data negotiation
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Jun 2023 11:40:34 +0200
parents a083f083f2ac
children 1a5b52c8e785
files Plugin/WadoRs.cpp
diffstat 1 files changed, 48 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/WadoRs.cpp	Wed Jun 28 11:18:19 2023 +0200
+++ b/Plugin/WadoRs.cpp	Wed Jun 28 11:40:34 2023 +0200
@@ -269,47 +269,63 @@
 
 
 
-static bool AcceptBulkData(const OrthancPluginHttpRequest* request)
+namespace
 {
+  class BulkDataNegotiation : public Orthanc::HttpContentNegociation::IHandler
+  {
+  public:
+    virtual void Handle(const std::string& type,
+                        const std::string& subtype,
+                        const Orthanc::HttpContentNegociation::Dictionary& parameters) ORTHANC_OVERRIDE
+    {
+      assert(type == "multipart" &&
+             subtype == "related");
+
+      Orthanc::HttpContentNegociation::Dictionary::const_iterator found = parameters.find("type");
+
+      if (found != parameters.end())
+      {
+        std::string s = found->second;
+        Orthanc::Toolbox::ToLowerCase(s);
+        if (s != "application/octet-stream")
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
+                                          "This WADO-RS plugin only supports application/octet-stream "
+                                          "return type for bulk data retrieval (" + found->second + ")");
+        }
+      }
+
+      if (parameters.find("range") != parameters.end())
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
+                                        "This WADO-RS plugin does not support Range retrieval, "
+                                        "it can only return entire bulk data object");
+      }
+    }
+  };
+}
+
+
+static void AcceptBulkData(const OrthancPluginHttpRequest* request)
+{
+  // By default, return "multipart/related; type=application/octet-stream;"
+
   std::string accept;
 
-  if (!OrthancPlugins::LookupHttpHeader(accept, request, "accept"))
+  if (OrthancPlugins::LookupHttpHeader(accept, request, "accept"))
   {
-    return true;   // By default, return "multipart/related; type=application/octet-stream;"
-  }
+    Orthanc::HttpContentNegociation negotiation;
 
-  std::string application;
-  std::map<std::string, std::string> attributes;
-  OrthancPlugins::ParseContentType(application, attributes, accept);
+    BulkDataNegotiation bulk;
+    negotiation.Register("multipart/related", bulk);
 
-  if (application != "multipart/related" &&
-      application != "*/*")
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
+    if (!negotiation.Apply(accept))
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
                                     "This WADO-RS plugin cannot generate the following "
                                     "bulk data type: " + accept);
-  }
-
-  if (attributes.find("type") != attributes.end())
-  {
-    std::string s = attributes["type"];
-    Orthanc::Toolbox::ToLowerCase(s);
-    if (s != "application/octet-stream")
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
-                                      "This WADO-RS plugin only supports application/octet-stream "
-                                      "return type for bulk data retrieval (" + accept + ")");
     }
   }
-
-  if (attributes.find("range") != attributes.end())
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest,
-                                    "This WADO-RS plugin does not support Range retrieval, "
-                                    "it can only return entire bulk data object");
-  }
-
-  return true;
 }
 
 
@@ -1209,11 +1225,7 @@
 {
   OrthancPluginContext* context = OrthancPlugins::GetGlobalContext();
 
-  if (!AcceptBulkData(request))
-  {
-    OrthancPluginSendHttpStatusCode(context, output, 400 /* Bad request */);
-    return;
-  }
+  AcceptBulkData(request);
 
   std::string orthancId, studyInstanceUid, seriesInstanceUid, sopInstanceUid;
   OrthancPlugins::MemoryBuffer content;