Example usage for javax.json JsonObject getJsonArray

List of usage examples for javax.json JsonObject getJsonArray

Introduction

In this page you can find the example usage for javax.json JsonObject getJsonArray.

Prototype

JsonArray getJsonArray(String name);

Source Link

Document

Returns the array value to which the specified name is mapped.

Usage

From source file:org.jboss.set.aphrodite.JsonStreamService.java

private void parseJson(JsonObject jsonObject) throws NotFoundException {
    JsonArray jsonArray = jsonObject.getJsonArray("streams");
    Objects.requireNonNull(jsonArray, "streams array must be specified in json file");

    for (JsonValue value : jsonArray) {
        JsonObject json = (JsonObject) value;

        String upstreamName = json.getString("upstream", null);
        Stream upstream = streamMap.get(upstreamName);

        JsonArray codebases = json.getJsonArray("codebases");
        Map<String, StreamComponent> codebaseMap = parseStreamCodebases(codebases);

        Stream currentStream = new Stream(json.getString("name"), upstream, codebaseMap);
        streamMap.put(currentStream.getName(), currentStream);
    }/*from   www . ja v  a  2s  .c  o  m*/
}

From source file:joachimeichborn.geotag.geocode.GoogleGeocoder.java

private Geocoding extractLocationInformation(final JsonObject aJsonRepresentation) {
    final JsonArray results = aJsonRepresentation.getJsonArray("results");
    logger.fine("Found " + String.valueOf(results.size()) + " entries");

    return createTextualRepresentation(results);
}

From source file:uk.ac.ebi.ep.adapter.bioportal.BioPortalService.java

@Override
public Disease getDisease(String term) throws BioportalAdapterException {
    String definition = "";

    // Reader reader = new StringReader(get(REST_URL + "/search?q=" + term+"&ontology={EFO,MeSH,OMIM}&exact_match=true"));
    Reader reader = new StringReader(get(REST_URL + "/search?q=" + term));

    JsonReader jsonReader = Json.createReader(reader);

    JsonObject jo = jsonReader.readObject();

    JsonArray jsonArray = jo.getJsonArray("collection");
    Disease disease = null;//from   w  ww  . j a  va  2 s .com
    for (JsonObject obj : jsonArray.getValuesAs(JsonObject.class)) {
        disease = new Disease();
        disease.setId(term);
        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {

            if (entry.getKey().equalsIgnoreCase(LABEL)) {
                String name = entry.getValue().toString();
                disease.setName(name);

            }
            if (entry.getKey().equalsIgnoreCase(DEFINITION)) {

                definition = entry.getValue().toString().replace("\"", "").replaceAll("\\[", "")
                        .replaceAll("\\]", "");
                disease.setDescription(definition);
            }

        }
    }

    return disease;
}

From source file:uk.ac.ebi.ep.adapter.bioportal.BioPortalService.java

@Override
public Disease getDiseaseByName(String term, String diseaseId) throws BioportalAdapterException {
    String definition = "";

    // Reader reader = new StringReader(get(REST_URL + "/search?q=" + term+"&ontology={EFO,MeSH,OMIM}&exact_match=true"));
    Reader reader = new StringReader(get(REST_URL + "/search?q=" + term));

    JsonReader jsonReader = Json.createReader(reader);

    JsonObject jo = jsonReader.readObject();

    JsonArray jsonArray = jo.getJsonArray("collection");
    Disease disease = null;/*from   www. j  ava  2  s. c o  m*/
    for (JsonObject obj : jsonArray.getValuesAs(JsonObject.class)) {
        disease = new Disease();
        disease.setId(diseaseId);
        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {

            switch (entry.getKey()) {
            case LABEL:
                if (entry.getKey().equalsIgnoreCase(LABEL)) {
                    String name = entry.getValue().toString();
                    name = name.replaceAll("^\"|\"$", "");
                    if (name.equalsIgnoreCase(term)) {
                        disease.setName(name);

                    }

                }
                break;
            case DEFINITION:
                if (entry.getKey().equalsIgnoreCase(DEFINITION)) {
                    definition = entry.getValue().toString().replace("\"", "").replaceAll("\\[", "")
                            .replaceAll("\\]", "");

                    disease.setDescription(definition);
                    return disease;
                }
                break;

            }

        }
    }

    return disease;
}

From source file:fr.ortolang.diffusion.client.cmd.ReindexCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;//from  w w w .j  a  v a 2s  . c o m
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }

        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        String type = cmd.getOptionValue("t");
        String service = cmd.getOptionValue("s");
        String status = cmd.getOptionValue("a", "PUBLISHED");
        boolean fakeMode = cmd.hasOption("F");

        if (service != null) {

            OrtolangClient client = OrtolangClient.getInstance();
            if (username.length() > 0) {
                client.getAccountManager().setCredentials(username, password);
                client.login(username);
            }
            System.out.println("Connected as user: " + client.connectedProfile());
            System.out.println("Retrieving for published objects from service " + service + ", with type "
                    + type + " and with status " + status + " ...");

            List<String> objectKeys = new ArrayList<>();

            int offset = 0;
            int limit = 100;
            JsonObject listOfObjects = client.listObjects(service, type, status, offset, limit);
            JsonArray keys = listOfObjects.getJsonArray("entries");

            while (!keys.isEmpty()) {
                objectKeys.addAll(keys.getValuesAs(JsonString.class).stream().map(JsonString::getString)
                        .collect(Collectors.toList()));
                offset += limit;
                listOfObjects = client.listObjects(service, type, status, offset, limit);
                keys = listOfObjects.getJsonArray("entries");
            }
            System.out.println("Reindex keys (" + objectKeys.size() + ") : " + objectKeys);
            if (!fakeMode) {
                for (String key : objectKeys) {
                    client.reindex(key);
                }
                System.out.println("All keys reindexed.");
            }
            client.logout();
            client.close();
        } else {
            help();
        }
    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:ro.cs.products.landsat.LandsatSearch.java

public List<ProductDescriptor> execute() throws IOException {
    List<ProductDescriptor> results = new ArrayList<>();
    String queryUrl = getQuery();
    Logger.getRootLogger().info(queryUrl);
    try (CloseableHttpResponse response = NetUtils.openConnection(queryUrl, credentials)) {
        switch (response.getStatusLine().getStatusCode()) {
        case 200:
            String body = EntityUtils.toString(response.getEntity());
            final JsonReader jsonReader = Json.createReader(new StringReader(body));
            Logger.getRootLogger().debug("Parsing json response");
            JsonObject responseObj = jsonReader.readObject();
            JsonArray jsonArray = responseObj.getJsonArray("results");
            for (int i = 0; i < jsonArray.size(); i++) {
                LandsatProductDescriptor currentProduct = new LandsatProductDescriptor();
                JsonObject result = jsonArray.getJsonObject(i);
                currentProduct.setName(result.getString("scene_id"));
                currentProduct.setId(result.getString("sceneID"));
                currentProduct.setPath(String.valueOf(result.getInt("path")));
                currentProduct.setRow(String.valueOf(result.getInt("row")));
                currentProduct.setSensingDate(result.getString("acquisitionDate"));
                results.add(currentProduct);
            }/*from w  ww.  j  a  v a 2 s .co  m*/

            break;
        case 401:
            Logger.getRootLogger().info("The supplied credentials are invalid!");
            break;
        default:
            Logger.getRootLogger().info("The request was not successful. Reason: %s",
                    response.getStatusLine().getReasonPhrase());
            break;
        }
    }
    Logger.getRootLogger().info("Query returned %s products", results.size());
    return results;
}

From source file:fr.ortolang.diffusion.client.cmd.ReindexAllRootCollectionCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;/*from ww w  .  j  av  a  2s .  c o m*/
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }

        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        boolean fakeMode = cmd.hasOption("F");

        OrtolangClient client = OrtolangClient.getInstance();
        if (username.length() > 0) {
            client.getAccountManager().setCredentials(username, password);
            client.login(username);
        }
        System.out.println("Connected as user: " + client.connectedProfile());
        System.out.println("Looking for root collection ...");

        // Looking for root collection
        List<String> rootCollectionKeys = new ArrayList<>();

        int offset = 0;
        int limit = 100;
        JsonObject listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
        JsonArray keys = listOfObjects.getJsonArray("entries");

        while (!keys.isEmpty()) {
            for (JsonString objectKey : keys.getValuesAs(JsonString.class)) {
                JsonObject objectRepresentation = client.getObject(objectKey.getString());
                JsonObject objectProperty = objectRepresentation.getJsonObject("object");
                boolean isRoot = objectProperty.getBoolean("root");
                if (isRoot) {
                    rootCollectionKeys.add(objectKey.getString());
                }
            }
            offset += limit;
            listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
            keys = listOfObjects.getJsonArray("entries");
        }

        System.out.println("Reindex keys : " + rootCollectionKeys);
        if (!fakeMode) {
            for (String key : rootCollectionKeys) {
                client.reindex(key);
            }
        }

        client.logout();
        client.close();

    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:joachimeichborn.geotag.geocode.GoogleGeocoder.java

private String getMatchingContent(final String aAttribute, final JsonArray aResults,
        final LocationType... aLocationTypes) {
    for (final LocationType locationType : aLocationTypes) {
        for (final JsonObject component : aResults.getValuesAs(JsonObject.class)) {
            final JsonArray types = component.getJsonArray(TYPES_KEY);
            if (types.containsAll(locationType.getIdentifiers()) && component.containsKey(aAttribute)) {
                return component.getString(aAttribute);
            }//from  w w w .j a  v a 2 s. c  om
        }
    }

    return null;
}

From source file:joachimeichborn.geotag.geocode.GoogleGeocoder.java

private String getMatchingContentFromAddressComponent(final String aAttribute, final JsonArray aResults,
        final LocationType... aLocationTypes) {
    for (final LocationType locationType : aLocationTypes) {
        for (final JsonObject result : aResults.getValuesAs(JsonObject.class)) {
            final JsonArray addressComponents = result.getJsonArray(ADDRESS_COMPONENTS_KEY);
            final String content = getMatchingContent(aAttribute, addressComponents, locationType);
            if (!StringUtils.isEmpty(content)) {
                return content;
            }/*  w  ww  . j a  v  a 2  s. co m*/
        }
    }

    return null;
}

From source file:org.opendaylight.eman.impl.EmanSNMPBinding.java

public String getEoAttrSNMP(String deviceIP, String attribute) {
    LOG.info("EmanSNMPBinding.getEoAttrSNMP: ");

    /* To do: generalize targetURL
        research using Java binding to make reference to 'local' ODL API
     *//*from   w w w .j  a v a2 s.  c om*/
    String targetUrl = "http://localhost:8181/restconf/operations/snmp:snmp-get";
    String bodyString = buildSNMPGetBody(deviceIP, attribute);
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String result = null;

    try {
        /* invoke ODL SNMP API */
        HttpPost httpPost = new HttpPost(targetUrl);
        httpPost.addHeader("Authorization", "Basic " + encodedAuthStr);

        StringEntity inputBody = new StringEntity(bodyString);
        inputBody.setContentType(CONTENTTYPE_JSON);
        httpPost.setEntity(inputBody);

        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = httpResponse.getEntity();
        LOG.info("EmanSNMPBinding.getEoAttrSNMP: Response Status: " + httpResponse.getStatusLine().toString());
        InputStream in = responseEntity.getContent();

        /* Parse response from ODL SNMP API */
        JsonReader rdr = Json.createReader(in);
        JsonObject obj = rdr.readObject();
        JsonObject output = obj.getJsonObject("output");
        JsonArray results = output.getJsonArray("results");
        JsonObject pwr = results.getJsonObject(0);
        String oid = pwr.getString("oid");
        result = pwr.getString("value");
        rdr.close();

        LOG.info("EmanSNMPBinding.getEoAttrSNMP: oid: " + oid + " value " + result);
    } catch (Exception ex) {
        LOG.info("Error: " + ex.getMessage(), ex);
    } finally {
        try {
            httpClient.close();
        } catch (IOException ex) {
            LOG.info("Error: " + ex.getMessage(), ex);
        }
    }
    return (result);
}