Search in sources :

Example 6 with ScriptingEngine

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

the class ScriptableApiSpi method afterExecute.

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

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

the class ScriptableApiSpi method onRequest.

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

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

the class JavascriptEngine method write.

@Override
public void write(ApiConsumer consumer, ApiRequest request, final ApiResponse response, ApiOutput output, final ApiResource template, final JsonObject mediaSpec) throws TemplateEngineException {
    try {
        ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, null);
        MapScriptContext vars = new MapScriptContext();
        vars.set(Json.getString(features, I18n, I18n), api.i18n(request.getLang()));
        vars.set(Json.getString(features, Request, Request), request.toJson());
        if (consumer != null) {
            vars.set(Json.getString(features, Consumer, Consumer), consumer.toJson());
        }
        JsonObject jOutput = null;
        if (output != null) {
            jOutput = output.data();
        }
        if (output != null) {
            vars.set(Json.getString(features, Output, Output), jOutput);
            vars.set(Json.getString(features, Meta, Meta), output.meta());
        }
        vars.set(Json.getString(features, Error, Error), response.getError());
        vars.set(Json.getString(features, Response, Response), response);
        JsonObject result;
        Object oResult = engine.eval(ScriptingEngine.Supported.Javascript, api, template, vars);
        if (oResult != null) {
            if (oResult instanceof JsonObject) {
                result = (JsonObject) oResult;
            } else if (oResult instanceof JsonArray) {
                result = new JsonObject();
                result.set(ApiOutput.Defaults.Items, oResult);
            } else {
                result = new JsonObject();
                result.set(Output, oResult);
            }
        } else {
            result = jOutput;
        }
        if (response.getError() != null) {
            result = response.getError();
        }
        boolean doNotWrite = Json.getBoolean(jOutput, DoNotWrite, false);
        if (!doNotWrite) {
            doNotWrite = Json.getBoolean(mediaSpec, DoNotWrite, false);
        }
        if (result != null && !doNotWrite) {
            response.flushHeaders();
            result.write(new AbstractEmitter() {

                @Override
                public JsonEmitter write(String chunk) {
                    try {
                        response.write(chunk);
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                    return this;
                }
            });
        }
    } catch (Exception e) {
        throw new TemplateEngineException(e.getMessage(), e);
    }
}
Also used : AbstractEmitter(com.bluenimble.platform.json.AbstractEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) MapScriptContext(com.bluenimble.platform.scripting.MapScriptContext) JsonArray(com.bluenimble.platform.json.JsonArray) JsonEmitter(com.bluenimble.platform.json.JsonEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 9 with ScriptingEngine

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

the class ScriptableApiServiceSpi method onStart.

@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    SpecAndSpiPair helper = (SpecAndSpiPair) api.getHelper();
    if (helper == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting or couldn't start correctly.");
    }
    Object jsApi = helper.spec();
    if (jsApi == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    JsonObject runtime = service.getRuntime();
    String script = Json.getString(runtime, Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("script not found 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 spec
    Object jsService = null;
    try {
        jsService = engine.invoke(null, ApiService.class.getSimpleName(), service);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsService == null) {
        throw new ApiManagementException("can't create 'service spec' js object");
    }
    // create the spi
    Object spi = null;
    try {
        spi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (spi == null) {
        throw new ApiManagementException("script returned an undefined object");
    }
    service.setHelper(new SpecAndSpiPair(jsService, spi));
    if (!engine.has(spi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(spi, Functions.OnStart, jsApi, jsService, 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) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 10 with ScriptingEngine

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

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