Search in sources :

Example 41 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class DeleteObjectSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String provider = (String) request.get(CommonSpec.Provider);
    Storage storage = space.feature(Storage.class, provider, request);
    String path = (String) request.get(Spec.Object);
    Boolean force = (Boolean) request.get(Spec.Force);
    if (force == null) {
        force = false;
    }
    StorageObject so = null;
    try {
        so = storage.root().get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    long count = so.count();
    if (so.isFolder() && count > 0 && !force) {
        throw new ApiServiceExecutionException("folder " + path + " isn't empty! It can't be deleted").status(ApiResponse.BAD_REQUEST);
    }
    boolean deleted = false;
    try {
        deleted = so.delete();
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, deleted));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 42 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class RenameObjectSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String provider = (String) request.get(CommonSpec.Provider);
    String path = (String) request.get(Spec.Object);
    String name = (String) request.get(StorageObject.Fields.Name);
    Storage storage = space.feature(Storage.class, provider, request);
    StorageObject so = null;
    try {
        so = storage.root().get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    try {
        so.rename(name);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Renamed, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 43 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class UpdateObjectSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String provider = (String) request.get(CommonSpec.Provider);
    String path = (String) request.get(Spec.Object);
    Boolean append = (Boolean) request.get(Spec.Append);
    if (append == null) {
        append = false;
    }
    ApiStreamSource ss = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
    Storage storage = space.feature(Storage.class, provider, request);
    StorageObject so = null;
    try {
        so = storage.root().get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    if (so.isFolder() && ss != null) {
        throw new ApiServiceExecutionException("object '" + path + "' isn't a valid content aware storage object").status(ApiResponse.BAD_REQUEST);
    }
    InputStream stream = ss.stream();
    try {
        so.update(stream, append);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Updated, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 44 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class CreateSpaceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String namespace = (String) request.get(Spec.Space);
    JsonObject oSpace = (JsonObject) spaceModel.duplicate().set(ApiSpace.Spec.Namespace, namespace);
    // set default secrets
    JsonObject defaultSecrets = Json.getObject(Json.getObject(oSpace, ApiSpace.Spec.secrets.class.getSimpleName()), ApiSpace.Secrets.Default);
    if (defaultSecrets != null) {
        defaultSecrets.set(ApiSpace.Spec.secrets.Key, Lang.UUID(16));
    }
    // create space
    ApiSpace newSpace = null;
    try {
        newSpace = api.space().create(oSpace);
    } catch (ApiManagementException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    // create root keys
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(CommonSpec.Role, Role.ADMIN.name());
    List<KeyPair> keys = null;
    try {
        keys = newSpace.keystore().create(1, null, properties);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    JsonObject result = newSpace.describe(DescribeOption.Info);
    if (keys != null) {
        result.set(CommonOutput.Keys, keys.get(0).toJson());
    }
    return new JsonApiOutput(result);
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) HashMap(java.util.HashMap) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) CommonSpec(com.bluenimble.platform.apis.mgm.CommonSpec) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 45 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class AbstractApiServer method getFeature.

@Override
public Object getFeature(ApiSpace space, Class<?> type, String name) {
    Feature aFeature = type.getAnnotation(Feature.class);
    JsonObject oFeature = Json.getObject(space.getFeatures(), aFeature.name());
    if (oFeature == null || oFeature.isEmpty()) {
        throw new FeatureNotFoundException("feature " + aFeature.name() + " not available in space " + space.getNamespace());
    }
    JsonObject oProvider = Json.getObject(oFeature, name);
    if (oProvider == null || oProvider.isEmpty()) {
        throw new FeatureNotFoundException("feature provider " + name + " not available in space " + space.getNamespace());
    }
    String provider = Json.getString(oProvider, ApiSpace.Features.Provider);
    if (Lang.isNullOrEmpty(provider)) {
        throw new FeatureNotFoundException("provider for feature " + aFeature.name() + Lang.SLASH + name + " is missing");
    }
    ServerFeature feature = features.get(aFeature.name() + FeatureProtocol + provider);
    if (feature == null) {
        throw new FeatureNotFoundException("feature " + name + Lang.COLON + aFeature.name() + FeatureProtocol + provider + " not found");
    }
    return feature.get(space, name);
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) JsonObject(com.bluenimble.platform.json.JsonObject) ServerFeature(com.bluenimble.platform.server.ServerFeature) Feature(com.bluenimble.platform.Feature) FeatureNotFoundException(com.bluenimble.platform.server.FeatureNotFoundException)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)230 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)40 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonArray (com.bluenimble.platform.json.JsonArray)37 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)34 Database (com.bluenimble.platform.db.Database)29 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 File (java.io.File)25 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)23 Map (java.util.Map)22 IOException (java.io.IOException)20 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)17 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 InputStream (java.io.InputStream)14 Date (java.util.Date)14 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)13 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)12 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)11 HashMap (java.util.HashMap)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)9