Search in sources :

Example 16 with InvalidArgumentException

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

the class HFCAClient method register.

/**
 * Register a user.
 *
 * @param request   Registration request with the following fields: name, role.
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return the enrollment secret.
 * @throws RegistrationException    if registration fails.
 * @throws InvalidArgumentException
 */
public String register(RegistrationRequest request, User registrar) throws RegistrationException, InvalidArgumentException {
    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }
    if (Utils.isNullOrEmpty(request.getEnrollmentID())) {
        throw new InvalidArgumentException("EntrollmentID cannot be null or empty");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    logger.debug(format("register  url: %s, registrar: %s", url, registrar.getName()));
    setUpSSL();
    try {
        String body = request.toJson();
        JsonObject resp = httpPost(url + HFCA_REGISTER, body, registrar);
        String secret = resp.getString("secret");
        if (secret == null) {
            throw new Exception("secret was not found in response");
        }
        logger.debug(format("register  url: %s, registrar: %s done.", url, registrar));
        return secret;
    } catch (Exception e) {
        RegistrationException registrationException = new RegistrationException(format("Error while registering the user %s url: %s  %s ", registrar, url, e.getMessage()), e);
        logger.error(registrar);
        throw registrationException;
    }
}
Also used : RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) 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 17 with InvalidArgumentException

use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException 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 18 with InvalidArgumentException

use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException 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

AffiliationException (org.hyperledger.fabric_ca.sdk.exception.AffiliationException)18 HTTPException (org.hyperledger.fabric_ca.sdk.exception.HTTPException)18 InvalidArgumentException (org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException)18 JsonObject (javax.json.JsonObject)17 IdentityException (org.hyperledger.fabric_ca.sdk.exception.IdentityException)14 IOException (java.io.IOException)10 MalformedURLException (java.net.MalformedURLException)10 URISyntaxException (java.net.URISyntaxException)10 KeyManagementException (java.security.KeyManagementException)10 KeyStoreException (java.security.KeyStoreException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 CertificateException (java.security.cert.CertificateException)10 ParseException (org.apache.http.ParseException)10 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)10 EnrollmentException (org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)10 GenerateCRLException (org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException)10 InfoException (org.hyperledger.fabric_ca.sdk.exception.InfoException)10 RegistrationException (org.hyperledger.fabric_ca.sdk.exception.RegistrationException)10 RevocationException (org.hyperledger.fabric_ca.sdk.exception.RevocationException)10