private static Adaptable handleSingleRetrieve(final ThingQueryCommand<?> command, final TopicPath.Channel channel) { final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(command.getThingId()); final CommandsTopicPathBuilder commandsTopicPathBuilder; commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = command.getClass().getSimpleName().toLowerCase(); if (!commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); command.getSelectedFields().ifPresent(payloadBuilder::withFields); return Adaptable.newBuilder(commandsTopicPathBuilder.retrieve().build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final String payload = extractPayloadAsString(message); final JsonifiableAdaptable jsonifiableAdaptable = DittoJsonException.wrapJsonRuntimeException(() -> ProtocolFactory.jsonifiableAdaptableFromJson(JsonFactory.newObject(payload)) ); final DittoHeaders mergedHeaders = mergeHeaders(message, jsonifiableAdaptable); return Optional.of( ProtocolFactory.newAdaptableBuilder(jsonifiableAdaptable).withHeaders(mergedHeaders).build()); }
/** * Returns a new {@code AdaptableBuilder} for the existing {@code existingAdaptable} and a specific * {@code overwriteTopicPath} to overwrite the one in {@code existingAdaptable}. * * @param existingAdaptable the existingAdaptable to initialize the AdaptableBuilder with. * @param overwriteTopicPath the specific {@code TopicPath} to set as overwrite. * @return the builder. */ public static AdaptableBuilder newAdaptableBuilder(final Adaptable existingAdaptable, final TopicPath overwriteTopicPath) { return ImmutableAdaptableBuilder.of(overwriteTopicPath).withPayload(existingAdaptable.getPayload()) .withHeaders(existingAdaptable.getHeaders().orElse(null)); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final String payload = extractPayloadAsString(message); final JsonifiableAdaptable jsonifiableAdaptable = DittoJsonException.wrapJsonRuntimeException(() -> ProtocolFactory.jsonifiableAdaptableFromJson(JsonFactory.newObject(payload)) ); final DittoHeaders mergedHeaders = mergeHeaders(message, jsonifiableAdaptable); return Optional.of( ProtocolFactory.newAdaptableBuilder(jsonifiableAdaptable).withHeaders(mergedHeaders).build()); }
/** * Returns a new {@code AdaptableBuilder} for the existing {@code existingAdaptable} and a specific * {@code overwriteTopicPath} to overwrite the one in {@code existingAdaptable}. * * @param existingAdaptable the existingAdaptable to initialize the AdaptableBuilder with. * @param overwriteTopicPath the specific {@code TopicPath} to set as overwrite. * @return the builder. */ public static AdaptableBuilder newAdaptableBuilder(final Adaptable existingAdaptable, final TopicPath overwriteTopicPath) { return ImmutableAdaptableBuilder.of(overwriteTopicPath).withPayload(existingAdaptable.getPayload()) .withHeaders(existingAdaptable.getHeaders().orElse(null)); }
private static Adaptable handleSingleRetrieve(final ThingQueryCommand<?> command, final TopicPath.Channel channel) { final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(command.getThingId()); final CommandsTopicPathBuilder commandsTopicPathBuilder; commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = command.getClass().getSimpleName().toLowerCase(); if (!commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); command.getSelectedFields().ifPresent(payloadBuilder::withFields); return Adaptable.newBuilder(commandsTopicPathBuilder.retrieve().build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final ExternalMessage enhancedMessage; final String correlationId; if (!message.getHeaders().containsKey(DittoHeaderDefinition.CORRELATION_ID.getKey())) { // if no correlation-id was provided in the ExternalMessage, generate one here: correlationId = UUID.randomUUID().toString(); enhancedMessage = ExternalMessageFactory.newExternalMessageBuilder(message) .withAdditionalHeaders(DittoHeaderDefinition.CORRELATION_ID.getKey(), correlationId) .build(); } else { correlationId = message.getHeaders().get(DittoHeaderDefinition.CORRELATION_ID.getKey()); enhancedMessage = message; } final Optional<Adaptable> mappedOpt = delegate.map(enhancedMessage); return mappedOpt.map(mapped -> { final DittoHeadersBuilder headersBuilder = DittoHeaders.newBuilder(); headersBuilder.correlationId(correlationId); Optional.ofNullable(message.getHeaders().get(ExternalMessage.REPLY_TO_HEADER)).ifPresent(replyTo -> headersBuilder.putHeader(ExternalMessage.REPLY_TO_HEADER, replyTo) ); final Optional<DittoHeaders> headersOpt = mapped.getHeaders(); headersOpt.ifPresent(headersBuilder::putHeaders); // overwrite with mapped headers (if any) return ProtocolFactory.newAdaptableBuilder(mapped) .withHeaders(headersBuilder.build()) .build(); }); }
private static Adaptable handleMultipleRetrieve(final RetrieveThings command, final TopicPath.Channel channel) { final String commandName = command.getClass().getSimpleName().toLowerCase(); if (!commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { throw UnknownCommandException.newBuilder(commandName).build(); } final String namespace = command.getNamespace().orElse("_"); final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilderFromNamespace(namespace); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); command.getSelectedFields().ifPresent(payloadBuilder::withFields); payloadBuilder.withValue(createIdsPayload(command.getThingIds())); return Adaptable.newBuilder(commandsTopicPathBuilder.retrieve().build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final ExternalMessage enhancedMessage; final String correlationId; if (!message.getHeaders().containsKey(DittoHeaderDefinition.CORRELATION_ID.getKey())) { // if no correlation-id was provided in the ExternalMessage, generate one here: correlationId = UUID.randomUUID().toString(); enhancedMessage = ExternalMessageFactory.newExternalMessageBuilder(message) .withAdditionalHeaders(DittoHeaderDefinition.CORRELATION_ID.getKey(), correlationId) .build(); } else { correlationId = message.getHeaders().get(DittoHeaderDefinition.CORRELATION_ID.getKey()); enhancedMessage = message; } final Optional<Adaptable> mappedOpt = delegate.map(enhancedMessage); return mappedOpt.map(mapped -> { final DittoHeadersBuilder headersBuilder = DittoHeaders.newBuilder(); headersBuilder.correlationId(correlationId); Optional.ofNullable(message.getHeaders().get(ExternalMessage.REPLY_TO_HEADER)).ifPresent(replyTo -> headersBuilder.putHeader(ExternalMessage.REPLY_TO_HEADER, replyTo) ); final Optional<DittoHeaders> headersOpt = mapped.getHeaders(); headersOpt.ifPresent(headersBuilder::putHeaders); // overwrite with mapped headers (if any) return ProtocolFactory.newAdaptableBuilder(mapped) .withHeaders(headersBuilder.build()) .build(); }); }
private static Adaptable handleMultipleRetrieve(final RetrieveThings command, final TopicPath.Channel channel) { final String commandName = command.getClass().getSimpleName().toLowerCase(); if (!commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { throw UnknownCommandException.newBuilder(commandName).build(); } final String namespace = command.getNamespace().orElse("_"); final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilderFromNamespace(namespace); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); command.getSelectedFields().ifPresent(payloadBuilder::withFields); payloadBuilder.withValue(createIdsPayload(command.getThingIds())); return Adaptable.newBuilder(commandsTopicPathBuilder.retrieve().build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Adaptable constructAdaptable(final ThingModifyCommand command, final TopicPath.Channel channel) { final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(command.getThingId()); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = command.getClass().getSimpleName().toLowerCase(); if (commandName.startsWith(TopicPath.Action.CREATE.toString())) { commandsTopicPathBuilder.create(); } else if (commandName.startsWith(TopicPath.Action.MODIFY.toString())) { commandsTopicPathBuilder.modify(); } else if (commandName.startsWith(TopicPath.Action.DELETE.toString())) { commandsTopicPathBuilder.delete(); } else { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); final Optional<JsonValue> value = command.getEntity(command.getImplementedSchemaVersion()); value.ifPresent(payloadBuilder::withValue); return Adaptable.newBuilder(commandsTopicPathBuilder.build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Adaptable constructAdaptable(final ThingModifyCommand command, final TopicPath.Channel channel) { final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(command.getThingId()); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = command.getClass().getSimpleName().toLowerCase(); if (commandName.startsWith(TopicPath.Action.CREATE.toString())) { commandsTopicPathBuilder.create(); } else if (commandName.startsWith(TopicPath.Action.MODIFY.toString())) { commandsTopicPathBuilder.modify(); } else if (commandName.startsWith(TopicPath.Action.DELETE.toString())) { commandsTopicPathBuilder.delete(); } else { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(command.getResourcePath()); final Optional<JsonValue> value = command.getEntity(command.getImplementedSchemaVersion()); value.ifPresent(payloadBuilder::withValue); return Adaptable.newBuilder(commandsTopicPathBuilder.build()) .withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(command.getDittoHeaders())) .build(); }
@Override public Adaptable constructAdaptable(final ThingModifyCommandResponse commandResponse, final TopicPath.Channel channel) { final String responseName = commandResponse.getClass().getSimpleName().toLowerCase(); if (!responseName.endsWith("response")) { throw UnknownCommandResponseException.newBuilder(responseName).build(); } final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(commandResponse.getId()); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = commandResponse.getClass().getSimpleName().toLowerCase(); if (commandName.startsWith(TopicPath.Action.CREATE.toString())) { commandsTopicPathBuilder.create(); } else if (commandName.startsWith(TopicPath.Action.MODIFY.toString())) { commandsTopicPathBuilder.modify(); } else if (commandName.startsWith(TopicPath.Action.DELETE.toString())) { commandsTopicPathBuilder.delete(); } else { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(commandResponse.getResourcePath()) // .withStatus(commandResponse.getStatusCode()); final Optional<JsonValue> value = commandResponse.getEntity(commandResponse.getImplementedSchemaVersion()); value.ifPresent(payloadBuilder::withValue); return Adaptable.newBuilder(commandsTopicPathBuilder.build()) // .withPayload(payloadBuilder.build()) // .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(commandResponse.getDittoHeaders())) // .build(); }
@Override public Adaptable constructAdaptable(final ThingModifyCommandResponse commandResponse, final TopicPath.Channel channel) { final String responseName = commandResponse.getClass().getSimpleName().toLowerCase(); if (!responseName.endsWith("response")) { throw UnknownCommandResponseException.newBuilder(responseName).build(); } final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(commandResponse.getId()); final CommandsTopicPathBuilder commandsTopicPathBuilder = fromTopicPathBuilderWithChannel(topicPathBuilder, channel); final String commandName = commandResponse.getClass().getSimpleName().toLowerCase(); if (commandName.startsWith(TopicPath.Action.CREATE.toString())) { commandsTopicPathBuilder.create(); } else if (commandName.startsWith(TopicPath.Action.MODIFY.toString())) { commandsTopicPathBuilder.modify(); } else if (commandName.startsWith(TopicPath.Action.DELETE.toString())) { commandsTopicPathBuilder.delete(); } else { throw UnknownCommandException.newBuilder(commandName).build(); } final PayloadBuilder payloadBuilder = Payload.newBuilder(commandResponse.getResourcePath()) // .withStatus(commandResponse.getStatusCode()); final Optional<JsonValue> value = commandResponse.getEntity(commandResponse.getImplementedSchemaVersion()); value.ifPresent(payloadBuilder::withValue); return Adaptable.newBuilder(commandsTopicPathBuilder.build()) // .withPayload(payloadBuilder.build()) // .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(commandResponse.getDittoHeaders())) // .build(); }
.withPayload(payload) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(commandResponse.getDittoHeaders())) .build();
.withPayload(payload) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(commandResponse.getDittoHeaders())) .build();
.withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(event.getDittoHeaders())) .build();
.withPayload(payloadBuilder.build()) .withHeaders(ProtocolFactory.newHeadersWithDittoContentType(event.getDittoHeaders())) .build();
.withPayload(payloadBuilder.build()) .withHeaders(DittoHeaders.of(externalHeaders)).build();
.withPayload(payloadBuilder.build()) .withHeaders(DittoHeaders.of(externalHeaders)).build();