changeset 107:fea72403194b dev

refactoring, definition of peers in the configuration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 27 Apr 2016 17:24:23 +0200
parents 100c20770a25
children bcc9e98bb725
files CMakeLists.txt Plugin/DicomWebPeers.cpp Plugin/DicomWebPeers.h Plugin/Plugin.cpp Plugin/StowRsClient.cpp Plugin/StowRsClient.h
diffstat 6 files changed, 635 insertions(+), 269 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Apr 27 13:45:08 2016 +0200
+++ b/CMakeLists.txt	Wed Apr 27 17:24:23 2016 +0200
@@ -121,9 +121,11 @@
   )
 
 add_library(OrthancDicomWeb SHARED ${CORE_SOURCES}
+  ${CMAKE_SOURCE_DIR}/Plugin/DicomWebPeers.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/Plugin.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/QidoRs.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/StowRs.cpp
+  ${CMAKE_SOURCE_DIR}/Plugin/StowRsClient.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/WadoRs.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/WadoRsRetrieveFrames.cpp
   ${CMAKE_SOURCE_DIR}/Plugin/WadoUri.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugin/DicomWebPeers.cpp	Wed Apr 27 17:24:23 2016 +0200
@@ -0,0 +1,141 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "DicomWebPeers.h"
+
+#include "Plugin.h"
+#include "../Orthanc/Core/OrthancException.h"
+
+namespace OrthancPlugins
+{
+  void DicomWebPeer::SetUrl(const std::string& url)
+  {
+    if (url.empty())
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+    }
+
+    url_ = url;
+
+    // Add trailing slash
+    if (url_[url.size() - 1] != '/')
+    {
+      url_ += '/';
+    }
+  }
+
+
+  void DicomWebPeers::Clear()
+  {
+    for (Peers::iterator it = peers_.begin(); it != peers_.end(); ++it)
+    {
+      delete it->second;
+    }
+  }
+
+
+  void DicomWebPeers::Load(const Json::Value& configuration)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    Clear();
+
+    if (!configuration.isMember("Peers"))
+    {
+      return;
+    }
+
+    bool ok = true;
+
+    if (configuration["Peers"].type() != Json::objectValue)
+    {
+      ok = false;
+    }
+    else
+    {
+      Json::Value::Members members = configuration["Peers"].getMemberNames();
+
+      for (size_t i = 0; i < members.size(); i++)
+      {
+        const Json::Value& peer = configuration["Peers"][members[i]];
+
+        if (peer.type() != Json::arrayValue ||
+            (peer.size() != 1 && peer.size() != 3) ||
+            peer[0].type() != Json::stringValue ||
+            (peer.size() == 3 && peer[1].type() != Json::stringValue) ||
+            (peer.size() == 3 && peer[2].type() != Json::stringValue))
+        {
+          ok = false;
+          break;
+        }
+        else
+        {
+          peers_[members[i]] = new DicomWebPeer(peer[0].asString(),
+                                                peer[1].asString(),
+                                                peer[2].asString());
+        }
+      }
+    }
+
+    if (!ok)
+    {
+      OrthancPluginLogError(context_, "Cannot parse the \"DicomWeb.Peers\" section of the configuration file");
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+    }
+  }
+
+
+  DicomWebPeers& DicomWebPeers::GetInstance()
+  {
+    static DicomWebPeers singleton;
+    return singleton;
+  }
+
+
+  DicomWebPeer DicomWebPeers::GetPeer(const std::string& name)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    Peers::const_iterator peer = peers_.find(name);
+
+    if (peer == peers_.end() ||
+        peer->second == NULL)
+    {
+      std::string s = "Inexistent peer: " + name;
+      OrthancPluginLogError(context_, s.c_str());
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
+    }
+    else
+    {
+      return *peer->second;
+    }
+  }
+
+
+  void DicomWebPeers::ListPeers(std::list<std::string>& peers)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    peers.clear();
+    for (Peers::const_iterator it = peers_.begin(); it != peers_.end(); ++it)
+    {
+      peers.push_back(it->first);
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugin/DicomWebPeers.h	Wed Apr 27 17:24:23 2016 +0200
@@ -0,0 +1,108 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#pragma once
+
+#include <list>
+#include <string>
+#include <boost/thread/mutex.hpp>
+#include <json/value.h>
+
+namespace OrthancPlugins
+{
+  class DicomWebPeer
+  {
+  private:
+    std::string url_;
+    std::string username_;
+    std::string password_;
+
+    void SetUrl(const std::string& url);
+
+  public:
+    DicomWebPeer(const std::string& url,
+                 const std::string& username,
+                 const std::string& password) :
+      username_(username),
+      password_(password)
+    {
+      SetUrl(url);
+    }
+
+    DicomWebPeer(const std::string& url)
+    {
+      SetUrl(url);
+    }
+
+    const std::string& GetUrl() const
+    {
+      return url_;
+    }
+
+    const std::string& GetUsername() const
+    {
+      return username_;
+    }
+
+    const std::string& GetPassword() const
+    {
+      return password_;
+    }
+
+    const char* GetUsernameC() const
+    {
+      return username_.empty() ? NULL : username_.c_str();
+    }
+
+    const char* GetPasswordC() const
+    {
+      return password_.empty() ? NULL : password_.c_str();
+    }
+  };
+
+
+  class DicomWebPeers
+  {
+  private:
+    typedef std::map<std::string, DicomWebPeer*>  Peers;
+
+    boost::mutex  mutex_;
+    Peers         peers_;
+
+    void Clear();
+
+    DicomWebPeers()  // Forbidden (singleton pattern)
+    {
+    }
+
+  public:
+    void Load(const Json::Value& configuration);
+
+    ~DicomWebPeers()
+    {
+      Clear();
+    }
+
+    static DicomWebPeers& GetInstance();
+
+    DicomWebPeer GetPeer(const std::string& name);
+
+    void ListPeers(std::list<std::string>& peers);
+  };
+}
--- a/Plugin/Plugin.cpp	Wed Apr 27 13:45:08 2016 +0200
+++ b/Plugin/Plugin.cpp	Wed Apr 27 17:24:23 2016 +0200
@@ -22,10 +22,11 @@
 
 #include "QidoRs.h"
 #include "StowRs.h"
+#include "StowRsClient.h"
 #include "WadoRs.h"
 #include "WadoUri.h"
 #include "Configuration.h"
-
+#include "DicomWebPeers.h"
 
 #include <gdcmDictEntry.h>
 #include <gdcmDict.h>
@@ -33,12 +34,6 @@
 #include <gdcmGlobal.h>
 
 
-#include <json/reader.h>
-#include <list>
-#include "../Orthanc/Core/ChunkedBuffer.h"
-#include "../Orthanc/Core/Toolbox.h"
-
-
 // Global state
 OrthancPluginContext* context_ = NULL;
 Json::Value configuration_;
@@ -66,7 +61,7 @@
   catch (Orthanc::OrthancException& e)
   {
     OrthancPluginLogError(context_, e.What());
-    return OrthancPluginErrorCode_Plugin;
+    return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
   }
   catch (boost::bad_lexical_cast& e)
   {
@@ -139,285 +134,53 @@
 
 
 
-
-
-
-static void AddInstance(std::list<std::string>& target,
-                        const Json::Value& instance)
+void ListPeers(OrthancPluginRestOutput* output,
+               const char* url,
+               const OrthancPluginHttpRequest* request)
 {
-  if (instance.type() != Json::objectValue ||
-      !instance.isMember("ID") ||
-      instance["ID"].type() != Json::stringValue)
+  if (request->method != OrthancPluginHttpMethod_Get)
   {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+    OrthancPluginSendMethodNotAllowed(context_, output, "GET");
   }
   else
   {
-    target.push_back(instance["ID"].asString());
+    std::list<std::string> peers;
+    OrthancPlugins::DicomWebPeers::GetInstance().ListPeers(peers);
+
+    Json::Value json = Json::arrayValue;
+    for (std::list<std::string>::const_iterator it = peers.begin(); it != peers.end(); ++it)
+    {
+      json.append(*it);
+    }
+
+    std::string answer = json.toStyledString(); 
+    OrthancPluginAnswerBuffer(context_, output, answer.c_str(), answer.size(), "application/json");
   }
 }
 
 
-
-
-static bool GetSequenceSize(size_t& result,
-                            const Json::Value& answer,
-                            const std::string& tag,
-                            bool isMandatory,
-                            const std::string& peer)
+void ListPeerOperations(OrthancPluginRestOutput* output,
+                        const char* url,
+                        const OrthancPluginHttpRequest* request)
 {
-  const Json::Value* value = NULL;
-
-  std::string upper, lower;
-  Orthanc::Toolbox::ToUpperCase(upper, tag);
-  Orthanc::Toolbox::ToLowerCase(lower, tag);
-  
-  if (answer.isMember(upper))
+  if (request->method != OrthancPluginHttpMethod_Get)
   {
-    value = &answer[upper];
-  }
-  else if (answer.isMember(lower))
-  {
-    value = &answer[lower];
-  }
-  else if (isMandatory)
-  {
-    std::string s = ("The STOW-RS JSON response from DICOMweb peer " + peer + 
-                     " does not contain the mandatory tag " + upper);
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+    OrthancPluginSendMethodNotAllowed(context_, output, "GET");
   }
   else
   {
-    return false;
-  }
-
-  if (value->type() != Json::objectValue ||
-      !value->isMember("Value") ||
-      (*value) ["Value"].type() != Json::arrayValue)
-  {
-    std::string s = "Unable to parse STOW-RS JSON response from DICOMweb peer " + peer;
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-  }
-
-  result = (*value) ["Value"].size();
-  return true;
-}
-
-
-static void SendStowRequest(const std::string& url,
-                            const char* username,
-                            const char* password,
-                            const std::string& body,
-                            const std::string& mime,
-                            size_t countInstances)
-{
-  const char* headersKeys[] = {
-    "Accept",
-    "Expect",
-    "Content-Type"
-  };
-
-  const char* headersValues[] = {
-    "application/json",
-    "",
-    mime.c_str()
-  };
+    // Make sure the peer does exist
+    OrthancPlugins::DicomWebPeers::GetInstance().GetPeer(request->groups[0]);
 
-  uint16_t status = 0;
-  OrthancPluginMemoryBuffer answer;
-  OrthancPluginErrorCode code = OrthancPluginHttpClient(context_, &answer, &status, OrthancPluginHttpMethod_Post,
-                                                        url.c_str(), 3, headersKeys, headersValues,
-                                                        body.c_str(), body.size(), username, password, 0);
-  if (code != OrthancPluginErrorCode_Success ||
-      (status != 200 && status != 202))
-  {
-    std::string s = ("Cannot send DICOM images through STOW-RS to DICOMweb peer " + url + 
-                     " (HTTP status: " + boost::lexical_cast<std::string>(status) + ")");
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
-  }
-
-  Json::Value response;
-  Json::Reader reader;
-  bool success = reader.parse(reinterpret_cast<const char*>(answer.data),
-                              reinterpret_cast<const char*>(answer.data) + answer.size, response);
-  OrthancPluginFreeMemoryBuffer(context_, &answer);
-
-  if (!success ||
-      response.type() != Json::objectValue ||
-      !response.isMember("00081199"))
-  {
-    std::string s = "Unable to parse STOW-RS JSON response from DICOMweb peer " + url;
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-  }
+    Json::Value json = Json::arrayValue;
+    json.append("stow");
 
-  size_t size;
-  if (!GetSequenceSize(size, response, "00081199", true, url) ||
-      size != countInstances)
-  {
-    std::string s = ("The STOW-RS server was only able to receive " + 
-                     boost::lexical_cast<std::string>(size) + " instances out of " +
-                     boost::lexical_cast<std::string>(countInstances));
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-  }
-
-  if (GetSequenceSize(size, response, "00081198", false, url) &&
-      size != 0)
-  {
-    std::string s = ("The response from the STOW-RS server contains " + 
-                     boost::lexical_cast<std::string>(size) + 
-                     " items in its Failed SOP Sequence (0008,1198) tag");
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);    
-  }
-
-  if (GetSequenceSize(size, response, "0008119A", false, url) &&
-      size != 0)
-  {
-    std::string s = ("The response from the STOW-RS server contains " + 
-                     boost::lexical_cast<std::string>(size) + 
-                     " items in its Other Failures Sequence (0008,119A) tag");
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);    
+    std::string answer = json.toStyledString(); 
+    OrthancPluginAnswerBuffer(context_, output, answer.c_str(), answer.size(), "application/json");
   }
 }
 
 
-static void GetListOfInstances(std::list<std::string>& instances,
-                               const OrthancPluginHttpRequest* request)
-{
-  Json::Value resources;
-  Json::Reader reader;
-  if (!reader.parse(request->body, request->body + request->bodySize, resources) ||
-      resources.type() != Json::arrayValue)
-  {
-    std::string s = "The list of resources to be sent through DICOMweb STOW-RS must be given as a JSON array";
-    OrthancPluginLogError(context_, s.c_str());
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
-  }
-
-  // Extract information about all the child instances
-  for (Json::Value::ArrayIndex i = 0; i < resources.size(); i++)
-  {
-    if (resources[i].type() != Json::stringValue)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
-    }
-
-    std::string resource = resources[i].asString();
-
-    Json::Value tmp;
-    if (OrthancPlugins::RestApiGetJson(tmp, context_, "/instances/" + resource, false))
-    {
-      AddInstance(instances, tmp);
-    }
-    else if ((OrthancPlugins::RestApiGetJson(tmp, context_, "/series/" + resource, false) &&
-              OrthancPlugins::RestApiGetJson(tmp, context_, "/series/" + resource + "/instances", false)) ||
-             (OrthancPlugins::RestApiGetJson(tmp, context_, "/studies/" + resource, false) &&
-              OrthancPlugins::RestApiGetJson(tmp, context_, "/studies/" + resource + "/instances", false)) ||
-             (OrthancPlugins::RestApiGetJson(tmp, context_, "/patients/" + resource, false) &&
-              OrthancPlugins::RestApiGetJson(tmp, context_, "/patients/" + resource + "/instances", false)))
-    {
-      if (tmp.type() != Json::arrayValue)
-      {
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
-      }
-
-      for (Json::Value::ArrayIndex j = 0; j < tmp.size(); j++)
-      {
-        AddInstance(instances, tmp[j]);
-      }
-    }
-    else
-    {
-      // Unkown resource
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
-    }   
-  }
-}
-
-
-
-void StowClient(OrthancPluginRestOutput* output,
-                const char* /*url*/,
-                const OrthancPluginHttpRequest* request)
-{
-  if (request->groupsCount != 1)
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest);
-  }
-
-  if (request->method != OrthancPluginHttpMethod_Post)
-  {
-    OrthancPluginSendMethodNotAllowed(context_, output, "POST");
-    return;
-  }
-
-  {
-    std::string peer(request->groups[0]);
-    // TODO
-  }
-
-  std::string url = "http://localhost:8043/dicom-web/studies";
-
-
-  std::list<std::string> instances;
-  GetListOfInstances(instances, request);
-
-  {
-    std::string s = ("Sending " + boost::lexical_cast<std::string>(instances.size()) + 
-                     " instances using STOW-RS to DICOMweb server: " + url);
-    OrthancPluginLogInfo(context_, s.c_str());
-  }
-
-  std::string boundary;
-
-  {
-    char* uuid = OrthancPluginGenerateUuid(context_);
-    try
-    {
-      boundary.assign(uuid);
-    }
-    catch (...)
-    {
-      OrthancPluginFreeString(context_, uuid);
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
-    }
-
-    OrthancPluginFreeString(context_, uuid);
-  }
-
-  std::string mime = "multipart/related; type=application/dicom; boundary=" + boundary;
-
-  Orthanc::ChunkedBuffer chunks;
-
-  for (std::list<std::string>::const_iterator it = instances.begin(); it != instances.end(); it++)
-  {
-    std::string dicom;
-    if (OrthancPlugins::RestApiGetString(dicom, context_, "/instances/" + *it + "/file"))
-    {
-      chunks.AddChunk("\r\n--" + boundary + "\r\n" +
-                      "Content-Type: application/dicom\r\n" +
-                      "Content-Length: " + boost::lexical_cast<std::string>(dicom.size()) +
-                      "\r\n\r\n");
-      chunks.AddChunk(dicom);
-    }
-  }
-
-  chunks.AddChunk("\r\n--" + boundary + "--\r\n");
-
-  std::string body;
-  chunks.Flatten(body);
-
-  // TODO Split the message
-
-  SendStowRequest(url, NULL, NULL, body, mime, instances.size());
-}
-
 
 extern "C"
 {
@@ -461,6 +224,9 @@
       }
     }
 
+    OrthancPlugins::DicomWebPeers::GetInstance().Load(configuration_);
+
+
     // Configure the DICOMweb callbacks
     if (OrthancPlugins::Configuration::GetBoolValue(configuration_, "Enable", true))
     {
@@ -485,6 +251,8 @@
       Register(root, "studies/([^/]*)/series/([^/]*)/instances/([^/]*)/frames", Protect<RetrieveFrames>);
       Register(root, "studies/([^/]*)/series/([^/]*)/instances/([^/]*)/frames/([^/]*)", Protect<RetrieveFrames>);
 
+      Register(root, "peers", Protect<ListPeers>);
+      Register(root, "peers/([^/]*)", Protect<ListPeerOperations>);
       Register(root, "peers/([^/]*)/stow", Protect<StowClient>);
     }
     else
@@ -497,14 +265,14 @@
     {
       std::string wado = OrthancPlugins::Configuration::GetWadoRoot(configuration_);
 
-      std::string message = "URI to the WADO API: " + wado;
+      std::string message = "URI to the WADO-URI API: " + wado;
       OrthancPluginLogWarning(context_, message.c_str());
 
       OrthancPluginRegisterRestCallback(context_, wado.c_str(), Protect<WadoUriCallback>);
     }
     else
     {
-      OrthancPluginLogWarning(context_, "WADO support is disabled");
+      OrthancPluginLogWarning(context_, "WADO-URI support is disabled");
     }
 
     return 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugin/StowRsClient.cpp	Wed Apr 27 17:24:23 2016 +0200
@@ -0,0 +1,319 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "StowRsClient.h"
+
+#include "Plugin.h"
+#include "DicomWebPeers.h"
+
+#include <json/reader.h>
+#include <list>
+#include <boost/lexical_cast.hpp>
+
+#include "../Orthanc/Core/ChunkedBuffer.h"
+#include "../Orthanc/Core/OrthancException.h"
+#include "../Orthanc/Core/Toolbox.h"
+
+
+static void AddInstance(std::list<std::string>& target,
+                        const Json::Value& instance)
+{
+  if (instance.type() != Json::objectValue ||
+      !instance.isMember("ID") ||
+      instance["ID"].type() != Json::stringValue)
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+  }
+  else
+  {
+    target.push_back(instance["ID"].asString());
+  }
+}
+
+
+static bool GetSequenceSize(size_t& result,
+                            const Json::Value& answer,
+                            const std::string& tag,
+                            bool isMandatory,
+                            const std::string& peer)
+{
+  const Json::Value* value = NULL;
+
+  std::string upper, lower;
+  Orthanc::Toolbox::ToUpperCase(upper, tag);
+  Orthanc::Toolbox::ToLowerCase(lower, tag);
+  
+  if (answer.isMember(upper))
+  {
+    value = &answer[upper];
+  }
+  else if (answer.isMember(lower))
+  {
+    value = &answer[lower];
+  }
+  else if (isMandatory)
+  {
+    std::string s = ("The STOW-RS JSON response from DICOMweb peer " + peer + 
+                     " does not contain the mandatory tag " + upper);
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+  }
+  else
+  {
+    return false;
+  }
+
+  if (value->type() != Json::objectValue ||
+      !value->isMember("Value") ||
+      (*value) ["Value"].type() != Json::arrayValue)
+  {
+    std::string s = "Unable to parse STOW-RS JSON response from DICOMweb peer " + peer;
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+  }
+
+  result = (*value) ["Value"].size();
+  return true;
+}
+
+
+static void SendStowRequest(const OrthancPlugins::DicomWebPeer& peer,
+                            const std::string& mime,
+                            const std::string& body,
+                            size_t countInstances)
+{
+  const char* headersKeys[] = {
+    "Accept",
+    "Expect",
+    "Content-Type"
+  };
+
+  const char* headersValues[] = {
+    "application/json",
+    "",
+    mime.c_str()
+  };
+
+  std::string url = peer.GetUrl() + "studies";
+
+  uint16_t status = 0;
+  OrthancPluginMemoryBuffer answer;
+  OrthancPluginErrorCode code = OrthancPluginHttpClient(context_, &answer, &status, OrthancPluginHttpMethod_Post,
+                                                        url.c_str(), 3, headersKeys, headersValues,
+                                                        body.c_str(), body.size(), peer.GetUsernameC(), peer.GetPasswordC(), 0);
+  if (code != OrthancPluginErrorCode_Success ||
+      (status != 200 && status != 202))
+  {
+    std::string s = ("Cannot send DICOM images through STOW-RS to DICOMweb peer " + peer.GetUrl() + 
+                     " (HTTP status: " + boost::lexical_cast<std::string>(status) + ")");
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
+  }
+
+  Json::Value response;
+  Json::Reader reader;
+  bool success = reader.parse(reinterpret_cast<const char*>(answer.data),
+                              reinterpret_cast<const char*>(answer.data) + answer.size, response);
+  OrthancPluginFreeMemoryBuffer(context_, &answer);
+
+  if (!success ||
+      response.type() != Json::objectValue ||
+      !response.isMember("00081199"))
+  {
+    std::string s = "Unable to parse STOW-RS JSON response from DICOMweb peer " + peer.GetUrl();
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+  }
+
+  size_t size;
+  if (!GetSequenceSize(size, response, "00081199", true, peer.GetUrl()) ||
+      size != countInstances)
+  {
+    std::string s = ("The STOW-RS server was only able to receive " + 
+                     boost::lexical_cast<std::string>(size) + " instances out of " +
+                     boost::lexical_cast<std::string>(countInstances));
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+  }
+
+  if (GetSequenceSize(size, response, "00081198", false, peer.GetUrl()) &&
+      size != 0)
+  {
+    std::string s = ("The response from the STOW-RS server contains " + 
+                     boost::lexical_cast<std::string>(size) + 
+                     " items in its Failed SOP Sequence (0008,1198) tag");
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);    
+  }
+
+  if (GetSequenceSize(size, response, "0008119A", false, peer.GetUrl()) &&
+      size != 0)
+  {
+    std::string s = ("The response from the STOW-RS server contains " + 
+                     boost::lexical_cast<std::string>(size) + 
+                     " items in its Other Failures Sequence (0008,119A) tag");
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);    
+  }
+}
+
+
+static void GetListOfInstances(std::list<std::string>& instances,
+                               const OrthancPluginHttpRequest* request)
+{
+  Json::Value resources;
+  Json::Reader reader;
+  if (!reader.parse(request->body, request->body + request->bodySize, resources) ||
+      resources.type() != Json::arrayValue)
+  {
+    std::string s = "The list of resources to be sent through DICOMweb STOW-RS must be given as a JSON array";
+    OrthancPluginLogError(context_, s.c_str());
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+  }
+
+  // Extract information about all the child instances
+  for (Json::Value::ArrayIndex i = 0; i < resources.size(); i++)
+  {
+    if (resources[i].type() != Json::stringValue)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+    }
+
+    std::string resource = resources[i].asString();
+
+    Json::Value tmp;
+    if (OrthancPlugins::RestApiGetJson(tmp, context_, "/instances/" + resource, false))
+    {
+      AddInstance(instances, tmp);
+    }
+    else if ((OrthancPlugins::RestApiGetJson(tmp, context_, "/series/" + resource, false) &&
+              OrthancPlugins::RestApiGetJson(tmp, context_, "/series/" + resource + "/instances", false)) ||
+             (OrthancPlugins::RestApiGetJson(tmp, context_, "/studies/" + resource, false) &&
+              OrthancPlugins::RestApiGetJson(tmp, context_, "/studies/" + resource + "/instances", false)) ||
+             (OrthancPlugins::RestApiGetJson(tmp, context_, "/patients/" + resource, false) &&
+              OrthancPlugins::RestApiGetJson(tmp, context_, "/patients/" + resource + "/instances", false)))
+    {
+      if (tmp.type() != Json::arrayValue)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+
+      for (Json::Value::ArrayIndex j = 0; j < tmp.size(); j++)
+      {
+        AddInstance(instances, tmp[j]);
+      }
+    }
+    else
+    {
+      // Unkown resource
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
+    }   
+  }
+}
+
+
+static void SendStowChunks(const OrthancPlugins::DicomWebPeer& peer,
+                           const std::string& mime,
+                           const std::string& boundary,
+                           Orthanc::ChunkedBuffer& chunks,
+                           size_t& countInstances,
+                           bool force)
+{
+  if ((force && countInstances > 0) ||
+      countInstances > 10 /* TODO Parameter */ ||
+      chunks.GetNumBytes() > 10 * 1024 * 1024 /* TODO Parameter */)
+  {
+    chunks.AddChunk("\r\n--" + boundary + "--\r\n");
+
+    std::string body;
+    chunks.Flatten(body);
+
+    SendStowRequest(peer, mime, body, countInstances);
+    countInstances = 0;
+  }
+}
+
+
+void StowClient(OrthancPluginRestOutput* output,
+                const char* /*url*/,
+                const OrthancPluginHttpRequest* request)
+{
+  if (request->groupsCount != 1)
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest);
+  }
+
+  if (request->method != OrthancPluginHttpMethod_Post)
+  {
+    OrthancPluginSendMethodNotAllowed(context_, output, "POST");
+    return;
+  }
+
+  OrthancPlugins::DicomWebPeer peer(OrthancPlugins::DicomWebPeers::GetInstance().GetPeer(request->groups[0]));
+
+  std::list<std::string> instances;
+  GetListOfInstances(instances, request);
+
+  {
+    std::string s = ("Sending " + boost::lexical_cast<std::string>(instances.size()) + 
+                     " instances using STOW-RS to DICOMweb server: " + peer.GetUrl());
+    OrthancPluginLogInfo(context_, s.c_str());
+  }
+
+  std::string boundary;
+
+  {
+    char* uuid = OrthancPluginGenerateUuid(context_);
+    try
+    {
+      boundary.assign(uuid);
+    }
+    catch (...)
+    {
+      OrthancPluginFreeString(context_, uuid);
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
+    }
+
+    OrthancPluginFreeString(context_, uuid);
+  }
+
+  std::string mime = "multipart/related; type=application/dicom; boundary=" + boundary;
+
+  Orthanc::ChunkedBuffer chunks;
+  size_t countInstances = 0;
+
+  for (std::list<std::string>::const_iterator it = instances.begin(); it != instances.end(); it++)
+  {
+    std::string dicom;
+    if (OrthancPlugins::RestApiGetString(dicom, context_, "/instances/" + *it + "/file"))
+    {
+      chunks.AddChunk("\r\n--" + boundary + "\r\n" +
+                      "Content-Type: application/dicom\r\n" +
+                      "Content-Length: " + boost::lexical_cast<std::string>(dicom.size()) +
+                      "\r\n\r\n");
+      chunks.AddChunk(dicom);
+      countInstances ++;
+
+      SendStowChunks(peer, mime, boundary, chunks, countInstances, false);
+    }
+  }
+
+  SendStowChunks(peer, mime, boundary, chunks, countInstances, true);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugin/StowRsClient.h	Wed Apr 27 17:24:23 2016 +0200
@@ -0,0 +1,28 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "Configuration.h"
+
+
+void StowClient(OrthancPluginRestOutput* output,
+                const char* url,
+                const OrthancPluginHttpRequest* request);