Search in sources :

Example 26 with ApiAccessDeniedException

use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.

the class AddObjectSpi 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);
    ApiStreamSource stream = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
    Storage storage = space.feature(Storage.class, provider, request);
    String path = (String) request.get(Spec.Object);
    try {
        Folder root = storage.root();
        if (!Lang.isNullOrEmpty(path)) {
            String[] aPath = Lang.split(path, Lang.SLASH);
            for (String p : aPath) {
                if (!root.contains(p)) {
                    root = root.add(p, true);
                } else {
                    StorageObject so = root.get(p);
                    if (!so.isFolder()) {
                        throw new StorageException(p + " isn't a valid folder");
                    }
                    root = (Folder) so;
                }
            }
        }
        if (stream != null) {
            root.add(stream, stream.name(), true);
        }
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, 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) Folder(com.bluenimble.platform.storage.Folder) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 27 with ApiAccessDeniedException

use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.

the class CopyObjectSpi 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 sFolder = (String) request.get(Spec.Folder);
    Boolean move = (Boolean) request.get(Spec.Move);
    if (move == null) {
        move = false;
    }
    Storage storage = space.feature(Storage.class, provider, request);
    Folder root = null;
    StorageObject so = null;
    try {
        root = storage.root();
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    try {
        so = root.get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    Folder folder = root;
    StorageObject soFolder = null;
    if (!Lang.isNullOrEmpty(sFolder)) {
        try {
            soFolder = root.get(sFolder);
        } catch (StorageException e) {
            throw new ApiServiceExecutionException("folder '" + sFolder + "' not found", e).status(ApiResponse.NOT_FOUND);
        }
    }
    if (!soFolder.isFolder()) {
        throw new ApiServiceExecutionException("storage object '" + sFolder + "' isn't a valid folder").status(ApiResponse.BAD_REQUEST);
    }
    try {
        so.copy(folder, move);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(move ? CommonOutput.Moved : CommonOutput.Copied, 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) Folder(com.bluenimble.platform.storage.Folder) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 28 with ApiAccessDeniedException

use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.

the class GetObjectSpi 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);
    String filter = (String) request.get(Spec.Filter);
    Folder root = null;
    StorageObject so = null;
    try {
        root = storage.root();
        so = root;
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    try {
        if (!Lang.isNullOrEmpty(path) && !path.trim().equals(Lang.DOT)) {
            so = root.get(path);
        }
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    try {
        return so.toOutput(StorageUtils.guessFilter(filter), null, null);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
}
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) Folder(com.bluenimble.platform.storage.Folder) StorageException(com.bluenimble.platform.storage.StorageException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) StorageException(com.bluenimble.platform.storage.StorageException)

Aggregations

ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)27 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)25 JsonObject (com.bluenimble.platform.json.JsonObject)22 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 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 Cache (com.bluenimble.platform.cache.Cache)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 Folder (com.bluenimble.platform.storage.Folder)3 Api (com.bluenimble.platform.api.Api)2 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)2 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)2 Role (com.bluenimble.platform.apis.mgm.Role)2 ExchangeOption (com.bluenimble.platform.db.Database.ExchangeOption)2