Search in sources :

Example 6 with HTTPException

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

the class HFCAAffiliation method read.

/**
 * gets a specific affiliation
 *
 * @param registrar The identity of the registrar
 * @return Returns response
 * @throws AffiliationException if getting an affiliation fails.
 * @throws InvalidArgumentException
 */
public int read(User registrar) throws AffiliationException, InvalidArgumentException {
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String readAffURL = "";
    try {
        readAffURL = HFCA_AFFILIATION + "/" + name;
        logger.debug(format("affiliation  url: %s, registrar: %s", readAffURL, registrar.getName()));
        JsonObject result = client.httpGet(readAffURL, registrar);
        logger.debug(format("affiliation  url: %s, registrar: %s done.", readAffURL, registrar));
        HFCAAffiliationResp resp = getResponse(result);
        this.childHFCAAffiliations = resp.getChildren();
        this.identities = resp.getIdentities();
        this.deleted = false;
        return resp.statusCode;
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while getting affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, readAffURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    } catch (Exception e) {
        String msg = format("Error while getting affiliation %s url: %s  %s ", this.name, readAffURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 7 with HTTPException

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

the class HFCAAffiliation method update.

/**
 * update an affiliation
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @param force Forces updating of child affiliations
 * @return Response of request
 * @throws AffiliationException If updating an affiliation fails
 * @throws InvalidArgumentException
 */
public HFCAAffiliationResp update(User registrar, boolean force) throws AffiliationException, InvalidArgumentException {
    if (this.deleted) {
        throw new AffiliationException("Affiliation has been deleted");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    if (Utils.isNullOrEmpty(name)) {
        throw new InvalidArgumentException("Affiliation name cannot be null or empty");
    }
    String updateURL = "";
    try {
        Map<String, String> queryParm = new HashMap<String, String>();
        queryParm.put("force", String.valueOf(force));
        updateURL = client.getURL(HFCA_AFFILIATION + "/" + this.name, queryParm);
        logger.debug(format("affiliation  url: %s, registrar: %s", updateURL, registrar.getName()));
        String body = client.toJson(affToJsonObject());
        JsonObject result = client.httpPut(updateURL, body, registrar);
        generateResponse(result);
        logger.debug(format("identity  url: %s, registrar: %s done.", updateURL, registrar));
        HFCAAffiliationResp resp = getResponse(result);
        this.childHFCAAffiliations = resp.childHFCAAffiliations;
        this.identities = resp.identities;
        return getResponse(result);
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while updating affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, updateURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    } catch (Exception e) {
        String msg = format("Error while updating affiliation %s url: %s  %s ", this.name, updateURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 8 with HTTPException

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

the class HFCAAffiliation method create.

/**
 * create an affiliation
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @param force Forces the creation of parent affiliations
 * @return Response of request
 * @throws AffiliationException    if adding an affiliation fails.
 * @throws InvalidArgumentException
 */
public HFCAAffiliationResp create(User registrar, boolean force) throws AffiliationException, InvalidArgumentException {
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    String createURL = "";
    try {
        createURL = client.getURL(HFCA_AFFILIATION);
        logger.debug(format("affiliation  url: %s, registrar: %s", createURL, registrar.getName()));
        Map<String, String> queryParm = new HashMap<String, String>();
        queryParm.put("force", String.valueOf(force));
        String body = client.toJson(affToJsonObject());
        JsonObject result = client.httpPost(createURL, body, registrar);
        logger.debug(format("identity  url: %s, registrar: %s done.", createURL, registrar));
        this.deleted = false;
        return getResponse(result);
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while creating affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, createURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    } catch (Exception e) {
        String msg = format("Error while creating affiliation %s url: %s  %s ", this.name, createURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 9 with HTTPException

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

the class HFCAClient method getHFCAAffiliations.

/**
 * gets all affiliations that the registrar is allowed to see
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @return The affiliations that were requested
 * @throws AffiliationException     if getting all affiliations fails
 * @throws InvalidArgumentException
 */
public HFCAAffiliation getHFCAAffiliations(User registrar) throws AffiliationException, InvalidArgumentException {
    if (cryptoSuite == null) {
        throw new InvalidArgumentException("Crypto primitives not set.");
    }
    if (registrar == null) {
        throw new InvalidArgumentException("Registrar should be a valid member");
    }
    logger.debug(format("affiliations  url: %s, registrar: %s", url, registrar.getName()));
    try {
        JsonObject result = httpGet(HFCAAffiliation.HFCA_AFFILIATION, registrar);
        HFCAAffiliation affiliations = new HFCAAffiliation(result);
        logger.debug(format("affiliations  url: %s, registrar: %s done.", url, registrar));
        return affiliations;
    } catch (HTTPException e) {
        String msg = format("[HTTP Status Code: %d] - Error while getting all affiliations from url '%s': %s", e.getStatusCode(), url, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    } catch (Exception e) {
        String msg = format("Error while getting all affiliations from url '%s': %s", url, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) 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 10 with HTTPException

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

Aggregations

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