Search in sources :

Example 1 with HTTPException

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

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

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

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

the class HFCAClient method getResult.

JsonObject getResult(HttpResponse response, String body, String type) throws HTTPException, ParseException, IOException {
    int respStatusCode = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();
    logger.trace(format("response status %d, HttpEntity %s ", respStatusCode, "" + entity));
    String responseBody = entity != null ? EntityUtils.toString(entity) : null;
    logger.trace(format("responseBody: %s ", responseBody));
    // be thrown, otherwise, we continue to read the response and return any error code that is less than 'statusCode'
    if (respStatusCode >= statusCode) {
        HTTPException e = new HTTPException(format("%s request to %s failed request body %s. Response: %s", type, url, body, responseBody), respStatusCode);
        logger.error(e.getMessage());
        throw e;
    }
    if (responseBody == null) {
        HTTPException e = new HTTPException(format("%s request to %s failed request body %s with null response body returned.", type, url, body), respStatusCode);
        logger.error(e.getMessage());
        throw e;
    }
    logger.debug("Status: " + respStatusCode);
    JsonReader reader = Json.createReader(new StringReader(responseBody));
    JsonObject jobj = (JsonObject) reader.read();
    JsonObjectBuilder job = Json.createObjectBuilder();
    job.add("statusCode", respStatusCode);
    JsonArray errors = jobj.getJsonArray("errors");
    // then encountered an error and we return back the status code, and log the error rather than throwing an exception.
    if (respStatusCode < statusCode && respStatusCode >= 400) {
        if (errors != null && !errors.isEmpty()) {
            JsonObject jo = errors.getJsonObject(0);
            String errorMsg = format("[HTTP Status Code: %d] - %s request to %s failed request body %s error message: [Error Code %d] - %s", respStatusCode, type, url, body, jo.getInt("code"), jo.getString("message"));
            logger.error(errorMsg);
        }
        JsonObject result = job.build();
        return result;
    }
    if (errors != null && !errors.isEmpty()) {
        JsonObject jo = errors.getJsonObject(0);
        HTTPException e = new HTTPException(format("%s request to %s failed request body %s error message: [Error Code %d] - %s", type, url, body, jo.getInt("code"), jo.getString("message")), respStatusCode);
        throw e;
    }
    boolean success = jobj.getBoolean("success");
    if (!success) {
        HTTPException e = new HTTPException(format("%s request to %s failed request body %s Body of response did not contain success", type, url, body), respStatusCode);
        logger.error(e.getMessage());
        throw e;
    }
    JsonObject result = jobj.getJsonObject("result");
    if (result == null) {
        HTTPException e = new HTTPException(format("%s request to %s failed request body %s " + "Body of response did not contain result", type, url, body), respStatusCode);
        logger.error(e.getMessage());
        throw e;
    }
    JsonArray messages = jobj.getJsonArray("messages");
    if (messages != null && !messages.isEmpty()) {
        JsonObject jo = messages.getJsonObject(0);
        String message = format("%s request to %s failed request body %s response message: [Error Code %d] - %s", type, url, body, jo.getInt("code"), jo.getString("message"));
        logger.info(message);
    }
    // Construct JSON object that contains the result and HTTP status code
    for (Entry<String, JsonValue> entry : result.entrySet()) {
        job.add(entry.getKey(), entry.getValue());
    }
    job.add("statusCode", respStatusCode);
    result = job.build();
    logger.debug(format("%s %s, body:%s result: %s", type, url, body, "" + result));
    return result;
}
Also used : JsonArray(javax.json.JsonArray) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) HttpEntity(org.apache.http.HttpEntity) StringReader(java.io.StringReader) JsonValue(javax.json.JsonValue) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 5 with HTTPException

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

the class HFCAAffiliation method delete.

/**
 * delete an affiliation
 *
 * @param registrar The identity of the registrar (i.e. who is performing the registration).
 * @param force Forces the deletion of affiliation
 * @return Response of request
 * @throws AffiliationException    if deleting an affiliation fails.
 * @throws InvalidArgumentException
 */
public HFCAAffiliationResp delete(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");
    }
    String deleteURL = "";
    try {
        Map<String, String> queryParm = new HashMap<String, String>();
        queryParm.put("force", String.valueOf(force));
        deleteURL = client.getURL(HFCA_AFFILIATION + "/" + this.name, queryParm);
        logger.debug(format("affiliation  url: %s, registrar: %s", deleteURL, registrar.getName()));
        JsonObject result = client.httpDelete(deleteURL, registrar);
        logger.debug(format("identity  url: %s, registrar: %s done.", deleteURL, registrar));
        this.deleted = true;
        return getResponse(result);
    } catch (HTTPException e) {
        String msg = format("[Code: %d] - Error while deleting affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, deleteURL, e.getMessage());
        AffiliationException affiliationException = new AffiliationException(msg, e);
        logger.error(msg);
        throw affiliationException;
    } catch (Exception e) {
        String msg = format("Error while deleting affiliation %s url: %s  %s ", this.name, deleteURL, 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)

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