changeset 1572:904096e7367e

More information about the origin request in OnStoredInstance() callbacks
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 12:10:12 +0200
parents 3232f1c995a5
children 3309878b3e16
files Core/Enumerations.cpp Core/Enumerations.h NEWS OrthancServer/DicomInstanceToStore.cpp OrthancServer/DicomInstanceToStore.h OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp OrthancServer/OrthancRestApi/OrthancRestApi.cpp OrthancServer/Scheduler/ModifyInstanceCommand.cpp OrthancServer/Scheduler/ModifyInstanceCommand.h OrthancServer/main.cpp Plugins/Engine/OrthancPlugins.cpp Resources/Samples/Lua/AutoroutingConditional.lua
diffstat 14 files changed, 163 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/Core/Enumerations.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -374,6 +374,9 @@
   {
     switch (origin)
     {
+      case RequestOrigin_Unknown:
+        return "Unknown";
+
       case RequestOrigin_DicomProtocol:
         return "DicomProtocol";
 
--- a/Core/Enumerations.h	Tue Aug 25 11:04:19 2015 +0200
+++ b/Core/Enumerations.h	Tue Aug 25 12:10:12 2015 +0200
@@ -302,6 +302,7 @@
 
   enum RequestOrigin
   {
+    RequestOrigin_Unknown,
     RequestOrigin_DicomProtocol,
     RequestOrigin_Http,
     RequestOrigin_Plugins,
--- a/NEWS	Tue Aug 25 11:04:19 2015 +0200
+++ b/NEWS	Tue Aug 25 12:10:12 2015 +0200
@@ -8,6 +8,11 @@
 * Support of "deflate" and "gzip" content-types in HTTP requests
 * Options to validate peers against CA certificates in HTTPS requests
 
+Lua
+---
+
+* More information about the origin request in "OnStoredInstance()" callbacks
+
 Plugins
 -------
 
--- a/OrthancServer/DicomInstanceToStore.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/DicomInstanceToStore.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -171,4 +171,96 @@
 
     return json_.GetConstContent();
   }
+
+
+
+  void DicomInstanceToStore::GetOriginInformation(Json::Value& result) const
+  {
+    result = Json::objectValue;
+    result["RequestOrigin"] = EnumerationToString(origin_);
+
+    switch (origin_)
+    {
+      case RequestOrigin_Unknown:
+      {
+        // None of the methods "SetDicomProtocolOrigin()", "SetHttpOrigin()",
+        // "SetLuaOrigin()" or "SetPluginsOrigin()" was called!
+        throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      }
+
+      case RequestOrigin_DicomProtocol:
+      {
+        result["RemoteAet"] = dicomRemoteAet_;
+        result["CalledAet"] = dicomCalledAet_;
+        break;
+      }
+
+      case RequestOrigin_Http:
+      {
+        result["RemoteIp"] = httpRemoteIp_;
+        result["Username"] = httpUsername_;
+        break;
+      }
+
+      case RequestOrigin_Lua:
+      case RequestOrigin_Plugins:
+      {
+        // No additional information available for these kinds of requests
+        break;
+      }
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
+  void DicomInstanceToStore::SetDicomProtocolOrigin(const char* remoteAet,
+                                                    const char* calledAet)
+  {
+    origin_ = RequestOrigin_DicomProtocol;
+    dicomRemoteAet_ = remoteAet;
+    dicomCalledAet_ = calledAet;
+  }
+
+  void DicomInstanceToStore::SetRestOrigin(const RestApiCall& call)
+  {
+    origin_ = call.GetRequestOrigin();
+
+    if (origin_ == RequestOrigin_Http)
+    {
+      httpRemoteIp_ = call.GetRemoteIp();
+      httpUsername_ = call.GetUsername();
+    }
+  }
+
+  void DicomInstanceToStore::SetHttpOrigin(const char* remoteIp,
+                                           const char* username)
+  {
+    origin_ = RequestOrigin_Http;
+    httpRemoteIp_ = remoteIp;
+    httpUsername_ = username;
+  }
+
+  void DicomInstanceToStore::SetLuaOrigin()
+  {
+    origin_ = RequestOrigin_Lua;
+  }
+
+  void DicomInstanceToStore::SetPluginsOrigin()
+  {
+    origin_ = RequestOrigin_Plugins;
+  }
+
+  const char* DicomInstanceToStore::GetRemoteAet() const
+  {
+    if (origin_ == RequestOrigin_DicomProtocol)
+    {
+      return dicomRemoteAet_.c_str();
+    }
+    else
+    {
+      return "";
+    }
+  }
 }
--- a/OrthancServer/DicomInstanceToStore.h	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/DicomInstanceToStore.h	Tue Aug 25 12:10:12 2015 +0200
@@ -35,6 +35,7 @@
 #include "ParsedDicomFile.h"
 #include "ServerIndex.h"
 #include "../Core/OrthancException.h"
+#include "../Core/RestApi/RestApiCall.h"
 
 namespace Orthanc
 {
@@ -143,13 +144,34 @@
     SmartContainer<DicomMap>  summary_;
     SmartContainer<Json::Value>  json_;
 
-    std::string remoteAet_;
-    std::string calledAet_;
+    RequestOrigin origin_;
+    std::string dicomRemoteAet_;
+    std::string dicomCalledAet_;
+    std::string httpRemoteIp_;
+    std::string httpUsername_;
     ServerIndex::MetadataMap metadata_;
 
     void ComputeMissingInformation();
 
   public:
+    DicomInstanceToStore() : origin_(RequestOrigin_Unknown)
+    {
+    }
+
+    void SetDicomProtocolOrigin(const char* remoteAet,
+                                const char* calledAet);
+
+    void SetRestOrigin(const RestApiCall& call);
+
+    void SetHttpOrigin(const char* remoteIp,
+                       const char* username);
+
+    void SetLuaOrigin();
+
+    void SetPluginsOrigin();
+
+    const char* GetRemoteAet() const; 
+
     void SetBuffer(const std::string& dicom)
     {
       buffer_.SetConstReference(dicom);
@@ -170,26 +192,6 @@
       json_.SetConstReference(json);
     }
 
-    const std::string& GetRemoteAet() const
-    {
-      return remoteAet_;
-    }
-
-    void SetRemoteAet(const std::string& aet)
-    {
-      remoteAet_ = aet;
-    }
-
-    const std::string& GetCalledAet() const
-    {
-      return calledAet_;
-    }
-
-    void SetCalledAet(const std::string& aet)
-    {
-      calledAet_ = aet;
-    }
-
     void AddMetadata(ResourceType level,
                      MetadataType metadata,
                      const std::string& value);
@@ -211,5 +213,7 @@
     const DicomMap& GetSummary();
     
     const Json::Value& GetJson();
+
+    void GetOriginInformation(Json::Value& result) const;
   };
 }
--- a/OrthancServer/LuaScripting.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/LuaScripting.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -245,7 +245,7 @@
       DicomModification modification;
       OrthancRestApi::ParseModifyRequest(modification, parameters);
 
-      std::auto_ptr<ModifyInstanceCommand> command(new ModifyInstanceCommand(context_, modification));
+      std::auto_ptr<ModifyInstanceCommand> command(new ModifyInstanceCommand(context_, RequestOrigin_Lua, modification));
       return command.release();
     }
 
@@ -366,8 +366,7 @@
   void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId,
                                            const Json::Value& simplifiedTags,
                                            const Json::Value& metadata,
-                                           const std::string& remoteAet,
-                                           const std::string& calledAet)
+                                           const DicomInstanceToStore& instance)
   {
     static const char* NAME = "OnStoredInstance";
 
@@ -379,8 +378,11 @@
       call.PushString(instanceId);
       call.PushJson(simplifiedTags);
       call.PushJson(metadata);
-      call.PushJson(remoteAet);
-      call.PushJson(calledAet);
+
+      Json::Value origin;
+      instance.GetOriginInformation(origin);
+      call.PushJson(origin);
+
       call.Execute();
 
       SubmitJob(std::string("Lua script: ") + NAME);
@@ -406,8 +408,7 @@
       }
     }
 
-    ApplyOnStoredInstance(publicId, simplifiedTags, metadata, 
-                          instance.GetRemoteAet(), instance.GetCalledAet());
+    ApplyOnStoredInstance(publicId, simplifiedTags, metadata, instance);
   }
 
 
--- a/OrthancServer/LuaScripting.h	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/LuaScripting.h	Tue Aug 25 12:10:12 2015 +0200
@@ -55,8 +55,7 @@
     void ApplyOnStoredInstance(const std::string& instanceId,
                                const Json::Value& simplifiedDicom,
                                const Json::Value& metadata,
-                               const std::string& remoteAet,
-                               const std::string& calledAet);
+                               const DicomInstanceToStore& instance);
 
     IServerCommand* ParseOperation(const std::string& operation,
                                    const Json::Value& parameters);
--- a/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -278,6 +278,7 @@
       modification.Apply(*modified);
 
       DicomInstanceToStore toStore;
+      toStore.SetRestOrigin(call);
       toStore.SetParsedDicomFile(*modified);
 
 
@@ -431,12 +432,14 @@
 
 
   static void StoreCreatedInstance(std::string& id /* out */,
-                                   ServerContext& context,
+                                   RestApiPostCall& call,
                                    ParsedDicomFile& dicom)
   {
     DicomInstanceToStore toStore;
+    toStore.SetRestOrigin(call);
     toStore.SetParsedDicomFile(dicom);
 
+    ServerContext& context = OrthancRestApi::GetContext(call);
     StoreStatus status = context.Store(id, toStore);
 
     if (status == StoreStatus_Failure)
@@ -447,6 +450,7 @@
 
 
   static void CreateDicomV1(ParsedDicomFile& dicom,
+                            RestApiPostCall& call,
                             const Json::Value& request)
   {
     // curl http://localhost:8042/tools/create-dicom -X POST -d '{"PatientName":"Hello^World"}'
@@ -570,7 +574,7 @@
         dicom->Replace(DICOM_TAG_INSTANCE_NUMBER, boost::lexical_cast<std::string>(i + 1));
         dicom->Replace(DICOM_TAG_IMAGE_INDEX, boost::lexical_cast<std::string>(i + 1));
 
-        StoreCreatedInstance(someInstance, context, *dicom);
+        StoreCreatedInstance(someInstance, call, *dicom);
       }
     }
     catch (OrthancException& e)
@@ -785,7 +789,7 @@
     }
 
     std::string id;
-    StoreCreatedInstance(id, context, dicom);
+    StoreCreatedInstance(id, call, dicom);
     OrthancRestApi::GetApi(call).AnswerStoredResource(call, id, ResourceType_Instance, StoreStatus_Success);
 
     return;
@@ -808,12 +812,11 @@
     else
     {
       // Compatibility with Orthanc <= 0.9.3
-      ServerContext& context = OrthancRestApi::GetContext(call);
       ParsedDicomFile dicom;
-      CreateDicomV1(dicom, request);
+      CreateDicomV1(dicom, call, request);
 
       std::string id;
-      StoreCreatedInstance(id, context, dicom);
+      StoreCreatedInstance(id, call, dicom);
       OrthancRestApi::GetApi(call).AnswerStoredResource(call, id, ResourceType_Instance, StoreStatus_Success);
     }
   }
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -84,6 +84,7 @@
     std::string postData(call.GetBodyData(), call.GetBodySize());
 
     DicomInstanceToStore toStore;
+    toStore.SetRestOrigin(call);
     toStore.SetBuffer(postData);
 
     std::string publicId;
--- a/OrthancServer/Scheduler/ModifyInstanceCommand.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/Scheduler/ModifyInstanceCommand.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -37,8 +37,10 @@
 namespace Orthanc
 {
   ModifyInstanceCommand::ModifyInstanceCommand(ServerContext& context,
+                                               RequestOrigin origin,
                                                const DicomModification& modification) :
     context_(context),
+    origin_(origin),
     modification_(modification)
   {
     modification_.SetAllowManualIdentifiers(true);
@@ -59,6 +61,12 @@
     {
       modification_.SetLevel(ResourceType_Instance);
     }
+
+    if (origin_ != RequestOrigin_Lua)
+    {
+      // TODO If issued from HTTP, "remoteIp" and "username" must be provided
+      throw OrthancException(ErrorCode_NotImplemented);
+    }
   }
 
 
@@ -82,6 +90,8 @@
         modification_.Apply(*modified);
 
         DicomInstanceToStore toStore;
+        assert(origin_ == RequestOrigin_Lua);
+        toStore.SetLuaOrigin();
         toStore.SetParsedDicomFile(*modified);
         // TODO other metadata
         toStore.AddMetadata(ResourceType_Instance, MetadataType_ModifiedFrom, *it);
--- a/OrthancServer/Scheduler/ModifyInstanceCommand.h	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/Scheduler/ModifyInstanceCommand.h	Tue Aug 25 12:10:12 2015 +0200
@@ -42,10 +42,12 @@
   {
   private:
     ServerContext& context_;
+    RequestOrigin origin_;
     DicomModification modification_;
 
   public:
     ModifyInstanceCommand(ServerContext& context,
+                          RequestOrigin origin,
                           const DicomModification& modification);
 
     const DicomModification& GetModification() const
--- a/OrthancServer/main.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/OrthancServer/main.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -75,11 +75,10 @@
     if (dicomFile.size() > 0)
     {
       DicomInstanceToStore toStore;
+      toStore.SetDicomProtocolOrigin(remoteAet.c_str(), calledAet.c_str());
       toStore.SetBuffer(dicomFile);
       toStore.SetSummary(dicomSummary);
       toStore.SetJson(dicomJson);
-      toStore.SetRemoteAet(remoteAet);
-      toStore.SetCalledAet(calledAet);
 
       std::string id;
       server_.Store(id, toStore);
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 25 11:04:19 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 25 12:10:12 2015 +0200
@@ -813,7 +813,7 @@
     switch (service)
     {
       case _OrthancPluginService_GetInstanceRemoteAet:
-        *p.resultString = instance.GetRemoteAet().c_str();
+        *p.resultString = instance.GetRemoteAet();
         return;
 
       case _OrthancPluginService_GetInstanceSize:
--- a/Resources/Samples/Lua/AutoroutingConditional.lua	Tue Aug 25 11:04:19 2015 +0200
+++ b/Resources/Samples/Lua/AutoroutingConditional.lua	Tue Aug 25 12:10:12 2015 +0200
@@ -1,9 +1,6 @@
-function OnStoredInstance(instanceId, tags, metadata, remoteAet, calledAet)
-   -- The "remoteAet" and "calledAet" arguments are only available
-   -- since Orthanc 0.8.6
-   if remoteAet ~=nil and calledAet ~= nil then
-      print ("Source AET: " .. remoteAet .. " => Called AET: " .. calledAet)
-   end
+function OnStoredInstance(instanceId, tags, metadata, origin)
+   -- The "origin" is only available since Orthanc 0.9.4
+   PrintRecursive(origin)
 
    -- Extract the value of the "PatientName" DICOM tag
    local patientName = string.lower(tags['PatientName'])