use of com.bluenimble.platform.api.ApiServiceExecutionException 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.ApiServiceExecutionException 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);
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class CreateKeysSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
Role role = Role.SUPER.equals(cRole) ? Role.ADMIN : Role.DEVELOPER;
String sRole = Json.getString(payload, CommonSpec.Role);
if (!Lang.isNullOrEmpty(sRole)) {
try {
role = Role.valueOf(sRole.trim().toUpperCase());
} catch (Exception ex) {
// undefined role
}
}
if (Role.SUPER.equals(cRole) && role.equals(Role.DEVELOPER)) {
throw new ApiServiceExecutionException("super users can't create developer keys").status(ApiResponse.FORBIDDEN);
}
if (Role.ADMIN.equals(cRole) && role.equals(Role.ADMIN)) {
throw new ApiServiceExecutionException("admin users can't create admin keys").status(ApiResponse.FORBIDDEN);
}
ApiSpace space;
if (Role.SUPER.equals(cRole)) {
String spaceNs = Json.getString(payload, Spec.Space);
if (Lang.isNullOrEmpty(spaceNs)) {
throw new ApiServiceExecutionException("no space found in payload").status(ApiResponse.BAD_REQUEST);
}
try {
space = api.space().space(spaceNs);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
} else {
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
}
if (space == null) {
throw new ApiServiceExecutionException("target space where to create the keys isn't found").status(ApiResponse.BAD_REQUEST);
}
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(CommonSpec.Role, role.name());
Date expiryDate = null;
if (!Json.isNullOrEmpty(payload)) {
expiryDate = (Date) payload.get(KeyPair.Fields.ExpiryDate);
Iterator<String> props = payload.keys();
while (props.hasNext()) {
String p = props.next();
if (Exclude.contains(p)) {
continue;
}
properties.put(p, payload.get(p));
}
}
List<KeyPair> list = null;
try {
list = space.keystore().create(1, expiryDate, properties);
} catch (SpaceKeyStoreException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
}
if (list == null) {
return new JsonApiOutput(null);
}
return new JsonApiOutput(list.get(0).toJson());
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class ListKeysSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
int offset = (Integer) request.get(Spec.Offset);
int length = (Integer) request.get(Spec.Length);
String sFilters = (String) request.get(Spec.Filters);
SpaceKeyStore.ListFilter[] filters = null;
if (!Lang.isNullOrEmpty(sFilters)) {
String[] aFilters = Lang.split(sFilters, Lang.COMMA, true);
filters = new SpaceKeyStore.ListFilter[aFilters.length + 1];
for (int i = 0; i < aFilters.length; i++) {
String f = aFilters[i];
int idexOfStartUnderscore = f.indexOf(Token);
if (idexOfStartUnderscore < -1) {
continue;
}
int idexOfEndUnderscore = f.indexOf(Token, idexOfStartUnderscore + 2);
if (idexOfEndUnderscore < -1) {
continue;
}
filters[i] = new SpaceKeyStore.ListFilter() {
@Override
public String name() {
return f.substring(0, idexOfStartUnderscore);
}
@Override
public Object value() {
String value = f.substring(idexOfEndUnderscore + 2);
if (Lang.isNullOrEmpty(value)) {
return null;
}
return value;
}
@Override
public Operator operator() {
try {
return Operator.valueOf(f.substring(idexOfStartUnderscore + 2, idexOfEndUnderscore));
} catch (Exception ex) {
return Operator.eq;
}
}
};
}
} else {
filters = new SpaceKeyStore.ListFilter[1];
}
JsonObject result = new JsonObject();
JsonArray aKeys = new JsonArray();
result.set(Output.Keys, aKeys);
if (Role.SUPER.equals(cRole)) {
filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {
@Override
public String name() {
return CommonSpec.Role;
}
@Override
public Object value() {
return Role.ADMIN.name();
}
@Override
public Operator operator() {
return Operator.eq;
}
};
try {
Collection<ApiSpace> spaces = api.space().spaces();
for (ApiSpace space : spaces) {
addSpaceKeys(space, offset, length, filters, aKeys);
}
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
} else {
filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {
@Override
public String name() {
return CommonSpec.Role;
}
@Override
public Object value() {
return Role.DEVELOPER.name();
}
@Override
public Operator operator() {
return Operator.eq;
}
};
ApiSpace consumerSpace;
try {
consumerSpace = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
addSpaceKeys(consumerSpace, offset, length, filters, aKeys);
}
return new JsonApiOutput(result);
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class AddEntrySpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
Cache cache = space.feature(Cache.class, provider, request);
cache.put(Json.getString(payload, Spec.Key), payload.get(Spec.Value), Json.getInteger(payload, Spec.Ttl, 0));
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
Aggregations