Search in sources :

Example 1 with ApiServiceExecutionException

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);
}
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 ApiServiceExecutionException

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);
    }
}
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 3 with ApiServiceExecutionException

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);
    }
}
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 ApiServiceExecutionException

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);
    }
}
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 5 with ApiServiceExecutionException

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);
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ApiResourceOutput(com.bluenimble.platform.api.impls.ApiResourceOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)52 JsonObject (com.bluenimble.platform.json.JsonObject)41 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 ApiSpace (com.bluenimble.platform.api.ApiSpace)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 ApiOutput (com.bluenimble.platform.api.ApiOutput)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)8 StorageException (com.bluenimble.platform.storage.StorageException)8 StorageObject (com.bluenimble.platform.storage.StorageObject)8 Api (com.bluenimble.platform.api.Api)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 JsonArray (com.bluenimble.platform.json.JsonArray)5 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)5 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)5 Date (java.util.Date)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 ApiVerb (com.bluenimble.platform.api.ApiVerb)4