Example usage for javax.json JsonObject get

List of usage examples for javax.json JsonObject get

Introduction

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

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

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   ww w.  jav  a2s. c o  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 BtcScript jsonScript(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }//from  w  ww .  j a  v a 2 s. c  o m
    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 BtcRawTransaction jsonRawTransaction(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/*from  w  w  w. j  av  a2s.c o  m*/
    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.hyperledger.fabric.sdk.NetworkConfig.java

private CAInfo createCA(String name, JsonObject jsonCA, OrgInfo org) throws NetworkConfigurationException {

    String url = getJsonValueAsString(jsonCA.get("url"));
    Properties httpOptions = extractProperties(jsonCA, "httpOptions");

    String enrollId = null;//from ww w. j a v a2s. com
    String enrollSecret = null;

    List<JsonObject> registrars = getJsonValueAsList(jsonCA.get("registrar"));
    List<UserInfo> regUsers = new LinkedList<>();
    if (registrars != null) {

        for (JsonObject reg : registrars) {
            enrollId = getJsonValueAsString(reg.get("enrollId"));
            enrollSecret = getJsonValueAsString(reg.get("enrollSecret"));
            try {
                regUsers.add(
                        new UserInfo(CryptoSuite.Factory.getCryptoSuite(), org.mspId, enrollId, enrollSecret));
            } catch (Exception e) {
                throw new NetworkConfigurationException(e.getMessage(), e);
            }
        }
    }

    CAInfo caInfo = new CAInfo(name, org.mspId, url, regUsers, httpOptions);

    String caName = getJsonValueAsString(jsonCA.get("caName"));
    if (caName != null) {
        caInfo.setCaName(caName);
    }

    Properties properties = new Properties();
    if (null != httpOptions && "false".equals(httpOptions.getProperty("verify"))) {
        properties.setProperty("allowAllHostNames", "true");
    }
    getTLSCerts(name, jsonCA, properties);
    caInfo.setProperties(properties);

    return caInfo;
}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private void getTLSCerts(String nodeName, JsonObject jsonOrderer, Properties props) {
    JsonObject jsonTlsCaCerts = getJsonObject(jsonOrderer, "tlsCACerts");
    if (jsonTlsCaCerts != null) {
        String pemFilename = getJsonValueAsString(jsonTlsCaCerts.get("path"));
        String pemBytes = getJsonValueAsString(jsonTlsCaCerts.get("pem"));

        if (pemFilename != null) {
            // let the sdk handle non existing errors could be they don't exist during parsing but are there later.
            props.put("pemFile", pemFilename);
        }//from  w  ww . ja v a  2 s . com

        if (pemBytes != null) {
            props.put("pemBytes", pemBytes.getBytes());
        }
    }
}

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

public BtcOutput jsonOutput(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/* w  ww .j  av a2 s. c  om*/
    BtcOutput output = new BtcOutput();
    output.setTransaction(object.getString(BTCOBJ_TX_OUTPUT_TRANSACTION, ""));
    output.setBestBlock(object.getString(BTCOBJ_TX_OUTPUT_BEST_BLOCK, ""));
    output.setConfirmations(jsonLong(object, BTCOBJ_TX_OUTPUT_CONFIRMATIONS));
    output.setValue(jsonDouble(object, BTCOBJ_TX_OUTPUT_VALUE));
    output.setIndex(jsonLong(object, BTCOBJ_TX_OUTPUT_INDEX));
    output.setOutput(jsonLong(object, BTCOBJ_TX_OUTPUT_OUTPUT));
    JsonValue scriptPubKey = object.get(BTCOBJ_TX_OUTPUT_SCRIPT_PUBLIC_KEY);
    if (scriptPubKey != null) {
        if ((scriptPubKey.getValueType() == JsonValue.ValueType.OBJECT)
                && (scriptPubKey instanceof JsonObject)) {
            output.setScript(jsonScript(scriptPubKey));
        }
        if ((scriptPubKey.getValueType() == JsonValue.ValueType.STRING)
                && (scriptPubKey instanceof JsonString)) {
            BtcScript script = new BtcScript();
            script.setPublicKey(jsonString(scriptPubKey));
            output.setScript(script);
        }
    }
    output.setVersion(jsonLong(object, BTCOBJ_TX_OUTPUT_VERSION));
    output.setCoinbase(object.getBoolean(BTCOBJ_TX_OUTPUT_COINBASE, false));
    output.setDetail(jsonTransactionDetail(object));
    return output;
}

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

public JsonValue invoke(String method, JsonArray parameters) throws BtcException {
    JsonObjectBuilder builder = Json.createObjectBuilder();
    builder.add(JSONRPC_REALM, JSONRPC_VERSION).add(JSONRPC_METHOD, method);
    if (parameters != null) {
        builder.add(JSONRPC_PARAMS, parameters);
    } else {/*  www . ja v a  2s .  c o  m*/
        builder.addNull(JSONRPC_PARAMS);
    }
    String guid = UUID.randomUUID().toString();
    builder.add(JSONRPC_ID, guid);
    JsonObject request = builder.build();
    JsonObject response = jsonObject(jsonValue(jsonInvoke(String.valueOf(request))));
    if (response == null) {
        LOG.severe(BTC4J_DAEMON_DATA_NULL_JSON);
        throw new BtcException(BtcException.BTC4J_ERROR_CODE,
                BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_NULL_JSON);
    }
    if (!(guid.equals(jsonId(response)))) {
        LOG.severe(BTC4J_DAEMON_DATA_INVALID_ID);
        throw new BtcException(BtcException.BTC4J_ERROR_CODE,
                BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_INVALID_ID);
    }
    JsonValue error = response.get(JSONRPC_ERROR);
    if ((error != null) && (error.getValueType().equals(ValueType.OBJECT))) {
        JsonObject errorObj = (JsonObject) error;
        int code = errorObj.getInt(JSONRPC_CODE);
        String message = errorObj.getString(JSONRPC_MESSAGE);
        JsonObject data = (JsonObject) errorObj.get(JSONRPC_DATA);
        String dataStr = (data == null) ? "" : (" " + String.valueOf(data));
        LOG.severe("error: " + code + " " + message + dataStr);
        throw new BtcException(code, message + dataStr);
    }
    return response.get(JSONRPC_RESULT);

}

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

public BtcBlock jsonBlock(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/*from w w w .  j ava  2s  .c o m*/
    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.hyperledger.fabric.sdk.NetworkConfig.java

private NetworkConfig(JsonObject jsonConfig) throws InvalidArgumentException, NetworkConfigurationException {

    this.jsonConfig = jsonConfig;

    // Extract the main details
    String configName = getJsonValueAsString(jsonConfig.get("name"));
    if (configName == null || configName.isEmpty()) {
        throw new InvalidArgumentException("Network config must have a name");
    }//ww  w.  ja  v  a  2 s .c o  m

    String configVersion = getJsonValueAsString(jsonConfig.get("version"));
    if (configVersion == null || configVersion.isEmpty()) {
        throw new InvalidArgumentException("Network config must have a version");
        // TODO: Validate the version
    }

    // Preload and create all peers, orderers, etc
    createAllPeers();
    createAllOrderers();

    Map<String, JsonObject> foundCertificateAuthorities = findCertificateAuthorities();
    //createAllCertificateAuthorities();
    createAllOrganizations(foundCertificateAuthorities);

    // Validate the organization for this client
    JsonObject jsonClient = getJsonObject(jsonConfig, "client");
    String orgName = jsonClient == null ? null : getJsonValueAsString(jsonClient.get("organization"));
    if (orgName == null || orgName.isEmpty()) {
        throw new InvalidArgumentException("A client organization must be specified");
    }

    clientOrganization = getOrganizationInfo(orgName);
    if (clientOrganization == null) {
        throw new InvalidArgumentException("Client organization " + orgName + " is not defined");
    }

}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private OrgInfo createOrg(String orgName, JsonObject jsonOrg,
        Map<String, JsonObject> foundCertificateAuthorities) throws NetworkConfigurationException {

    String msgPrefix = format("Organization %s", orgName);

    String mspId = getJsonValueAsString(jsonOrg.get("mspid"));

    OrgInfo org = new OrgInfo(orgName, mspId);

    // Peers/*from   w ww. ja va  2  s  .  c om*/
    JsonArray jsonPeers = getJsonValueAsArray(jsonOrg.get("peers"));
    if (jsonPeers != null) {
        for (JsonValue peer : jsonPeers) {
            String peerName = getJsonValueAsString(peer);
            if (peerName != null) {
                org.addPeerName(peerName);
            }
        }
    }

    // CAs
    JsonArray jsonCertificateAuthorities = getJsonValueAsArray(jsonOrg.get("certificateAuthorities"));
    if (jsonCertificateAuthorities != null) {
        for (JsonValue jsonCA : jsonCertificateAuthorities) {

            String caName = getJsonValueAsString(jsonCA);

            if (caName != null) {
                JsonObject jsonObject = foundCertificateAuthorities.get(caName);
                if (jsonObject != null) {
                    org.addCertificateAuthority(createCA(caName, jsonObject, org));
                } else {
                    throw new NetworkConfigurationException(
                            format("%s: Certificate Authority %s is not defined", msgPrefix, caName));
                }
            }
        }
    }

    String adminPrivateKeyString = extractPemString(jsonOrg, "adminPrivateKey", msgPrefix);
    String signedCert = extractPemString(jsonOrg, "signedCert", msgPrefix);

    if (!isNullOrEmpty(adminPrivateKeyString) && !isNullOrEmpty(signedCert)) {

        PrivateKey privateKey = null;

        try {
            privateKey = getPrivateKeyFromString(adminPrivateKeyString);
        } catch (IOException ioe) {
            throw new NetworkConfigurationException(format("%s: Invalid private key", msgPrefix), ioe);
        }

        final PrivateKey privateKeyFinal = privateKey;

        try {
            org.peerAdmin = new UserInfo(CryptoSuite.Factory.getCryptoSuite(), mspId,
                    "PeerAdmin_" + mspId + "_" + orgName, null);
        } catch (Exception e) {
            throw new NetworkConfigurationException(e.getMessage(), e);
        }
        org.peerAdmin.setEnrollment(new X509Enrollment(privateKeyFinal, signedCert));

    }

    return org;
}