Search in sources :

Example 1 with StorageObject

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

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

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

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

the class FileSystemStorageObject method toJson.

public JsonObject toJson(Folder.Filter filter, boolean fetchChildren) {
    JsonObject data = (JsonObject) new JsonObject().set(StorageObject.Fields.Name, name()).set(StorageObject.Fields.Timestamp, Lang.toUTC(timestamp()));
    try {
        if (isFolder()) {
            long count = new FileSystemFolder(source, false).count();
            data.set(ApiOutput.Defaults.Count, count);
            data.set(StorageObject.Fields.Length, length());
            // get files
            if (count > 0 && fetchChildren) {
                final JsonArray items = new JsonArray();
                data.set(ApiOutput.Defaults.Items, items);
                Folder folder = toFolder();
                folder.list(new Folder.Visitor() {

                    @Override
                    public void visit(StorageObject o) {
                        items.add(((FileSystemStorageObject) o).toJson(null, false));
                    }
                }, filter);
            }
        } else {
            data.set(StorageObject.Fields.Length, length()).set(StorageObject.Fields.ContentType, contentType());
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
    return data;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) StorageObject(com.bluenimble.platform.storage.StorageObject) JsonObject(com.bluenimble.platform.json.JsonObject) Folder(com.bluenimble.platform.storage.Folder) StorageException(com.bluenimble.platform.storage.StorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with StorageObject

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

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