Search in sources :

Example 1 with JavascriptException

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);
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) Message(nl.nn.adapterframework.stream.Message) IOException(java.io.IOException) ISender(nl.nn.adapterframework.core.ISender) JavascriptException(nl.nn.adapterframework.extensions.javascript.JavascriptException) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

IOException (java.io.IOException)1 ISender (nl.nn.adapterframework.core.ISender)1 ParameterException (nl.nn.adapterframework.core.ParameterException)1 SenderException (nl.nn.adapterframework.core.SenderException)1 JavascriptException (nl.nn.adapterframework.extensions.javascript.JavascriptException)1 ParameterValue (nl.nn.adapterframework.parameters.ParameterValue)1 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)1 Message (nl.nn.adapterframework.stream.Message)1