changeset 1444:b2b09a3dbd8e

refactoring: OrthancHttpHandler inside OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 12:09:58 +0200
parents 895ab369d63c
children d26c8a93d05a
files CMakeLists.txt Core/HttpServer/HttpToolbox.cpp Core/RestApi/RestApiHierarchy.cpp OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h OrthancServer/ServerContext.h OrthancServer/main.cpp
diffstat 7 files changed, 42 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Jul 01 11:30:19 2015 +0200
+++ b/CMakeLists.txt	Wed Jul 01 12:09:58 2015 +0200
@@ -133,8 +133,6 @@
 
   Plugins/Engine/SharedLibrary.cpp
   Plugins/Engine/PluginsManager.cpp
-  Plugins/Engine/OrthancPlugins.cpp
-  Plugins/Engine/OrthancPluginDatabase.cpp
   )
 
 
@@ -176,6 +174,8 @@
   OrthancServer/QueryRetrieveHandler.cpp
   OrthancServer/LuaScripting.cpp
   OrthancServer/OrthancHttpHandler.cpp
+  Plugins/Engine/OrthancPlugins.cpp
+  Plugins/Engine/OrthancPluginDatabase.cpp
 
   # From "lua-scripting" branch
   OrthancServer/DicomInstanceToStore.cpp
--- a/Core/HttpServer/HttpToolbox.cpp	Wed Jul 01 11:30:19 2015 +0200
+++ b/Core/HttpServer/HttpToolbox.cpp	Wed Jul 01 12:09:58 2015 +0200
@@ -33,6 +33,7 @@
 #include "../PrecompiledHeaders.h"
 #include "HttpToolbox.h"
 
+#include <stdio.h>
 #include <string.h>
 #include <iostream>
 
--- a/Core/RestApi/RestApiHierarchy.cpp	Wed Jul 01 11:30:19 2015 +0200
+++ b/Core/RestApi/RestApiHierarchy.cpp	Wed Jul 01 12:09:58 2015 +0200
@@ -462,12 +462,13 @@
   {
     IHttpHandler::Arguments components;
     AcceptedMethodsVisitor visitor(methods);
-    LookupResource(components, uri, visitor, 0);
-
-    Json::Value d;
-    if (GetDirectory(d, uri))
+    if (LookupResource(components, uri, visitor, 0))
     {
-      methods.insert(HttpMethod_Get);
+      Json::Value d;
+      if (GetDirectory(d, uri))
+      {
+        methods.insert(HttpMethod_Get);
+      }
     }
   }
 }
--- a/OrthancServer/LuaScripting.cpp	Wed Jul 01 11:30:19 2015 +0200
+++ b/OrthancServer/LuaScripting.cpp	Wed Jul 01 12:09:58 2015 +0200
@@ -51,17 +51,17 @@
 
 namespace Orthanc
 {
-  OrthancRestApi* LuaScripting::GetRestApi(lua_State *state)
+  ServerContext* LuaScripting::GetServerContext(lua_State *state)
   {
-    const void* value = LuaContext::GetGlobalVariable(state, "_RestApi");
-    return const_cast<OrthancRestApi*>(reinterpret_cast<const OrthancRestApi*>(value));
+    const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext");
+    return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value));
   }
 
 
-  int LuaScripting::OrthancApiGet(lua_State *state)
+  int LuaScripting::RestApiGet(lua_State *state)
   {
-    OrthancRestApi* restApi = GetRestApi(state);
-    if (restApi == NULL)
+    ServerContext* serverContext = GetServerContext(state);
+    if (serverContext == NULL)
     {
       LOG(ERROR) << "Lua: The Orthanc API is unavailable";
       lua_pushnil(state);
@@ -70,23 +70,30 @@
 
     // Check the types of the arguments
     int nArgs = lua_gettop(state);
-    if (nArgs != 1 || !lua_isstring(state, 1))  // URI
+    if ((nArgs != 1 && nArgs != 2) || 
+        !lua_isstring(state, 1) ||                 // URI
+        (nArgs == 2 && !lua_isboolean(state, 2)))  // Restrict to built-in API?
     {
-      LOG(ERROR) << "Lua: Bad parameters to OrthancApiGet()";
+      LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
       lua_pushnil(state);
       return 1;
     }
 
     const char* uri = lua_tostring(state, 1);
+    bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) : false);
+
+    IHttpHandler& handler = (builtin ? 
+                             serverContext->GetHttpHandler().GetOrthancRestApi() : 
+                             serverContext->GetHttpHandler());
     
     std::string str;
-    if (HttpToolbox::SimpleGet(str, *restApi, uri))
+    if (HttpToolbox::SimpleGet(str, handler, uri))
     {
       lua_pushstring(state, str.c_str());
     }
     else
     {
-      LOG(ERROR) << "Lua: Error in OrthancApiGet() for URI " << uri;
+      LOG(ERROR) << "Lua: Error in RestApiGet() for URI " << uri;
       lua_pushnil(state);
     }
 
@@ -244,27 +251,16 @@
   }
 
 
-  LuaScripting::LuaScripting(ServerContext& context) : context_(context), restApi_(NULL)
+  LuaScripting::LuaScripting(ServerContext& context) : context_(context)
   {
-    lua_.RegisterFunction("OrthancApiGet", OrthancApiGet);
+    lua_.SetGlobalVariable("_ServerContext", &context);
+    lua_.RegisterFunction("RestApiGet", RestApiGet);
 
     lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
     lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", ""));
   }
 
 
-  void LuaScripting::SetOrthancRestApi(OrthancRestApi& restApi)
-  {
-    lua_.SetGlobalVariable("_RestApi", &restApi);
-  }
-
-
-  void LuaScripting::ResetOrthancRestApi()
-  {
-    lua_.SetGlobalVariable("_RestApi", NULL);
-  }
-
-
   void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId,
                                            const Json::Value& simplifiedTags,
                                            const Json::Value& metadata,
--- a/OrthancServer/LuaScripting.h	Wed Jul 01 11:30:19 2015 +0200
+++ b/OrthancServer/LuaScripting.h	Wed Jul 01 12:09:58 2015 +0200
@@ -35,7 +35,6 @@
 #include "IServerListener.h"
 #include "../Core/Lua/LuaContext.h"
 #include "Scheduler/IServerCommand.h"
-#include "OrthancRestApi/OrthancRestApi.h"
 
 namespace Orthanc
 {
@@ -44,9 +43,9 @@
   class LuaScripting : public IServerListener
   {
   private:
-    static OrthancRestApi* GetRestApi(lua_State *state);
+    static ServerContext* GetServerContext(lua_State *state);
 
-    static int OrthancApiGet(lua_State *state);
+    static int RestApiGet(lua_State *state);
 
     void ApplyOnStoredInstance(const std::string& instanceId,
                                const Json::Value& simplifiedDicom,
@@ -66,7 +65,6 @@
     boost::mutex    mutex_;
     LuaContext      lua_;
     ServerContext&  context_;
-    OrthancRestApi* restApi_;
 
   public:
     class Locker : public boost::noncopyable
@@ -93,10 +91,6 @@
 
     LuaScripting(ServerContext& context);
     
-    void SetOrthancRestApi(OrthancRestApi& restApi);
-
-    void ResetOrthancRestApi();
-
     virtual void SignalStoredInstance(const std::string& publicId,
                                       DicomInstanceToStore& instance,
                                       const Json::Value& simplifiedTags);
--- a/OrthancServer/ServerContext.h	Wed Jul 01 11:30:19 2015 +0200
+++ b/OrthancServer/ServerContext.h	Wed Jul 01 12:09:58 2015 +0200
@@ -47,6 +47,7 @@
 #include "ParsedDicomFile.h"
 #include "Scheduler/ServerScheduler.h"
 #include "ServerIndex.h"
+#include "OrthancHttpHandler.h"
 
 #include <boost/filesystem.hpp>
 #include <boost/thread.hpp>
@@ -126,6 +127,7 @@
         
     SharedArchive  queryRetrieveArchive_;
     std::string defaultLocalAet_;
+    OrthancHttpHandler  httpHandler_;
 
   public:
     class DicomCacheLocker : public boost::noncopyable
@@ -230,6 +232,11 @@
       return lua_;
     }
 
+    OrthancHttpHandler& GetHttpHandler()
+    {
+      return httpHandler_;
+    }
+
 
     /**
      * Management of the plugins
--- a/OrthancServer/main.cpp	Wed Jul 01 11:30:19 2015 +0200
+++ b/OrthancServer/main.cpp	Wed Jul 01 12:09:58 2015 +0200
@@ -51,7 +51,6 @@
 #include "ServerToolbox.h"
 #include "../Plugins/Engine/OrthancPlugins.h"
 #include "FromDcmtkBridge.h"
-#include "OrthancHttpHandler.h"
 
 using namespace Orthanc;
 
@@ -435,12 +434,10 @@
   }
 
 
-  OrthancHttpHandler httpHandler;
-
 #if ENABLE_PLUGINS == 1
   OrthancRestApi restApi(*context);
   plugins.SetServerContext(*context);
-  httpHandler.Register(plugins, false);
+  context->GetHttpHandler().Register(plugins, false);
   context->SetPlugins(plugins);
 #endif
 
@@ -450,9 +447,8 @@
   FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer");
 #endif
 
-  httpHandler.Register(staticResources, false);
-  httpHandler.Register(restApi, true);
-
+  context->GetHttpHandler().Register(staticResources, false);
+  context->GetHttpHandler().Register(restApi, true);
 
 
   MyDicomServerFactory serverFactory(*context);
@@ -493,7 +489,7 @@
       httpServer.SetSslEnabled(false);
     }
 
-    httpServer.Register(httpHandler);
+    httpServer.Register(context->GetHttpHandler());
 
 
 #if ENABLE_PLUGINS == 1
@@ -510,7 +506,6 @@
     }
     
     context->SetStorageArea(*storage);
-    context->GetLua().SetOrthancRestApi(restApi);
 
     // GO !!! Start the requested servers
     if (Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
@@ -556,8 +551,6 @@
     LOG(WARNING) << "    Plugins have stopped";
 #endif
 
-    context->GetLua().ResetOrthancRestApi();
-
     dicomServer.Stop();
     LOG(WARNING) << "    DICOM server has stopped";