use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class KeyStoreAwareApiSpi method findConsumer.
@Override
public void findConsumer(Api api, ApiService service, ApiRequest request, ApiConsumer consumer) throws ApiAuthenticationException {
String accessKey = (String) consumer.get(ApiConsumer.Fields.AccessKey);
if ("container".equals(request.getChannel())) {
consumer.override((ApiConsumer) request.get(ApiRequest.Consumer));
return;
}
if (!MgmUtils.isSecure(service)) {
if (root.accessKey().equals(accessKey)) {
consumer.set(ApiConsumer.Fields.SecretKey, root.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, root.expiryDate());
consumer.set(CommonSpec.Role, Role.SUPER.name());
}
return;
}
if (!consumer.type().equals(Type.Signature)) {
throw new ApiAuthenticationException("unsupported authentication scheme");
}
JsonArray roles = Json.getArray(service.getSecurity(), ApiService.Spec.Security.Roles);
if (root.accessKey().equals(accessKey)) {
if (roles == null || roles.isEmpty() || !roles.contains(Role.SUPER.name().toLowerCase())) {
throw new ApiAuthenticationException("insuffisant permissions");
}
consumer.set(ApiConsumer.Fields.SecretKey, root.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, root.expiryDate());
consumer.set(CommonSpec.Role, Role.SUPER.name());
} else {
int indexOfDot = accessKey.indexOf(Lang.DOT);
if (indexOfDot <= 0) {
throw new ApiAuthenticationException("invalid accessKey");
}
String consumerSpaceNs = accessKey.substring(0, indexOfDot);
accessKey = accessKey.substring(indexOfDot + 1);
ApiSpace consumerSpace;
try {
consumerSpace = api.space().space(consumerSpaceNs);
} catch (ApiAccessDeniedException e) {
throw new ApiAuthenticationException("instance manager can't access requested space");
}
KeyPair skp;
try {
skp = consumerSpace.keystore().get(accessKey, true);
} catch (SpaceKeyStoreException e) {
throw new ApiAuthenticationException("instance manager can't access space keystore");
}
if (skp == null) {
throw new ApiAuthenticationException("accessKey " + accessKey + " not found");
}
String role = (String) skp.property(CommonSpec.Role);
if (Lang.isNullOrEmpty(role)) {
throw new ApiAuthenticationException("no role defined for consumer");
}
if (roles != null && !roles.isEmpty() && !roles.contains(role.toLowerCase())) {
throw new ApiAuthenticationException("insuffisant permissions");
}
consumer.set(ApiConsumer.Fields.Space, consumerSpaceNs);
consumer.set(ApiConsumer.Fields.SecretKey, skp.secretKey());
consumer.set(ApiConsumer.Fields.ExpiryDate, skp.expiryDate());
Iterator<String> props = skp.properties();
if (props != null) {
while (props.hasNext()) {
String p = props.next();
consumer.set(p, skp.property(p));
}
}
}
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class FileSystemStorageObject method toJson.
public JsonObject toJson(Folder.Filter filter, boolean fetchChildren) {
JsonObject data = (JsonObject) new JsonObject().set(StorageObject.Fields.Name, name()).set(StorageObject.Fields.Timestamp, Lang.toUTC(timestamp()));
try {
if (isFolder()) {
long count = new FileSystemFolder(source, false).count();
data.set(ApiOutput.Defaults.Count, count);
data.set(StorageObject.Fields.Length, length());
// get files
if (count > 0 && fetchChildren) {
final JsonArray items = new JsonArray();
data.set(ApiOutput.Defaults.Items, items);
Folder folder = toFolder();
folder.list(new Folder.Visitor() {
@Override
public void visit(StorageObject o) {
items.add(((FileSystemStorageObject) o).toJson(null, false));
}
}, filter);
}
} else {
data.set(StorageObject.Fields.Length, length()).set(StorageObject.Fields.ContentType, contentType());
}
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
return data;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class CookieConsumerResolver method resolve.
@Override
public ApiConsumer resolve(Api api, ApiService service, ApiRequest request) throws ApiAuthenticationException {
JsonObject oResolver = Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), MethodName);
String cookie = (String) request.get(ApiHeaders.Cookie, Scope.Header);
if (Lang.isNullOrEmpty(cookie)) {
return null;
}
JsonArray cookiesNames = Json.getArray(oResolver, Spec.Names);
if (cookiesNames == null) {
cookiesNames = Defaults.Cookies;
}
if (cookiesNames.isEmpty()) {
return null;
}
String token = null;
for (int i = 0; i < cookiesNames.count(); i++) {
String cookieName = String.valueOf(cookiesNames.get(i));
String[] cookieEntries = cookie.split(Lang.SEMICOLON);
for (String cookieEntry : cookieEntries) {
cookieEntry = cookieEntry.trim();
if (cookieEntry.startsWith(cookieName + Lang.EQUALS)) {
token = cookieEntry.substring((cookieName + Lang.EQUALS).length());
}
}
if (!Lang.isNullOrEmpty(token)) {
break;
}
}
if (Lang.isNullOrEmpty(token)) {
return null;
}
ApiConsumer consumer = new DefaultApiConsumer(ApiConsumer.Type.Cookie);
consumer.set(ApiConsumer.Fields.Token, token);
return consumer;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class DatabaseObjectImpl method toJson.
@SuppressWarnings({ "unchecked", "rawtypes" })
private JsonObject toJson(DatabaseObjectImpl dbo, DatabaseObjectSerializer serializer, int level, boolean refresh) {
if (serializer == null) {
serializer = DatabaseObjectSerializer.Default;
}
String entity = dbo.entity();
Fields fields = serializer.fields(level);
if (fields == null || Fields.None.equals(fields)) {
return null;
}
Set<String> keys = dbo.document.keySet();
if (keys == null || keys.isEmpty()) {
return null;
}
String[] fieldNames = null;
if (Fields.All.equals(fields)) {
fieldNames = keys.toArray(new String[keys.size()]);
} else {
fieldNames = MinimalFields;
}
// If object is partial and more than minimal fields are requested
if (dbo.partial && Fields.All.equals(fields) && refresh) {
dbo.refresh();
keys = dbo.document.keySet();
fieldNames = keys.toArray(new String[keys.size()]);
}
JsonObject json = serializer.create(entity, level);
if (json == null) {
return null;
}
for (String f : fieldNames) {
Object v = dbo.get(f);
if (v == null) {
continue;
}
if (ObjectIdKey.equals(f)) {
f = Database.Fields.Id;
}
if (v instanceof Date) {
v = Lang.toUTC((Date) v);
} else if (v instanceof Map) {
v = new JsonObject((Map<String, Object>) v, true);
} else if (v instanceof DatabaseObjectImpl) {
v = toJson(((DatabaseObjectImpl) v), serializer, level + 1, true);
} else if (v instanceof DatabaseObjectList) {
List<Object> list = (List<Object>) v;
if (list.isEmpty()) {
continue;
}
JsonArray arr = new JsonArray();
for (Object o : list) {
if (o == null) {
continue;
}
if (o instanceof DatabaseObjectImpl) {
arr.add(toJson(((DatabaseObjectImpl) o), serializer, level + 1, true));
} else {
if (o instanceof Date) {
arr.add(Lang.toUTC((Date) o));
} else if (Map.class.isAssignableFrom(o.getClass())) {
arr.add(new JsonObject((Map) o, true));
} else if (List.class.isAssignableFrom(o.getClass())) {
arr.add(new JsonArray((List<Object>) o));
} else {
arr.add(o);
}
}
}
v = arr;
}
serializer.set(entity, json, f, v);
}
return json;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class MongoDatabaseImpl method describe.
@Override
public JsonObject describe() {
JsonObject describe = new JsonObject();
describe.set(Describe.Size, 0);
MongoIterable<String> collections = db.listCollectionNames();
if (collections == null) {
return describe;
}
long size = 0;
JsonArray aEntities = new JsonArray();
describe.set(Describe.Entities, aEntities);
for (String collection : collections) {
JsonObject oEntity = new JsonObject();
oEntity.set(Describe.Name, collection);
oEntity.putAll(db.runCommand(new Document(SpiDescribe.CollStats, collection)));
aEntities.add(oEntity);
size += Json.getLong(oEntity, SpiDescribe.CollStats, 0);
}
describe.set(Describe.Size, size);
return describe;
}
Aggregations