List of usage examples for com.fasterxml.jackson.databind.node ArrayNode get
public JsonNode get(String paramString)
From source file:org.envirocar.server.rest.util.GeoJSON.java
@Override public GeometryCollection decodeGeometryCollection(JsonNode json) throws GeometryConverterException { if (!json.has(GEOMETRIES_KEY)) { throw new GeometryConverterException("missing 'geometries' field"); }/* w w w .j a v a 2 s.c o m*/ ArrayNode geometries = toList(json.get(GEOMETRIES_KEY)); Geometry[] geoms = new Geometry[geometries.size()]; for (int i = 0; i < geometries.size(); ++i) { geoms[i] = decodeGeometry(geometries.get(i)); } return getGeometryFactory().createGeometryCollection(geoms); }
From source file:org.envirocar.server.rest.util.GeoJSON.java
protected Coordinate[] decodeCoordinates(ArrayNode list) throws GeometryConverterException { Coordinate[] coordinates = new Coordinate[list.size()]; for (int i = 0; i < list.size(); ++i) { coordinates[i] = decodeCoordinate(toList(list.get(i))); }/*from w ww . j a v a 2s.c om*/ return coordinates; }
From source file:org.envirocar.server.rest.util.GeoJSON.java
@Override public MultiPolygon decodeMultiPolygon(JsonNode json) throws GeometryConverterException { ArrayNode coordinates = requireCoordinates(json); Polygon[] polygons = new Polygon[coordinates.size()]; for (int i = 0; i < coordinates.size(); ++i) { polygons[i] = decodePolygonCoordinates(toList(coordinates.get(i))); }//from w w w .ja v a2s . c o m return getGeometryFactory().createMultiPolygon(polygons); }
From source file:com.marklogic.entityservices.TestEsEntityTypeSPARQL.java
@Test public void testSPARQLEntityTypeDoc() throws JsonGenerationException, JsonMappingException, IOException { // This test verifies that EntityType doc SchemaCompleteEntityType-0.0.1 has all the ET in it and version and title String assertTypeDocHasRDFType = "PREFIX t: <http://marklogic.com/testing-entity-type#> " + "SELECT ?p ?o " + "WHERE { t:SchemaCompleteEntityType-0.0.1 ?p ?o }" + "order by ?s"; JacksonHandle handle2 = queryMgr.executeSelect(queryMgr.newQueryDefinition(assertTypeDocHasRDFType), new JacksonHandle()); JsonNode results2 = handle2.get();//from ww w . jav a 2s . c om // to see on System.out //new ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(System.out, results2); ArrayNode bindings2 = (ArrayNode) results2.get("results").get("bindings"); //logger.info(bindings2.toString()); assertEquals(6, bindings2.size()); // Verify that Entity type doc has RDF type in it. assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", bindings2.get(0).get("p").get("value").asText()); assertEquals("http://marklogic.com/entity-services#Model", bindings2.get(0).get("o").get("value").asText()); // Verify that Entity type doc has EnityType OrderDetails in it. assertEquals("http://marklogic.com/entity-services#definitions", bindings2.get(1).get("p").get("value").asText()); assertEquals("http://marklogic.com/testing-entity-type/SchemaCompleteEntityType-0.0.1/OrderDetails", bindings2.get(1).get("o").get("value").asText()); // Verify that Entity type doc has EnityType OrderDetails in it. assertEquals("http://marklogic.com/entity-services#definitions", bindings2.get(2).get("p").get("value").asText()); assertEquals( "http://marklogic.com/testing-entity-type/SchemaCompleteEntityType-0.0.1/SchemaCompleteEntityType", bindings2.get(2).get("o").get("value").asText()); // Verify that Entity type doc has version in it. assertEquals("http://marklogic.com/entity-services#version", bindings2.get(3).get("p").get("value").asText()); assertEquals("0.0.1", bindings2.get(3).get("o").get("value").asText()); // Verify that Entity type doc has title in it. assertEquals("http://marklogic.com/entity-services#description", bindings2.get(4).get("p").get("value").asText()); assertEquals( "All Schema Elements represented in this type. Collations and datatypes are all happy-path and valid.", bindings2.get(4).get("o").get("value").asText()); // Verify that Entity type doc has title in it. assertEquals("http://marklogic.com/entity-services#title", bindings2.get(5).get("p").get("value").asText()); assertEquals("SchemaCompleteEntityType", bindings2.get(5).get("o").get("value").asText()); }
From source file:eu.vital.orchestrator.rest.EvaluationRESTService.java
@POST @Path("/prediction") public Response executePredictionScenario(JsonNode input) throws Exception { String dmsUrl = "https://local.vital-iot.eu:8443/vital-core-dms"; // 1. Get List of sensors from DMS observing AvailableBikes Client dmsClient = ClientBuilder.newClient(); WebTarget dmsTarget = dmsClient.target(dmsUrl).path("querySensor").queryParam("encodeKeys", "false"); ObjectNode sensorQuery = objectMapper.createObjectNode(); sensorQuery.put("http://purl\\u002eoclc\\u002eorg/NET/ssnx/ssn#observes.@type", "http://vital-iot.eu/ontology/ns/Speed"); ArrayNode sensorList = dmsTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(sensorQuery), ArrayNode.class); // 2. Find the nearest sensor double minDistance = Double.MAX_VALUE; JsonNode nearestSensor = null;/*from w ww . j a v a2 s.co m*/ for (int i = 0; i < sensorList.size(); i++) { JsonNode sensor = sensorList.get(i); // Calculate Distance: double tmp = distance(input.get("lat").asDouble(), input.get("lng").asDouble(), sensor.get("hasLastKnownLocation").get("geo:lat").asDouble(), sensor.get("hasLastKnownLocation").has("geo:long") ? sensor.get("hasLastKnownLocation").get("geo:long").asDouble() : sensor.get("hasLastKnownLocation").get("geo:lon").asDouble()); if (tmp < minDistance) { minDistance = tmp; nearestSensor = sensor; } } // 3. Get all observations of this sensor from DMS archive dmsTarget = dmsClient.target(dmsUrl).path("queryObservation").queryParam("encodeKeys", "false"); ObjectNode observationQuery = objectMapper.createObjectNode(); observationQuery.put("http://purl\\u002eoclc\\u002eorg/NET/ssnx/ssn#observedBy.@value", nearestSensor.get("id").asText()); observationQuery.put("http://purl\\u002eoclc\\u002eorg/NET/ssnx/ssn#observationProperty.@type", "http://vital-iot.eu/ontology/ns/Speed"); ArrayNode observationList = dmsTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(observationQuery), ArrayNode.class); // 4. Run the prediction algorithm SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < observationList.size(); i++) { JsonNode observation = observationList.get(i); double value = observation.get("ssn:observationResult").get("ssn:hasValue").get("value").asDouble(); String dateStr = observation.get("ssn:observationResultTime").get("time:inXSDDateTime").asText(); Calendar date = javax.xml.bind.DatatypeConverter.parseDateTime(dateStr); regression.addData(date.getTimeInMillis(), value); } double futureMillis = javax.xml.bind.DatatypeConverter.parseDateTime(input.get("atDate").asText()) .getTimeInMillis(); double prediction = regression.predict(futureMillis); // 5. Return the result: ObjectNode result = objectMapper.createObjectNode(); result.put("predictionValue", prediction); result.put("predictionDate", input.get("atDate").asText()); return Response.ok(result).build(); }
From source file:com.joyent.manta.client.multipart.ServerSideMultipartManagerTest.java
public void canCreateCommitRequestBody() throws IOException { MantaMultipartUploadTuple[] unsortedTuples = new MantaMultipartUploadTuple[] { new MantaMultipartUploadTuple(5, new UUID(0L, 5L)), new MantaMultipartUploadTuple(3, new UUID(0L, 3L)), new MantaMultipartUploadTuple(1, new UUID(0L, 1L)), new MantaMultipartUploadTuple(2, new UUID(0L, 2L)), new MantaMultipartUploadTuple(4, new UUID(0L, 4L)) }; Stream<MantaMultipartUploadTuple> partsStream = Arrays.stream(unsortedTuples); byte[] jsonRequest = ServerSideMultipartManager.createCommitRequestBody(partsStream).getLeft(); ObjectNode objectNode = MantaObjectMapper.INSTANCE.readValue(jsonRequest, ObjectNode.class); @SuppressWarnings("unchecked") ArrayNode partsNode = (ArrayNode) objectNode.get("parts"); // Verify that the parts are in the correct order try {/* w w w . java 2s. com*/ Assert.assertEquals(partsNode.get(0).textValue(), unsortedTuples[2].getEtag()); Assert.assertEquals(partsNode.get(1).textValue(), unsortedTuples[3].getEtag()); Assert.assertEquals(partsNode.get(2).textValue(), unsortedTuples[1].getEtag()); Assert.assertEquals(partsNode.get(3).textValue(), unsortedTuples[4].getEtag()); Assert.assertEquals(partsNode.get(4).textValue(), unsortedTuples[0].getEtag()); } catch (AssertionError e) { System.err.println(new String(jsonRequest, StandardCharsets.UTF_8)); throw e; } }
From source file:org.envirocar.server.rest.util.GeoJSON.java
protected Polygon decodePolygonCoordinates(ArrayNode coordinates) throws GeometryConverterException { if (coordinates.size() < 1) { throw new GeometryConverterException("missing polygon shell"); }/*from w w w.j ava 2s. co m*/ LinearRing shell = getGeometryFactory().createLinearRing(decodeCoordinates(toList(coordinates.get(0)))); LinearRing[] holes = new LinearRing[coordinates.size() - 1]; for (int i = 1; i < coordinates.size(); ++i) { holes[i - 1] = getGeometryFactory().createLinearRing(decodeCoordinates(toList(coordinates.get(i)))); } return getGeometryFactory().createPolygon(shell, holes); }
From source file:eu.vital.orchestrator.rest.EvaluationRESTService.java
@POST @Path("/latest") public Response executeLatestScenario(JsonNode input) throws Exception { String dmsUrl = "https://local.vital-iot.eu:8443/vital-core-dms"; // 1. Get List of sensors from DMS observing AvailableBikes Client dmsClient = ClientBuilder.newClient(); WebTarget dmsTarget = dmsClient.target(dmsUrl).path("querySensor").queryParam("encodeKeys", "false"); ObjectNode sensorQuery = objectMapper.createObjectNode(); sensorQuery.put("http://purl\\u002eoclc\\u002eorg/NET/ssnx/ssn#observes.@type", "http://vital-iot.eu/ontology/ns/Speed"); ArrayNode sensorList = dmsTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(sensorQuery), ArrayNode.class); // 2. Find the nearest sensor double minDistance = Double.MAX_VALUE; JsonNode nearestSensor = null;//from w w w .ja v a 2 s .c om for (int i = 0; i < sensorList.size(); i++) { JsonNode sensor = sensorList.get(i); // Calculate Distance: double tmp = distance(input.get("lat").asDouble(), input.get("lng").asDouble(), sensor.get("hasLastKnownLocation").get("geo:lat").asDouble(), sensor.get("hasLastKnownLocation").has("geo:long") ? sensor.get("hasLastKnownLocation").get("geo:long").asDouble() : sensor.get("hasLastKnownLocation").get("geo:lon").asDouble()); if (tmp < minDistance) { minDistance = tmp; nearestSensor = sensor; } } // 3. Find the System of the Sensor dmsTarget = dmsClient.target(dmsUrl).path("querySystem").queryParam("encodeKeys", "false"); ObjectNode systemQuery = objectMapper.createObjectNode(); systemQuery.put("http://vital-iot\\u002eeu/ontology/ns/managesSensor.@id", nearestSensor.get("id").asText()); ArrayNode systemList = dmsTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(systemQuery), ArrayNode.class); JsonNode system = systemList.get(0); // 4. Find the Observation Service of the System dmsTarget = dmsClient.target(dmsUrl).path("queryService").queryParam("encodeKeys", "false"); ObjectNode serviceAndQuery = objectMapper.createObjectNode(); ArrayNode serviceAndParameters = objectMapper.createArrayNode(); serviceAndQuery.put("$and", serviceAndParameters); ObjectNode serviceIdQuery = objectMapper.createObjectNode(); ObjectNode serviceInQuery = objectMapper.createObjectNode(); serviceInQuery.put("$in", system.get("services")); serviceIdQuery.put("@id", serviceInQuery); serviceAndParameters.add(serviceIdQuery); ObjectNode serviceTypeQuery = objectMapper.createObjectNode(); serviceTypeQuery.put("@type", "http://vital-iot.eu/ontology/ns/ObservationService"); serviceAndParameters.add(serviceTypeQuery); ArrayNode serviceList = dmsTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(serviceAndQuery), ArrayNode.class); JsonNode observationService = serviceList.get(0); // 5. Call GetObservation operation of the service String operationUrl = observationService.get("operations").get("hrest:hasAddress").asText(); Client systemClient = ClientBuilder.newClient(); WebTarget systemTarget = systemClient.target(operationUrl); ObjectNode operationInput = objectMapper.createObjectNode(); ArrayNode sensorsArray = objectMapper.createArrayNode(); sensorsArray.add(nearestSensor.get("id").asText()); operationInput.put("sensor", sensorsArray); operationInput.put("property", "http://vital-iot.eu/ontology/ns/Speed"); ArrayNode observationList = systemTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(operationInput), ArrayNode.class); JsonNode latestObservation = observationList.get(0); // 6. Parse Result and return response ObjectNode result = objectMapper.createObjectNode(); result.put("measurementValue", latestObservation.get("ssn:observationResult").get("ssn:hasValue").get("value")); result.put("measurementDate", latestObservation.get("ssn:observationResultTime").get("time:inXSDDateTime")); return Response.ok(result).build(); }
From source file:org.modeshape.web.jcr.rest.handler.RestItemHandlerImpl.java
/** * Performs a bulk deletion of items, using a single {@link javax.jcr.Session}. If any of the items cannot be deleted for whatever * reason, the entire operation fails./*from w ww .j ava2 s . com*/ * * @param request the servlet request; may not be null or unauthenticated * @param repositoryName the URL-encoded repository name * @param workspaceName the URL-encoded workspace name * @param requestContent the JSON-encoded array of the nodes to remove * @return a {@code non-null} {@link Result} * @throws javax.jcr.RepositoryException if any of the JCR operations fail * @see RestItemHandlerImpl#deleteItem(Request, String, String, String) */ @Override public void deleteItems(Request request, String repositoryName, String workspaceName, String requestContent) throws RepositoryException { ArrayNode requestArray = stringToJSONArray(requestContent); if (requestArray.size() == 0) { return; } Session session = getSession(request, repositoryName, workspaceName); TreeSet<String> pathsInOrder = new TreeSet<>(); for (int i = 0; i < requestArray.size(); i++) { pathsInOrder.add(absPath(requestArray.get(i).toString())); } List<String> pathsInOrderList = new ArrayList<>(pathsInOrder); Collections.reverse(pathsInOrderList); for (String path : pathsInOrderList) { doDelete(path, session); } session.save(); }
From source file:org.wisdom.wamp.WampControllerPubSubTest.java
/** * Use the following PUBLISH message format: [TYPE, URI, Event, ExcludeMe] * @throws RegistryException/*from w w w .j av a 2s . c o m*/ */ @Test public void testExcludeMe() throws RegistryException { clear(); json = new JsonService(); WampController controller = createWampControllerAndConnectClient(json); controller.open("id2"); clear(); // client 1 subscribes to a topic clear(); ArrayNode msg = json.newArray(); msg.add(MessageType.SUBSCRIBE.code()); msg.add("http://example.com:9001/wamp/topic"); controller.onMessage(CLIENT_ID, msg); // client 2 subscribes to the same topic clear(); msg = json.newArray(); msg.add(MessageType.SUBSCRIBE.code()); msg.add("http://example.com:9001/wamp/topic"); controller.onMessage("id2", msg); // client 2 sends an event on the topic, client 2 is a subscriber of the topic clear(); msg = json.newArray(); msg.add(MessageType.PUBLISH.code()); msg.add("http://example.com:9001/wamp/topic"); msg.add("hello"); controller.onMessage("id2", msg); // We have two messages in the queue assertThat(message.size()).isEqualTo(2); ArrayNode node = get(0); assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code()); assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic"); assertThat(node.get(2).asText()).isEqualTo("hello"); node = get(1); assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code()); assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic"); assertThat(node.get(2).asText()).isEqualTo("hello"); clear(); // Send the same message but with the ignoreMe option set to true msg = json.newArray(); msg.add(MessageType.PUBLISH.code()); msg.add("http://example.com:9001/wamp/topic"); msg.add("hello"); msg.add(true); controller.onMessage("id2", msg); // We have only one message in the queue assertThat(message.size()).isEqualTo(1); node = get(0); assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code()); assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic"); assertThat(node.get(2).asText()).isEqualTo("hello"); clear(); // Send the same message but with the ignoreMe option set to false msg = json.newArray(); msg.add(MessageType.PUBLISH.code()); msg.add("http://example.com:9001/wamp/topic"); msg.add("hello"); msg.add(false); controller.onMessage("id2", msg); assertThat(message.size()).isEqualTo(2); node = get(0); assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code()); assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic"); assertThat(node.get(2).asText()).isEqualTo("hello"); node = get(1); assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code()); assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic"); assertThat(node.get(2).asText()).isEqualTo("hello"); }