use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class CreateEntitySpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
String sEntity = (String) request.get(CommonSpec.Entity);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
JsonArray aFields = Json.getArray(payload, CommonSpec.Fields);
if (aFields == null || aFields.isEmpty()) {
throw new ApiServiceExecutionException("missing fields definition").status(ApiResponse.BAD_REQUEST);
}
Field[] fields = new Field[aFields.count()];
for (int i = 0; i < aFields.count(); i++) {
Object o = aFields.get(i);
if (o == null || !(o instanceof JsonObject)) {
continue;
}
final JsonObject oField = (JsonObject) o;
fields[i] = new Field() {
@Override
public String name() {
return Json.getString(oField, Spec.Field.Name);
}
@Override
public boolean required() {
return Json.getBoolean(oField, Spec.Field.Required, false);
}
@Override
public Type type() {
try {
return Type.valueOf(Json.getString(oField, Spec.Field.Type, Type.String.name()));
} catch (Exception ex) {
return Type.String;
}
}
@Override
public boolean unique() {
return Json.getBoolean(oField, Spec.Field.Unique, false);
}
};
}
try {
Database db = space.feature(Database.class, provider, request);
db.createEntity(sEntity, fields);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.
the class DescribeDatabaseSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
JsonObject describe;
try {
describe = space.feature(Database.class, provider, request).describe();
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput(describe);
}
use of com.bluenimble.platform.api.ApiSpace 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);
}
use of com.bluenimble.platform.api.ApiSpace 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));
}
use of com.bluenimble.platform.api.ApiSpace 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));
}
Aggregations