diff OrthancServer/ServerContext.cpp @ 397:941ea46e9e26 lua-scripting

lua filter of new instances
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 May 2013 16:34:00 +0200
parents 9784f19f7e1b
children 2d269089078f
line wrap: on
line diff
--- a/OrthancServer/ServerContext.cpp	Thu May 02 11:02:15 2013 +0200
+++ b/OrthancServer/ServerContext.cpp	Thu May 02 16:34:00 2013 +0200
@@ -33,12 +33,15 @@
 #include "ServerContext.h"
 
 #include "../Core/HttpServer/FilesystemHttpSender.h"
+#include "../Core/Lua/LuaFunctionCall.h"
+#include "ServerToolbox.h"
 
 #include <glog/logging.h>
 #include <EmbeddedResources.h>
 
 #define ENABLE_DICOM_CACHE  1
 
+static const char* RECEIVED_INSTANCE_FILTER = "ReceivedInstanceFilter";
 
 static const size_t DICOM_CACHE_SIZE = 2;
 
@@ -85,6 +88,23 @@
                                    const Json::Value& dicomJson,
                                    const std::string& remoteAet)
   {
+    // Test if the instance must be filtered out
+    if (lua_.IsExistingFunction(RECEIVED_INSTANCE_FILTER))
+    {
+      Json::Value simplified;
+      SimplifyTags(simplified, dicomJson);
+
+      LuaFunctionCall call(lua_, RECEIVED_INSTANCE_FILTER);
+      call.PushJSON(simplified);
+      call.PushString(remoteAet);
+
+      if (!call.ExecutePredicate())
+      {
+        LOG(INFO) << "An incoming instance has been discarded by a filter";
+        return StoreStatus_FilteredOut;
+      }
+    }
+
     if (compressionEnabled_)
     {
       accessor_.SetCompressionForNextOperations(CompressionType_Zlib);
@@ -111,17 +131,21 @@
 
     switch (status)
     {
-    case StoreStatus_Success:
-      LOG(INFO) << "New instance stored";
-      break;
+      case StoreStatus_Success:
+        LOG(INFO) << "New instance stored";
+        break;
+
+      case StoreStatus_AlreadyStored:
+        LOG(INFO) << "Already stored";
+        break;
 
-    case StoreStatus_AlreadyStored:
-      LOG(INFO) << "Already stored";
-      break;
+      case StoreStatus_Failure:
+        LOG(ERROR) << "Store failure";
+        break;
 
-    case StoreStatus_Failure:
-      LOG(ERROR) << "Store failure";
-      break;
+      default:
+        // This should never happen
+        break;
     }
 
     return status;