Search in sources :

Example 6 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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 7 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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)

Example 8 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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 9 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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 10 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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)

Aggregations

ApiSpace (com.bluenimble.platform.api.ApiSpace)48 JsonObject (com.bluenimble.platform.json.JsonObject)33 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)27 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)27 Feature (com.bluenimble.platform.Feature)10 Plugin (com.bluenimble.platform.plugins.Plugin)10 ServerFeature (com.bluenimble.platform.server.ServerFeature)10 AbstractPlugin (com.bluenimble.platform.plugins.impls.AbstractPlugin)9 Database (com.bluenimble.platform.db.Database)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 KeyPair (com.bluenimble.platform.security.KeyPair)5 Api (com.bluenimble.platform.api.Api)4 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)3