List of usage examples for javax.json JsonValue asJsonObject
default JsonObject asJsonObject()
From source file:io.bitgrillr.gocddockerexecplugin.utils.GoTestUtils.java
/** * Returns the status of the named pipeline instance. * * @param pipeline Name of the pipeline. * @param counter Counter of the pipeline instance. * @return The status.// ww w . ja v a 2s .co m * @throws GoError If Go.CD returns a non 2XX reponse. * @throws IOException If a communication error occurs. */ public static String getPipelineStatus(String pipeline, int counter) throws GoError, IOException { final HttpResponse response = executor .execute(Request.Get(PIPELINES + pipeline + "/instance/" + Integer.toString(counter))) .returnResponse(); final int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { throw new GoError(status); } final JsonArray stages = Json.createReader(response.getEntity().getContent()).readObject() .getJsonArray("stages"); String pipelineStatus = "Completed"; for (JsonValue stage : stages) { final JsonArray jobs = ((JsonObject) stage).getJsonArray("jobs"); for (JsonValue job : jobs) { pipelineStatus = job.asJsonObject().getString("state"); if (!"Completed".equals(pipelineStatus)) { return pipelineStatus; } } } return pipelineStatus; }
From source file:io.bitgrillr.gocddockerexecplugin.utils.GoTestUtils.java
/** * Schedules the named pipeline to run.// ww w.j a v a 2s .c om * * @param pipeline Name of the Pipeline to run. * @return Counter of the pipeline instance scheduled. * @throws GoError If Go.CD returns a non 2XX response. * @throws IOException If a communication error occurs. * @throws InterruptedException If something has gone horribly wrong. */ public static int runPipeline(String pipeline) throws GoError, IOException, InterruptedException { final Response scheduleResponse = executor .execute(Request.Post(PIPELINES + pipeline + "/schedule").addHeader("Confirm", "true")); final int scheduleStatus = scheduleResponse.returnResponse().getStatusLine().getStatusCode(); if (scheduleStatus != HttpStatus.SC_ACCEPTED) { throw new GoError(scheduleStatus); } Thread.sleep(5 * 1000); final HttpResponse historyResponse = executor.execute(Request.Get(PIPELINES + pipeline + "/history")) .returnResponse(); final int historyStatus = historyResponse.getStatusLine().getStatusCode(); if (historyStatus != HttpStatus.SC_OK) { throw new GoError(historyStatus); } final JsonArray pipelineInstances = Json.createReader(historyResponse.getEntity().getContent()).readObject() .getJsonArray("pipelines"); JsonObject lastPipelineInstance = pipelineInstances.getJsonObject(0); for (JsonValue pipelineInstance : pipelineInstances) { if (pipelineInstance.asJsonObject().getInt("counter") > lastPipelineInstance.getInt("counter")) { lastPipelineInstance = pipelineInstance.asJsonObject(); } } return lastPipelineInstance.getInt("counter"); }
From source file:org.wildfly.test.integration.microprofile.health.MicroProfileHealthHTTPEndpointTestCase.java
void checkGlobalOutcome(ManagementClient managementClient, boolean mustBeUP, String probeName) throws IOException { final String healthURL = "http://" + managementClient.getMgmtAddress() + ":" + managementClient.getMgmtPort() + "/health"; try (CloseableHttpClient client = HttpClients.createDefault()) { CloseableHttpResponse resp = client.execute(new HttpGet(healthURL)); assertEquals(mustBeUP ? 200 : 503, resp.getStatusLine().getStatusCode()); String content = getContent(resp); resp.close();/*from w w w. java 2s.c om*/ try (JsonReader jsonReader = Json.createReader(new StringReader(content))) { JsonObject payload = jsonReader.readObject(); String outcome = payload.getString("outcome"); assertEquals(mustBeUP ? "UP" : "DOWN", outcome); if (probeName != null) { for (JsonValue check : payload.getJsonArray("checks")) { if (probeName.equals(check.asJsonObject().getString("name"))) { // probe name found assertEquals(mustBeUP ? "UP" : "DOWN", check.asJsonObject().getString("state")); return; } } fail("Probe named " + probeName + " not found in " + content); } } } }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
Collection.CollectionConfigPackage parse(JsonArray jsonConfig) throws ChaincodeCollectionConfigurationException { Collection.CollectionConfigPackage.Builder colcofbuilder = Collection.CollectionConfigPackage.newBuilder(); for (int i = jsonConfig.size() - 1; i > -1; --i) { Collection.StaticCollectionConfig.Builder ssc = Collection.StaticCollectionConfig.newBuilder(); JsonValue j = jsonConfig.get(i); if (j.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException(format( "Expected StaticCollectionConfig to be Object type but got: %s", j.getValueType().name())); }//from w ww .j a v a 2 s . com JsonObject jsonObject = j.asJsonObject(); JsonObject scf = getJsonObject(jsonObject, "StaticCollectionConfig"); // oneof .. may have different values in the future ssc.setName(getJsonString(scf, "name")).setBlockToLive(getJsonLong(scf, "blockToLive")) .setMaximumPeerCount(getJsonInt(scf, "maximumPeerCount")) .setMemberOrgsPolicy(Collection.CollectionPolicyConfig.newBuilder() .setSignaturePolicy(parseSignaturePolicyEnvelope(scf)).build()) .setRequiredPeerCount(getJsonInt(scf, "requiredPeerCount")); colcofbuilder .addConfig(Collection.CollectionConfig.newBuilder().setStaticCollectionConfig(ssc).build()); } return colcofbuilder.build(); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, JsonObject policy) throws ChaincodeCollectionConfigurationException { if (policy.size() != 1) { throw new ChaincodeCollectionConfigurationException( format("Expected policy size of 1 but got %d", policy.size())); }//from ww w .j av a2 s. co m final String key = policy.entrySet().iterator().next().getKey(); if ("signed-by".equals(key)) { final String vo = getJsonString(policy, key); MSPPrincipal mspPrincipal = identities.get(vo); if (null == mspPrincipal) { throw new ChaincodeCollectionConfigurationException( format("No identity found by name %s in signed-by.", vo)); } return SignaturePolicy.newBuilder().setSignedBy(identities.getKeysIndex(vo)).build(); } else { Matcher match = noofPattern.matcher(key); final JsonArray vo = getJsonArray(policy, key); if (match.matches() && match.groupCount() == 1) { String matchStingNo = match.group(1).trim(); int matchNo = Integer.parseInt(matchStingNo); if (vo.size() < matchNo) { throw new ChaincodeCollectionConfigurationException( format("%s expected to have at least %d items to match but only found %d.", key, matchNo, vo.size())); } SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder().setN(matchNo); for (int i = vo.size() - 1; i >= 0; --i) { JsonValue jsonValue = vo.get(i); if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException( format("Expected object type in Nof but got %s", jsonValue.getValueType().name())); } SignaturePolicy sp = parsePolicy(identities, jsonValue.asJsonObject()); spBuilder.addRules(sp); } return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build(); } else { throw new ChaincodeCollectionConfigurationException(format("Unsupported policy type %s", key)); } } }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private IndexedHashMap<String, MSPPrincipal> parseIdentities(JsonArray identities) throws ChaincodeCollectionConfigurationException { IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>(); for (JsonValue jsonValue : identities) { if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException( format("Expected in identies user to be Object type but got: %s", jsonValue.getValueType().name())); }//www . ja v a 2 s .c o m JsonObject user = jsonValue.asJsonObject(); if (user.entrySet().size() != 1) { throw new ChaincodeCollectionConfigurationException( "Only expected on property for user entry in identies."); } Map.Entry<String, JsonValue> next = user.entrySet().iterator().next(); String name = next.getKey(); jsonValue = next.getValue(); if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException( format("Expected in identies role to be Object type but got: %s", jsonValue.getValueType().name())); } JsonObject role = jsonValue.asJsonObject(); JsonObject roleObj = getJsonObject(role, "role"); String roleName = getJsonString(roleObj, "name"); String mspId = getJsonString(roleObj, "mspId"); MSPRole.MSPRoleType mspRoleType; switch (roleName.intern()) { case "member": mspRoleType = MSPRole.MSPRoleType.MEMBER; break; case "admin": mspRoleType = MSPRole.MSPRoleType.ADMIN; break; case "client": mspRoleType = MSPRole.MSPRoleType.CLIENT; break; case "peer": mspRoleType = MSPRole.MSPRoleType.PEER; break; default: throw new ChaincodeCollectionConfigurationException(format( "In identities with key %s name expected member, admin, client, or peer in role got %s ", name, roleName)); } MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType).setMspIdentifier(mspId).build(); MSPPrincipal principal = MSPPrincipal.newBuilder() .setPrincipalClassification(MSPPrincipal.Classification.ROLE) .setPrincipal(mspRole.toByteString()).build(); ret.put(name, principal); } return ret; }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static JsonObject getJsonObject(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }// www. j a v a 2 s . c om if (ret.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected object got %s", prop, ret.getValueType().name())); } return ret.asJsonObject(); }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static JsonObject getJsonValueAsObject(JsonValue value) { return (value != null && value.getValueType() == ValueType.OBJECT) ? value.asJsonObject() : null; }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static List<JsonObject> getJsonValueAsList(JsonValue value) { if (value != null) { if (value.getValueType() == ValueType.ARRAY) { return value.asJsonArray().getValuesAs(JsonObject.class); } else if (value.getValueType() == ValueType.OBJECT) { List<JsonObject> ret = new ArrayList<>(); ret.add(value.asJsonObject()); return ret; }//w w w . j a v a 2s. com } return null; }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static JsonObject getJsonObject(JsonObject object, String propName) { JsonObject obj = null;//from w w w . ja va 2 s . c o m JsonValue val = object.get(propName); if (val != null && val.getValueType() == ValueType.OBJECT) { obj = val.asJsonObject(); } return obj; }