switch (placeholder) { case NAMESPACE_PLACEHOLDER: return Optional.of(topicPath.getNamespace()); case ENTITYID_PLACEHOLDER: return Optional.of(topicPath.getId()); case GROUP_PLACEHOLDER: return Optional.of(topicPath.getGroup().getName()); case CHANNEL_PLACEHOLDER: return Optional.of(topicPath.getChannel().getName()); case CRITERION_PLACEHOLDER: return Optional.of(topicPath.getCriterion().getName()); case ACTION_PLACEHOLDER: return topicPath.getAction().map(TopicPath.Action::getName); case SUBJECT_PLACEHOLDER: return topicPath.getSubject(); case ACTION_OR_SUBJECT_PLACEHOLDER: topicPath.getSubject() .orElseGet(() -> topicPath.getAction().map(TopicPath.Action::getName).orElse(null) return Optional.of(topicPath.getPath()); default: return Optional.empty();
@Override protected String getType(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); if (topicPath.isWildcardTopic()) { return RetrieveThings.TYPE; } else { final JsonPointer path = adaptable.getPayload().getPath(); final String commandName = getAction(topicPath) + upperCaseFirst(PathMatcher.match(path)); return topicPath.getGroup() + "." + topicPath.getCriterion() + ":" + commandName; } }
protected static String thingIdFrom(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); return topicPath.getNamespace() + ":" + topicPath.getId(); }
@Override protected String getType(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); final JsonPointer path = adaptable.getPayload().getPath(); final String eventName = PathMatcher.match(path) + getActionNameWithFirstLetterUpperCase(topicPath); return topicPath.getGroup() + "." + topicPath.getCriterion() + ":" + eventName; }
topicPath.getNamespace() + ":" + topicPath.getId()); dittoHeadersBuilder.putHeader(SUBJECT.getKey(), topicPath.getSubject().orElse("")); adaptable.getPayload().getPath().getDirection().ifPresent(direction -> dittoHeadersBuilder.putHeader(DIRECTION.getKey(), direction.name()));
@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; }
/** * A mutable builder for a {@code UnknownTopicPathException}. * * @param topicPath the topic path of the adaptable not supported. * @return the builder. */ public static Builder newBuilder(final TopicPath topicPath) { return new Builder(topicPath.getPath()); }
@Override public Optional<ExternalMessage> map(final Adaptable adaptable) { final Map<String, String> headers = new LinkedHashMap<>(adaptable.getHeaders().orElse(DittoHeaders.empty())); final String jsonString = ProtocolFactory.wrapAsJsonifiableAdaptable(adaptable).toJsonString(); final boolean isError = TopicPath.Criterion.ERRORS.equals(adaptable.getTopicPath().getCriterion()); final boolean isResponse = adaptable.getPayload().getStatus().isPresent(); return Optional.of( ExternalMessageFactory.newExternalMessageBuilder(headers) .withTopicPath(adaptable.getTopicPath()) .withText(jsonString) .asResponse(isResponse) .asError(isError) .build()); }
@Override public Signal<?> fromAdaptable(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); if (TopicPath.Group.THINGS.equals(topicPath.getGroup())) { // /things final TopicPath.Channel channel = topicPath.getChannel(); if (channel.equals(TopicPath.Channel.LIVE)) { // /things/live return fromLiveAdaptable(adaptable); } else if (channel.equals(TopicPath.Channel.TWIN)) { // /things/twin return fromTwinAdaptable(adaptable); } } throw UnknownTopicPathException.newBuilder(topicPath).build(); }
protected static TopicPath.Action getAction(final TopicPath topicPath) { return topicPath.getAction() .orElseThrow(() -> new NullPointerException("TopicPath did not contain an Action!")); }
@Override protected String getType(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); if (topicPath.isWildcardTopic()) { return RetrieveThingsResponse.TYPE; } else { final JsonPointer path = adaptable.getPayload().getPath(); final String commandName = getAction(topicPath) + upperCaseFirst(PathMatcher.match(path)); return topicPath.getGroup() + ".responses:" + commandName; } }
@Override protected String getType(final Adaptable adaptable) { if (adaptable.getTopicPath().getSubject().filter(KnownMessageSubjects.CLAIM_SUBJECT::equals).isPresent()) { return SendClaimMessageResponse.TYPE; } else if (!adaptable.getHeaders().map(DittoHeaders::isResponseRequired).orElse(true)) { return SendMessageAcceptedResponse.TYPE; } else if (adaptable.getPayload().getPath().getFeatureId().isPresent()) { return SendFeatureMessageResponse.TYPE; } else { return SendThingMessageResponse.TYPE; } }
protected static String namespaceFrom(final Adaptable adaptable) { final String namespace = adaptable.getTopicPath().getNamespace(); return "_".equals(namespace) ? null : namespace; }
@Override protected String getType(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); final JsonPointer path = adaptable.getPayload().getPath(); final String commandName = getAction(topicPath) + upperCaseFirst(PathMatcher.match(path)); return topicPath.getGroup() + ".responses:" + commandName; }
/** * Add any extra information in topic path as Ditto headers. Currently "channel" is the only relevant header. * * @param topicPath the topic path to extract information from. * @return headers containing extra information from topic path. */ private static DittoHeaders mapTopicPathToHeaders(final TopicPath topicPath) { if (topicPath.getChannel() == TopicPath.Channel.LIVE) { return DittoHeaders.newBuilder() .channel(TopicPath.Channel.LIVE.getName()) .build(); } else { return DittoHeaders.empty(); } }
default boolean isWildcardTopic() { return ID_PLACEHOLDER.equals(getId()); }
@Override protected String getType(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); final JsonPointer path = adaptable.getPayload().getPath(); final String eventName = PathMatcher.match(path) + getActionNameWithFirstLetterUpperCase(topicPath); return topicPath.getGroup() + "." + topicPath.getCriterion() + ":" + eventName; }
topicPath.getNamespace() + ":" + topicPath.getId()); dittoHeadersBuilder.putHeader(SUBJECT.getKey(), topicPath.getSubject().orElse("")); adaptable.getPayload().getPath().getDirection().ifPresent(direction -> dittoHeadersBuilder.putHeader(DIRECTION.getKey(), direction.name()));
protected static String thingIdFrom(final Adaptable adaptable) { final TopicPath topicPath = adaptable.getTopicPath(); return topicPath.getNamespace() + ":" + topicPath.getId(); }
@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; }