use of com.bluenimble.platform.api.ApiServiceExecutionException 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.api.ApiServiceExecutionException 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.api.ApiServiceExecutionException 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);
}
}
use of com.bluenimble.platform.api.ApiServiceExecutionException 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);
}
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class GetResourceApiServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String path = (String) request.get(Spec.Path);
if (Lang.isNullOrEmpty(path)) {
throw new ApiServiceExecutionException("Resource / not found").status(ApiResponse.BAD_REQUEST);
}
String location = (String) Json.find(request.getService().getCustom(), Custom.Resources, Custom.Root);
if (!Lang.isNullOrEmpty(location)) {
path = location + Lang.SLASH + path;
}
ApiResource r;
try {
r = api.getResourcesManager().get(Lang.split(path, Lang.SLASH));
} catch (ApiResourcesManagerException e) {
throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.BAD_REQUEST);
}
if (r == null) {
throw new ApiServiceExecutionException("Resource " + path + " not found").status(ApiResponse.NOT_FOUND);
}
return new ApiResourceOutput(r);
}
Aggregations