use of org.eclipse.kapua.service.device.management.command.DeviceCommandInput in project kapua by eclipse.
the class UploadRequest method doPostCommand.
private void doPostCommand(KapuaFormFields kapuaFormFields, HttpServletResponse resp) throws ServletException, IOException {
try {
List<FileItem> fileItems = kapuaFormFields.getFileItems();
final String scopeIdString = kapuaFormFields.get("scopeIdString");
final String deviceIdString = kapuaFormFields.get("deviceIdString");
final String command = kapuaFormFields.get("command");
final String password = kapuaFormFields.get("password");
final String timeoutString = kapuaFormFields.get("timeout");
if (scopeIdString == null || scopeIdString.isEmpty()) {
throw new IllegalArgumentException("scopeId");
}
if (deviceIdString == null || deviceIdString.isEmpty()) {
throw new IllegalArgumentException("deviceId");
}
if (command == null || command.isEmpty()) {
throw new IllegalArgumentException("command");
}
if (fileItems.size() > 1) {
throw new IllegalArgumentException("file");
}
Integer timeout = null;
if (timeoutString != null && !timeoutString.isEmpty()) {
try {
timeout = Integer.parseInt(timeoutString);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("timeout");
}
}
KapuaLocator locator = KapuaLocator.getInstance();
DeviceCommandManagementService deviceService = locator.getService(DeviceCommandManagementService.class);
// FIXME: set a max size on the MQtt payload
byte[] data = fileItems.size() == 0 ? null : fileItems.get(0).get();
DeviceCommandFactory deviceCommandFactory = locator.getFactory(DeviceCommandFactory.class);
DeviceCommandInput commandInput = deviceCommandFactory.newCommandInput();
StringTokenizer st = new StringTokenizer(command);
int count = st.countTokens();
String cmd = count > 0 ? st.nextToken() : null;
String[] args = count > 1 ? new String[count - 1] : null;
int i = 0;
while (st.hasMoreTokens()) {
args[i++] = st.nextToken();
}
commandInput.setCommand(cmd);
commandInput.setPassword((password == null || password.isEmpty()) ? null : password);
commandInput.setArguments(args);
commandInput.setTimeout(timeout);
commandInput.setWorkingDir("/tmp/");
commandInput.setBody(data);
DeviceCommandOutput deviceCommandOutput = deviceService.exec(KapuaEid.parseShortId(scopeIdString), KapuaEid.parseShortId(deviceIdString), commandInput, null);
resp.setContentType("text/plain");
if (deviceCommandOutput.getStderr() != null && deviceCommandOutput.getStderr().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getStderr());
}
if (deviceCommandOutput.getStdout() != null && deviceCommandOutput.getStdout().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getStdout());
}
if (deviceCommandOutput.getExceptionMessage() != null && deviceCommandOutput.getExceptionMessage().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getExceptionMessage());
}
} catch (IllegalArgumentException iae) {
resp.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
return;
} catch (KapuaEntityNotFoundException eenfe) {
resp.sendError(400, eenfe.getMessage());
return;
} catch (KapuaUnauthenticatedException eiae) {
resp.sendError(401, eiae.getMessage());
return;
} catch (KapuaIllegalAccessException eiae) {
resp.sendError(403, eiae.getMessage());
return;
} catch (Exception e) {
s_logger.error("Error sending command", e);
throw new ServletException(e);
}
}
use of org.eclipse.kapua.service.device.management.command.DeviceCommandInput in project kapua by eclipse.
the class GwtDeviceManagementServiceImpl method executeCommand.
//
// Command
//
@Override
public GwtDeviceCommandOutput executeCommand(GwtXSRFToken xsrfToken, GwtDevice gwtDevice, GwtDeviceCommandInput gwtCommandInput) throws GwtKapuaException {
//
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);
GwtDeviceCommandOutput gwtCommandOutput = new GwtDeviceCommandOutput();
try {
// execute the command
KapuaLocator locator = KapuaLocator.getInstance();
DeviceCommandManagementService deviceCommandManagementService = locator.getService(DeviceCommandManagementService.class);
DeviceCommandFactory deviceCommandFactory = locator.getFactory(DeviceCommandFactory.class);
StringTokenizer st = new StringTokenizer(gwtCommandInput.getCommand());
int count = st.countTokens();
String command = count > 0 ? st.nextToken() : null;
String[] args = count > 1 ? new String[count - 1] : null;
int i = 0;
while (st.hasMoreTokens()) {
args[i++] = st.nextToken();
}
DeviceCommandInput commandInput = deviceCommandFactory.newCommandInput();
// commandInput.setArguments(gwtCommandInput.getArguments());
commandInput.setArguments(args);
// commandInput.setCommand(gwtCommandInput.getCommand());
commandInput.setCommand(command);
commandInput.setEnvironment(gwtCommandInput.getEnvironment());
commandInput.setRunAsynch(gwtCommandInput.isRunAsynch() != null ? gwtCommandInput.isRunAsynch().booleanValue() : false);
commandInput.setStdin(gwtCommandInput.getStdin());
commandInput.setTimeout(gwtCommandInput.getTimeout() != null ? gwtCommandInput.getTimeout().intValue() : 0);
commandInput.setWorkingDir(gwtCommandInput.getWorkingDir());
commandInput.setBody(gwtCommandInput.getZipBytes());
KapuaId scopeId = KapuaEid.parseShortId(gwtDevice.getScopeId());
KapuaId deviceId = KapuaEid.parseShortId(gwtDevice.getId());
DeviceCommandOutput commandOutput = deviceCommandManagementService.exec(scopeId, deviceId, commandInput, null);
if (commandOutput.getExceptionMessage() != null) {
gwtCommandOutput.setExceptionMessage(commandOutput.getExceptionMessage().replace("\n", "<br>"));
}
if (commandOutput.getExceptionStack() != null) {
gwtCommandOutput.setExceptionStack(commandOutput.getExceptionStack().replace("\n", "<br>"));
}
gwtCommandOutput.setExitCode(commandOutput.getExitCode());
if (commandOutput.getStderr() != null) {
gwtCommandOutput.setStderr(commandOutput.getStderr().replace("\n", "<br>"));
}
gwtCommandOutput.setStdout(commandOutput.getStdout());
gwtCommandOutput.setTimedout(commandOutput.getHasTimedout());
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtCommandOutput;
}
Aggregations