Search in sources :

Example 6 with ApiStreamSource

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

Aggregations

ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)6 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)4 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)4 JsonObject (com.bluenimble.platform.json.JsonObject)4 ApiSpace (com.bluenimble.platform.api.ApiSpace)3 Storage (com.bluenimble.platform.storage.Storage)3 StorageException (com.bluenimble.platform.storage.StorageException)3 StorageObject (com.bluenimble.platform.storage.StorageObject)3 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)2 Api (com.bluenimble.platform.api.Api)1 MessengerException (com.bluenimble.platform.messaging.MessengerException)1 Serializer (com.bluenimble.platform.remote.Serializer)1 JsonSerializer (com.bluenimble.platform.remote.impls.serializers.JsonSerializer)1 StreamSerializer (com.bluenimble.platform.remote.impls.serializers.StreamSerializer)1 TextSerializer (com.bluenimble.platform.remote.impls.serializers.TextSerializer)1 XmlSerializer (com.bluenimble.platform.remote.impls.serializers.XmlSerializer)1 Folder (com.bluenimble.platform.storage.Folder)1 VariableResolver (com.bluenimble.platform.templating.VariableResolver)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1