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);
}
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);
}
}
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);
}
}
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());
}
}
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);
}
}
Aggregations