Example usage for io.vertx.core.json JsonArray size

List of usage examples for io.vertx.core.json JsonArray size

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray size.

Prototype

public int size() 

Source Link

Document

Get the number of values in this JSON array

Usage

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

public Future<JsonArray> getPermissionsForUser(String user, String tenant, Boolean expand) {
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<JsonArray> future = Future.future();
    mongoClient.find("users", query, (AsyncResult<List<JsonObject>> res) -> {
        if (res.result().size() < 1) {
            future.fail("No such user");
        } else {/*from   www.  j a v a  2 s . co m*/
            JsonObject userObject = res.result().get(0);
            logger.debug("Permissions> Permissions for user " + user + ": " + userObject.encode());
            JsonArray permissions = userObject.getJsonArray("permissions");
            if (expand) {
                ArrayList<Future> futureList = new ArrayList<>();
                for (Object o : permissions) {
                    String permissionName = (String) o;
                    Future<JsonArray> expandPermissionFuture = this.getExpandedPermissions(permissionName,
                            tenant);
                    futureList.add(expandPermissionFuture);
                }
                logger.debug("Permissions> Assembling CompositeFuture of " + futureList.size()
                        + " permissions to expand");
                CompositeFuture compositeFuture = CompositeFuture.all(futureList);
                compositeFuture.setHandler(res2 -> {
                    if (res2.failed()) {
                        future.fail(res2.cause());
                    } else {
                        JsonArray allPermissions = new JsonArray();
                        for (Future f : futureList) {
                            JsonArray arr = (JsonArray) f.result();
                            for (Object o : arr) {
                                String perm = (String) o;
                                if (!allPermissions.contains(perm)) {
                                    allPermissions.add(perm);
                                }
                            }
                        }
                        logger.debug(
                                "Permissions> Returning list of " + allPermissions.size() + " permissions");
                        future.complete(allPermissions);
                    }
                });
            } else {
                future.complete(permissions);
            }
        }
    });
    return future;
}

From source file:org.mustertech.webapp.vertxutils.VerticleDeployer.java

License:Open Source License

public static void deploy(final Vertx vertx, final String cfgJson, Handler<AsyncResult<JsonObject>> handler) {
    if (null == vertx) {
        NullPointerException e = new NullPointerException("NULL vertxutils instance is passed!");
        handler.handle(makeAsyncResult(e, null));

        return;//  w  ww. jav a 2  s  .co  m
    }

    // Store the vertx instance
    VerticleDeployer.vertx = vertx;

    try {
        InputStream schStream = VerticleDeployer.class.getResourceAsStream("/schema/deploy-schema.json");
        StringWriter strWriter = new StringWriter();

        IOUtils.copy(schStream, strWriter, "UTF-8");
        JsonValidator.validateJson(strWriter.toString(), cfgJson);
        IOUtils.closeQuietly(schStream);

        JsonObject cfgObj = new JsonObject(cfgJson);
        JsonObject globalCfg = null;
        JsonObject optObj = null, optCfg = null;
        JsonArray deployOpts = cfgObj.getJsonArray("deployOpts");

        int optsLen = deployOpts.size();
        ArrayList<Promise<JsonObject, Exception, Double>> promises = new ArrayList<Promise<JsonObject, Exception, Double>>();

        if (cfgObj.containsKey("globalConf")) {
            globalCfg = cfgObj.getJsonObject("globalConf");
        }

        for (int idx = 0; idx < optsLen; idx++) {
            optObj = (JsonObject) deployOpts.getJsonObject(idx);

            if (cfgObj.containsKey("appendGlobal") && cfgObj.getBoolean("appendGlobal")) {
                if (optObj.containsKey("config")) {
                    optCfg = optObj.getJsonObject("config");

                    if (!optCfg.containsKey("global")) {
                        optCfg.put("global", globalCfg);
                    }
                } else {
                    optCfg = new JsonObject();

                    optCfg.put("global", globalCfg);
                    optObj.put("config", optCfg);
                }
            }

            promises.add(deployWithOpts(optObj));
        }

        DeferredManager defMgr = new DefaultDeferredManager();

        defMgr.when(promises.toArray(new Promise[] {})).done(new DoneCallback<MultipleResults>() {
            public void onDone(MultipleResults results) {
                JsonArray resArr = new JsonArray();
                Iterator<OneResult> oneResIter = results.iterator();

                while (oneResIter.hasNext()) {
                    resArr.add(oneResIter.next().getResult());
                }

                JsonObject resObj = new JsonObject();
                resObj.put("deployInfo", resArr);

                handler.handle(makeAsyncResult(null, resObj));
            }
        }).fail(new FailCallback<OneReject>() {
            public void onFail(OneReject err) {
                handler.handle(makeAsyncResult((Throwable) err.getReject(), null));
            }
        });
    } catch (Exception e) {
        handler.handle(makeAsyncResult(e, null));
    }
}

From source file:org.pac4j.vertx.core.DefaultJsonConverter.java

License:Apache License

@Override
public Object decodeObject(Object value) {
    if (value == null) {
        return null;
    } else if (isPrimitiveType(value)) {
        return value;
    } else if (value instanceof JsonArray) {
        JsonArray src = (JsonArray) value;
        List<Object> list = new ArrayList<>(src.size());
        for (Object object : src) {
            list.add(decodeObject(object));
        }/* w  w  w . j av  a  2 s.  c o m*/
        return list.toArray();
    } else if (value instanceof JsonObject) {
        JsonObject src = (JsonObject) value;
        try {
            return decode(src.getJsonObject("value").encode(), Class.forName(src.getString("class")));
        } catch (Exception e) {
            throw new TechnicalException("Error while decoding object", e);
        }
    }
    return null;
}

From source file:org.pac4j.vertx.VertxWebContext.java

License:Apache License

public VertxWebContext(final RoutingContext routingContext) {
    final HttpServerRequest request = routingContext.request();
    this.routingContext = routingContext;
    this.method = request.method().toString();

    this.fullUrl = request.absoluteURI();
    URI uri;/* w  w w .j  ava2s  .  c  o  m*/
    try {
        uri = new URI(fullUrl);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new InvalidParameterException(
                "Request to invalid URL " + fullUrl + " while constructing VertxWebContext");
    }
    this.scheme = uri.getScheme();
    this.serverName = uri.getHost();
    this.serverPort = (uri.getPort() != -1) ? uri.getPort() : scheme.equals("http") ? 80 : 443;
    this.remoteAddress = request.remoteAddress().toString();

    headers = new JsonObject();
    for (String name : request.headers().names()) {
        headers.put(name, request.headers().get(name));
    }

    parameters = new JsonObject();
    for (String name : request.params().names()) {
        parameters.put(name, new JsonArray(Arrays.asList(request.params().getAll(name).toArray())));
    }

    mapParameters = new HashMap<>();
    for (String name : parameters.fieldNames()) {
        JsonArray params = parameters.getJsonArray(name);
        String[] values = new String[params.size()];
        int i = 0;
        for (Object o : params) {
            values[i++] = (String) o;
        }
        mapParameters.put(name, values);
    }

}

From source file:org.pac4j.vertx.VertxWebContext.java

License:Apache License

@Override
public String getRequestParameter(String name) {
    JsonArray values = parameters.getJsonArray(name);
    if (values != null && values.size() > 0) {
        return values.getString(0);
    }/*from  ww  w  .  j a  v  a  2  s . c o m*/
    return null;
}

From source file:org.perfcake.reporting.destination.c3chart.C3ChartData.java

License:Apache License

/**
 * Checks whether all values in the field are null (except for the first index column).
 *
 * @param a//from  w  w w  .j a  v  a  2 s. c om
 *       The array to be checked.
 * @return True if and only if all values in the field are null.
 */
private static boolean isAllNull(final JsonArray a) {
    int i = 1;
    while (i < a.size()) {
        if (a.getValue(i) != null) {
            return false;
        }
        i++;
    }

    return true;
}

From source file:se.liquidbytes.jel.owfs.OwfsAdapter.java

License:Apache License

/**
 * Set value on device with specified hwId.
 *
 * @param hwId Id on device/*from w  ww .  j av  a  2  s  .  com*/
 * @param value value to set on device.
 * @throws DeviceMissingException throws exception if specified device does not exist.
 * @throws OwServerConnectionException throws exception if command fails for any reason.
 */
private void setDeviceValue(String hwId, String value)
        throws DeviceMissingException, OwServerConnectionException {

    if (hwId == null || !deviceLookup.containsKey(hwId)) {
        throw new DeviceMissingException("Trying to perform a action on a non existing device.", hwId);
    }

    if (hwId.contains(CHILDSEPARATOR)) {

        String parentId = hwId.split(CHILDSEPARATOR)[0];
        String childSuffix = hwId.split(CHILDSEPARATOR)[1];
        JsonObject parentDevice = deviceLookup.get(parentId);
        JsonObject typeInfo = parentDevice.getJsonObject("typeInfo");

        if (typeInfo == null) {
            throw new OwServerConnectionException(String.format("typeInfo missing for device with type '%s'",
                    parentDevice.getString("type")));
        }

        JsonArray childDevices = typeInfo.getJsonArray("childDevices");
        if (childDevices != null && childDevices.size() > 0) {
            String writePath = childDevices.stream().filter(t -> t instanceof JsonObject)
                    .map(t -> (JsonObject) t).filter((d) -> d.getString("idSuffix").equals(childSuffix))
                    .map((cd) -> cd.getString("valueWritePath")).findFirst().get();
            writePath = parentDevice.getString("path") + writePath;

            // Queue command. Record time so we could measure how long command has been queued before executed, if we want to.
            this.commandQueue.offer(new JsonObject().put("path", writePath).put("value", value).put("nanoTime",
                    System.nanoTime()));
        }

    } else {

        JsonObject device = deviceLookup.get(hwId);
        JsonObject typeInfo = device.getJsonObject("typeInfo");

        if (typeInfo == null) {
            throw new OwServerConnectionException(
                    String.format("typeInfo missing for device with type '%s'", device.getString("type")));
        }
        // Check if this type of device is writable.
        if (typeInfo.containsKey("valueWritePath")) {
            String writePath = device.getString("path") + typeInfo.getString("valueWritePath");

            // Queue command. Record time so we could measure how long command has been queued before executed, if we want to.
            this.commandQueue.offer(new JsonObject().put("path", writePath).put("value", value).put("nanoTime",
                    System.nanoTime()));
        }
    }
}