Search in sources :

Example 11 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class ExportDatabaseSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String[] aEntities = Lang.split((String) request.get(Spec.Entities), Lang.COMMA, true);
    Set<String> entities = null;
    if (aEntities != null && aEntities.length > 0) {
        entities = new HashSet<String>();
        for (String entity : aEntities) {
            entities.add(entity.toUpperCase());
        }
    }
    String[] aOptions = Lang.split((String) request.get(Spec.Options), Lang.COMMA, true);
    Map<ExchangeOption, Boolean> options = null;
    if (aOptions != null && aOptions.length > 0) {
        for (String o : aOptions) {
            try {
                options.put(ExchangeOption.valueOf(o.toLowerCase()), true);
            } catch (Exception ex) {
            // ignore malformed options
            }
        }
    }
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String file = (String) request.get(Spec.File) + DotGz;
    JsonObject result = new JsonObject();
    result.set(Spec.File, file);
    final StringBuilder sb = new StringBuilder();
    try {
        space.feature(Database.class, provider, request).exp(entities, space.feature(Storage.class, provider, request).root().get(file).writer(request), options, new Database.ExchangeListener() {

            @Override
            public void onMessage(String message) {
                sb.append(message).append(Lang.ENDLN);
            }
        });
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    result.set(Output.Feedback, sb.toString());
    sb.setLength(0);
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ExchangeOption(com.bluenimble.platform.db.Database.ExchangeOption)

Example 12 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class StopWorkerSpi method execute.

@Override
public ApiOutput execute(Api api, final 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.NOT_FOUND);
    }
    boolean interrupted = space.executor() == null ? false : space.executor().interrupt((Long) request.get(Spec.Worker));
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Stopped, interrupted));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 13 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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 14 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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 15 with JsonApiOutput

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

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)39 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 JsonObject (com.bluenimble.platform.json.JsonObject)34 ApiSpace (com.bluenimble.platform.api.ApiSpace)27 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)25 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 ApiOutput (com.bluenimble.platform.api.ApiOutput)7 Api (com.bluenimble.platform.api.Api)6 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 JsonArray (com.bluenimble.platform.json.JsonArray)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)4 Date (java.util.Date)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)3 Cache (com.bluenimble.platform.cache.Cache)3