use of com.bluenimble.platform.api.ApiAccessDeniedException 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.ApiAccessDeniedException 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.ApiAccessDeniedException 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));
}
use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.
the class GetEntrySpi 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 key = (String) request.get(Spec.Key);
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);
return new JsonApiOutput((JsonObject) new JsonObject().set(Spec.Key, key).set(Output.Value, cache.get(key, false)));
}
use of com.bluenimble.platform.api.ApiAccessDeniedException in project serverless by bluenimble.
the class BulkRecordsSpi 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);
}
try {
return new JsonApiOutput(space.feature(Database.class, provider, request).bulk(payload));
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
}
Aggregations