// Build a proper configuration from POST data.
Configuration inputConfig = new Configuration(lr.configuration);
// Build input.
final MessageInput input;
try {
input = inputRegistry.create(lr.type, inputConfig);
input.setTitle(lr.title);
input.setGlobal(lr.global);
input.setCreatorUserId(getCurrentUser().getName());
input.setCreatedAt(Tools.iso8601());
input.setConfiguration(inputConfig);
input.checkConfiguration();
} catch (NoSuchInputTypeException e) {
LOG.error("There is no such input type registered.", e);
throw new WebApplicationException(e, Response.Status.NOT_FOUND);
} catch (ConfigurationException e) {
LOG.error("Missing or invalid input configuration.", e);
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
// Don't run if exclusive and another instance is already running.
if (input.isExclusive() && inputRegistry.hasTypeRunning(input.getClass())) {
final String error = "Type is exclusive and already has input running.";
LOG.error(error);
throw new BadRequestException(error);
}
String inputId = UUID.randomUUID().toString();
// Build MongoDB data
Map<String, Object> inputData = Maps.newHashMap();
inputData.put(MessageInput.FIELD_INPUT_ID, inputId);
inputData.put(MessageInput.FIELD_TITLE, lr.title);
inputData.put(MessageInput.FIELD_TYPE, lr.type);
inputData.put(MessageInput.FIELD_CREATOR_USER_ID, getCurrentUser().getName());
inputData.put(MessageInput.FIELD_CONFIGURATION, lr.configuration);
inputData.put(MessageInput.FIELD_CREATED_AT, Tools.iso8601());
if (lr.global) {
inputData.put(MessageInput.FIELD_GLOBAL, true);
} else {
inputData.put(MessageInput.FIELD_NODE_ID, serverStatus.getNodeId().toString());
}
// ... and check if it would pass validation. We don't need to go on if it doesn't.
Input mongoInput = new InputImpl(inputData);
// Persist input.
String id;
id = inputService.save(mongoInput);
input.setPersistId(id);
input.initialize();
// Launch input. (this will run async and clean up itself in case of an error.)
inputRegistry.launch(input, inputId);
Map<String, Object> result = Maps.newHashMap();