Search in sources :

Example 1 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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 JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class GetRecordSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    String record = (String) request.get(Spec.Record);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.get(sEntity, record);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (dbo == null) {
        return null;
    }
    return new JsonApiOutput(dbo.toJson(null));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 3 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class ChangeApiStatusSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String apiNs = (String) request.get(CommonSpec.Api);
    String sAction = (String) request.getResource()[request.getResource().length - 1];
    Action action = null;
    try {
        action = Action.valueOf(sAction);
    } catch (Exception ex) {
    // ignore
    }
    if (action == null) {
        throw new ApiServiceExecutionException("unknown change-status action " + sAction).status(ApiResponse.BAD_REQUEST);
    }
    ApiSpace space = null;
    try {
        space = MgmUtils.space(consumer, api);
        switch(action) {
            case start:
                space.start(apiNs);
                break;
            case stop:
                space.stop(apiNs);
                break;
            case pause:
                space.pause(apiNs);
                break;
            case resume:
                space.resume(apiNs);
                break;
            default:
                break;
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    Api targetApi = space.api(apiNs);
    JsonObject result = (JsonObject) new JsonObject().set(Api.Spec.Status, targetApi.status().name());
    if (ApiStatus.Failed.equals(targetApi.status())) {
        result.set(Output.Reason, targetApi.getFailure());
    }
    return new JsonApiOutput(result);
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Api(com.bluenimble.platform.api.Api) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 4 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class DescribeApiSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String apiNs = (String) request.get(CommonSpec.Api);
    Api uApi;
    try {
        uApi = MgmUtils.api(consumer, api, apiNs);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
    }
    if (uApi == null) {
        throw new ApiServiceExecutionException("api '" + apiNs + "' not found").status(ApiResponse.NOT_FOUND);
    }
    return new JsonApiOutput(uApi.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 5 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class InstallApiFromFileSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String spaceFolder = (String) request.get(Spec.SpaceFolder);
    String apiFile = (String) request.get(Spec.ApiFile);
    Boolean start = (Boolean) request.get(Spec.Start);
    if (start == null) {
        start = true;
    }
    ApiSpace targetSpace = null;
    Api installed = null;
    try {
        targetSpace = MgmUtils.space(consumer, api);
        installed = targetSpace.install(spaceFolder, apiFile);
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (installed == null) {
        throw new ApiServiceExecutionException("can't install api. Server can't return a valid api object");
    }
    try {
        if (start) {
            targetSpace.start(installed.getNamespace());
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    return new JsonApiOutput(installed.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)39 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 JsonObject (com.bluenimble.platform.json.JsonObject)34 ApiSpace (com.bluenimble.platform.api.ApiSpace)27 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)25 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 ApiOutput (com.bluenimble.platform.api.ApiOutput)7 Api (com.bluenimble.platform.api.Api)6 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 JsonArray (com.bluenimble.platform.json.JsonArray)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)4 Date (java.util.Date)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)3 Cache (com.bluenimble.platform.cache.Cache)3