changeset 2974:5f64ecbdfcb4

merge
author am@osimis.io
date Thu, 06 Dec 2018 10:39:27 +0100
parents 9b734c3e1095 (current diff) 4be6a2b46f1e (diff)
children e62e296a5714
files
diffstat 4 files changed, 88 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomFormat/DicomInstanceHasher.cpp	Thu Dec 06 10:39:08 2018 +0100
+++ b/Core/DicomFormat/DicomInstanceHasher.cpp	Thu Dec 06 10:39:27 2018 +0100
@@ -53,7 +53,7 @@
         seriesUid_.size() == 0 ||
         instanceUid_.size() == 0)
     {
-      throw OrthancException(ErrorCode_BadFileFormat);
+      throw OrthancException(ErrorCode_BadFileFormat, "missing StudyInstanceUID, SeriesInstanceUID or SOPInstanceUID");
     }
   }
 
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Thu Dec 06 10:39:08 2018 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Thu Dec 06 10:39:27 2018 +0100
@@ -196,6 +196,20 @@
     }
   }
 
+  
+  unsigned int OrthancRestApi::GetJobRequestPriority(const Json::Value& body)
+  {
+    if (body.type() != Json::objectValue ||
+        !body.isMember(KEY_PRIORITY))
+    {
+      return 0;   // Default priority
+    }
+    else 
+    {
+      return SerializationToolbox::ReadInteger(body, KEY_PRIORITY);
+    }
+  }
+  
 
   void OrthancRestApi::SubmitGenericJob(RestApiPostCall& call,
                                         IJob* job,
@@ -214,18 +228,11 @@
       throw OrthancException(ErrorCode_BadFileFormat);
     }
 
-    int priority = 0;
-
-    if (body.isMember(KEY_PRIORITY))
-    {
-      priority = SerializationToolbox::ReadInteger(body, KEY_PRIORITY);
-    }
-
     if (IsSynchronousJobRequest(isDefaultSynchronous, body))
     {
       Json::Value successContent;
       if (context_.GetJobsEngine().GetRegistry().SubmitAndWait
-          (successContent, raii.release(), priority))
+          (successContent, raii.release(), GetJobRequestPriority(body)))
       {
         // Success in synchronous execution
         call.GetOutput().AnswerJson(successContent);
@@ -240,7 +247,8 @@
     {
       // Asynchronous mode: Submit the job, but don't wait for its completion
       std::string id;
-      context_.GetJobsEngine().GetRegistry().Submit(id, raii.release(), priority);
+      context_.GetJobsEngine().GetRegistry().Submit
+        (id, raii.release(), GetJobRequestPriority(body));
 
       Json::Value v;
       v["ID"] = id;
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.h	Thu Dec 06 10:39:08 2018 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.h	Thu Dec 06 10:39:27 2018 +0100
@@ -106,6 +106,8 @@
     static bool IsSynchronousJobRequest(bool isDefaultSynchronous,
                                         const Json::Value& body);
     
+    static unsigned int GetJobRequestPriority(const Json::Value& body);
+    
     void SubmitGenericJob(RestApiPostCall& call,
                           IJob* job,
                           bool isDefaultSynchronous,
--- a/OrthancServer/OrthancRestApi/OrthancRestArchive.cpp	Thu Dec 06 10:39:08 2018 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestArchive.cpp	Thu Dec 06 10:39:27 2018 +0100
@@ -96,12 +96,15 @@
 
   static void GetJobParameters(bool& synchronous,         /* out */
                                bool& extended,            /* out */
+                               int& priority,             /* out */
                                const Json::Value& body,   /* in */
                                const bool defaultExtended /* in */)
   {
     synchronous = OrthancRestApi::IsSynchronousJobRequest
       (true /* synchronous by default */, body);
 
+    priority = OrthancRestApi::GetJobRequestPriority(body);
+
     if (body.type() == Json::objectValue &&
         body.isMember(KEY_EXTENDED))
     {
@@ -114,9 +117,11 @@
   }
 
 
-  static void SubmitJob(RestApiCall& call,
+  static void SubmitJob(RestApiOutput& output,
                         ServerContext& context,
                         std::auto_ptr<ArchiveJob>& job,
+                        int priority,
+                        bool synchronous,
                         const std::string& filename)
   {
     if (job.get() == NULL)
@@ -126,29 +131,38 @@
 
     job->SetDescription("REST API");
 
-    boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile);
-    job->SetSynchronousTarget(tmp);
-    
-    Json::Value publicContent;
-    if (context.GetJobsEngine().GetRegistry().SubmitAndWait
-        (publicContent, job.release(), 0 /* TODO priority */))
+    if (synchronous)
     {
-      // The archive is now created: Prepare the sending of the ZIP file
-      FilesystemHttpSender sender(tmp->GetPath());
-      sender.SetContentType(MimeType_Gzip);
-      sender.SetContentFilename(filename);
+      boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile);
+      job->SetSynchronousTarget(tmp);
+    
+      Json::Value publicContent;
+      if (context.GetJobsEngine().GetRegistry().SubmitAndWait
+          (publicContent, job.release(), priority))
+      {
+        // The archive is now created: Prepare the sending of the ZIP file
+        FilesystemHttpSender sender(tmp->GetPath());
+        sender.SetContentType(MimeType_Gzip);
+        sender.SetContentFilename(filename);
 
-      // Send the ZIP
-      call.GetOutput().AnswerStream(sender);
+        // Send the ZIP
+        output.AnswerStream(sender);
+      }
+      else
+      {
+        output.SignalError(HttpStatus_500_InternalServerError);
+      }
     }
     else
     {
-      call.GetOutput().SignalError(HttpStatus_500_InternalServerError);
-    }      
+      throw OrthancException(ErrorCode_NotImplemented);
+    }
   }
 
   
-  static void CreateBatchArchive(RestApiPostCall& call)
+  template <bool IS_MEDIA,
+            bool DEFAULT_IS_EXTENDED  /* only makes sense for media (i.e. not ZIP archives) */ >
+  static void CreateBatch(RestApiPostCall& call)
   {
     ServerContext& context = OrthancRestApi::GetContext(call);
 
@@ -156,34 +170,12 @@
     if (call.ParseJsonRequest(body))
     {
       bool synchronous, extended;
-      GetJobParameters(synchronous, extended, body, false /* by default, not extended */);
+      int priority;
+      GetJobParameters(synchronous, extended, priority, body, DEFAULT_IS_EXTENDED);
       
-      std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended));
+      std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended));
       AddResourcesOfInterest(*job, body);
-      SubmitJob(call, context, job, "Archive.zip");
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_BadFileFormat,
-                             "Expected a list of resources to archive in the body");
-    }
-  }  
-
-  
-  template <bool DEFAULT_EXTENDED>
-  static void CreateBatchMedia(RestApiPostCall& call)
-  {
-    ServerContext& context = OrthancRestApi::GetContext(call);
-
-    Json::Value body;
-    if (call.ParseJsonRequest(body))
-    {
-      bool synchronous, extended;
-      GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED);
-      
-      std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended));
-      AddResourcesOfInterest(*job, body);
-      SubmitJob(call, context, job, "Archive.zip");
+      SubmitJob(call.GetOutput(), context, job, priority, synchronous, "Archive.zip");
     }
     else
     {
@@ -193,44 +185,53 @@
   }
   
 
-  static void CreateArchive(RestApiGetCall& call)
-  {
-    ServerContext& context = OrthancRestApi::GetContext(call);
-
-    std::string id = call.GetUriComponent("id", "");
-
-    std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false));
-    job->AddResource(id);
-
-    SubmitJob(call, context, job, id + ".zip");
-  }
-
-
-  static void CreateMedia(RestApiGetCall& call)
+  template <bool IS_MEDIA,
+            bool DEFAULT_IS_EXTENDED  /* only makes sense for media (i.e. not ZIP archives) */ >
+  static void CreateSingleGet(RestApiGetCall& call)
   {
     ServerContext& context = OrthancRestApi::GetContext(call);
 
     std::string id = call.GetUriComponent("id", "");
 
-    std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended")));
+    bool extended;
+    if (IS_MEDIA)
+    {
+      extended = call.HasArgument("extended");
+    }
+    else
+    {
+      extended = false;
+    }
+    
+    std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended));
     job->AddResource(id);
 
-    SubmitJob(call, context, job, id + ".zip");
+    SubmitJob(call.GetOutput(), context, job, 0 /* priority */,
+              true /* synchronous */, id + ".zip");
   }
 
 
   void OrthancRestApi::RegisterArchive()
   {
-    Register("/patients/{id}/archive", CreateArchive);
-    Register("/studies/{id}/archive", CreateArchive);
-    Register("/series/{id}/archive", CreateArchive);
+    Register("/patients/{id}/archive",
+             CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>);
+    Register("/studies/{id}/archive",
+             CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>);
+    Register("/series/{id}/archive",
+             CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>);
 
-    Register("/patients/{id}/media", CreateMedia);
-    Register("/studies/{id}/media", CreateMedia);
-    Register("/series/{id}/media", CreateMedia);
+    Register("/patients/{id}/media",
+             CreateSingleGet<true /* media */, false /* not extended by default */>);
+    Register("/studies/{id}/media",
+             CreateSingleGet<true /* media */, false /* not extended by default */>);
+    Register("/series/{id}/media",
+             CreateSingleGet<true /* media */, false /* not extended by default */>);
 
-    Register("/tools/create-archive", CreateBatchArchive);
-    Register("/tools/create-media", CreateBatchMedia<false>);
-    Register("/tools/create-media-extended", CreateBatchMedia<true>);
+    Register("/tools/create-archive",
+             CreateBatch<false /* ZIP */,  false /* extended makes no sense in ZIP */>);
+    Register("/tools/create-media",
+             CreateBatch<true /* media */, false /* not extended by default */>);
+    Register("/tools/create-media-extended",
+             CreateBatch<true /* media */, true /* extended by default */>);
   }
 }