annotate OrthancServer/LuaScripting.cpp @ 1798:e92cd8841a5f worklists

conformance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Nov 2015 12:00:34 +0100
parents d78b87f93bcf
children b1291df2f780
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PrecompiledHeadersServer.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "LuaScripting.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "ServerContext.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "OrthancInitialization.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "../Core/Lua/LuaFunctionCall.h"
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
39 #include "../Core/HttpServer/StringHttpOutput.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1465
diff changeset
40 #include "../Core/Logging.h"
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include "Scheduler/DeleteInstanceCommand.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include "Scheduler/StoreScuCommand.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include "Scheduler/StorePeerCommand.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 #include "Scheduler/ModifyInstanceCommand.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 #include "Scheduler/CallSystemCommand.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 #include "OrthancRestApi/OrthancRestApi.h"
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 #include <EmbeddedResources.h>
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 namespace Orthanc
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
54 ServerContext* LuaScripting::GetServerContext(lua_State *state)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
55 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
56 const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext");
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
57 return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value));
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
58 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
59
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
60
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
61 // Syntax in Lua: RestApiGet(uri, builtin)
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
62 int LuaScripting::RestApiGet(lua_State *state)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
63 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
64 ServerContext* serverContext = GetServerContext(state);
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
65 if (serverContext == NULL)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
66 {
1440
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1437
diff changeset
67 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
68 lua_pushnil(state);
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
69 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
70 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
71
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
72 // Check the types of the arguments
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
73 int nArgs = lua_gettop(state);
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
74 if ((nArgs != 1 && nArgs != 2) ||
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
75 !lua_isstring(state, 1) || // URI
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
76 (nArgs == 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
77 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
78 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
79 lua_pushnil(state);
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
80 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
81 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
82
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
83 const char* uri = lua_tostring(state, 1);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
84 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
85
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
86 try
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
87 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
88 std::string result;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
89 if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
90 RequestOrigin_Lua, uri))
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
91 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
92 lua_pushlstring(state, result.c_str(), result.size());
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
93 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
94 }
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
95 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
96 catch (OrthancException& e)
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
97 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
98 LOG(ERROR) << "Lua: " << e.What();
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
99 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
100
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
101 LOG(ERROR) << "Lua: Error in RestApiGet() for URI: " << uri;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
102 lua_pushnil(state);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
103 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
104 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
105
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
106
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
107 int LuaScripting::RestApiPostOrPut(lua_State *state,
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
108 bool isPost)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
109 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
110 ServerContext* serverContext = GetServerContext(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
111 if (serverContext == NULL)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
112 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
113 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
114 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
115 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
116 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
117
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
118 // Check the types of the arguments
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
119 int nArgs = lua_gettop(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
120 if ((nArgs != 2 && nArgs != 3) ||
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
121 !lua_isstring(state, 1) || // URI
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
122 !lua_isstring(state, 2) || // Body
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
123 (nArgs == 3 && !lua_isboolean(state, 3))) // Restrict to built-in API?
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
124 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
125 LOG(ERROR) << "Lua: Bad parameters to " << (isPost ? "RestApiPost()" : "RestApiPut()");
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
126 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
127 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
128 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
129
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
130 const char* uri = lua_tostring(state, 1);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
131 size_t bodySize = 0;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
132 const char* bodyData = lua_tolstring(state, 2, &bodySize);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
133 bool builtin = (nArgs == 3 ? lua_toboolean(state, 3) != 0 : false);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
134
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
135 try
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
136 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
137 std::string result;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
138 if (isPost ?
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
139 HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
140 RequestOrigin_Lua, uri, bodyData, bodySize) :
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
141 HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
142 RequestOrigin_Lua, uri, bodyData, bodySize))
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
143 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
144 lua_pushlstring(state, result.c_str(), result.size());
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
145 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
146 }
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
147 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
148 catch (OrthancException& e)
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
149 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
150 LOG(ERROR) << "Lua: " << e.What();
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
151 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
152
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
153 LOG(ERROR) << "Lua: Error in " << (isPost ? "RestApiPost()" : "RestApiPut()") << " for URI: " << uri;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
154 lua_pushnil(state);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
155 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
156 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
157
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
158
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
159 // Syntax in Lua: RestApiPost(uri, body, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
160 int LuaScripting::RestApiPost(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
161 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
162 return RestApiPostOrPut(state, true);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
163 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
164
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
165
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
166 // Syntax in Lua: RestApiPut(uri, body, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
167 int LuaScripting::RestApiPut(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
168 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
169 return RestApiPostOrPut(state, false);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
170 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
171
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
172
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
173 // Syntax in Lua: RestApiDelete(uri, builtin)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
174 int LuaScripting::RestApiDelete(lua_State *state)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
175 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
176 ServerContext* serverContext = GetServerContext(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
177 if (serverContext == NULL)
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
178 {
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
179 LOG(ERROR) << "Lua: The Orthanc API is unavailable";
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
180 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
181 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
182 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
183
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
184 // Check the types of the arguments
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
185 int nArgs = lua_gettop(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
186 if ((nArgs != 1 && nArgs != 2) ||
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
187 !lua_isstring(state, 1) || // URI
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
188 (nArgs == 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
189 {
1449
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1447
diff changeset
190 LOG(ERROR) << "Lua: Bad parameters to RestApiDelete()";
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
191 lua_pushnil(state);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
192 return 1;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
193 }
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
194
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
195 const char* uri = lua_tostring(state, 1);
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1486
diff changeset
196 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
197
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
198 try
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
199 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
200 if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
201 RequestOrigin_Lua, uri))
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
202 {
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
203 lua_pushboolean(state, 1);
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
204 return 1;
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
205 }
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
206 }
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
207 catch (OrthancException& e)
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
208 {
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
209 LOG(ERROR) << "Lua: " << e.What();
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
210 }
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
211
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
212 LOG(ERROR) << "Lua: Error in RestApiDelete() for URI: " << uri;
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
213 lua_pushnil(state);
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
214
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
215 return 1;
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
216 }
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
217
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
218
1587
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
219 // Syntax in Lua: GetOrthancConfiguration()
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
220 int LuaScripting::GetOrthancConfiguration(lua_State *state)
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
221 {
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
222 Json::Value configuration;
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
223 Configuration::GetConfiguration(configuration);
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
224
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
225 LuaContext::GetLuaContext(state).PushJson(configuration);
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
226
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
227 return 1;
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
228 }
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
229
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
230
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 IServerCommand* LuaScripting::ParseOperation(const std::string& operation,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 const Json::Value& parameters)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 if (operation == "delete")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
236 LOG(INFO) << "Lua script to delete resource " << parameters["Resource"].asString();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 return new DeleteInstanceCommand(context_);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 if (operation == "store-scu")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 std::string localAet;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 if (parameters.isMember("LocalAet"))
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 localAet = parameters["LocalAet"].asString();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 else
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 localAet = context_.GetDefaultLocalApplicationEntityTitle();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 std::string modality = parameters["Modality"].asString();
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
253 LOG(INFO) << "Lua script to send resource " << parameters["Resource"].asString()
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 << " to modality " << modality << " using Store-SCU";
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 return new StoreScuCommand(context_, localAet,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 Configuration::GetModalityUsingSymbolicName(modality), true);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 if (operation == "store-peer")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 std::string peer = parameters["Peer"].asString();
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
262 LOG(INFO) << "Lua script to send resource " << parameters["Resource"].asString()
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 << " to peer " << peer << " using HTTP";
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 OrthancPeerParameters parameters;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 Configuration::GetOrthancPeer(parameters, peer);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 return new StorePeerCommand(context_, parameters, true);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 if (operation == "modify")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
272 LOG(INFO) << "Lua script to modify resource " << parameters["Resource"].asString();
1698
d78b87f93bcf DicomModification use Json::Value
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
273 std::auto_ptr<DicomModification> modification(new DicomModification);
d78b87f93bcf DicomModification use Json::Value
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
274 OrthancRestApi::ParseModifyRequest(*modification, parameters);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
1698
d78b87f93bcf DicomModification use Json::Value
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
276 std::auto_ptr<ModifyInstanceCommand> command
d78b87f93bcf DicomModification use Json::Value
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
277 (new ModifyInstanceCommand(context_, RequestOrigin_Lua, modification.release()));
d78b87f93bcf DicomModification use Json::Value
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
278
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 return command.release();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 if (operation == "call-system")
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
284 LOG(INFO) << "Lua script to call system command on " << parameters["Resource"].asString();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 const Json::Value& argsIn = parameters["Arguments"];
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 if (argsIn.type() != Json::arrayValue)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 throw OrthancException(ErrorCode_BadParameterType);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 std::vector<std::string> args;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 args.reserve(argsIn.size());
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 for (Json::Value::ArrayIndex i = 0; i < argsIn.size(); ++i)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 // http://jsoncpp.sourceforge.net/namespace_json.html#7d654b75c16a57007925868e38212b4e
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 switch (argsIn[i].type())
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 case Json::stringValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 args.push_back(argsIn[i].asString());
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 case Json::intValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asInt()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 case Json::uintValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asUInt()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 case Json::realValue:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asFloat()));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 break;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 default:
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 throw OrthancException(ErrorCode_BadParameterType);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 return new CallSystemCommand(context_, parameters["Command"].asString(), args);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 throw OrthancException(ErrorCode_ParameterOutOfRange);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
327 void LuaScripting::InitializeJob()
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
328 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
329 lua_.Execute("_InitializeJob()");
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
330 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
331
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
332
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
333 void LuaScripting::SubmitJob(const std::string& description)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
334 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
335 Json::Value operations;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
336 LuaFunctionCall call2(lua_, "_AccessJob");
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1587
diff changeset
337 call2.ExecuteToJson(operations, false);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
338
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
339 if (operations.type() != Json::arrayValue)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
340 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
341 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
342 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
343
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
344 ServerJob job;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
345 ServerCommandInstance* previousCommand = NULL;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
346
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
347 for (Json::Value::ArrayIndex i = 0; i < operations.size(); ++i)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
348 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
349 if (operations[i].type() != Json::objectValue ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
350 !operations[i].isMember("Operation"))
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
351 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
352 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
353 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
354
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
355 const Json::Value& parameters = operations[i];
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
356 std::string operation = parameters["Operation"].asString();
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
357
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
358 ServerCommandInstance& command = job.AddCommand(ParseOperation(operation, operations[i]));
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
359
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
360 if (!parameters.isMember("Resource"))
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
361 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
362 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
363 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
364
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
365 std::string resource = parameters["Resource"].asString();
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
366 if (resource.empty())
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
367 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
368 previousCommand->ConnectOutput(command);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
369 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
370 else
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
371 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
372 command.AddInput(resource);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
373 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
374
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
375 previousCommand = &command;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
376 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
377
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
378 job.SetDescription(description);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
379 context_.GetScheduler().Submit(job);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
380 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
381
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
382
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
383 LuaScripting::LuaScripting(ServerContext& context) : context_(context)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 {
1444
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
385 lua_.SetGlobalVariable("_ServerContext", &context);
b2b09a3dbd8e refactoring: OrthancHttpHandler inside OrthancServer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
386 lua_.RegisterFunction("RestApiGet", RestApiGet);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
387 lua_.RegisterFunction("RestApiPost", RestApiPost);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
388 lua_.RegisterFunction("RestApiPut", RestApiPut);
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
389 lua_.RegisterFunction("RestApiDelete", RestApiDelete);
1587
d7e569640d09 New function "GetOrthancConfiguration()" to get the Orthanc configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1574
diff changeset
390 lua_.RegisterFunction("GetOrthancConfiguration", GetOrthancConfiguration);
1437
02f5a3f5c0a0 access to the REST API from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1436
diff changeset
391
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 lua_.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 lua_.SetHttpProxy(Configuration::GetGlobalStringParameter("HttpProxy", ""));
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 void LuaScripting::ApplyOnStoredInstance(const std::string& instanceId,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 const Json::Value& simplifiedTags,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 const Json::Value& metadata,
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
400 const DicomInstanceToStore& instance)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
402 static const char* NAME = "OnStoredInstance";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
403
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
404 if (lua_.IsExistingFunction(NAME))
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
406 InitializeJob();
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
408 LuaFunctionCall call(lua_, NAME);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 call.PushString(instanceId);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 call.PushJson(simplifiedTags);
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 call.PushJson(metadata);
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
412
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
413 Json::Value origin;
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
414 instance.GetOriginInformation(origin);
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
415 call.PushJson(origin);
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
416
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 call.Execute();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
419 SubmitJob(std::string("Lua script: ") + NAME);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 void LuaScripting::SignalStoredInstance(const std::string& publicId,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 DicomInstanceToStore& instance,
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 const Json::Value& simplifiedTags)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
1456
68827c07e683 fix recursive calls with Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
428 boost::recursive_mutex::scoped_lock lock(mutex_);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 Json::Value metadata = Json::objectValue;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 for (ServerIndex::MetadataMap::const_iterator
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 it = instance.GetMetadata().begin();
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 it != instance.GetMetadata().end(); ++it)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 if (it->first.first == ResourceType_Instance)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 metadata[EnumerationToString(it->first.second)] = it->second;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441
1572
904096e7367e More information about the origin request in OnStoredInstance() callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
442 ApplyOnStoredInstance(publicId, simplifiedTags, metadata, instance);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
446
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
447 void LuaScripting::OnStableResource(const ServerIndexChange& change)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
448 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
449 const char* name;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
450
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
451 switch (change.GetChangeType())
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
452 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
453 case ChangeType_StablePatient:
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
454 name = "OnStablePatient";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
455 break;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
456
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
457 case ChangeType_StableStudy:
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
458 name = "OnStableStudy";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
459 break;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
460
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
461 case ChangeType_StableSeries:
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
462 name = "OnStableSeries";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
463 break;
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
464
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
465 default:
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
466 throw OrthancException(ErrorCode_InternalError);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
467 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
468
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
469
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
470 Json::Value tags, metadata;
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
471 if (context_.GetIndex().LookupResource(tags, change.GetPublicId(), change.GetResourceType()) &&
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
472 context_.GetIndex().GetMetadata(metadata, change.GetPublicId()))
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
473 {
1456
68827c07e683 fix recursive calls with Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
474 boost::recursive_mutex::scoped_lock lock(mutex_);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
475
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
476 if (lua_.IsExistingFunction(name))
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
477 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
478 InitializeJob();
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
479
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
480 LuaFunctionCall call(lua_, name);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
481 call.PushString(change.GetPublicId());
1436
0a3e3be59094 uncoupling of SignalChange for Lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1435
diff changeset
482 call.PushJson(tags["MainDicomTags"]);
1447
5ba7471780ae refactoring: HttpToolbox, DumpJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1445
diff changeset
483 call.PushJson(metadata);
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
484 call.Execute();
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
485
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
486 SubmitJob(std::string("Lua script: ") + name);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
487 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
488 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
489 }
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
490
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
491
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
492
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 void LuaScripting::SignalChange(const ServerIndexChange& change)
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
495 if (change.GetChangeType() == ChangeType_StablePatient ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
496 change.GetChangeType() == ChangeType_StableStudy ||
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
497 change.GetChangeType() == ChangeType_StableSeries)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
498 {
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
499 OnStableResource(change);
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
500 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503
1574
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
504 bool LuaScripting::FilterIncomingInstance(const DicomInstanceToStore& instance,
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
505 const Json::Value& simplified)
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
507 static const char* NAME = "ReceivedInstanceFilter";
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
508
1456
68827c07e683 fix recursive calls with Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
509 boost::recursive_mutex::scoped_lock lock(mutex_);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
511 if (lua_.IsExistingFunction(NAME))
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 {
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1433
diff changeset
513 LuaFunctionCall call(lua_, NAME);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 call.PushJson(simplified);
1574
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
515
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
516 Json::Value origin;
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
517 instance.GetOriginInformation(origin);
0c29ebe80ac9 give access to full request origin in ReceivedInstanceFilter (Lua)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1572
diff changeset
518 call.PushJson(origin);
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 if (!call.ExecutePredicate())
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 {
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 return false;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 }
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 return true;
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 }
1455
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
528
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
529
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
530 void LuaScripting::Execute(const std::string& command)
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
531 {
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
532 LuaScripting::Locker locker(*this);
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
533
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
534 if (locker.GetLua().IsExistingFunction(command.c_str()))
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
535 {
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
536 LuaFunctionCall call(locker.GetLua(), command.c_str());
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
537 call.Execute();
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
538 }
a68545767975 Initialize() and Finalize() events in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1449
diff changeset
539 }
1433
461e7554bff7 refactoring: LuaScripting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }