changeset 4339:fc5caed6f940

"/tools/dicom-echo": Execute C-Echo SCU to a modality that is not registered in "/modalities"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 03 Dec 2020 11:47:03 +0100
parents 1263e727d048
children 6fa8bb987be2
files NEWS OrthancServer/Sources/OrthancRestApi/OrthancRestModalities.cpp
diffstat 2 files changed, 48 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Dec 03 10:43:31 2020 +0100
+++ b/NEWS	Thu Dec 03 11:47:03 2020 +0100
@@ -12,6 +12,7 @@
 --------
 
 * API version upgraded to 9
+* "/tools/dicom-echo": Execute C-Echo SCU to a modality that is not registered in "/modalities"
 * "/tools/log-level-*": Dynamically access and/or change the verbosity of logging categories
 * "/peers/{id}/configuration": Get the configuration of one peer (cf. "/peers?expand")
 * "/modalities/{id}/configuration": Get the configuration of one modality (cf. "/modalities?expand")
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestModalities.cpp	Thu Dec 03 10:43:31 2020 +0100
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestModalities.cpp	Thu Dec 03 11:47:03 2020 +0100
@@ -73,11 +73,8 @@
   static void InjectAssociationTimeout(DicomAssociationParameters& params,
                                        const Json::Value& body)
   {
-    if (body.type() != Json::objectValue)
-    {
-      throw OrthancException(ErrorCode_BadFileFormat, "Must provide a JSON object");
-    }
-    else if (body.isMember(KEY_TIMEOUT))
+    if (body.type() == Json::objectValue &&
+        body.isMember(KEY_TIMEOUT))
     {
       // New in Orthanc 1.7.0
       params.SetTimeout(SerializationToolbox::ReadUnsignedInteger(body, KEY_TIMEOUT));
@@ -102,7 +99,12 @@
   static DicomAssociationParameters GetAssociationParameters(RestApiPostCall& call)
   {
     Json::Value body;
-    call.ParseJsonRequest(body);
+
+    if (!call.ParseJsonRequest(body))
+    {
+      throw OrthancException(ErrorCode_BadFileFormat, "Cannot parse the JSON body");
+    }
+      
     return GetAssociationParameters(call, body);
   }
   
@@ -111,23 +113,52 @@
    * DICOM C-Echo SCU
    ***************************************************************************/
 
-  static void DicomEcho(RestApiPostCall& call)
+  static void ExecuteEcho(RestApiOutput& output,
+                          const DicomAssociationParameters& parameters)
   {
-    DicomControlUserConnection connection(GetAssociationParameters(call));
+    DicomControlUserConnection connection(parameters);
 
     if (connection.Echo())
     {
       // Echo has succeeded
-      call.GetOutput().AnswerBuffer("{}", MimeType_Json);
+      output.AnswerBuffer("{}", MimeType_Json);
       return;
     }
     else
     {
       // Echo has failed
-      call.GetOutput().SignalError(HttpStatus_500_InternalServerError);
+      output.SignalError(HttpStatus_500_InternalServerError);
     }
   }
+  
+  
+  static void DicomEcho(RestApiPostCall& call)
+  {
+    ExecuteEcho(call.GetOutput(), GetAssociationParameters(call));
+  }
+  
 
+  static void DicomEchoTool(RestApiPostCall& call)
+  {
+    Json::Value body;
+    if (call.ParseJsonRequest(body))
+    {
+      RemoteModalityParameters modality;
+      modality.Unserialize(body);
+
+      const std::string& localAet =
+        OrthancRestApi::GetContext(call).GetDefaultLocalApplicationEntityTitle();
+      
+      DicomAssociationParameters params(localAet, modality);
+      InjectAssociationTimeout(params, body);
+
+      ExecuteEcho(call.GetOutput(), params);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadFileFormat, "Cannot parse the JSON body");
+    }
+  }
 
 
   /***************************************************************************
@@ -1295,6 +1326,10 @@
 
       call.GetOutput().AnswerBuffer("", MimeType_PlainText);
     }
+    else
+    {
+      throw OrthancException(ErrorCode_BadFileFormat);
+    }
   }
 
 
@@ -1686,5 +1721,7 @@
     Register("/modalities/{id}/storage-commitment", StorageCommitmentScu);
     Register("/storage-commitment/{id}", GetStorageCommitmentReport);
     Register("/storage-commitment/{id}/remove", RemoveAfterStorageCommitment);
+
+    Register("/tools/dicom-echo", DicomEchoTool);  // New in 1.8.1
   }
 }