Search in sources :

Example 1 with IdentityException

use of org.hyperledger.fabric_ca.sdk.exception.IdentityException in project fabric-sdk-java by hyperledger.

the class HFCAClient method getHFCAIdentities.

/**
 * gets all identities that the registrar is allowed to see
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return the identity that was requested
 * @throws IdentityException        if adding an identity fails.
 * @throws InvalidArgumentException Invalid (null) argument specified
 */
public Collection<HFCAIdentity> getHFCAIdentities(User registrar) throws IdentityException, InvalidArgumentException {
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    logger.debug(format("identity  url: %s, registrar: %s", url, registrar.getName()));
    try {
        JsonObject result = httpGet(HFCAIdentity.HFCA_IDENTITY, registrar);
        Collection<HFCAIdentity> allIdentities = new ArrayList<HFCAIdentity>();
        JsonArray identities = result.getJsonArray("identities");
        if (identities != null && !identities.isEmpty()) {
            for (int i = 0; i < identities.size(); i++) {
                JsonObject identity = identities.getJsonObject(i);
                HFCAIdentity idObj = new HFCAIdentity(identity);
                allIdentities.add(idObj);
            }
        }
        logger.debug(format("identity  url: %s, registrar: %s done.", url, registrar));
        return allIdentities;
    } catch (HTTPException e) {
        String msg = format("[HTTP Status Code: %d] - Error while getting all users from url '%s': %s", e.getStatusCode(), url, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    } catch (Exception e) {
        String msg = format("Error while getting all users from url '%s': %s", url, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    }
}
Also used : JsonArray(javax.json.JsonArray) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException)

Example 2 with IdentityException

use of org.hyperledger.fabric_ca.sdk.exception.IdentityException in project fabric-sdk-java by hyperledger.

the class HFCAIdentity method read.

/**
 * read retrieves a specific identity
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return statusCode The HTTP status code in the response
 * @throws IdentityException    if retrieving an identity fails.
 * @throws InvalidArgumentException Invalid (null) argument specified
 */
public int read(User registrar) throws IdentityException, InvalidArgumentException {
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String readIdURL = "";
    try {
        readIdURL = HFCA_IDENTITY + "/" + enrollmentID;
        logger.debug(format("identity  url: %s, registrar: %s", readIdURL, registrar.getName()));
        JsonObject result = client.httpGet(readIdURL, registrar);
        statusCode = result.getInt("statusCode");
        if (statusCode < 400) {
            type = result.getString("type");
            maxEnrollments = result.getInt("max_enrollments");
            affiliation = result.getString("affiliation");
            JsonArray attributes = result.getJsonArray("attrs");
            Collection<Attribute> attrs = new ArrayList<Attribute>();
            if (attributes != null && !attributes.isEmpty()) {
                for (int i = 0; i < attributes.size(); i++) {
                    JsonObject attribute = attributes.getJsonObject(i);
                    Attribute attr = new Attribute(attribute.getString("name"), attribute.getString("value"), attribute.getBoolean("ecert", false));
                    attrs.add(attr);
                }
            }
            this.attrs = attrs;
            logger.debug(format("identity  url: %s, registrar: %s done.", readIdURL, registrar));
        }
        this.deleted = false;
        return statusCode;
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while getting user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), readIdURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    } catch (Exception e) {
        String msg = format("Error while getting user '%s' from url '%s': %s", enrollmentID, readIdURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    }
}
Also used : JsonArray(javax.json.JsonArray) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 3 with IdentityException

use of org.hyperledger.fabric_ca.sdk.exception.IdentityException in project fabric-sdk-java by hyperledger.

the class HFCAIdentity method create.

/**
 * create an identity
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return statusCode The HTTP status code in the response
 * @throws IdentityException    if creating an identity fails.
 * @throws InvalidArgumentException Invalid (null) argument specified
 */
public int create(User registrar) throws IdentityException, InvalidArgumentException {
    if (this.deleted) {
        throw new IdentityException("Identity has been deleted");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String createURL = "";
    try {
        createURL = client.getURL(HFCA_IDENTITY);
        logger.debug(format("identity  url: %s, registrar: %s", createURL, registrar.getName()));
        String body = client.toJson(idToJsonObject());
        JsonObject result = client.httpPost(createURL, body, registrar);
        statusCode = result.getInt("statusCode");
        if (statusCode >= 400) {
            getHFCAIdentity(result);
            logger.debug(format("identity  url: %s, registrar: %s done.", createURL, registrar));
        }
        this.deleted = false;
        return statusCode;
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while creating user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), createURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    } catch (Exception e) {
        String msg = format("Error while creating user '%s' from url '%s':  %s", getEnrollmentId(), createURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 4 with IdentityException

use of org.hyperledger.fabric_ca.sdk.exception.IdentityException in project fabric-sdk-java by hyperledger.

the class HFCAIdentity method delete.

/**
 * delete an identity
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return statusCode The HTTP status code in the response
 * @throws IdentityException    if adding an identity fails.
 * @throws InvalidArgumentException Invalid (null) argument specified
 */
public int delete(User registrar) throws IdentityException, InvalidArgumentException {
    if (this.deleted) {
        throw new IdentityException("Identity has been deleted");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String deleteURL = "";
    try {
        deleteURL = client.getURL(HFCA_IDENTITY + "/" + getEnrollmentId());
        logger.debug(format("identity  url: %s, registrar: %s", deleteURL, registrar.getName()));
        JsonObject result = client.httpDelete(deleteURL, registrar);
        statusCode = result.getInt("statusCode");
        if (statusCode < 400) {
            getHFCAIdentity(result);
            logger.debug(format("identity  url: %s, registrar: %s done.", deleteURL, registrar));
        }
        this.deleted = true;
        return statusCode;
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while deleting user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), deleteURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    } catch (Exception e) {
        String msg = format("Error while deleting user '%s' from url '%s':  %s", getEnrollmentId(), deleteURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 5 with IdentityException

use of org.hyperledger.fabric_ca.sdk.exception.IdentityException in project fabric-sdk-java by hyperledger.

the class HFCAIdentity method update.

/**
 * update an identity
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return statusCode The HTTP status code in the response
 * @throws IdentityException    if adding an identity fails.
 * @throws InvalidArgumentException Invalid (null) argument specified
 */
public int update(User registrar) throws IdentityException, InvalidArgumentException {
    if (this.deleted) {
        throw new IdentityException("Identity has been deleted");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String updateURL = "";
    try {
        updateURL = client.getURL(HFCA_IDENTITY + "/" + getEnrollmentId());
        logger.debug(format("identity  url: %s, registrar: %s", updateURL, registrar.getName()));
        String body = client.toJson(idToJsonObject());
        JsonObject result = client.httpPut(updateURL, body, registrar);
        statusCode = result.getInt("statusCode");
        if (statusCode < 400) {
            getHFCAIdentity(result);
            logger.debug(format("identity  url: %s, registrar: %s done.", updateURL, registrar));
        }
        return statusCode;
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while updating user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), updateURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    } catch (Exception e) {
        String msg = format("Error while updating user '%s' from url '%s':  %s", getEnrollmentId(), updateURL, e.getMessage());
        IdentityException identityException = new IdentityException(msg, e);
        logger.error(msg);
        throw identityException;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Aggregations

JsonObject (javax.json.JsonObject)5 AffiliationException (org.hyperledger.fabric_ca.sdk.exception.AffiliationException)5 HTTPException (org.hyperledger.fabric_ca.sdk.exception.HTTPException)5 IdentityException (org.hyperledger.fabric_ca.sdk.exception.IdentityException)5 InvalidArgumentException (org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException)5 ArrayList (java.util.ArrayList)2 JsonArray (javax.json.JsonArray)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 ParseException (org.apache.http.ParseException)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 EnrollmentException (org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)1 GenerateCRLException (org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException)1 InfoException (org.hyperledger.fabric_ca.sdk.exception.InfoException)1