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

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

Introduction

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

Prototype

public boolean containsKey(String key) 

Source Link

Document

Does the JSON object contain the specified key?

Usage

From source file:io.openshift.booster.service.impl.JdbcProductStore.java

License:Apache License

@Override
public Single<JsonObject> create(JsonObject item) {
    if (item == null) {
        return Single.error(new IllegalArgumentException("The item must not be null"));
    }//from  www .  j a va 2s .co  m
    if (item.getString("name") == null || item.getString("name").isEmpty()) {
        return Single.error(new IllegalArgumentException("The name must not be null or empty"));
    }
    if (item.getInteger("stock", 0) < 0) {
        return Single.error(new IllegalArgumentException("The stock must greater or equal to 0"));
    }
    if (item.containsKey("id")) {
        return Single.error(new IllegalArgumentException("The created item already contains an 'id'"));
    }

    return db.rxGetConnection().flatMap(conn -> {
        JsonArray params = new JsonArray().add(item.getValue("name")).add(item.getValue("stock", 0));
        return conn.rxUpdateWithParams(INSERT, params).map(ur -> item.put("id", ur.getKeys().getLong(0)))
                .doAfterTerminate(conn::close);
    });
}

From source file:io.openshift.booster.service.impl.JdbcProductStore.java

License:Apache License

@Override
public Completable update(long id, JsonObject item) {
    if (item == null) {
        return Completable.error(new IllegalArgumentException("The item must not be null"));
    }//from  w  w w  .jav  a 2 s.  c om
    if (item.getString("name") == null || item.getString("name").isEmpty()) {
        return Completable.error(new IllegalArgumentException("The name must not be null or empty"));
    }
    if (item.getInteger("stock", 0) < 0) {
        return Completable.error(new IllegalArgumentException("The stock must greater or equal to 0"));
    }
    if (item.containsKey("id") && id != item.getInteger("id")) {
        return Completable.error(new IllegalArgumentException("The 'id' cannot be changed"));
    }

    return db.rxGetConnection().flatMapCompletable(conn -> {
        JsonArray params = new JsonArray().add(item.getValue("name")).add(item.getValue("stock", 0)).add(id);
        return conn.rxUpdateWithParams(UPDATE, params).flatMapCompletable(up -> {
            if (up.getUpdated() == 0) {
                return Completable.error(new NoSuchElementException("Unknown item '" + id + "'"));
            }
            return Completable.complete();
        }).doAfterTerminate(conn::close);
    });
}

From source file:io.techcode.logbulk.pipeline.output.SyslogOutput.java

License:Open Source License

/**
 * Populate header based on data./*from  www  . jav  a  2s  .c o  m*/
 *
 * @param buf    buffer to write in.
 * @param header header to populate.
 * @param data   data involved.
 */
private void populate(Buffer buf, SyslogHeader header, JsonObject data) {
    String key = mapping.get(header);
    if (mapping.containsKey(header) && data.containsKey(key)) {
        buf.appendString(String.valueOf(data.getValue(key)));
        buf.appendString(" ");
    } else {
        buf.appendString("- ");
    }
}

From source file:microservicerx.example.impl.MicroServiceRxImpl.java

@Override
public void cold(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) {
    System.out.println("Processing...");
    Observable<JsonObject> observable;

    JsonObject result = document.copy();
    if (!document.containsKey("name")) {
        observable = Observable.error(new ServiceException(NO_NAME_ERROR, "No name in the document"));
    } else if (document.getString("name").isEmpty() || document.getString("name").equalsIgnoreCase("bad")) {
        observable = Observable.error(new ServiceException(BAD_NAME_ERROR, "Bad name in the document"));
    } else {/*from  ww  w. j a  va  2 s .c o m*/
        result.put("approved", true);
        observable = Observable.just(result.copy().put("id", 0), result.copy().put("id", 1));
    }
    DistributedObservable dist = DistributedObservable.toDistributable(observable.map(j -> (Object) j), vertx);
    resultHandler.handle(Future.succeededFuture(dist.toJsonObject()));
}

From source file:microservicerx.example.impl.MicroServiceRxImpl.java

@Override
public void hot(JsonObject document, Handler<AsyncResult<JsonObject>> resultHandler) {
    System.out.println("Processing...");
    BehaviorSubject<Object> subject = BehaviorSubject.create();

    JsonObject result = document.copy();
    if (!document.containsKey("name")) {
        subject.onError(new ServiceException(NO_NAME_ERROR, "No name in the document"));
    } else if (document.getString("name").isEmpty() || document.getString("name").equalsIgnoreCase("bad")) {
        subject.onError(new ServiceException(BAD_NAME_ERROR, "Bad name in the document"));
    } else {//from   www. j  a  v  a 2  s  . c  o  m
        Long timerId = vertx.setPeriodic(1000, l -> {
            JsonObject event = result.copy().put("approved", true).put("now", System.currentTimeMillis());
            subject.onNext(event);
        });

        vertx.setTimer(3 * 1000, l -> {
            vertx.cancelTimer(timerId);
            subject.onCompleted();
        });
    }
    DistributedObservable dist = DistributedObservable.toDistributable(subject, vertx);
    resultHandler.handle(Future.succeededFuture(dist.toJsonObject()));
}

From source file:net.kuujo.vertigo.cluster.ClusterOptions.java

License:Apache License

@SuppressWarnings("unchecked")
public ClusterOptions(JsonObject options) {
    this.clustered = options.containsKey("clustered") ? options.getBoolean("clustered")
            : options.getString("cluster") != null;
    this.clusterAddress = options.getString("cluster", clusterAddress);
    this.nodeAddress = options.getString("node", nodeAddress);
    this.nodes = new HashSet(options.getJsonArray("nodes", new JsonArray()).getList());
}

From source file:net.kuujo.vertigo.deployment.ComponentFactory.java

License:Apache License

@Override
public void resolve(String identifier, DeploymentOptions options, ClassLoader classLoader,
        Future<String> resolution) {

    identifier = VerticleFactory.removePrefix(identifier);
    JsonObject config = Configs.load(identifier);
    String main = config.getString("identifier");
    if (main == null) {
        throw new VertxException(identifier + " does not contain a identifier field");
    }//from   w  ww  .  j  a  v a  2s  . co  m

    JsonObject deployment = config.getJsonObject("deployment");
    if (deployment != null) {
        if (deployment.containsKey("worker")) {
            options.setWorker(deployment.getBoolean("worker"));
        }
        if (deployment.containsKey("multi-threaded")) {
            options.setMultiThreaded(deployment.getBoolean("multi-threaded"));
        }
    }

    resolution.complete(main);
}

From source file:net.kuujo.vertigo.network.impl.BasePortConfigImpl.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void update(JsonObject port) {
    if (this.name == null) {
        this.name = port.getString(PORT_NAME);
    }/*www .  j a  va2 s.  c om*/
    String type = port.getString(PORT_TYPE);
    if (type != null) {
        this.type = resolver.resolve(type);
    } else {
        this.type = Object.class;
    }
    String codec = port.getString(PORT_CODEC);
    if (codec != null) {
        try {
            this.codec = (Class<? extends MessageCodec>) Class.forName(codec);
        } catch (ClassNotFoundException e) {
            throw new VertigoException(e);
        }
    }
    if (port.containsKey(PORT_PERSISTENT)) {
        this.persistent = port.getBoolean(PORT_PERSISTENT, false);
    }
}

From source file:net.kuujo.vertigo.network.impl.ComponentConfigImpl.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void update(JsonObject component) {
    if (this.name == null) {
        this.name = component.getString(COMPONENT_NAME, UUID.randomUUID().toString());
    }/* www. jav  a2  s  . c om*/
    if (this.identifier == null) {
        this.identifier = component.getString(COMPONENT_IDENTIFIER);
        if (this.identifier == null) {
            throw new NetworkFormatException("Component " + this.name + " did not specify an identifier.");
        }
    }
    if (component.containsKey(COMPONENT_CONFIG)) {
        this.config = component.getJsonObject(COMPONENT_CONFIG);
    }
    if (component.containsKey(COMPONENT_WORKER)) {
        this.worker = component.getBoolean(COMPONENT_WORKER);
    }
    if (component.containsKey(COMPONENT_MULTI_THREADED)) {
        this.multiThreaded = component.getBoolean(COMPONENT_MULTI_THREADED);
    }
    if (component.containsKey(COMPONENT_STATEFUL)) {
        this.stateful = component.getBoolean(COMPONENT_STATEFUL);
    }
    if (component.containsKey(COMPONENT_REPLICAS)) {
        this.replicas = component.getInteger(COMPONENT_REPLICAS, 1);
    }
    if (component.containsKey(COMPONENT_RESOURCES)) {
        this.resources.addAll(component.getJsonArray(COMPONENT_RESOURCES, new JsonArray()).getList());
    }
    JsonObject inputs = component.getJsonObject(COMPONENT_INPUT);
    if (inputs == null) {
        inputs = new JsonObject();
    }
    if (this.input == null) {
        this.input = new InputConfigImpl(inputs).setComponent(this);
    } else {
        this.input.update(inputs);
    }
    JsonObject outputs = component.getJsonObject(COMPONENT_OUTPUT);
    if (outputs == null) {
        outputs = new JsonObject();
    }
    if (this.output == null) {
        this.output = new OutputConfigImpl(outputs).setComponent(this);
    } else {
        this.output.update(outputs);
    }
}

From source file:net.kuujo.vertigo.network.impl.ConnectionConfigImpl.java

License:Apache License

@Override
public void update(JsonObject connection) {
    if (connection.containsKey(CONNECTION_SOURCE)) {
        if (this.source == null) {
            this.source = new SourceConfigImpl(connection.getJsonObject(CONNECTION_SOURCE));
        } else {/*  w  ww . j a va 2  s . c om*/
            this.source.update(connection.getJsonObject(CONNECTION_SOURCE));
        }
    }
    if (connection.containsKey(CONNECTION_TARGET)) {
        if (this.target == null) {
            this.target = new TargetConfigImpl(connection.getJsonObject(CONNECTION_TARGET));
        } else {
            this.target.update(connection.getJsonObject(CONNECTION_TARGET));
        }
    }
    if (connection.containsKey(CONNECTION_ORDERED)) {
        this.ordered = connection.getBoolean(CONNECTION_ORDERED);
    }
    if (connection.containsKey(CONNECTION_AT_LEAST_ONCE)) {
        this.atLeastOnce = connection.getBoolean(CONNECTION_AT_LEAST_ONCE);
    }
    if (connection.containsKey(CONNECTION_SEND_TIMEOUT)) {
        this.sendTimeout = connection.getLong(CONNECTION_SEND_TIMEOUT);
    }
}