Search in sources :

Example 26 with JsonArray

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));
            }
        }
    }
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) KeyPair(com.bluenimble.platform.security.KeyPair) ApiSpace(com.bluenimble.platform.api.ApiSpace) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException)

Example 27 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) StorageObject(com.bluenimble.platform.storage.StorageObject) JsonObject(com.bluenimble.platform.json.JsonObject) Folder(com.bluenimble.platform.storage.Folder) StorageException(com.bluenimble.platform.storage.StorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 28 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) DefaultApiConsumer(com.bluenimble.platform.server.security.impls.DefaultApiConsumer) ApiConsumer(com.bluenimble.platform.api.security.ApiConsumer) JsonObject(com.bluenimble.platform.json.JsonObject) DefaultApiConsumer(com.bluenimble.platform.server.security.impls.DefaultApiConsumer)

Example 29 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Fields(com.bluenimble.platform.db.DatabaseObjectSerializer.Fields) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Date(java.util.Date)

Example 30 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonObject(com.bluenimble.platform.json.JsonObject) Document(org.bson.Document)

Aggregations

JsonArray (com.bluenimble.platform.json.JsonArray)42 JsonObject (com.bluenimble.platform.json.JsonObject)37 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 Map (java.util.Map)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)5 Database (com.bluenimble.platform.db.Database)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)4 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)4 ApiService (com.bluenimble.platform.api.ApiService)3 DescribeOption (com.bluenimble.platform.api.DescribeOption)3 DatabaseException (com.bluenimble.platform.db.DatabaseException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 ArrayList (java.util.ArrayList)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2