List of usage examples for javax.json JsonObject getInt
int getInt(String name);
From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java
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)); // If the status code in the response is greater or equal to the status code set in the client object then an exception will // 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);/*from w ww. j a va 2 s . c o m*/ 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"); // If the status code is greater than or equal to 400 but less than or equal to the client status code setting, // 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); } return job.build(); } 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; }
From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java
/** idemixEnroll returns an Identity Mixer Enrollment, which supports anonymity and unlinkability * * @param enrollment a x509 enrollment credential * @return IdemixEnrollment//from w w w . j a v a2 s . c om * @throws EnrollmentException * @throws InvalidArgumentException */ public Enrollment idemixEnroll(Enrollment enrollment, String mspID) throws EnrollmentException, InvalidArgumentException { if (cryptoSuite == null) { throw new InvalidArgumentException("Crypto primitives not set"); } if (enrollment == null) { throw new InvalidArgumentException("enrollment is missing"); } if (Utils.isNullOrEmpty(mspID)) { throw new InvalidArgumentException("mspID cannot be null or empty"); } if (enrollment instanceof IdemixEnrollment) { throw new InvalidArgumentException("enrollment type must be x509"); } final RAND rng = IdemixUtils.getRand(); try { setUpSSL(); // Get nonce IdemixEnrollmentRequest idemixEnrollReq = new IdemixEnrollmentRequest(); String body = idemixEnrollReq.toJson(); JsonObject result = httpPost(url + HFCA_IDEMIXCRED, body, enrollment); if (result == null) { throw new EnrollmentException("No response received for idemix enrollment request"); } String nonceString = result.getString("Nonce"); if (Utils.isNullOrEmpty(nonceString)) { throw new InvalidArgumentException( "fabric-ca-server did not return a nonce in the response from " + HFCA_IDEMIXCRED); } byte[] nonceBytes = Base64.getDecoder().decode(nonceString.getBytes()); BIG nonce = BIG.fromBytes(nonceBytes); // Get issuer public key and revocation key from the cainfo section of response JsonObject info = result.getJsonObject("CAInfo"); if (info == null) { throw new Exception( "fabric-ca-server did not return 'cainfo' in the response from " + HFCA_IDEMIXCRED); } IdemixIssuerPublicKey ipk = getIssuerPublicKey(info.getString("IssuerPublicKey")); PublicKey rpk = getRevocationPublicKey(info.getString("IssuerRevocationPublicKey")); // Create and send idemix credential request BIG sk = new BIG(IdemixUtils.randModOrder(rng)); IdemixCredRequest idemixCredRequest = new IdemixCredRequest(sk, nonce, ipk); idemixEnrollReq.setIdemixCredReq(idemixCredRequest); body = idemixEnrollReq.toJson(); result = httpPost(url + HFCA_IDEMIXCRED, body, enrollment); if (result == null) { throw new EnrollmentException("No response received for idemix enrollment request"); } // Deserialize idemix credential String credential = result.getString("Credential"); if (Utils.isNullOrEmpty(credential)) { throw new InvalidArgumentException( "fabric-ca-server did not return a 'credential' in the response from " + HFCA_IDEMIXCRED); } byte[] credBytes = Base64.getDecoder().decode(credential.getBytes(UTF_8)); Idemix.Credential credProto = Idemix.Credential.parseFrom(credBytes); IdemixCredential cred = new IdemixCredential(credProto); // Deserialize idemix cri (Credential Revocation Information) String criStr = result.getString("CRI"); if (Utils.isNullOrEmpty(criStr)) { throw new InvalidArgumentException( "fabric-ca-server did not return a 'CRI' in the response from " + HFCA_IDEMIXCRED); } byte[] criBytes = Base64.getDecoder().decode(criStr.getBytes(UTF_8)); Idemix.CredentialRevocationInformation cri = Idemix.CredentialRevocationInformation.parseFrom(criBytes); JsonObject attrs = result.getJsonObject("Attrs"); if (attrs == null) { throw new EnrollmentException( "fabric-ca-server did not return 'attrs' in the response from " + HFCA_IDEMIXCRED); } String ou = attrs.getString("OU"); if (Utils.isNullOrEmpty(ou)) { throw new InvalidArgumentException( "fabric-ca-server did not return a 'ou' attribute in the response from " + HFCA_IDEMIXCRED); } int role = attrs.getInt("Role"); // Encoded IdemixRole from Fabric-Ca // Return the idemix enrollment return new IdemixEnrollment(ipk, rpk, mspID, sk, cred, cri, ou, role); } catch (EnrollmentException ee) { logger.error(ee.getMessage(), ee); throw ee; } catch (Exception e) { EnrollmentException ee = new EnrollmentException("Failed to get Idemix credential", e); logger.error(e.getMessage(), e); throw ee; } }