changeset 298:2271b2474d4c refactoring

DicomWebServers::ConfigureHttpClient()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jun 2019 12:12:09 +0200 (2019-06-12)
parents f364490f9a95
children e31077162aa1
files Plugin/DicomWebServers.cpp Plugin/DicomWebServers.h Plugin/Plugin.cpp
diffstat 3 files changed, 116 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/DicomWebServers.cpp	Tue Jun 11 21:51:28 2019 +0200
+++ b/Plugin/DicomWebServers.cpp	Wed Jun 12 12:12:09 2019 +0200
@@ -116,6 +116,43 @@
   }
 
 
+  void DicomWebServers::ConfigureHttpClient(HttpClient& client,
+                                            const std::string& name,
+                                            const std::string& uri)
+  {
+    const Orthanc::WebServiceParameters parameters = GetServer(name);
+
+    std::string url = parameters.GetUrl();
+    
+    if (url.empty() ||
+        url[url.size() - 1] != '/')
+    {
+      url += '/';
+    }
+    
+    std::string normalizedUri = uri;
+    while (!normalizedUri.empty() &&
+           normalizedUri[0] == '/')
+    {
+      normalizedUri = normalizedUri.substr(1);
+    }
+    
+    client.SetUrl(url + normalizedUri);
+
+    for (Orthanc::WebServiceParameters::Dictionary::const_iterator
+           it = parameters.GetHttpHeaders().begin();
+         it != parameters.GetHttpHeaders().end(); ++it)
+    {
+      client.AddHeader(it->first, it->second);
+    }
+
+    if (!parameters.GetUsername().empty())
+    {
+      client.SetCredentials(parameters.GetUsername(), parameters.GetPassword());
+    }
+  }    
+
+
   static const char* ConvertToCString(const std::string& s)
   {
     if (s.empty())
--- a/Plugin/DicomWebServers.h	Tue Jun 11 21:51:28 2019 +0200
+++ b/Plugin/DicomWebServers.h	Wed Jun 12 12:12:09 2019 +0200
@@ -57,6 +57,10 @@
     Orthanc::WebServiceParameters GetServer(const std::string& name);
 
     void ListServers(std::list<std::string>& servers);
+
+    void ConfigureHttpClient(HttpClient& client,
+                             const std::string& name,
+                             const std::string& uri);
   };
 
 
--- a/Plugin/Plugin.cpp	Tue Jun 11 21:51:28 2019 +0200
+++ b/Plugin/Plugin.cpp	Wed Jun 12 12:12:09 2019 +0200
@@ -139,7 +139,7 @@
 public:
   StowClientBody() :
     position_(0),
-    boundary_(Orthanc::Toolbox::GenerateUuid())
+    boundary_(Orthanc::Toolbox::GenerateUuid() + "-" + Orthanc::Toolbox::GenerateUuid())
   {
     //boost::filesystem::path p("/home/jodogne/DICOM/Demo/KNIX/Knee (R)/AX.  FSE PD - 5");
     boost::filesystem::path p("/tmp/dicom");
@@ -198,31 +198,83 @@
   {
     try
     {
-      StowClientBody stow;
+#if 0
+      {
+        StowClientBody stow;
       
-      OrthancPlugins::HttpClient client;
-      client.SetUrl("http://localhost:8080/dicom-web/studies");
-      client.SetMethod(OrthancPluginHttpMethod_Post);
-      client.AddHeader("Accept", "application/dicom+json");
-      client.AddHeader("Expect", "");
-      client.AddHeader("Content-Type", "multipart/related; type=application/dicom; boundary=" + stow.GetBoundary());
-      client.SetTimeout(120);
-      client.SetBody(stow);
+        OrthancPlugins::HttpClient client;
+        client.SetUrl("http://localhost:8080/dicom-web/studies");
+        client.SetMethod(OrthancPluginHttpMethod_Post);
+        client.AddHeader("Accept", "application/dicom+json");
+        client.AddHeader("Expect", "");
+        client.AddHeader("Content-Type", "multipart/related; type=application/dicom; boundary=" + stow.GetBoundary());
+        client.SetTimeout(120);
+        client.SetBody(stow);
+
+        OrthancPlugins::HttpClient::HttpHeaders headers;
+        std::string answer;
+        client.Execute(headers, answer);
+
+        Json::Value v;
+        Json::Reader reader;
+        if (reader.parse(answer, v))
+        {
+          std::cout << v["00081190"].toStyledString() << std::endl;
+        }
+        else
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+        }
+      }
+#endif
+
+#if 0
+      {
+        OrthancPlugins::HttpClient client;
+        OrthancPlugins::DicomWebServers::GetInstance().ConfigureHttpClient(client, "google", "/studies");
+
+        OrthancPlugins::HttpClient::HttpHeaders headers;
+        Json::Value body;
+        client.Execute(headers, body);
 
-      OrthancPlugins::HttpClient::HttpHeaders headers;
-      std::string answer;
-      client.Execute(headers, answer);
+        std::cout << body.toStyledString() << std::endl;
+      }
+#endif
+
+#if 0
+      {
+        OrthancPlugins::HttpClient client;
+        OrthancPlugins::DicomWebServers::GetInstance().ConfigureHttpClient(client, "self", "/studies");
+
+        client.SetMethod(OrthancPluginHttpMethod_Post);
+        client.AddHeader("Accept", "application/dicom+json");
+        client.AddHeader("Expect", "");
+
+        std::string boundary = Orthanc::Toolbox::GenerateUuid() + "-" + Orthanc::Toolbox::GenerateUuid();
+        client.AddHeader("Content-Type", "multipart/related; type=application/dicom; boundary=" + boundary);
 
-      Json::Value v;
-      Json::Reader reader;
-      if (reader.parse(answer, v))
-      {
-        std::cout << v["00081190"].toStyledString() << std::endl;
+        std::string f;
+        Orthanc::SystemToolbox::ReadFile(f, "/home/jodogne/Subversion/orthanc-tests/Database/Encodings/DavidClunie/SCSI2"); // Korean
+        //Orthanc::SystemToolbox::ReadFile(f, "/home/jodogne/Subversion/orthanc-tests/Database/Encodings/DavidClunie/SCSH31"); // Kanji
+        //Orthanc::SystemToolbox::ReadFile(f, "/home/jodogne/DICOM/Alain.dcm");
+        
+        std::string body;
+        body += ("--" + boundary + "\r\nContent-Type: application/dicom\r\nContent-Length: " +
+                 boost::lexical_cast<std::string>(f.size()) + "\r\n\r\n");
+        body += f;
+        body += "\r\n--" + boundary + "--";
+
+        Orthanc::SystemToolbox::WriteFile(body, "/tmp/toto");
+        
+        client.SetBody(body);
+        
+        OrthancPlugins::HttpClient::HttpHeaders headers;
+        Json::Value answer;
+        client.Execute(headers, answer);
+
+        std::cout << answer.toStyledString() << std::endl;
       }
-      else
-      {
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
-      }
+#endif
     }
     catch (Orthanc::OrthancException& e)
     {
@@ -262,7 +314,7 @@
       // Read the configuration
       OrthancPlugins::Configuration::Initialize();
 
-      //OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);  // TODO => REMOVE
+      OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);  // TODO => REMOVE
 
       // Initialize GDCM
       OrthancPlugins::GdcmParsedDicomFile::Initialize();