Example usage for javax.json JsonObject isEmpty

List of usage examples for javax.json JsonObject isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

private String revokeInternal(User revoker, String serial, String aki, String reason, boolean genCRL)
        throws RevocationException, InvalidArgumentException {

    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }/*from w w w  . ja  va 2 s.co m*/

    if (Utils.isNullOrEmpty(serial)) {
        throw new IllegalArgumentException("Serial number id required to revoke ceritificate");
    }
    if (Utils.isNullOrEmpty(aki)) {
        throw new IllegalArgumentException("AKI is required to revoke certificate");
    }
    if (revoker == null) {
        throw new InvalidArgumentException("revoker is not set");
    }

    logger.debug(format("revoke revoker: %s, reason: %s, url: %s", revoker.getName(), reason, url));

    try {
        setUpSSL();

        // build request body
        RevocationRequest req = new RevocationRequest(caName, null, serial, aki, reason, genCRL);
        String body = req.toJson();

        // send revoke request
        JsonObject resp = httpPost(url + HFCA_REVOKE, body, revoker);
        logger.debug("revoke done");

        if (genCRL) {
            if (resp.isEmpty()) {
                throw new RevocationException("Failed to return CRL, revoke response is empty");
            }
            if (resp.isNull("CRL")) {
                throw new RevocationException("Failed to return CRL");
            }
            return resp.getString("CRL");
        }
        return null;
    } catch (CertificateException e) {
        logger.error("Cannot validate certificate. Error is: " + e.getMessage());
        throw new RevocationException("Error while revoking cert. " + e.getMessage(), e);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RevocationException("Error while revoking the user. " + e.getMessage(), e);
    }
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

private String revokeInternal(User revoker, Enrollment enrollment, String reason, boolean genCRL)
        throws RevocationException, InvalidArgumentException {

    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }/*from   w  w  w  .  j a  v  a  2s  .c om*/

    if (enrollment == null) {
        throw new InvalidArgumentException("revokee enrollment is not set");
    }
    if (revoker == null) {
        throw new InvalidArgumentException("revoker is not set");
    }

    logger.debug(format("revoke revoker: %s, reason: %s, url: %s", revoker.getName(), reason, url));

    try {
        setUpSSL();

        // get cert from to-be-revoked enrollment
        BufferedInputStream pem = new BufferedInputStream(
                new ByteArrayInputStream(enrollment.getCert().getBytes()));
        CertificateFactory certFactory = CertificateFactory
                .getInstance(Config.getConfig().getCertificateFormat());
        X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(pem);

        // get its serial number
        String serial = DatatypeConverter.printHexBinary(certificate.getSerialNumber().toByteArray());

        // get its aki
        // 2.5.29.35 : AuthorityKeyIdentifier
        byte[] extensionValue = certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId());
        ASN1OctetString akiOc = ASN1OctetString.getInstance(extensionValue);
        String aki = DatatypeConverter
                .printHexBinary(AuthorityKeyIdentifier.getInstance(akiOc.getOctets()).getKeyIdentifier());

        // build request body
        RevocationRequest req = new RevocationRequest(caName, null, serial, aki, reason, genCRL);
        String body = req.toJson();

        // send revoke request
        JsonObject resp = httpPost(url + HFCA_REVOKE, body, revoker);
        logger.debug("revoke done");

        if (genCRL) {
            if (resp.isEmpty()) {
                throw new RevocationException("Failed to return CRL, revoke response is empty");
            }
            if (resp.isNull("CRL")) {
                throw new RevocationException("Failed to return CRL");
            }
            return resp.getString("CRL");
        }
        return null;
    } catch (CertificateException e) {
        logger.error("Cannot validate certificate. Error is: " + e.getMessage());
        throw new RevocationException("Error while revoking cert. " + e.getMessage(), e);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RevocationException("Error while revoking the user. " + e.getMessage(), e);

    }
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

private String revokeInternal(User revoker, String revokee, String reason, boolean genCRL)
        throws RevocationException, InvalidArgumentException {

    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }/*  w  ww . j a va2s  .  c o m*/

    logger.debug(format("revoke revoker: %s, revokee: %s, reason: %s", revoker, revokee, reason));

    if (Utils.isNullOrEmpty(revokee)) {
        throw new InvalidArgumentException("revokee user is not set");
    }
    if (revoker == null) {
        throw new InvalidArgumentException("revoker is not set");
    }

    try {
        setUpSSL();

        // build request body
        RevocationRequest req = new RevocationRequest(caName, revokee, null, null, reason, genCRL);
        String body = req.toJson();

        // send revoke request
        JsonObject resp = httpPost(url + HFCA_REVOKE, body, revoker);

        logger.debug(format("revoke revokee: %s done.", revokee));

        if (genCRL) {
            if (resp.isEmpty()) {
                throw new RevocationException("Failed to return CRL, revoke response is empty");
            }
            if (resp.isNull("CRL")) {
                throw new RevocationException("Failed to return CRL");
            }
            return resp.getString("CRL");
        }
        return null;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RevocationException("Error while revoking the user. " + e.getMessage(), e);
    }
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public void startParse(JsonValue node) throws SAXException {
    if (node instanceof JsonObject) {
        JsonObject root = (JsonObject) node;
        if (StringUtils.isEmpty(getRootElement())) {
            if (root.isEmpty()) {
                throw new SAXException("no names found");
            }/*from w  w w.j  ava 2  s.  c o  m*/
            if (root.size() > 1) {
                String namesList = null;
                int i = 0;
                for (String name : root.keySet()) {
                    if (namesList == null) {
                        namesList = name;
                    } else {
                        namesList += "," + name;
                    }
                    if (i++ > 5) {
                        namesList += ", ...";
                        break;
                    }
                }
                throw new SAXException("too many names [" + namesList + "]");
            }
            setRootElement((String) root.keySet().toArray()[0]);
        }
        // determine somewhat heuristically whether the json contains a 'root' node:
        // if the outermost JsonObject contains only one key, that has the name of the root element, 
        // then we'll assume that that is the root element...
        if (root.size() == 1 && getRootElement().equals(root.keySet().toArray()[0])) {
            node = root.get(getRootElement());
        }
    }
    if (node instanceof JsonArray && !insertElementContainerElements && strictSyntax) {
        throw new SAXException(
                MSG_EXPECTED_SINGLE_ELEMENT + " [" + getRootElement() + "] or array element container");
    }
    super.startParse(node);
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Map<String, String> getAttributes(XSElementDeclaration elementDeclaration, JsonValue node)
        throws SAXException {
    if (!readAttributes) {
        return null;
    }//from  w  ww.  j  av  a2  s.c o m
    if (!(node instanceof JsonObject)) {
        if (DEBUG)
            log.debug("getAttributes() parent node is not a JsonObject, but a [" + node.getClass().getName()
                    + "] isParentOfSingleMultipleOccurringChildElement ["
                    + isParentOfSingleMultipleOccurringChildElement() + "]  value [" + node
                    + "], returning null");
        return null;
    }
    JsonObject o = (JsonObject) node;
    if (o.isEmpty()) {
        if (DEBUG)
            log.debug("getAttributes() no children");
        return null;
    }
    try {
        Map<String, String> result = new HashMap<String, String>();
        for (String key : o.keySet()) {
            if (key.startsWith(attributePrefix)) {
                String attributeName = key.substring(attributePrefix.length());
                String value = getText(elementDeclaration, o.get(key));
                if (DEBUG)
                    log.debug("getAttributes() attribute [" + attributeName + "] = [" + value + "]");
                result.put(attributeName, value);
            }
        }
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Set<String> getAllNodeChildNames(XSElementDeclaration elementDeclaration, JsonValue node)
        throws SAXException {
    if (DEBUG)//from  w  ww  .j  a v a  2  s  .  c o m
        log.debug("getAllChildNames() node isParentOfSingleMultipleOccurringChildElement ["
                + isParentOfSingleMultipleOccurringChildElement() + "] [" + node.getClass().getName() + "]["
                + node + "]");
    try {
        if (isParentOfSingleMultipleOccurringChildElement()) {
            if ((insertElementContainerElements || !strictSyntax) && node instanceof JsonArray) {
                if (DEBUG)
                    log.debug(
                            "getAllChildNames() parentOfSingleMultipleOccurringChildElement,JsonArray,(insertElementContainerElements || !strictSyntax)");
                Set<String> result = new HashSet<String>();
                result.addAll(getMultipleOccurringChildElements());
                if (DEBUG)
                    log.debug("getAllChildNames() isParentOfSingleMultipleOccurringChildElement, result ["
                            + result + "]");
                return result;
            }
            if ((insertElementContainerElements && strictSyntax) && !(node instanceof JsonArray)) {
                throw new SAXException(MSG_FULL_INPUT_IN_STRICT_COMPACTING_MODE);
            }
        }
        if (!(node instanceof JsonObject)) {
            if (DEBUG)
                log.debug("getAllChildNames() parent node is not a JsonObject, but a ["
                        + node.getClass().getName() + "] isParentOfSingleMultipleOccurringChildElement ["
                        + isParentOfSingleMultipleOccurringChildElement() + "]  value [" + node
                        + "], returning null");
            return null;
        }
        JsonObject o = (JsonObject) node;
        if (o.isEmpty()) {
            if (DEBUG)
                log.debug("getAllChildNames() no children");
            return new HashSet<String>();
        }
        Set<String> result = new HashSet<String>();
        for (String key : o.keySet()) {
            if (!readAttributes || !key.startsWith(attributePrefix)) {
                result.add(key);
                if (DEBUG)
                    log.debug("getAllChildNames() key [" + key + "] added to set");
            }
        }
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:org.apache.johnzon.maven.plugin.ExampleToModelMojo.java

private void generateFieldsAndMethods(final Writer writer, final JsonObject object, final String prefix,
        final Collection<String> imports) throws IOException {
    final Map<String, JsonObject> nestedTypes = new TreeMap<String, JsonObject>();
    {//from   w w w  .  j  av a 2s.  c  o m
        final Iterator<Map.Entry<String, JsonValue>> iterator = object.entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<String, JsonValue> entry = iterator.next();
            final String key = entry.getKey();
            final String fieldName = toJavaFieldName(key);
            switch (entry.getValue().getValueType()) {
            case ARRAY:
                imports.add("java.util.List");
                handleArray(writer, prefix, nestedTypes, entry.getValue(), key, fieldName, 1, imports);
                break;
            case OBJECT:
                final String type = toJavaName(fieldName);
                nestedTypes.put(type, JsonObject.class.cast(entry.getValue()));
                fieldGetSetMethods(writer, key, fieldName, type, prefix, 0, imports);
                break;
            case TRUE:
            case FALSE:
                fieldGetSetMethods(writer, key, fieldName, "Boolean", prefix, 0, imports);
                break;
            case NUMBER:
                fieldGetSetMethods(writer, key, fieldName, "Double", prefix, 0, imports);
                break;
            case STRING:
                fieldGetSetMethods(writer, key, fieldName, "String", prefix, 0, imports);
                break;
            case NULL:
            default:
                throw new UnsupportedOperationException("Unsupported " + entry.getValue() + ".");
            }
            if (iterator.hasNext()) {
                writer.write("\n");
            }
        }
    }

    if (!object.isEmpty() && !nestedTypes.isEmpty()) {
        writer.write("\n");
    }

    final Iterator<Map.Entry<String, JsonObject>> entries = nestedTypes.entrySet().iterator();
    while (entries.hasNext()) {
        final Map.Entry<String, JsonObject> entry = entries.next();
        writer.write(prefix + "public static class " + entry.getKey() + " {\n");
        generateFieldsAndMethods(writer, entry.getValue(), "    " + prefix, imports);
        writer.write(prefix + "}\n");
        if (entries.hasNext()) {
            writer.write("\n");
        }
    }
}