Search in sources :

Example 6 with StorageObject

use of com.bluenimble.platform.storage.StorageObject 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 7 with StorageObject

use of com.bluenimble.platform.storage.StorageObject 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)

Example 8 with StorageObject

use of com.bluenimble.platform.storage.StorageObject in project serverless by bluenimble.

the class PutStorageObjectApiServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Storage storage = api.space().feature(Storage.class, provider, request);
    String path = (String) request.get(objectParameter);
    if (Lang.isNullOrEmpty(path)) {
        throw new ApiServiceExecutionException("missing object path parameter '" + objectParameter + "'").status(ApiResponse.BAD_REQUEST);
    }
    String objectName = null;
    StorageObject so = null;
    long length = -1;
    ApiStreamSource stream = null;
    try {
        stream = (ApiStreamSource) request.get(streamParameter, Scope.Stream);
        if (Lang.isNullOrEmpty(stream.name())) {
            objectName = Lang.UUID(20);
        }
        so = findFolder(storage.root(), this.folder).add(stream, objectName, true);
        length = so.length();
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(StorageObject.Fields.Name, so.name()).set(StorageObject.Fields.Timestamp, Lang.toUTC(so.timestamp())).set(StorageObject.Fields.Length, length));
}
Also used : Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) 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 9 with StorageObject

use of com.bluenimble.platform.storage.StorageObject in project serverless by bluenimble.

the class AbstractStorageApiServiceSpi method findFolder.

protected Folder findFolder(Folder parent, String path) throws StorageException {
    Folder folder = parent;
    if (Lang.isNullOrEmpty(path)) {
        return folder;
    }
    StorageObject so = folder.get(path);
    if (so == null) {
        throw new StorageException(path + " not found");
    }
    if (!so.isFolder()) {
        throw new StorageException(path + " isn't a valid folder");
    }
    return (Folder) so;
}
Also used : StorageObject(com.bluenimble.platform.storage.StorageObject) Folder(com.bluenimble.platform.storage.Folder) StorageException(com.bluenimble.platform.storage.StorageException)

Example 10 with StorageObject

use of com.bluenimble.platform.storage.StorageObject in project serverless by bluenimble.

the class GetStorageObjectApiServiceSpi method getObject.

private ApiOutput getObject(Api api, ApiRequest request) throws ApiServiceExecutionException {
    Storage storage = api.space().feature(Storage.class, provider, request);
    String path = (String) request.get(objectParameter);
    if (Lang.isNullOrEmpty(path)) {
        throw new ApiServiceExecutionException("object path not found. Missing request parameter '" + objectParameter + "'").status(ApiResponse.BAD_REQUEST);
    }
    ApiOutput output;
    try {
        StorageObject object = findFolder(storage.root(), this.folder).get(path);
        output = object.toOutput(null, (String) request.get(Spec.As), MediaTypeUtils.getMediaForFile((String) request.get(Spec.Type)));
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (output == null) {
        throw new ApiServiceExecutionException("object " + this.folder + Lang.SLASH + path + " not found").status(ApiResponse.BAD_REQUEST);
    }
    request.set(Output, output);
    return output;
}
Also used : Storage(com.bluenimble.platform.storage.Storage) ApiOutput(com.bluenimble.platform.api.ApiOutput) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) StorageException(com.bluenimble.platform.storage.StorageException)

Aggregations

StorageException (com.bluenimble.platform.storage.StorageException)10 StorageObject (com.bluenimble.platform.storage.StorageObject)10 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 Storage (com.bluenimble.platform.storage.Storage)8 JsonObject (com.bluenimble.platform.json.JsonObject)7 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)6 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)6 Folder (com.bluenimble.platform.storage.Folder)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1