/** * Creates a Action from the passed Action {@code name} if such an enum value exists, otherwise an empty * Optional. * * @param name the Action name to create the Action enum value of. * @return the optional Action. */ public static Optional<Action> forName(final String name) { return Stream.of(values()) // .filter(a -> Objects.equals(a.getName(), name)) // .findFirst(); }
private Signal<?> processCommandResponseSignalFromAdaptable(final Adaptable adaptable, final TopicPath topicPath) { final Optional<HttpStatusCode> status = adaptable.getPayload().getStatus(); final boolean isErrorResponse = status.isPresent() && status.get().toInt() >= HttpStatusCode.BAD_REQUEST.toInt(); if (TopicPath.Action.RETRIEVE.equals(topicPath.getAction().orElse(null))) { return isErrorResponse ? thingErrorResponseFromAdaptable(adaptable) : thingQueryCommandResponseAdapter.fromAdaptable(adaptable); } else { return isErrorResponse ? thingErrorResponseFromAdaptable(adaptable) : thingModifyCommandResponseAdapter.fromAdaptable(adaptable); } }
TopicPath.Action.forName(parts[5]) .orElseThrow(() -> UnknownTopicPathException.newBuilder(path).build()); return ImmutableTopicPath.of(namespace, id, group, channel, criterion, action);
TopicPath.Action.forName(parts[5]) .orElseThrow(() -> UnknownTopicPathException.newBuilder(path).build()); return ImmutableTopicPath.of(namespace, id, group, channel, criterion, action);
if (commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { commandsTopicPathBuilder.retrieve(); } else {
if (eventName.contains(TopicPath.Action.CREATED.toString())) { eventsTopicPathBuilder.created(); } else if (eventName.contains(TopicPath.Action.MODIFIED.toString())) { eventsTopicPathBuilder.modified(); } else if (eventName.contains(TopicPath.Action.DELETED.toString())) { eventsTopicPathBuilder.deleted(); } else {
if (commandName.startsWith(TopicPath.Action.RETRIEVE.toString())) { commandsTopicPathBuilder.retrieve(); } else {
@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(); }
if (eventName.contains(TopicPath.Action.CREATED.toString())) { eventsTopicPathBuilder.created(); } else if (eventName.contains(TopicPath.Action.MODIFIED.toString())) { eventsTopicPathBuilder.modified(); } else if (eventName.contains(TopicPath.Action.DELETED.toString())) { eventsTopicPathBuilder.deleted(); } else {
@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(); }
@Nullable private Signal<?> signalFromAdaptable(final Adaptable adaptable, final TopicPath topicPath) { if (TopicPath.Criterion.COMMANDS.equals(topicPath.getCriterion())) { if (adaptable.getPayload().getStatus().isPresent()) { // this was a command response: return processCommandResponseSignalFromAdaptable(adaptable, topicPath); } else if (TopicPath.Action.RETRIEVE.equals(topicPath.getAction().orElse(null))) { return thingQueryCommandAdapter.fromAdaptable(adaptable); } else { return thingModifyCommandAdapter.fromAdaptable(adaptable); } } else if (TopicPath.Criterion.EVENTS.equals(topicPath.getCriterion())) { return thingEventAdapter.fromAdaptable(adaptable); } else if (TopicPath.Criterion.ERRORS.equals(topicPath.getCriterion())) { return thingErrorResponseFromAdaptable(adaptable); } return null; }
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(); }
@Nullable private Signal<?> signalFromAdaptable(final Adaptable adaptable, final TopicPath topicPath) { if (TopicPath.Criterion.COMMANDS.equals(topicPath.getCriterion())) { if (adaptable.getPayload().getStatus().isPresent()) { // this was a command response: return processCommandResponseSignalFromAdaptable(adaptable, topicPath); } else if (TopicPath.Action.RETRIEVE.equals(topicPath.getAction().orElse(null))) { return thingQueryCommandAdapter.fromAdaptable(adaptable); } else { return thingModifyCommandAdapter.fromAdaptable(adaptable); } } else if (TopicPath.Criterion.EVENTS.equals(topicPath.getCriterion())) { return thingEventAdapter.fromAdaptable(adaptable); } else if (TopicPath.Criterion.ERRORS.equals(topicPath.getCriterion())) { return thingErrorResponseFromAdaptable(adaptable); } return null; }
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(); }
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(); }
@Test public void testReplaceActionOrSubject() { assertThat(UNDER_TEST.apply(KNOWN_TOPIC_PATH, "action|subject")).contains(KNOWN_ACTION.getName()); }
@Test public void testReplaceAction() { assertThat(UNDER_TEST.apply(KNOWN_TOPIC_PATH, "action")).contains(KNOWN_ACTION.getName()); }
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(); }