Search in sources :

Example 51 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException 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 52 with ApiServiceExecutionException

use of com.bluenimble.platform.api.ApiServiceExecutionException 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

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)52 JsonObject (com.bluenimble.platform.json.JsonObject)41 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 ApiSpace (com.bluenimble.platform.api.ApiSpace)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 ApiOutput (com.bluenimble.platform.api.ApiOutput)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)8 StorageException (com.bluenimble.platform.storage.StorageException)8 StorageObject (com.bluenimble.platform.storage.StorageObject)8 Api (com.bluenimble.platform.api.Api)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 JsonArray (com.bluenimble.platform.json.JsonArray)5 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)5 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)5 Date (java.util.Date)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 ApiVerb (com.bluenimble.platform.api.ApiVerb)4