Search in sources :

Example 6 with JsonApiOutput

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

the class InstallApiSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiStreamSource stream = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
    Boolean start = (Boolean) request.get(Spec.Start);
    if (start == null) {
        start = false;
    }
    ApiSpace targetSpace = null;
    Api installed = null;
    try {
        targetSpace = MgmUtils.space(consumer, api);
        installed = targetSpace.install(stream);
    } 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) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 7 with JsonApiOutput

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

the class DeleteEntrySpi 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 key = (String) request.get(Spec.Key);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    Cache cache = space.feature(Cache.class, provider, request);
    cache.delete(key);
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) Cache(com.bluenimble.platform.cache.Cache)

Example 8 with JsonApiOutput

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

the class AddRecordSpi 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);
    final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    DatabaseObject dbo;
    try {
        Database db = space.feature(Database.class, provider, request);
        dbo = db.create(sEntity);
        dbo.load(payload);
        dbo.save();
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(ApiOutput.Defaults.Id, dbo.getId()).set(ApiOutput.Defaults.Timestamp, Lang.toUTC(dbo.getTimestamp())));
}
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) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 9 with JsonApiOutput

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

the class CreateEntitySpi 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);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    JsonArray aFields = Json.getArray(payload, CommonSpec.Fields);
    if (aFields == null || aFields.isEmpty()) {
        throw new ApiServiceExecutionException("missing fields definition").status(ApiResponse.BAD_REQUEST);
    }
    Field[] fields = new Field[aFields.count()];
    for (int i = 0; i < aFields.count(); i++) {
        Object o = aFields.get(i);
        if (o == null || !(o instanceof JsonObject)) {
            continue;
        }
        final JsonObject oField = (JsonObject) o;
        fields[i] = new Field() {

            @Override
            public String name() {
                return Json.getString(oField, Spec.Field.Name);
            }

            @Override
            public boolean required() {
                return Json.getBoolean(oField, Spec.Field.Required, false);
            }

            @Override
            public Type type() {
                try {
                    return Type.valueOf(Json.getString(oField, Spec.Field.Type, Type.String.name()));
                } catch (Exception ex) {
                    return Type.String;
                }
            }

            @Override
            public boolean unique() {
                return Json.getBoolean(oField, Spec.Field.Unique, false);
            }
        };
    }
    try {
        Database db = space.feature(Database.class, provider, request);
        db.createEntity(sEntity, fields);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonArray(com.bluenimble.platform.json.JsonArray) Field(com.bluenimble.platform.db.Database.Field) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 10 with JsonApiOutput

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

the class DescribeDatabaseSpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    JsonObject describe;
    try {
        describe = space.feature(Database.class, provider, request).describe();
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput(describe);
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) 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