List of usage examples for javax.json JsonObject getJsonObject
JsonObject getJsonObject(String name);
From source file:io.bitgrillr.gocddockerexecplugin.DockerExecPlugin.java
private GoPluginApiResponse handleValidateRequest(JsonObject requestBody) { final Map<String, Object> responseBody = new HashMap<>(); final Map<String, String> errors = new HashMap<>(); final String image = requestBody.getJsonObject(IMAGE).getString(VALUE); if (!imageValid(image)) { errors.put(IMAGE, (new StringBuilder()).append("'").append(image) .append("' is not a valid image identifier").toString()); }//from www . j a v a2 s . co m responseBody.put("errors", errors); return DefaultGoPluginApiResponse.success(Json.createObjectBuilder(responseBody).build().toString()); }
From source file:org.hyperledger.fabric_ca.sdk.MockHFCAClient.java
@Override JsonObject httpPost(String url, String body, User admin) throws Exception { JsonObject response; if (httpPostResponse == null) { response = super.httpPost(url, body, admin); } else {// w w w . j ava 2 s . c o m JsonReader reader = Json.createReader(new StringReader(httpPostResponse)); response = (JsonObject) reader.read(); // TODO: HFCAClient could do with some minor refactoring to avoid duplicating this code here!! JsonObject result = response.getJsonObject("result"); if (result == null) { EnrollmentException e = new EnrollmentException(format( "POST request to %s failed request body %s " + "Body of response did not contain result", url, body), new Exception()); throw e; } } return response; }
From source file:tools.xor.logic.DefaultJson.java
protected void checkEntityField() { final String TASK_NAME = "SETUP_DSL"; // Create task JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); jsonBuilder.add("name", TASK_NAME); jsonBuilder.add("displayName", "Setup DSL"); jsonBuilder.add("description", "Setup high-speed broadband internet using DSL technology"); // Create quote final BigDecimal price = new BigDecimal("123456789.987654321"); jsonBuilder.add("quote", Json.createObjectBuilder().add("price", price)); Settings settings = getSettings();/*from w ww. j ava 2 s.c o m*/ settings.setEntityClass(Task.class); Task task = (Task) aggregateService.create(jsonBuilder.build(), settings); assert (task.getId() != null); assert (task.getName().equals(TASK_NAME)); assert (task.getQuote() != null); assert (task.getQuote().getId() != null); assert (task.getQuote().getPrice().equals(price)); Object jsonObject = aggregateService.read(task, settings); JsonObject jsonTask = (JsonObject) jsonObject; System.out.println("JSON string: " + jsonTask.toString()); assert (((JsonString) jsonTask.get("name")).getString().equals(TASK_NAME)); JsonObject jsonQuote = jsonTask.getJsonObject("quote"); assert (((JsonNumber) jsonQuote.get("price")).bigDecimalValue().equals(price)); }
From source file:org.ocelotds.integration.AbstractOcelotTest.java
/** * Becareful result is not unmarshalled// w ww . j a v a 2 s. c o m * * @param json * @return */ protected static MessageToClient mtcFromJson(String json) { try (JsonReader reader = Json.createReader(new StringReader(json))) { JsonObject root = reader.readObject(); MessageToClient message = new MessageToClient(); message.setId(root.getString(Constants.Message.ID)); message.setTime(root.getInt(Constants.Message.TIME)); message.setType(MessageType.valueOf(root.getString(Constants.Message.TYPE))); message.setDeadline(root.getInt(Constants.Message.DEADLINE)); if (null != message.getType()) { switch (message.getType()) { case FAULT: JsonObject faultJs = root.getJsonObject(Constants.Message.RESPONSE); Fault f = Fault.createFromJson(faultJs.toString()); message.setFault(f); break; case MESSAGE: message.setResult("" + root.get(Constants.Message.RESPONSE)); message.setType(MessageType.MESSAGE); break; case CONSTRAINT: JsonArray result = root.getJsonArray(Constants.Message.RESPONSE); List<ConstraintViolation> list = new ArrayList<>(); for (JsonValue jsonValue : result) { list.add(getJava(ConstraintViolation.class, ((JsonObject) jsonValue).toString())); } message.setConstraints(list.toArray(new ConstraintViolation[] {})); break; default: message.setResult("" + root.get(Constants.Message.RESPONSE)); break; } } return message; } }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Access all properties. The response of: * //from ww w .j ava 2s. com * <pre> * { * "action": "get", * "node": { * "key": "/", * "dir": true, * "nodes": [ * { * "key": "/foo_dir", * "dir": true, * "modifiedIndex": 2, * "createdIndex": 2 * }, * { * "key": "/foo", * "value": "two", * "modifiedIndex": 1, * "createdIndex": 1 * } * ] * } * } * </pre> * * is mapped to a regular Tamaya properties map as follows: * * <pre> * key1=myvalue * _key1.source=[etcd]http://127.0.0.1:4001 * _key1.createdIndex=12 * _key1.modifiedIndex=34 * _key1.ttl=300 * _key1.expiration=... * * key2=myvaluexxx * _key2.source=[etcd]http://127.0.0.1:4001 * _key2.createdIndex=12 * * key3=val3 * _key3.source=[etcd]http://127.0.0.1:4001 * _key3.createdIndex=12 * _key3.modifiedIndex=2 * </pre> * * @param directory remote directory to query. * @param recursive allows to set if querying is performed recursively * @return all properties read from the remote server. */ public Map<String, String> getProperties(String directory, boolean recursive) { final Map<String, String> result = new HashMap<>(); try { final HttpGet get = new HttpGet(serverURL + "/v2/keys/" + directory + "?recursive=" + recursive); get.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(get)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); final JsonReader reader = readerFactory .createReader(new StringReader(EntityUtils.toString(entity))); final JsonObject o = reader.readObject(); final JsonObject node = o.getJsonObject("node"); if (node != null) { addNodes(result, node); } EntityUtils.consume(entity); } } } catch (final Exception e) { LOG.log(Level.INFO, "Error reading properties for '" + directory + "' from etcd: " + serverURL, e); result.put("_ERROR", "Error reading properties for '" + directory + "' from etcd: " + serverURL + ": " + e.toString()); } return result; }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Deletes a given key. The response is as follows: * /*from w w w . j a v a 2s . c o m*/ * <pre> * _key.source=[etcd]http://127.0.0.1:4001 * _key.createdIndex=12 * _key.modifiedIndex=34 * _key.ttl=300 * _key.expiry=... * // optional * _key.prevNode.createdIndex=12 * _key.prevNode.modifiedIndex=34 * _key.prevNode.ttl=300 * _key.prevNode.expiration=... * _key.prevNode.value=... * </pre> * * @param key the key to be deleted. * @return the response mpas as described above. */ public Map<String, String> delete(String key) { final Map<String, String> result = new HashMap<>(); try { final HttpDelete delete = new HttpDelete(serverURL + "/v2/keys/" + key); delete.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(delete)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); final JsonReader reader = readerFactory .createReader(new StringReader(EntityUtils.toString(entity))); final JsonObject o = reader.readObject(); final JsonObject node = o.getJsonObject("node"); if (node.containsKey("createdIndex")) { result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); } if (node.containsKey("modifiedIndex")) { result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); } if (node.containsKey("expiration")) { result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); } if (node.containsKey("ttl")) { result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); } parsePrevNode(key, result, o); EntityUtils.consume(entity); } } } catch (final Exception e) { LOG.log(Level.INFO, "Error deleting key '" + key + "' from etcd: " + serverURL, e); result.put("_ERROR", "Error deleting '" + key + "' from etcd: " + serverURL + ": " + e.toString()); } return result; }
From source file:de.pangaea.fixo3.CreateEsonetYellowPages.java
private void addDeviceType(String name, String label, String comment, String seeAlso, JsonArray equivalentClasses, JsonArray subClasses) { IRI deviceTypeIRI = IRI.create(name); m.addClass(deviceTypeIRI);//w w w . j av a 2 s . com m.addLabel(deviceTypeIRI, label); m.addComment(deviceTypeIRI, comment); m.addSeeAlso(deviceTypeIRI, seeAlso); // Default sub class, though implicit with curated sensing device type // hierarchy // m.addSubClass(deviceTypeIRI, SSN.SensingDevice); for (JsonObject equivalentClass : equivalentClasses.getValuesAs(JsonObject.class)) { if (equivalentClass.containsKey("type")) { m.addEquivalentClass(deviceTypeIRI, IRI.create(equivalentClass.getString("type"))); } } for (JsonObject subClass : subClasses.getValuesAs(JsonObject.class)) { if (subClass.containsKey("type")) { m.addSubClass(deviceTypeIRI, IRI.create(subClass.getString("type"))); } else if (subClass.containsKey("observes")) { JsonObject observesJson = subClass.getJsonObject("observes"); String propertyLabel = observesJson.getString("label"); String propertyType = observesJson.getString("type"); JsonObject featureJson = observesJson.getJsonObject("isPropertyOf"); String featureLabel = featureJson.getString("label"); String featureType = featureJson.getString("type"); IRI propertyIRI = IRI.create(EYP.ns.toString() + md5Hex(propertyLabel)); IRI propertyTypeIRI = IRI.create(propertyType); IRI featureIRI = IRI.create(EYP.ns.toString() + md5Hex(featureLabel)); IRI featureTypeIRI = IRI.create(featureType); m.addObjectValue(deviceTypeIRI, observes, propertyIRI); m.addIndividual(propertyIRI); m.addType(propertyIRI, propertyTypeIRI); m.addLabel(propertyIRI, propertyLabel); m.addIndividual(featureIRI); m.addType(featureIRI, featureTypeIRI); m.addLabel(featureIRI, featureLabel); m.addObjectAssertion(propertyIRI, isPropertyOf, featureIRI); } else if (subClass.containsKey("detects")) { JsonObject detectsJson = subClass.getJsonObject("detects"); String stimulusLabel = detectsJson.getString("label"); String stimulusType = detectsJson.getString("type"); IRI stimulusIRI = IRI.create(EYP.ns.toString() + md5Hex(stimulusLabel)); IRI stimulusTypeIRI = IRI.create(stimulusType); m.addObjectValue(deviceTypeIRI, detects, stimulusIRI); m.addIndividual(stimulusIRI); m.addType(stimulusIRI, stimulusTypeIRI); m.addLabel(stimulusIRI, stimulusLabel); } else if (subClass.containsKey("capability")) { JsonObject capabilityJson = subClass.getJsonObject("capability"); String capabilityLabel = capabilityJson.getString("label"); JsonObject propertyJson = capabilityJson.getJsonObject("property"); String propertyLabel = propertyJson.getString("label"); String propertyType = propertyJson.getString("type"); JsonObject valueJson = propertyJson.getJsonObject("value"); String valueLabel = valueJson.getString("label"); IRI capabilityIRI = IRI.create(EYP.ns.toString() + md5Hex(capabilityLabel)); IRI propertyIRI = IRI.create(EYP.ns.toString() + md5Hex(propertyLabel)); IRI propertyTypeIRI = IRI.create(propertyType); IRI valueIRI = IRI.create(EYP.ns.toString() + md5Hex(valueLabel)); m.addObjectValue(deviceTypeIRI, hasMeasurementCapability, capabilityIRI); m.addIndividual(capabilityIRI); m.addType(capabilityIRI, MeasurementCapability); m.addLabel(capabilityIRI, capabilityLabel); m.addObjectAssertion(capabilityIRI, hasMeasurementProperty, propertyIRI); m.addIndividual(propertyIRI); m.addType(propertyIRI, SSN.MeasurementProperty); m.addType(propertyIRI, propertyTypeIRI); m.addLabel(propertyIRI, propertyLabel); m.addObjectAssertion(propertyIRI, hasValue, valueIRI); m.addIndividual(valueIRI); m.addType(valueIRI, SSN.ObservationValue); m.addLabel(valueIRI, valueLabel); if (valueJson.containsKey("value")) { m.addType(valueIRI, QuantitativeValue); m.addDataAssertion(valueIRI, value, Float.valueOf(valueJson.getString("value"))); } else if (valueJson.containsKey("minValue") && valueJson.containsKey("maxValue")) { m.addType(valueIRI, QuantitativeValue); m.addDataAssertion(valueIRI, minValue, Float.valueOf(valueJson.getString("minValue"))); m.addDataAssertion(valueIRI, maxValue, Float.valueOf(valueJson.getString("maxValue"))); } else { throw new RuntimeException("Expected value or min/max value [valueJson = " + valueJson + "]"); } m.addObjectAssertion(valueIRI, unitCode, IRI.create(valueJson.getString("unitCode"))); } } }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Ask etcd for a single key, value pair. Hereby the response returned from * etcd:/*from w w w . j a v a 2 s .c o m*/ * * <pre> * { * "action": "get", * "node": { * "createdIndex": 2, * "key": "/message", * "modifiedIndex": 2, * "value": "Hello world" * } * } * </pre> * * is mapped to: * * <pre> * key=value * _key.source=[etcd]http://127.0.0.1:4001 * _key.createdIndex=12 * _key.modifiedIndex=34 * _key.ttl=300 * _key.expiration=... * </pre> * * @param key the requested key * @return the mapped result, including meta-entries. */ public Map<String, String> get(String key) { final Map<String, String> result = new HashMap<>(); try { final HttpGet httpGet = new HttpGet(serverURL + "/v2/keys/" + key); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); final JsonReader reader = readerFactory .createReader(new StringReader(EntityUtils.toString(entity))); final JsonObject o = reader.readObject(); final JsonObject node = o.getJsonObject("node"); if (node.containsKey("value")) { result.put(key, node.getString("value")); result.put("_" + key + ".source", "[etcd]" + serverURL); } if (node.containsKey("createdIndex")) { result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); } if (node.containsKey("modifiedIndex")) { result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); } if (node.containsKey("expiration")) { result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); } if (node.containsKey("ttl")) { result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); } EntityUtils.consume(entity); } else { result.put("_" + key + ".NOT_FOUND.target", "[etcd]" + serverURL); } } } catch (final Exception e) { LOG.log(Level.INFO, "Error reading key '" + key + "' from etcd: " + serverURL, e); result.put("_ERROR", "Error reading key '" + key + "' from etcd: " + serverURL + ": " + e.toString()); } return result; }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Creates/updates an entry in etcd. The response as follows: * //from w w w . j a v a 2s . c o m * <pre> * { * "action": "set", * "node": { * "createdIndex": 3, * "key": "/message", * "modifiedIndex": 3, * "value": "Hello etcd" * }, * "prevNode": { * "createdIndex": 2, * "key": "/message", * "value": "Hello world", * "modifiedIndex": 2 * } * } * </pre> * * is mapped to: * * <pre> * key=value * _key.source=[etcd]http://127.0.0.1:4001 * _key.createdIndex=12 * _key.modifiedIndex=34 * _key.ttl=300 * _key.expiry=... * // optional * _key.prevNode.createdIndex=12 * _key.prevNode.modifiedIndex=34 * _key.prevNode.ttl=300 * _key.prevNode.expiration=... * </pre> * * @param key the property key, not null * @param value the value to be set * @param ttlSeconds the ttl in seconds (optional) * @return the result map as described above. */ public Map<String, String> set(String key, String value, Integer ttlSeconds) { final Map<String, String> result = new HashMap<>(); try { final HttpPut put = new HttpPut(serverURL + "/v2/keys/" + key); put.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); final List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("value", value)); if (ttlSeconds != null) { nvps.add(new BasicNameValuePair("ttl", ttlSeconds.toString())); } put.setEntity(new UrlEncodedFormEntity(nvps)); try (CloseableHttpResponse response = httpclient.execute(put)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); final JsonReader reader = readerFactory .createReader(new StringReader(EntityUtils.toString(entity))); final JsonObject o = reader.readObject(); final JsonObject node = o.getJsonObject("node"); if (node.containsKey("createdIndex")) { result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); } if (node.containsKey("modifiedIndex")) { result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); } if (node.containsKey("expiration")) { result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); } if (node.containsKey("ttl")) { result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); } result.put(key, node.getString("value")); result.put("_" + key + ".source", "[etcd]" + serverURL); parsePrevNode(key, result, node); EntityUtils.consume(entity); } } } catch (final Exception e) { LOG.log(Level.INFO, "Error writing to etcd: " + serverURL, e); result.put("_ERROR", "Error writing '" + key + "' to etcd: " + serverURL + ": " + e.toString()); } return result; }
From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java
private void scheduleTalk(JsonObject timeslotObject, Room room, Timeslot timeslot) { Talk talk = talkCodeToTalkMap.get(timeslotObject.getJsonObject("talk").getString("id")); if (talk == null) { throw new IllegalStateException("The timeslot (" + timeslotObject.getString("slotId") + ") has a talk (" + timeslotObject.getJsonObject("talk").getString("id") + ") that does not exist in the talk list"); }/*from w ww .j a v a 2s. c om*/ if (talk.isPinnedByUser()) { throw new IllegalStateException("The timeslot (" + timeslotObject.getString("slotId") + ") has a talk (" + timeslotObject.getJsonObject("talk").getString("id") + ") that is already pinned by user at another timeslot (" + talk.getTimeslot().toString() + ")."); } talk.setRoom(room); talk.setTimeslot(timeslot); talk.setPinnedByUser(true); }