Example usage for javax.json JsonArray getValuesAs

List of usage examples for javax.json JsonArray getValuesAs

Introduction

In this page you can find the example usage for javax.json JsonArray getValuesAs.

Prototype

default <T, K extends JsonValue> List<T> getValuesAs(Function<K, T> func) 

Source Link

Document

Returns a list view for the array.

Usage

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcScript jsonScript(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/*  ww w.j  a v a 2 s. com*/
    BtcScript script = new BtcScript();
    script.setAsm(object.getString(BTCOBJ_SCRIPT_ASM, ""));
    script.setPublicKey(object.getString(BTCOBJ_SCRIPT_PUBLIC_KEY, ""));
    script.setRequiredSignatures(jsonLong(object, BTCOBJ_SCRIPT_REQUIRED_SIGNATURES));
    script.setType(BtcScript.Type.getValue(object.getString(BTCOBJ_SCRIPT_TYPE, "")));
    List<String> addresses = new ArrayList<String>();
    JsonValue addrs = object.get(BTCOBJ_SCRIPT_ADDRESSES);
    if ((addrs) != null && (addrs.getValueType() == JsonValue.ValueType.ARRAY)
            && (addrs instanceof JsonArray)) {
        JsonArray addrsArray = (JsonArray) addrs;
        for (JsonValue addr : addrsArray.getValuesAs(JsonValue.class)) {
            addresses.add(jsonString(addr));
        }
    }
    script.setAddresses(addresses);
    return script;
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcAddedNode jsonAddedNode(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }//from w  w w  .ja  v a  2  s  .c  o  m
    BtcAddedNode addedNode = new BtcAddedNode();
    addedNode.setAddedNode(object.getString(BTCOBJ_NODE_ADDED_NODE, ""));
    addedNode.setConnected(object.getBoolean(BTCOBJ_NODE_CONNECTED, false));
    List<BtcNode> nodes = new ArrayList<BtcNode>();
    JsonValue addresses = object.get(BTCOBJ_NODE_ADDRESSES);
    if ((addresses != null) && (addresses.getValueType() == JsonValue.ValueType.ARRAY)
            && (addresses instanceof JsonArray)) {
        JsonArray addressesArray = (JsonArray) addresses;
        for (JsonValue address : addressesArray.getValuesAs(JsonValue.class)) {
            nodes.add(jsonNode(address));
        }
    }
    addedNode.setAddresses(nodes);
    return addedNode;
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcBlockTemplate jsonBlockTemplate(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }//from  w w w .  j  av a 2  s .co m
    BtcBlockTemplate block = new BtcBlockTemplate();
    block.setVersion(jsonLong(object, BTCOBJ_BLOCK_VERSION));
    block.setPreviousBlockHash(object.getString(BTCOBJ_BLOCK_PREVIOUS_BLOCK_HASH, ""));
    List<BtcTransactionTemplate> transactions = new ArrayList<BtcTransactionTemplate>();
    JsonValue txs = object.get(BTCOBJ_BLOCK_TEMPLATE_TRANSACTIONS);
    if ((txs != null) && (txs.getValueType() == JsonValue.ValueType.ARRAY) && (txs instanceof JsonArray)) {
        JsonArray txsArray = (JsonArray) txs;
        for (JsonValue tx : txsArray.getValuesAs(JsonValue.class)) {
            transactions.add(jsonTransactionTemplate(tx));
        }
    }
    block.setTransactions(transactions);
    block.setCoinbase(jsonCoinbase(value));
    block.setTarget(object.getString(BTCOBJ_BLOCK_TEMPLATE_TARGET, ""));
    block.setMinimumTime(jsonLong(object, BTCOBJ_BLOCK_TEMPLATE_MIN_TIME));
    List<String> mutable = new ArrayList<String>();
    JsonValue mutableIds = object.get(BTCOBJ_BLOCK_TEMPLATE_MUTABLE);
    if ((mutableIds != null) && (mutableIds.getValueType() == JsonValue.ValueType.ARRAY)
            && (mutableIds instanceof JsonArray)) {
        JsonArray mutableIdsArray = (JsonArray) mutableIds;
        for (JsonValue mutableId : mutableIdsArray.getValuesAs(JsonValue.class)) {
            mutable.add(jsonString(mutableId));
        }
    }
    block.setMutable(mutable);
    block.setNonceRange(object.getString(BTCOBJ_BLOCK_TEMPLATE_NONCE_RANGE, ""));
    block.setSignatureOperations(jsonLong(object, BTCOBJ_BLOCK_TEMPLATE_SIGNATURE_OPERATIONS));
    block.setSize(jsonLong(object, BTCOBJ_BLOCK_TEMPLATE_SIZE));
    block.setTime(jsonLong(object, BTCOBJ_BLOCK_TEMPLATE_TIME));
    block.setBits(object.getString(BTCOBJ_BLOCK_BITS, ""));
    block.setHeight(jsonLong(object, BTCOBJ_BLOCK_HEIGHT));
    return block;
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcRawTransaction jsonRawTransaction(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/*from   ww  w  . j a  v a  2s . com*/
    BtcRawTransaction transaction = new BtcRawTransaction();
    transaction.setHex(object.getString(BTCOBJ_TX_HEX, ""));
    transaction.setTransaction(object.getString(BTCOBJ_TX_TRANSACTION, ""));
    transaction.setVersion(jsonLong(object, BTCOBJ_TX_VERSION));
    transaction.setLockTime(jsonLong(object, BTCOBJ_TX_LOCK_TIME));
    List<BtcInput> inputTransactions = new ArrayList<BtcInput>();
    JsonValue inputs = object.get(BTCOBJ_TX_INPUTS);
    if ((inputs != null) && (inputs.getValueType() == JsonValue.ValueType.ARRAY)
            && (inputs instanceof JsonArray)) {
        JsonArray inputsArray = (JsonArray) inputs;
        for (JsonValue input : inputsArray.getValuesAs(JsonValue.class)) {
            inputTransactions.add(jsonInput(input));
        }
    }
    transaction.setInputs(inputTransactions);
    List<BtcOutput> outputTransactions = new ArrayList<BtcOutput>();
    JsonValue outputs = object.get(BTCOBJ_TX_OUTPUTS);
    if ((outputs != null) && (outputs.getValueType() == JsonValue.ValueType.ARRAY)
            && (outputs instanceof JsonArray)) {
        JsonArray outputsArray = (JsonArray) outputs;
        for (JsonValue output : outputsArray.getValuesAs(JsonValue.class)) {
            outputTransactions.add(jsonOutput(output));
        }
    }
    transaction.setOutputs(outputTransactions);
    transaction.setBlockHash(object.getString(BTCOBJ_TX_BLOCK_HASH, ""));
    transaction.setConfirmations(jsonLong(object, BTCOBJ_TX_CONFIRMATIONS));
    transaction.setTime(jsonLong(object, BTCOBJ_TX_TIME));
    transaction.setBlockTime(jsonLong(object, BTCOBJ_TX_BLOCK_TIME));
    transaction.setComplete(object.getBoolean(BTCOBJ_TX_COMPLETE, true));
    return transaction;
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcBlock jsonBlock(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }// w w  w .  j ava2s .com
    BtcBlock block = new BtcBlock();
    block.setHash(object.getString(BTCOBJ_BLOCK_HASH, ""));
    block.setConfirmations(jsonLong(object, BTCOBJ_BLOCK_CONFIRMATIONS));
    block.setSize(jsonLong(object, BTCOBJ_BLOCK_SIZE));
    block.setHeight(jsonLong(object, BTCOBJ_BLOCK_HEIGHT));
    block.setVersion(jsonLong(object, BTCOBJ_BLOCK_VERSION));
    block.setMerkleRoot(object.getString(BTCOBJ_BLOCK_MERKLE_ROOT, ""));
    List<BtcTransaction> transactions = new ArrayList<BtcTransaction>();
    JsonValue txIds = object.get(BTCOBJ_BLOCK_TRANSACTIONS);
    if ((txIds != null) && (txIds.getValueType() == JsonValue.ValueType.ARRAY)
            && (txIds instanceof JsonArray)) {
        JsonArray txIdsArray = (JsonArray) txIds;
        for (JsonValue transactionId : txIdsArray.getValuesAs(JsonValue.class)) {
            BtcTransaction transaction = new BtcTransaction();
            transaction.setTransaction(jsonString(transactionId));
            transactions.add(transaction);
        }
    }
    block.setTransactions(transactions);
    block.setTime(jsonLong(object, BTCOBJ_BLOCK_TIME));
    block.setNonce(jsonLong(object, BTCOBJ_BLOCK_NONCE));
    block.setBits(object.getString(BTCOBJ_BLOCK_BITS, ""));
    block.setDifficulty(jsonDouble(object, BTCOBJ_BLOCK_DIFFICULTY));
    block.setPreviousBlockHash(object.getString(BTCOBJ_BLOCK_PREVIOUS_BLOCK_HASH, ""));
    block.setNextBlockHash(object.getString(BTCOBJ_BLOCK_NEXT_BLOCK_HASH, ""));
    return block;
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public BtcTransaction jsonTransaction(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/* w  w  w  . j a  v a 2  s .  co  m*/
    BtcTransaction transaction = new BtcTransaction();
    transaction.setTransaction(object.getString(BTCOBJ_TX_TRANSACTION, ""));
    transaction.setAmount(jsonDouble(object, BTCOBJ_TX_AMOUNT));
    transaction.setFee(jsonDouble(object, BTCOBJ_TX_FEE));
    transaction.setConfirmations(jsonLong(object, BTCOBJ_TX_CONFIRMATIONS));
    transaction.setTime(jsonLong(object, BTCOBJ_TX_TIME));
    transaction.setTimeReceived(jsonLong(object, BTCOBJ_TX_TIME_RECEIVED));
    transaction.setBlockHash(object.getString(BTCOBJ_TX_BLOCK_HASH, ""));
    transaction.setBlockIndex(jsonLong(object, BTCOBJ_TX_BLOCK_INDEX));
    transaction.setBlockTime(jsonLong(object, BTCOBJ_TX_BLOCK_TIME));
    List<BtcTransactionDetail> details = new ArrayList<BtcTransactionDetail>();
    JsonValue txDetails = object.get(BTCOBJ_TX_DETAILS);
    if ((txDetails != null) && (txDetails.getValueType() == JsonValue.ValueType.ARRAY)
            && (txDetails instanceof JsonArray)) {
        JsonArray txDetailsArray = (JsonArray) txDetails;
        for (JsonValue txDetail : txDetailsArray.getValuesAs(JsonValue.class)) {
            details.add(jsonTransactionDetail(txDetail));
        }
    } else {
        details.add(jsonTransactionDetail(value));
    }
    transaction.setDetails(details);
    return transaction;
}

From source file:de.tu_dortmund.ub.data.dswarm.Task.java

@Override
public String call() {

    // init logger
    PropertyConfigurator.configure(config.getProperty("service.log4j-conf"));

    logger.info("[" + config.getProperty("service.name") + "] " + "Starting 'Task' ...");

    // init IDs of the prototype project
    String dataModelID = config.getProperty("prototype.dataModelID");
    String projectID = config.getProperty("prototype.projectID");
    String outputDataModelID = config.getProperty("prototype.outputDataModelID");

    // init process values
    String inputResourceID = null;
    String message = null;//from w w w .ja v a2  s .com

    try {

        // get the resource id of the current data model >> updateResourceID replaces resourceID
        String updateResourceID = null;
        try {
            updateResourceID = getProjectResourceID(dataModelID);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        logger.info("[" + config.getProperty("service.name") + "] updateResourceID = " + updateResourceID);

        // upload resource and update a InputDataModel
        String inputResourceJson = uploadFileAndUpdateResource(updateResourceID, resource,
                "resource for project '" + resource, config.getProperty("project.name") + "' - case " + cnt);
        JsonReader jsonReader = Json.createReader(IOUtils.toInputStream(inputResourceJson, "UTF-8"));
        inputResourceID = jsonReader.readObject().getString("uuid");
        logger.info("[" + config.getProperty("service.name") + "] inputResourceID = " + inputResourceID);

        if (updateResourceID != null) {

            // update the datamodel (will use it's (update) resource)
            updateDataModel(dataModelID);

            // configuration and processing of the task
            String jsonResponse = executeTask(dataModelID, projectID, outputDataModelID);

            if (jsonResponse != null) {

                if (Boolean.parseBoolean(config.getProperty("results.persistInFolder"))) {

                    if (Boolean.parseBoolean(config.getProperty("results.writeDMPJson"))) {
                        // save DMP results in files
                        FileUtils.writeStringToFile(new File(config.getProperty("results.folder")
                                + File.separatorChar + dataModelID + "." + cnt + ".json"), jsonResponse);
                    }

                    // build rdf graph
                    ValueFactory factory = ValueFactoryImpl.getInstance();

                    Graph graph = new LinkedHashModel();

                    URI graphUri = factory.createURI(config.getProperty("results.rdf.graph"));

                    URI subject = null;
                    URI predicate = null;
                    URI object = null;
                    Literal literal = null;
                    Statement statement = null;

                    JsonReader dmpJsonResult = Json.createReader(IOUtils.toInputStream(jsonResponse, "UTF-8"));
                    JsonArray records = dmpJsonResult.readArray();

                    for (JsonObject record : records.getValuesAs(JsonObject.class)) {

                        subject = factory
                                .createURI(record.getJsonString("__record_id").toString().replaceAll("\"", ""));

                        for (JsonObject triple : record.getJsonArray("__record_data")
                                .getValuesAs(JsonObject.class)) {

                            for (String key : triple.keySet()) {

                                if (key.endsWith("rdf-syntax-ns#type")) {
                                    predicate = RDF.TYPE;
                                    object = factory.createURI(
                                            triple.getJsonString(key).toString().replaceAll("\"", ""));
                                    statement = factory.createStatement(subject, predicate, object, graphUri);
                                    graph.add(statement);
                                } else {

                                    predicate = factory.createURI(key);

                                    switch (triple.get(key).getValueType().toString()) {

                                    case "STRING": {

                                        try {
                                            object = factory.createURI(
                                                    triple.getJsonString(key).toString().replaceAll("\"", ""));
                                            statement = factory.createStatement(subject, predicate, object,
                                                    graphUri);
                                            graph.add(statement);
                                        } catch (Exception e) {
                                            literal = factory.createLiteral(
                                                    triple.getJsonString(key).toString().replaceAll("\"", ""));
                                            statement = factory.createStatement(subject, predicate, literal,
                                                    graphUri);
                                            graph.add(statement);
                                        }
                                        break;
                                    }
                                    case "ARRAY": {

                                        for (JsonString value : triple.getJsonArray(key)
                                                .getValuesAs(JsonString.class)) {

                                            try {
                                                object = factory
                                                        .createURI(value.toString().replaceAll("\"", ""));
                                                statement = factory.createStatement(subject, predicate, object,
                                                        graphUri);
                                                graph.add(statement);
                                            } catch (Exception e) {
                                                literal = factory
                                                        .createLiteral(value.toString().replaceAll("\"", ""));
                                                statement = factory.createStatement(subject, predicate, literal,
                                                        graphUri);
                                                graph.add(statement);
                                            }
                                        }
                                        break;
                                    }
                                    default: {

                                        logger.info("Unhandled ValueType: " + triple.get(key).getValueType());
                                    }
                                    }
                                }
                            }
                        }
                    }

                    if (graph.size() > 0) {
                        // save rdf data as 'results.rdf.format' in 'results.folder'
                        RDFFormat format = null;
                        switch (config.getProperty("results.rdf.format")) {

                        case "xml": {

                            format = RDFFormat.RDFXML;
                            break;
                        }
                        case "nquads": {

                            format = RDFFormat.NQUADS;
                            break;
                        }
                        case "jsonld": {

                            format = RDFFormat.JSONLD;
                            break;
                        }
                        case "ttl": {

                            format = RDFFormat.TURTLE;
                            break;
                        }
                        default: {

                            format = RDFFormat.RDFXML;
                        }
                        }

                        try {
                            FileOutputStream out = new FileOutputStream(
                                    config.getProperty("results.folder") + File.separatorChar + dataModelID
                                            + "." + cnt + ".rdf." + config.getProperty("results.rdf.format"));
                            RDFWriter writer = Rio.createWriter(format, out);

                            writer.startRDF();
                            for (Statement st : graph) {
                                writer.handleStatement(st);
                            }
                            writer.endRDF();

                            out.close();

                        } catch (RDFHandlerException | IOException e) {
                            e.printStackTrace();
                        }

                        message = "'" + resource + "' transformed. results in '"
                                + config.getProperty("results.folder") + File.separatorChar + dataModelID + "."
                                + cnt + ".rdf." + config.getProperty("results.rdf.format") + "'";
                    } else {

                        message = "'" + resource + "' transformed but result is empty.";
                    }
                }
            } else {

                message = "'" + resource + "' not transformed: error in task execution.";
            }
        }
    } catch (Exception e) {

        logger.error("[" + config.getProperty("service.name") + "] Processing resource '" + resource
                + "' failed with a " + e.getClass().getSimpleName());
        e.printStackTrace();
    }

    return message;
}

From source file:de.tu_dortmund.ub.hb_ng.SolRDF.java

@Override
public String getAccessRights(String graph, String uri) throws LinkedDataStorageException {

    this.logger.info("getAccessRights: graph=" + graph);
    this.logger.info("getAccessRights: uri=" + uri);

    String accessRights = "";

    if (uri.endsWith("/about")) {

        accessRights = "public";
    } else {/* w  ww  .  ja v  a2  s  .  c o m*/

        // TODO config.properties
        String sparql = "SELECT ?o WHERE { GRAPH <http://data.ub.tu-dortmund.de/graph/"
                + this.config.getProperty("storage.graph.main") + "-public> { <" + uri
                + "/about> <http://purl.org/dc/terms#accessRights> ?o } }";

        try {

            JsonReader jsonReader = Json.createReader(
                    IOUtils.toInputStream(this.sparqlQuery(graph, URLEncoder.encode(sparql, "UTF-8"),
                            "application/sparql-results+json;charset=UTF-8"), "UTF-8"));

            JsonObject jsonObject = jsonReader.readObject();

            JsonArray bindings = jsonObject.getJsonObject("results").getJsonArray("bindings");

            if (bindings.size() == 0) {

                accessRights = "internal";
            } else {

                for (JsonObject binding : bindings.getValuesAs(JsonObject.class)) {

                    accessRights = binding.getJsonObject("o").getJsonString("value").getString();
                }
            }

            this.logger.info("accessRights: " + accessRights);
        } catch (IOException e) {

            this.logger.error("something went wrong", e);
            throw new LinkedDataStorageException(e.getMessage(), e.getCause());
        }
    }

    return accessRights;
}