diff OrthancServer/OrthancRestApi/OrthancRestApi.cpp @ 2867:251614c2edac

DicomMoveScuJob
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Oct 2018 16:08:51 +0200
parents a21b244efb37
children 5dd649de253d
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Mon Oct 08 11:40:31 2018 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Mon Oct 08 16:08:51 2018 +0200
@@ -35,6 +35,7 @@
 #include "OrthancRestApi.h"
 
 #include "../../Core/Logging.h"
+#include "../../Core/SerializationToolbox.h"
 #include "../ServerContext.h"
 
 namespace Orthanc
@@ -139,4 +140,102 @@
   {
     return GetContext(call).GetIndex();
   }
+
+
+
+  static const char* KEY_PERMISSIVE = "Permissive";
+  static const char* KEY_PRIORITY = "Priority";
+  static const char* KEY_SYNCHRONOUS = "Synchronous";
+  static const char* KEY_ASYNCHRONOUS = "Asynchronous";
+  
+  void OrthancRestApi::SubmitCommandsJob(RestApiPostCall& call,
+                                         SetOfCommandsJob* job,
+                                         bool isDefaultSynchronous,
+                                         const Json::Value& body) const
+  {
+    std::auto_ptr<SetOfCommandsJob> raii(job);
+    
+    if (job == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    if (body.type() != Json::objectValue)
+    {
+      throw OrthancException(ErrorCode_BadFileFormat);
+    }
+
+    job->SetDescription("REST API");
+    
+    if (body.isMember(KEY_PERMISSIVE))
+    {
+      job->SetPermissive(SerializationToolbox::ReadBoolean(body, KEY_PERMISSIVE));
+    }
+    else
+    {
+      job->SetPermissive(false);
+    }
+
+    int priority = 0;
+
+    if (body.isMember(KEY_PRIORITY))
+    {
+      priority = SerializationToolbox::ReadInteger(body, KEY_PRIORITY);
+    }
+
+    bool synchronous = isDefaultSynchronous;
+    
+    if (body.isMember(KEY_SYNCHRONOUS))
+    {
+      synchronous = SerializationToolbox::ReadBoolean(body, KEY_SYNCHRONOUS);
+    }
+    else if (body.isMember(KEY_ASYNCHRONOUS))
+    {
+      synchronous = !SerializationToolbox::ReadBoolean(body, KEY_ASYNCHRONOUS);
+    }
+
+    if (synchronous)
+    {
+      Json::Value successContent;
+      if (context_.GetJobsEngine().GetRegistry().SubmitAndWait
+          (successContent, raii.release(), priority))
+      {
+        // Success in synchronous execution
+        call.GetOutput().AnswerJson(successContent);
+      }
+      else
+      {
+        // Error during synchronous execution
+        call.GetOutput().SignalError(HttpStatus_500_InternalServerError);
+      }
+    }
+    else
+    {
+      // Asynchronous mode: Submit the job, but don't wait for its completion
+      std::string id;
+      context_.GetJobsEngine().GetRegistry().Submit(id, raii.release(), priority);
+
+      Json::Value v;
+      v["ID"] = id;
+      v["Path"] = "/jobs/" + id;
+      call.GetOutput().AnswerJson(v);
+    }
+  }
+  
+
+  void OrthancRestApi::SubmitCommandsJob(RestApiPostCall& call,
+                                         SetOfCommandsJob* job,
+                                         bool isDefaultSynchronous) const
+  {
+    std::auto_ptr<SetOfCommandsJob> raii(job);
+    
+    Json::Value body;
+    
+    if (!call.ParseJsonRequest(body))
+    {
+      body = Json::objectValue;
+    }
+
+    SubmitCommandsJob(call, raii.release(), isDefaultSynchronous, body);
+  }
 }