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: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 {/*  w ww.  jav a 2s .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 {// w w w . ja v  a  2  s .com
        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:microservicerx.rx.DistributedObservableCodec.java

@Override
public DistributedObservable decodeFromWire(int position, Buffer buffer) {
    // My custom message starting from this *position* of buffer
    int _pos = position;

    // Length of JSON
    int length = buffer.getInt(_pos);

    // Get JSON string by it`s length
    // Jump 4 because getInt() == 4 bytes
    String jsonStr = buffer.getString(_pos += 4, _pos += length);
    JsonObject contentJson = new JsonObject(jsonStr);

    // Get fields
    String address = contentJson.getString("address");

    // We can finally create custom message object
    return new DistributedObservable(address);
}

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 w  w .  j  av a2  s. c om*/

    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.impl.ConfigPortTypeResolver.java

License:Apache License

@Override
public Class<?> resolve(String type) {
    JsonObject config = Configs.load();//from w  w  w. ja v a 2 s  .  c om
    JsonObject ports = Args.checkNotNull(config.getJsonObject("port"));
    JsonObject types = Args.checkNotNull(ports.getJsonObject("type"));
    String typeClass = types.getString(type);
    if (typeClass == null) {
        throw new VertigoException(String.format("Invalid port type %s", type));
    }
    try {
        return Class.forName(typeClass);
    } catch (ClassNotFoundException e) {
        throw new VertigoException(e);
    }
}

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);
    }//from   w w  w .j a  va2  s. co m
    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.EndpointConfigImpl.java

License:Apache License

@Override
public void update(JsonObject endpoint) {
    if (endpoint.containsKey(ENDPOINT_COMPONENT)) {
        this.component = endpoint.getString(ENDPOINT_COMPONENT);
    }//w ww  .j  a  va2  s  . c o m
    if (endpoint.containsKey(ENDPOINT_PORT)) {
        this.port = endpoint.getString(ENDPOINT_PORT);
    }
    if (endpoint.containsKey(ENDPOINT_IS_NETWORK)) {
        this.isNetwork = endpoint.getBoolean(ENDPOINT_IS_NETWORK);
    }
}

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

License:Apache License

@Override
public void update(JsonObject network) {
    this.name = network.getString(NETWORK_NAME);
    if (this.name == null) {
        throw new NetworkFormatException("Network name is mandatory.");
    }/* www .j  a  va  2s .  c om*/
    JsonObject components = network.getJsonObject(NETWORK_COMPONENTS);
    if (components != null) {
        for (String name : components.fieldNames()) {
            this.components.add(
                    NetworkConfig.component(components.getJsonObject(name)).setName(name).setNetwork(this));
        }
    }
    JsonArray connections = network.getJsonArray(NETWORK_CONNECTIONS);
    if (connections != null) {
        for (Object connection : connections) {
            ConnectionConfig connectionConfig = NetworkConfig.connection((JsonObject) connection);
            SourceConfig source = connectionConfig.getSource();
            if (!source.getIsNetwork()) {
                String componentName = connectionConfig.getSource().getComponent();
                if (componentName == null) {
                    throw new NetworkFormatException("A connection source does not specify a component.");
                }
                ComponentConfig component = this.getComponent(componentName);
                if (component == null) {
                    throw new NetworkFormatException("No component with name " + componentName
                            + " was found while trying to create source vertigo connection.");
                }
                OutputConfig output = component.getOutput();
                if (output.getPort(source.getPort()) == null) {
                    output.addPort(source.getPort());
                }
            }
            TargetConfig target = connectionConfig.getTarget();
            if (!target.getIsNetwork()) {
                String componentName = connectionConfig.getTarget().getComponent();
                if (componentName == null) {
                    throw new NetworkFormatException("A connection target does not specify a component.");
                }
                ComponentConfig component = this.getComponent(componentName);
                if (component == null) {
                    throw new NetworkFormatException("No target component with name " + componentName
                            + " was found while trying to create target vertigo connection.");
                }
                InputConfig input = component.getInput();
                if (input.getPort(target.getPort()) == null) {
                    input.addPort(target.getPort());
                }
            }
            this.connections.add(connectionConfig);
        }
    }
}

From source file:net.sf.sgsimulator.sgsrest.SGSVertxServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    String configFile = null;//ww w  . jav  a2 s.  c o  m
    if (args.length == 0) {
        configFile = "config.json";
    } else {
        configFile = args[0];
    }

    Path configPath = Paths.get(configFile);
    JsonObject config = new JsonObject(new String(Files.readAllBytes(configPath)));

    Random r = new Random(new Date().getTime());
    char c1 = (char) (r.nextInt(26) + 'a');
    char c2 = (char) (r.nextInt(26) + 'a');
    char c3 = (char) (r.nextInt(26) + 'a');
    String admincode = "" + c1 + c2 + c3;
    System.getProperties().put("sgsimulator.admincode", admincode);

    Vertx vertx = Vertx.vertx();
    System.out.println("****************************");
    System.out.println("Starting...");
    System.out.println("To load pages:");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/panel");
    System.out.println("Admin interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println("http://" + config.getString("host") + ":" + config.getInteger("port")
            + "/sg/pages/admin/" + admincode);
    System.out.println("Projector screen interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/screen");

    System.out.println("****************************");

    VertxNubes nubes = new VertxNubes(vertx, config);

    // System.err.println("IP:"+ip.substring(1));

    // config.put("host", ip.substring(1));
    System.getProperties().put("sgsimulator.scenario", config.getJsonObject("gridlab").getString("scenario"));
    System.getProperties().put("sgsimulator.adminip", config.getString("adminip"));
    nubes.bootstrap(res -> {
        if (res.succeeded()) {
            final Router router = res.result();
            final HttpServer server = vertx.createHttpServer(new HttpServerOptions(config));
            server.requestHandler(router::accept);
            server.listen();
            System.out.println("****************************");
            System.out.println("Started...");
            System.out.println("****************************");
        } else {
            res.cause().printStackTrace();
        }
    });
    // nubes.stop((_void) -> {
    // vertx.close();
    // });
}