Example usage for io.vertx.core.json JsonObject getString

List of usage examples for io.vertx.core.json JsonObject getString

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getString.

Prototype

public String getString(String key) 

Source Link

Document

Get the string value with the specified key, special cases are addressed for extended JSON types Instant , byte[] and Enum which can be converted to String.

Usage

From source file:io.engagingspaces.graphql.marshaller.schema.Unmarshaller.java

License:Open Source License

/**
 * Used internally for un-marshaling of schema objects.
 *
 * @param json    the json object to un-marshall
 * @param context the schema context/*  www  .  j a v a2  s.c om*/
 * @param parent  the parent schema decorator object (can be null)
 * @param <T>     type parameter indicating the schema object type to return
 * @param <U>     type parameter indicating the type of the parent schema object
 * @return the un-marshaled schema decorator object
 */
@SuppressWarnings("unchecked")
static <T, U> T unmarshall(JsonObject json, SchemaContext context, U parent) {
    String original = json.getString(PropNames.MARSHALED_TYPE);
    if (StaticDataFetcher.class.getName().equals(original)) {
        original = DataFetcher.class.getName();
    } else if (json.containsKey(PropNames.SCHEMAS)) {
        original = GraphQLSchema.class.getName();
    } else if (original == null) {
        throw new IllegalStateException("Failed to unmarshall, incorrect format or missing marshaling data");
    }

    String marshallToType = DECORATOR_PACKAGE + original.substring(original.lastIndexOf(DOT))
            + DECORATOR_POSTFIX;
    try {
        Class<?> clazz = Class.forName(marshallToType);
        Constructor<?> jsonConstructor;
        if (parent == null) {
            jsonConstructor = clazz.getConstructor(JsonObject.class, SchemaContext.class);
            return (T) jsonConstructor.newInstance(json, context);
        }
        Class<?> parentClass = parent.getClass();
        if (parent instanceof GraphQLObjectTypeDO || parent instanceof GraphQLInterfaceTypeDO) {
            parentClass = GraphQLType.class;
        }
        jsonConstructor = clazz.getConstructor(JsonObject.class, SchemaContext.class, parentClass);
        return (T) jsonConstructor.newInstance(json, context, parent);
    } catch (Exception ex) {
        throw new IllegalStateException("Failed to marshal '" + original + "' to: " + marshallToType, ex);
    }
}

From source file:io.engagingspaces.graphql.schema.SchemaDefinitionOptions.java

License:Open Source License

/**
 * Creates a {@link SchemaDefinitionOptions} from its json representation.
 *
 * @param json the serialized options instance
 *///from  ww w. ja  v  a2 s. c  o m
public SchemaDefinitionOptions(JsonObject json) {
    Objects.requireNonNull(json, "Schema definition options json cannot be null");
    this.schemaName = json.getString("schemaName");
    this.deliveryOptions = new DeliveryOptions(json.getJsonObject("deliveryOptions"));
    this.proxyType = Enum.valueOf(SchemaProxyType.class, json.getString("proxyType"));
    this.isInternal = json.getBoolean("isInternal");
}

From source file:io.engagingspaces.graphql.schema.SchemaMetadata.java

License:Open Source License

/**
 * Creates a new instance from its json representation.
 *
 * @param json the serialized json object
 *///from  w w  w  .j  av a  2s  . c o m
public SchemaMetadata(JsonObject json) {
    Objects.requireNonNull(json, "Schema metadata json cannot be null");
    this.proxyJson = json.getJsonObject("proxyJson", new JsonObject());
    this.metadata = json.getJsonObject("metadata");
    this.options = new SchemaDefinitionOptions(json.getJsonObject("schemaDefinitionOptions"));
    this.serviceAddress = json.getString("serviceAddress");
}

From source file:io.flowly.auth.AuthServer.java

License:Open Source License

private void initAuthProvider(JsonObject config) {
    JsonObject authConfig = new JsonObject().put(ObjectKeys.KEY_STORE,
            new JsonObject().put("path", config.getString(ObjectKeys.VAULT_PATH))
                    .put("type", Vault.JCEKS_KEY_STORE)
                    .put("password", config.getString(ObjectKeys.VAULT_KEY)));

    authProvider = new ExtJwtAuthProvider(vertx, authConfig);
}

From source file:io.flowly.auth.ExtJwtAuthProvider.java

License:Open Source License

@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
    try {//from   w  w w. j  a  va  2  s.  com
        final JsonObject payload = jwt.decode(authInfo.getString("jwt"));

        final JsonObject options = authInfo.getJsonObject("options", EMPTY_OBJECT);

        // All dates in JWT are of type NumericDate
        // a NumericDate is: numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until
        // the specified UTC date/time, ignoring leap seconds
        final long now = System.currentTimeMillis() / 1000;

        if (payload.containsKey("exp") && !options.getBoolean("ignoreExpiration", false)) {
            if (now >= payload.getLong("exp")) {
                resultHandler.handle(Future.failedFuture("Expired JWT token: exp <= now"));
                return;
            }
        }

        if (payload.containsKey("iat")) {
            Long iat = payload.getLong("iat");
            // issue at must be in the past
            if (iat > now) {
                resultHandler.handle(Future.failedFuture("Invalid JWT token: iat > now"));
                return;
            }
        }

        if (payload.containsKey("nbf")) {
            Long nbf = payload.getLong("nbf");
            // not before must be after now
            if (nbf > now) {
                resultHandler.handle(Future.failedFuture("Invalid JWT token: nbf > now"));
                return;
            }
        }

        if (options.containsKey("audience")) {
            JsonArray audiences = options.getJsonArray("audience", EMPTY_ARRAY);
            JsonArray target = payload.getJsonArray("aud", EMPTY_ARRAY);

            if (Collections.disjoint(audiences.getList(), target.getList())) {
                resultHandler
                        .handle(Future.failedFuture("Invalid JWT audience. expected: " + audiences.encode()));
                return;
            }
        }

        if (options.containsKey("issuer")) {
            if (!options.getString("issuer").equals(payload.getString("iss"))) {
                resultHandler.handle(Future.failedFuture("Invalid JWT issuer"));
                return;
            }
        }

        resultHandler.handle(Future.succeededFuture(new ExtJwtUser(payload, permissionsClaimKey)));

    } catch (RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

From source file:io.flowly.auth.graph.AuthGraph.java

License:Open Source License

private void prepareVault(JsonObject config, FileSystem fileSystem) throws Exception {
    String path = Objects.requireNonNull(config.getString(ObjectKeys.VAULT_PATH),
            "Vault path must be specified.");
    String key = Objects.requireNonNull(config.getString(ObjectKeys.VAULT_KEY),
            "Vault key has to be specified");
    vault = new Vault(key.toCharArray(), path, fileSystem);
}

From source file:io.flowly.auth.graph.AuthGraph.java

License:Open Source License

private void prepareGraph(JsonObject config) throws Exception {
    TitanFactory.Builder titanConfig = TitanFactory.build();

    // TODO: Use Cassandra.
    titanConfig.set(ObjectKeys.DB_STORAGE_BACKEND, "berkeleyje");
    titanConfig.set(ObjectKeys.DB_STORAGE_DIRECTORY, config.getString(ObjectKeys.DB_STORAGE_DIRECTORY));

    graph = titanConfig.open();/*from   ww  w .ja v a2 s .com*/
    configureSchema(graph);
}

From source file:io.flowly.auth.manager.BaseManager.java

License:Open Source License

/**
 * Retrieves direct and indirect memberships that a user or group holds.
 *
 * @param vertex vertex in the auth graph representing a user or group.
 * @param jsonObject JSON object representing the user or group to which retrieved memberships are added.
 * @param includeEffectiveMemberships indicates if all the user or group memberships are to be retrieved.
 * @param includeDirectMemberships indicates if the user's direct memberships are to be retrieved.
 * @param includePermissions indicates if the permissions granted to each group are to be retrieved.
 *//*from w w  w  . j a  v  a 2s . c  o m*/
protected void getMemberships(Vertex vertex, JsonObject jsonObject, boolean includeEffectiveMemberships,
        boolean includeDirectMemberships, boolean includePermissions) {
    boolean isUserVertex = jsonObject.containsKey(User.USER_ID);
    String uniqueId = isUserVertex ? jsonObject.getString(User.USER_ID) : jsonObject.getString(Group.GROUP_ID);

    if (includeEffectiveMemberships || includePermissions) {
        JsonArray effectiveMemberships = new JsonArray();
        jsonObject.put(User.EFFECTIVE_MEMBERSHIPS, effectiveMemberships);

        List<Vertex> groupVertices = graph.traversal().V(vertex).repeat(__.outE(Schema.E_MEMBER_OF).inV())
                .emit().toList();
        getDistinctMemberships(groupVertices, effectiveMemberships, uniqueId, isUserVertex, includePermissions);
    }

    if (includeDirectMemberships) {
        JsonArray directMemberships = new JsonArray();
        jsonObject.put(User.DIRECT_MEMBERSHIPS, directMemberships);

        getDistinctMemberships(graph.traversal().V(vertex).outE(Schema.E_MEMBER_OF).inV().toList(),
                directMemberships, null, false, false);
    }
}

From source file:io.flowly.auth.manager.GroupManager.java

License:Open Source License

@Override
public Handler<Message<JsonObject>> getHandler() {
    return message -> {
        JsonObject args = message.body();
        message.reply(get(args.getString(Group.GROUP_ID), args.getBoolean("includeDirectMemberships"),
                args.getBoolean("includeEffectiveMemberships"), args.getBoolean("includeDirectMembers"),
                args.getBoolean("includeEffectiveUsers"), args.getBoolean("includeDirectPermissions")));
    };/*from   ww w. java 2s. c  o m*/
}

From source file:io.flowly.auth.manager.ResourceManager.java

License:Open Source License

@Override
public Handler<Message<JsonObject>> getHandler() {
    return message -> {
        JsonObject args = message.body();
        message.reply(get(args.getString(Resource.RESOURCE_ID)));
    };//from  ww  w .j  a v a2  s  . c o m
}