use of nl.nn.adapterframework.extensions.javascript.JavascriptException in project iaf by ibissource.
the class JavascriptSender method sendMessage.
@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException {
Object jsResult = "";
int numberOfParameters = 0;
JavascriptEngine<?> jsInstance = engine.create();
try {
jsInstance.startRuntime();
} catch (JavascriptException e) {
throw new SenderException("unable to start Javascript engine", e);
}
// Create a Parameter Value List
ParameterValueList pvl = null;
try {
if (getParameterList() != null) {
pvl = getParameterList().getValues(message, session);
}
} catch (ParameterException e) {
throw new SenderException(getLogPrefix() + " exception extracting parameters", e);
}
if (pvl != null) {
numberOfParameters = pvl.size();
}
// This array will contain the parameters given in the configuration
Object[] jsParameters = new Object[numberOfParameters];
for (int i = 0; i < numberOfParameters; i++) {
ParameterValue pv = pvl.getParameterValue(i);
Object value = pv.getValue();
try {
jsParameters[i] = value instanceof Message ? ((Message) value).asString() : value;
} catch (IOException e) {
throw new SenderException(getLogPrefix(), e);
}
}
for (ISender sender : getSenders()) {
jsInstance.registerCallback(sender, session);
}
try {
// Compile the given Javascript and execute the given Javascript function
jsInstance.executeScript(adaptES6Literals(fileInput));
jsResult = jsInstance.executeFunction(jsFunctionName, jsParameters);
} catch (JavascriptException e) {
throw new SenderException("unable to execute script/function", e);
} finally {
jsInstance.closeRuntime();
}
// Pass jsResult, the result of the Javascript function.
// It is recommended to have the result of the Javascript function be of type String, which will be the output of the sender
String result = String.valueOf(jsResult);
if (StringUtils.isEmpty(result) || "null".equals(result) || "undefined".equals(result)) {
return Message.nullMessage();
}
return new Message(result);
}
Aggregations