private String encodeCorrelationId(final DittoHeaders dittoHeaders) { return JsonObject.newBuilder() .set(BATCH_ID_FIELD, batchId) .set(RANDOM_FIELD, UUID.randomUUID().toString()) .set(ORIGINAL_CORRELATION_ID, dittoHeaders.getCorrelationId().orElse(UUID.randomUUID().toString())) .build() .toString(); }
@Override @Nonnull public JsonObject toJson(@Nonnull final JsonSchemaVersion schemaVersion, @Nonnull final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); jsonObjectBuilder.set(JSON_STREAMING_TYPE, streamingType.getDistributedPubSubTopic(), predicate); jsonObjectBuilder.set(JSON_SUBSCRIBED, subscribed, predicate); return jsonObjectBuilder.build(); }
@Override public void toBinary(final Object object, final ByteBuffer buf) { if (object instanceof Jsonifiable) { final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); final DittoHeaders dittoHeaders = getDittoHeadersOrEmpty(object); jsonObjectBuilder.set(JSON_DITTO_HEADERS, dittoHeaders.toJson()); final JsonValue jsonValue; if (object instanceof Jsonifiable.WithPredicate) { final JsonSchemaVersion schemaVersion = dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST); jsonValue = ((Jsonifiable.WithPredicate) object).toJson(schemaVersion, FieldType.regularOrSpecial()); } else { jsonValue = ((Jsonifiable) object).toJson(); } jsonObjectBuilder.set(JSON_PAYLOAD, jsonValue); buf.put(UTF8_CHARSET.encode(jsonObjectBuilder.build() .toString()) ); } else { LOG.error("Could not serialize class <{}> as it does not implement <{}>!", object.getClass(), Jsonifiable.WithPredicate.class); final String error = new NotSerializableException(object.getClass().getName()).getMessage(); buf.put(UTF8_CHARSET.encode(error)); } }
@Override public void toBinary(final Object object, final ByteBuffer buf) { if (object instanceof Jsonifiable) { final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); final DittoHeaders dittoHeaders = getDittoHeadersOrEmpty(object); jsonObjectBuilder.set(JSON_DITTO_HEADERS, dittoHeaders.toJson()); final JsonValue jsonValue; if (object instanceof Jsonifiable.WithPredicate) { final JsonSchemaVersion schemaVersion = dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST); jsonValue = ((Jsonifiable.WithPredicate) object).toJson(schemaVersion, FieldType.regularOrSpecial()); } else { jsonValue = ((Jsonifiable) object).toJson(); } jsonObjectBuilder.set(JSON_PAYLOAD, jsonValue); buf.put(UTF8_CHARSET.encode(jsonObjectBuilder.build() .toString()) ); } else { LOG.error("Could not serialize class <{}> as it does not implement <{}>!", object.getClass(), Jsonifiable.WithPredicate.class); final String error = new NotSerializableException(object.getClass().getName()).getMessage(); buf.put(UTF8_CHARSET.encode(error)); } }
private JsonObject buildJsonRepresentation(final Predicate<JsonField> predicate) { final JsonObjectBuilder builder = JsonObject.newBuilder(); aggregatedResponses.forEach(cmdR -> builder.set("/" + calculateSource(cmdR), cmdR.toJson(predicate))); if (builder.isEmpty()) { return JsonFactory.nullObject(); } else { return builder.build(); } }
private static JsonObject buildJsonRepresentation(final List<CommandResponse<?>> commandResponses, final DittoHeaders dittoHeaders) { final JsonObjectBuilder builder = JsonObject.newBuilder(); commandResponses.forEach(cmdR -> builder.set("/" + calculateServiceName(cmdR) + "/" + calculateInstance(cmdR), cmdR.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST)))); if (builder.isEmpty()) { return JsonFactory.nullObject(); } else { return builder.build(); } }
private JsonObject buildJsonRepresentation(final Predicate<JsonField> predicate) { final JsonObjectBuilder builder = JsonObject.newBuilder(); aggregatedResponses.forEach(cmdR -> builder.set("/" + calculateSource(cmdR), cmdR.toJson(predicate))); if (builder.isEmpty()) { return JsonFactory.nullObject(); } else { return builder.build(); } }
log.error(throwable, "Could not determine 'ShardRegionState' for shard region <{}>", shardRegionName); return RetrieveStatisticsDetailsResponse.of(JsonObject.newBuilder() .set(shardRegionName, JsonFactory.newObject()) .build(), .collect(JsonCollectors.fieldsToObject()); final JsonObject thingNamespacesStats = JsonObject.newBuilder() .set(shardRegionName, namespaceStats) .build(); log.warning("Unexpected answer to " + "'ShardRegion.getShardRegionStateInstance()': {}", result); return RetrieveStatisticsDetailsResponse.of(JsonObject.newBuilder() .set(shardRegionName, JsonFactory.newObject()) .build(),
log.error(throwable, "Could not determine 'ShardRegionState' for shard region <{}>", shardRegionName); return RetrieveStatisticsDetailsResponse.of(JsonObject.newBuilder() .set(shardRegionName, JsonFactory.newObject()) .build(), .collect(JsonCollectors.fieldsToObject()); final JsonObject thingNamespacesStats = JsonObject.newBuilder() .set(shardRegionName, namespaceStats) .build(); log.warning("Unexpected answer to " + "'ShardRegion.getShardRegionStateInstance()': {}", result); return RetrieveStatisticsDetailsResponse.of(JsonObject.newBuilder() .set(shardRegionName, JsonFactory.newObject()) .build(),
return JsonObject.newBuilder() .set(addressString, throwable.getMessage()).build(); } else { return ((SimpleCommandResponse) o).getPayload() .map(JsonValue::asObject) .orElse(JsonObject.newBuilder().build()); .map(JsonValue::asString) .orElse("?"); final JsonObjectBuilder subBuilder = JsonObject.newBuilder(); subStatusObj.forEach(subBuilder::set); if (roleStatusBuilder.build().contains(key)) { JsonObject.newBuilder().set(role, rolesStatus).build());
@Override public JsonObject toJson() { return JsonObject.newBuilder() .set(JSON_ID, id) .set(JSON_TYPE, type) .set(JSON_MESSAGE, message) .set(JSON_DITTO_HEADERS, dittoHeaders.toJson()) .build(); }
@Override public JsonObject toJson() { return JsonObject.newBuilder() .set(JSON_ID, id) .set(JSON_TYPE, type) .set(JSON_MESSAGE, message) .set(JSON_DITTO_HEADERS, dittoHeaders.toJson()) .build(); }
private static JsonObject buildJsonRepresentation(final List<CommandResponse<?>> commandResponses, final DittoHeaders dittoHeaders) { final JsonObjectBuilder builder = JsonObject.newBuilder(); int i = 0; for (final CommandResponse<?> cmdR : commandResponses) { builder.set("/" + calculateServiceName(cmdR) + "/" + calculateInstance(cmdR, i++), cmdR.toJson(dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST))); } if (builder.isEmpty()) { return JsonFactory.nullObject(); } else { return builder.build(); } }
/** * Create a {@code Thing} with the counter {@code Feature}. Blocks until Thing has been created. * * @return a handle for the counter. */ public FeatureHandle createThingWithCounter() { final Thing thing = Thing.newBuilder() // .setId(thingId) // .setFeature(Feature.newBuilder() // .properties(JsonObject.newBuilder() // .set(COUNTER_VALUE, 0) // .build()) // .withId(COUNTER) // .build()) // .build(); FeatureHandle featureHandle = null; try { featureHandle = twin.create(thing) // .thenApply(created -> twin.forFeature(thingId, COUNTER)) // .get(TIMEOUT, TimeUnit.SECONDS); LOGGER.info("Thing with ID '{}' created.", thingId); } catch (InterruptedException | ExecutionException | TimeoutException e) { LOGGER.error(e.getMessage()); } return featureHandle; }
@Override public JsonObject toJson() { final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); jsonObjectBuilder.set(JsonFields.PATH, path.toString()); if (null != value) { jsonObjectBuilder.set(JsonFields.VALUE, value); } if (null != status) { jsonObjectBuilder.set(JsonFields.STATUS, status.toInt()); } if (null != revision) { jsonObjectBuilder.set(JsonFields.REVISION, revision); } if (null != timestamp) { jsonObjectBuilder.set(JsonFields.TIMESTAMP, timestamp.toString()); } if (null != fields) { jsonObjectBuilder.set(JsonFields.FIELDS, fields.toString()); } return jsonObjectBuilder.build(); }
@Override public JsonObject toJson() { final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); jsonObjectBuilder.set(JsonFields.PATH, path.toString()); if (null != value) { jsonObjectBuilder.set(JsonFields.VALUE, value); } if (null != status) { jsonObjectBuilder.set(JsonFields.STATUS, status.toInt()); } if (null != revision) { jsonObjectBuilder.set(JsonFields.REVISION, revision); } if (null != timestamp) { jsonObjectBuilder.set(JsonFields.TIMESTAMP, timestamp.toString()); } if (null != fields) { jsonObjectBuilder.set(JsonFields.FIELDS, fields.toString()); } return jsonObjectBuilder.build(); }
return StatusInfo.fromStatus(StatusInfo.Status.UP); } else { final JsonObject missingRolesJson = JsonObject.newBuilder() .set(JSON_KEY_MISSING_ROLES, healthClusterRolesExpected.stream() .map(JsonValue::of)
@SuppressWarnings("unchecked") private static JsonObject filterByTrie(final JsonObject self, final JsonFieldSelectorTrie trie) { if (trie.isEmpty()) { return self; } final JsonObjectBuilder builder = JsonObject.newBuilder(); for (final JsonKey key : trie.getKeys()) { self.getField(key).ifPresent(child -> { final JsonValue childValue = child.getValue(); final JsonValue filteredChildValue = childValue.isObject() ? filterByTrie(childValue.asObject(), trie.descend(key)) : childValue; final Optional<JsonFieldDefinition> childFieldDefinition = child.getDefinition(); if (childFieldDefinition.isPresent()) { builder.set(childFieldDefinition.get(), filteredChildValue); } else { builder.set(key, filteredChildValue); } }); } return builder.build(); }
private Graph<SourceShape<QueryThingsResponse>, NotUsed> retrieveThingsForIds(final ResultList<String> thingIds, final QueryThings queryThings) { final Graph<SourceShape<QueryThingsResponse>, NotUsed> result; final DittoHeaders dittoHeaders = queryThings.getDittoHeaders(); final Optional<String> correlationIdOpt = dittoHeaders.getCorrelationId(); LogUtil.enhanceLogWithCorrelationId(log, correlationIdOpt); if (thingIds.isEmpty()) { result = Source.<QueryThingsResponse>single(QueryThingsResponse.of(SearchModelFactory.emptySearchResult(), dittoHeaders)); } else { // only respond with the determined "thingIds", the lookup of the things is done in gateway: final JsonArray items = thingIds.stream() .map(JsonValue::of) .map(jsonStr -> JsonObject.newBuilder() .set(Thing.JsonFields.ID.getPointer(), jsonStr) .build() ) .collect(JsonCollectors.valuesToArray()); final SearchResult searchResult = SearchModelFactory.newSearchResult(items, thingIds.nextPageOffset()); result = Source.<QueryThingsResponse>single(QueryThingsResponse.of(searchResult, dittoHeaders)); } return result; }
private Route handleSudoCountThingsPerRequest(final RequestContext ctx, final SudoCountThings command) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); Source.single(command) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); final CompletionStage<HttpResponse> allThingsCountHttpResponse = Source.fromCompletionStage(httpResponseFuture) .flatMapConcat(httpResponse -> httpResponse.entity().getDataBytes()) .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(Integer::valueOf) .map(count -> JsonObject.newBuilder().set("allThingsCount", count).build()) .map(jsonObject -> HttpResponse.create() .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(jsonObject.toString())) .withStatus(HttpStatusCode.OK.toInt())) .runWith(Sink.head(), materializer); return completeWithFuture(allThingsCountHttpResponse); }