use of com.bluenimble.platform.storage.Storage 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));
}
use of com.bluenimble.platform.storage.Storage 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));
}
use of com.bluenimble.platform.storage.Storage 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));
}
use of com.bluenimble.platform.storage.Storage 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));
}
use of com.bluenimble.platform.storage.Storage 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));
}
Aggregations