Search in sources :

Example 11 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class ScriptableApiSpi method onExecute.

@Override
public void onExecute(Api api, ApiConsumer consumer, ApiService service, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.OnExecute)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
    }
    try {
        // update consumer id
        if (consumer.getReference() != null) {
            engine.invoke(consumer.getReference(), ConsumerSet, ApiConsumer.Fields.Id, consumer.get(ApiConsumer.Fields.Id));
        }
        // invoke onExecute
        engine.invoke(spi, Functions.OnExecute, jsApi, consumer, service, request, response);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 12 with ScriptingEngineException

use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.

the class ScriptableApiSpi method findConsumer.

@Override
public void findConsumer(Api api, ApiService service, ApiRequest request, ApiConsumer consumer) throws ApiAuthenticationException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.FindConsumer)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiAuthenticationException("api or spi not attached on Api OnStart");
    }
    // invoke findConsumer
    try {
        engine.invoke(spi, Functions.FindConsumer, jsApi, service, request, consumer);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiAuthenticationException(ex.getMessage(), ex);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Aggregations

ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)12 JsonObject (com.bluenimble.platform.json.JsonObject)10 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)10 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)7 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)3 ApiResource (com.bluenimble.platform.api.ApiResource)2 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 Referenceable (com.bluenimble.platform.Referenceable)1 ApiOutput (com.bluenimble.platform.api.ApiOutput)1 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 Scriptable (com.bluenimble.platform.scripting.Scriptable)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 SequenceInputStream (java.io.SequenceInputStream)1 ArrayList (java.util.ArrayList)1