comparison OrthancServer/ServerContext.cpp @ 1065:921532f67770

Lua scripts can invoke system commands, with CallSystem()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Jul 2014 16:59:33 +0200
parents 160dfe770618
children bb82e5e818e9
comparison
equal deleted inserted replaced
1064:cd20e2568fc2 1065:921532f67770
42 #include <glog/logging.h> 42 #include <glog/logging.h>
43 #include <EmbeddedResources.h> 43 #include <EmbeddedResources.h>
44 #include <dcmtk/dcmdata/dcfilefo.h> 44 #include <dcmtk/dcmdata/dcfilefo.h>
45 45
46 46
47 #include "Scheduler/CallSystemCommand.h"
47 #include "Scheduler/DeleteInstanceCommand.h" 48 #include "Scheduler/DeleteInstanceCommand.h"
48 #include "Scheduler/ModifyInstanceCommand.h" 49 #include "Scheduler/ModifyInstanceCommand.h"
49 #include "Scheduler/StoreScuCommand.h" 50 #include "Scheduler/StoreScuCommand.h"
50 #include "Scheduler/StorePeerCommand.h" 51 #include "Scheduler/StorePeerCommand.h"
51 #include "OrthancRestApi/OrthancRestApi.h" 52 #include "OrthancRestApi/OrthancRestApi.h"
158 std::auto_ptr<ModifyInstanceCommand> command(new ModifyInstanceCommand(context)); 159 std::auto_ptr<ModifyInstanceCommand> command(new ModifyInstanceCommand(context));
159 OrthancRestApi::ParseModifyRequest(command->GetModification(), parameters); 160 OrthancRestApi::ParseModifyRequest(command->GetModification(), parameters);
160 return command.release(); 161 return command.release();
161 } 162 }
162 163
164 if (operation == "call-system")
165 {
166 LOG(INFO) << "Lua script to call system command on " << parameters["Instance"].asString();
167
168 const Json::Value& argsIn = parameters["Arguments"];
169 if (argsIn.type() != Json::arrayValue)
170 {
171 throw OrthancException(ErrorCode_BadParameterType);
172 }
173
174 std::vector<std::string> args;
175 args.reserve(argsIn.size());
176 for (Json::Value::ArrayIndex i = 0; i < argsIn.size(); ++i)
177 {
178 // http://jsoncpp.sourceforge.net/namespace_json.html#7d654b75c16a57007925868e38212b4e
179 switch (argsIn[i].type())
180 {
181 case Json::stringValue:
182 args.push_back(argsIn[i].asString());
183 break;
184
185 case Json::intValue:
186 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asInt()));
187 break;
188
189 case Json::uintValue:
190 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asUInt()));
191 break;
192
193 case Json::realValue:
194 args.push_back(boost::lexical_cast<std::string>(argsIn[i].asFloat()));
195 break;
196
197 default:
198 throw OrthancException(ErrorCode_BadParameterType);
199 }
200 }
201
202 return new CallSystemCommand(context, parameters["Command"].asString(), args);
203 }
204
163 throw OrthancException(ErrorCode_ParameterOutOfRange); 205 throw OrthancException(ErrorCode_ParameterOutOfRange);
164 } 206 }
165 207
166 208
167 void ServerContext::ApplyOnStoredInstance(const std::string& instanceId, 209 void ServerContext::ApplyOnStoredInstance(const std::string& instanceId,