Search in sources :

Example 1 with ScriptingEngine

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

the class ScriptableApiServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    SpecAndSpiPair serviceHelper = (SpecAndSpiPair) request.getService().getHelper();
    Object spi = serviceHelper.spi();
    if (spi == null) {
        throw new ApiServiceExecutionException("service spi not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
    if (!engine.has(spi, Functions.Execute)) {
        return null;
    }
    // invoke execute
    Object result = null;
    try {
        result = engine.invoke(spi, Functions.Execute, jsApi, consumer, request, response);
    } catch (ScriptingEngineException ex) {
        ex.setScript(Json.getString(request.getService().getRuntime(), Api.Spec.Runtime.Function));
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (result == null || (result instanceof Undefined)) {
        return null;
    }
    if (ApiOutput.class.isAssignableFrom(result.getClass())) {
        return (ApiOutput) result;
    }
    if (ScriptObjectMirror.class.isAssignableFrom(result.getClass())) {
        ScriptObjectMirror som = (ScriptObjectMirror) result;
        Object clazz = som.get(ClassField);
        if (clazz == null) {
            return new ApiSomOutput(som);
        }
        if (clazz.equals(ApiOutputClass)) {
            return (ApiOutput) som.getMember(ProxyField);
        }
    }
    Object converted = Converters.convert(result);
    if (converted instanceof JsonArray) {
        converted = new JsonObject().set(ApiOutput.Defaults.Items, converted);
    }
    if (!(converted instanceof JsonObject)) {
        throw new ApiServiceExecutionException("result should be a valid json object");
    }
    return new JsonApiOutput((JsonObject) converted);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Undefined(jdk.nashorn.internal.runtime.Undefined) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ApiOutput(com.bluenimble.platform.api.ApiOutput) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 2 with ScriptingEngine

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

the class ScriptableApiServiceSpi method onStop.

@Override
public void onStop(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    SpecAndSpiPair apiHelper = (SpecAndSpiPair) api.getHelper();
    if (apiHelper == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    Object jsApi = apiHelper.spec();
    if (jsApi == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    SpecAndSpiPair serviceHelper = (SpecAndSpiPair) service.getHelper();
    Object spi = serviceHelper.spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    if (!engine.has(spi, Functions.OnStop)) {
        return;
    }
    // invoke onStop
    try {
        engine.invoke(spi, Functions.OnStop, jsApi, serviceHelper.spec(), context);
    } catch (ScriptingEngineException ex) {
        api.tracer().log(Level.Error, Lang.BLANK, ex);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 3 with ScriptingEngine

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

the class ScriptableApiSpi method onService.

@Override
public void onService(Api api, 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.OnService)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
    }
    // invoke onService
    try {
        engine.invoke(spi, Functions.OnService, jsApi, 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 4 with ScriptingEngine

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

the class ScriptableApiSpi method onError.

@Override
public void onError(Api api, ApiService service, ApiConsumer consumer, ApiRequest request, ApiResponse response, JsonObject error) {
    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.OnError)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (api == null || jsApi == null) {
        Object msg = error.get(ApiResponse.Error.Message);
        if (msg != null) {
            msg += Lang.ENDLN + "api or spi not attached on Api OnStart";
        } else {
            msg = Lang.ENDLN + "api or spi not attached on Api OnStart";
        }
        api.tracer().log(Level.Error, msg);
        error.set(ApiResponse.Error.Message, msg);
        return;
    }
    // invoke onError
    try {
        engine.invoke(spi, Functions.OnError, jsApi, service, consumer, request, response, error);
    } catch (ScriptingEngineException ex) {
        api.tracer().log(Level.Error, Lang.BLANK, ex);
        error.set(ApiResponse.Error.Message, ex.getMessage());
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 5 with ScriptingEngine

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

the class ScriptableApiSpi method onStart.

@Override
public void onStart(Api api, ApiContext context) throws ApiManagementException {
    String script = Json.getString(api.getRuntime(), Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("function not defined in " + ApiUtils.RuntimeKey);
    }
    String[] path = Lang.split(script, Lang.SLASH);
    ApiResource rScript = null;
    try {
        rScript = api.getResourcesManager().get(path);
    } catch (ApiResourcesManagerException ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (rScript == null) {
        throw new ApiManagementException("function '" + Lang.join(path, Lang.SLASH) + "' not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    // create the spi
    Object jsSpi = null;
    try {
        jsSpi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsSpi == null) {
        throw new ApiManagementException("function returned an undefined object");
    }
    // create the api object
    Object jsApi = null;
    try {
        jsApi = engine.invoke(null, Api.class.getSimpleName(), api);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsApi == null) {
        throw new ApiManagementException("can't create 'api spec' js object");
    }
    api.setHelper(new SpecAndSpiPair(jsApi, jsSpi));
    if (!engine.has(jsSpi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(jsSpi, Functions.OnStart, jsApi, context);
    } catch (ScriptingEngineException ex) {
        ex.setScript(script);
        throw new ApiManagementException(ex.getMessage(), ex);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) 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)

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