Search in sources :

Example 11 with ScriptingEngine

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

the class ScriptableApiSpi method onStop.

@Override
public void onStop(Api api, ApiContext context) throws ApiManagementException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    if (!engine.has(spi, Functions.OnStop)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        return;
    }
    // invoke onStop
    try {
        engine.invoke(spi, Functions.OnStop, jsApi, context);
    } catch (Exception ex) {
        api.tracer().log(Level.Error, Lang.BLANK, ex);
    }
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 12 with ScriptingEngine

use of com.bluenimble.platform.scripting.ScriptingEngine 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

JsonObject (com.bluenimble.platform.json.JsonObject)12 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)12 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)11 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)4 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)3 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)3 ApiResource (com.bluenimble.platform.api.ApiResource)2 JsonArray (com.bluenimble.platform.json.JsonArray)2 ApiOutput (com.bluenimble.platform.api.ApiOutput)1 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)1 TemplateEngineException (com.bluenimble.platform.api.impls.media.engines.TemplateEngineException)1 AbstractEmitter (com.bluenimble.platform.json.AbstractEmitter)1 JsonEmitter (com.bluenimble.platform.json.JsonEmitter)1 MapScriptContext (com.bluenimble.platform.scripting.MapScriptContext)1 IOException (java.io.IOException)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 Undefined (jdk.nashorn.internal.runtime.Undefined)1