comparison OrthancServer/LuaScripting.cpp @ 1444:b2b09a3dbd8e

refactoring: OrthancHttpHandler inside OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 12:09:58 +0200
parents f3672356c121
children d26c8a93d05a
comparison
equal deleted inserted replaced
1443:895ab369d63c 1444:b2b09a3dbd8e
49 #include <EmbeddedResources.h> 49 #include <EmbeddedResources.h>
50 50
51 51
52 namespace Orthanc 52 namespace Orthanc
53 { 53 {
54 OrthancRestApi* LuaScripting::GetRestApi(lua_State *state) 54 ServerContext* LuaScripting::GetServerContext(lua_State *state)
55 { 55 {
56 const void* value = LuaContext::GetGlobalVariable(state, "_RestApi"); 56 const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext");
57 return const_cast<OrthancRestApi*>(reinterpret_cast<const OrthancRestApi*>(value)); 57 return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value));
58 } 58 }
59 59
60 60
61 int LuaScripting::OrthancApiGet(lua_State *state) 61 int LuaScripting::RestApiGet(lua_State *state)
62 { 62 {
63 OrthancRestApi* restApi = GetRestApi(state); 63 ServerContext* serverContext = GetServerContext(state);
64 if (restApi == NULL) 64 if (serverContext == NULL)
65 { 65 {
66 LOG(ERROR) << "Lua: The Orthanc API is unavailable"; 66 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
67 lua_pushnil(state); 67 lua_pushnil(state);
68 return 1; 68 return 1;
69 } 69 }
70 70
71 // Check the types of the arguments 71 // Check the types of the arguments
72 int nArgs = lua_gettop(state); 72 int nArgs = lua_gettop(state);
73 if (nArgs != 1 || !lua_isstring(state, 1)) // URI 73 if ((nArgs != 1 && nArgs != 2) ||
74 { 74 !lua_isstring(state, 1) || // URI
75 LOG(ERROR) << "Lua: Bad parameters to OrthancApiGet()"; 75 (nArgs == 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
76 {
77 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
76 lua_pushnil(state); 78 lua_pushnil(state);
77 return 1; 79 return 1;
78 } 80 }
79 81
80 const char* uri = lua_tostring(state, 1); 82 const char* uri = lua_tostring(state, 1);
83 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) : false);
84
85 IHttpHandler& handler = (builtin ?
86 serverContext->GetHttpHandler().GetOrthancRestApi() :
87 serverContext->GetHttpHandler());
81 88
82 std::string str; 89 std::string str;
83 if (HttpToolbox::SimpleGet(str, *restApi, uri)) 90 if (HttpToolbox::SimpleGet(str, handler, uri))
84 { 91 {
85 lua_pushstring(state, str.c_str()); 92 lua_pushstring(state, str.c_str());
86 } 93 }
87 else 94 else
88 { 95 {
89 LOG(ERROR) << "Lua: Error in OrthancApiGet() for URI " << uri; 96 LOG(ERROR) << "Lua: Error in RestApiGet() for URI " << uri;
90 lua_pushnil(state); 97 lua_pushnil(state);
91 } 98 }
92 99
93 return 1; 100 return 1;
94 } 101 }
242 job.SetDescription(description); 249 job.SetDescription(description);
243 context_.GetScheduler().Submit(job); 250 context_.GetScheduler().Submit(job);
244 } 251 }
245 252
246 253
247 LuaScripting::LuaScripting(ServerContext& context) : context_(context), restApi_(NULL) 254 LuaScripting::LuaScripting(ServerContext& context) : context_(context)
248 { 255 {
249 lua_.RegisterFunction("OrthancApiGet", OrthancApiGet); 256 lua_.SetGlobalVariable("_ServerContext", &context);
257 lua_.RegisterFunction("RestApiGet", RestApiGet);
250 258
251 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); 259 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
252 lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", "")); 260 lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", ""));
253 }
254
255
256 void LuaScripting::SetOrthancRestApi(OrthancRestApi& restApi)
257 {
258 lua_.SetGlobalVariable("_RestApi", &restApi);
259 }
260
261
262 void LuaScripting::ResetOrthancRestApi()
263 {
264 lua_.SetGlobalVariable("_RestApi", NULL);
265 } 261 }
266 262
267 263
268 void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId, 264 void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId,
269 const Json::Value& simplifiedTags, 265 const Json::Value& simplifiedTags,