use of com.bluenimble.platform.api.ApiAccessDeniedException 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.api.ApiAccessDeniedException 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.api.ApiAccessDeniedException in project serverless by bluenimble.
the class DescribeServiceSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String apiNs = (String) request.get(CommonSpec.Api);
Api uApi;
try {
uApi = MgmUtils.api(consumer, api, apiNs);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
if (uApi == null) {
throw new ApiServiceExecutionException("api '" + apiNs + "' not found").status(ApiResponse.NOT_FOUND);
}
String sVerb = (String) request.get(CommonSpec.Verb);
ApiVerb verb = null;
try {
verb = ApiVerb.valueOf(sVerb.toUpperCase());
} catch (Exception ex) {
verb = ApiVerb.GET;
}
String endpoint = (String) request.get(CommonSpec.Endpoint);
ApiService service = uApi.getServicesManager().get(verb, Lang.SLASH + endpoint);
if (service == null) {
throw new ApiServiceExecutionException("service '" + verb + Lang.SPACE + endpoint + "' not found").status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput(service.toJson());
}
use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.
the class ImportDatabaseSpi 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);
JsonObject result = new JsonObject();
result.set(Spec.File, file);
final StringBuilder sb = new StringBuilder();
try {
space.feature(Database.class, provider, request).imp(entities, space.feature(Storage.class, provider, request).root().get(file).reader(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.ApiAccessDeniedException in project serverless by bluenimble.
the class QueryEntitySpi 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);
String serializer = (String) request.get(CommonSpec.Serializer);
String[] levels = Lang.split(serializer, Lang.COMMA);
int allStopLevel = 2;
if (levels != null && levels.length > 0) {
try {
allStopLevel = Integer.valueOf(levels[0]);
} catch (Exception ex) {
}
}
int minStopLevel = 3;
if (levels != null && levels.length > 0) {
try {
minStopLevel = Integer.valueOf(levels[1]);
} catch (Exception ex) {
}
}
JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
payload.set(Database.Fields.Entity, sEntity);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
List<DatabaseObject> records = null;
try {
Database db = space.feature(Database.class, provider, request);
records = db.find(null, new JsonQuery(payload), null);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
JsonObject result = new JsonObject();
JsonArray aRecords = new JsonArray();
result.set(Output.Records, aRecords);
if (records == null || records.isEmpty()) {
return new JsonApiOutput(result);
}
for (int i = 0; i < records.size(); i++) {
aRecords.add(records.get(i).toJson(new DefaultDatabaseObjectSerializer(allStopLevel, minStopLevel)));
}
return new JsonApiOutput(result);
}
Aggregations