private MessageHeadersBuilder(final Map<String, String> headers) { super(headers, Arrays.asList(MessageHeaderDefinition.values()), MessageHeadersBuilder.class); }
@Nonnull @Override public String toString() { return getKey(); }
@Override protected Optional<HeaderDefinition> getSpecificDefinitionByKey(final CharSequence key) { return MessageHeaderDefinition.forKey(key); }
@SuppressWarnings({"squid:S2201", "ResultOfMethodCallIgnored"}) @Override public void validateValue(@Nullable final CharSequence value) { super.validateValue(value); try { Duration.ofSeconds(Long.parseLong(String.valueOf(value))); } catch (final NumberFormatException | DateTimeParseException e) { final String msgTemplate = "<{0}> is not a valid timeout!"; throw new IllegalArgumentException(MessageFormat.format(msgTemplate, value), e); } } },
@SuppressWarnings({"squid:S2201", "ResultOfMethodCallIgnored"}) @Override public void validateValue(@Nullable final CharSequence value) { super.validateValue(value); try { OffsetDateTime.parse(String.valueOf(value)); } catch (final DateTimeParseException e) { final String msgTemplate = "<{0}> is not a valid timestamp!"; throw new IllegalArgumentException(MessageFormat.format(msgTemplate, value), e); } } },
private static HeaderTranslator createHeaderTranslator(final ProtocolConfigReader protocolConfigReader) { final HeaderDefinition[] blacklist = protocolConfigReader.blacklist() .stream() .map(Ignored::new) .toArray(HeaderDefinition[]::new); return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values(), blacklist); } }
private static void validateMandatoryHeaders(final Map<String, String> headers) { for (final MessageHeaderDefinition mandatoryHeader : MANDATORY_HEADERS) { if (!headers.containsKey(mandatoryHeader.getKey())) { final String msgTemplate = "The headers did not contain a value for mandatory header with key <{0}>!"; throw new IllegalArgumentException(MessageFormat.format(msgTemplate, mandatoryHeader.getKey())); } } }
@SuppressWarnings({"squid:S2201", "ResultOfMethodCallIgnored"}) @Override public void validateValue(@Nullable final CharSequence value) { super.validateValue(value); HttpStatusCode.forInt(Integer.parseInt(String.valueOf(value))).orElseThrow(() -> { final String msgTemplate = "<{0}> is not a HTTP status code!"; return new IllegalArgumentException(MessageFormat.format(msgTemplate, value)); }); } },
@Override protected void validateValueType(final CharSequence key, final CharSequence value) { super.validateValueType(key, value); MessageHeaderDefinition.forKey(key).ifPresent(definition -> { if (MANDATORY_HEADERS.contains(definition)) { final String msgTemplate = "Value for mandatory header with key <{0}> cannot be overwritten!"; throw new IllegalArgumentException(MessageFormat.format(msgTemplate, key)); } definition.validateValue(value); }); }
/** * Create a default header translator for this protocol adapter. * * @return the default header translator. */ public static HeaderTranslator headerTranslator() { return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values()); }
/** * Returns a new instance of {@code MessageHeadersBuilder}. * * @param direction the direction of the message. * @param thingId the thing ID of the message. * @param subject the subject of the message. * @return the instance. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code thingId} or {@code subject} is empty. * @throws SubjectInvalidException if {@code subject} is invalid. */ public static MessageHeadersBuilder newInstance(final MessageDirection direction, final CharSequence thingId, final CharSequence subject) { checkNotNull(direction, MessageHeaderDefinition.DIRECTION.getKey()); argumentNotEmpty(thingId, MessageHeaderDefinition.THING_ID.getKey()); argumentNotEmpty(subject, MessageHeaderDefinition.SUBJECT.getKey()); final Map<String, String> initialHeaders = new HashMap<>(); initialHeaders.put(MessageHeaderDefinition.DIRECTION.getKey(), direction.toString()); initialHeaders.put(MessageHeaderDefinition.THING_ID.getKey(), thingId.toString()); initialHeaders.put(MessageHeaderDefinition.SUBJECT.getKey(), subject.toString()); return of(initialHeaders); }
@Override public void validateValue(@Nullable final CharSequence value) { super.validateValue(value); final Validator subjectValidator = IdValidator.newInstance(value, SUBJECT_REGEX); if (!subjectValidator.isValid()) { final String msgTemplate = "The subject <{0}> is invalid because it did not match the pattern <{1}>!"; throw SubjectInvalidException.newBuilder(String.valueOf(value)) .message(() -> MessageFormat.format(msgTemplate, value, SUBJECT_REGEX)) .build(); } } },
/** * Create a default header translator for this protocol adapter. * * @return the default header translator. */ public static HeaderTranslator headerTranslator() { return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values()); }
/** * Sets the timeout of the Message to build. * * @param timeout the duration of the Message to time out. * @return this builder to allow method chaining. */ public MessageHeadersBuilder timeout(@Nullable final Duration timeout) { final MessageHeaderDefinition definition = MessageHeaderDefinition.TIMEOUT; if (null != timeout) { putCharSequence(definition, String.valueOf(timeout.getSeconds())); } else { removeHeader(definition.getKey()); } return myself; }
/** * Sets the specified URL which is used for message validation. * * @param validationUrl the validation URL to be set. * @return this builder to allow method chaining. * @throws IllegalArgumentException if {@code validationUrl} is empty. */ public MessageHeadersBuilder validationUrl(@Nullable final CharSequence validationUrl) { final MessageHeaderDefinition definition = MessageHeaderDefinition.VALIDATION_URL; if (null != validationUrl) { putCharSequence(definition, validationUrl); } else { removeHeader(definition.getKey()); } return myself; }
/** * Sets the status code of the Message to build. * * @param statusCode the status code. * @return this builder to allow method chaining. */ public MessageHeadersBuilder statusCode(@Nullable final HttpStatusCode statusCode) { final MessageHeaderDefinition definition = MessageHeaderDefinition.STATUS_CODE; if (null != statusCode) { putCharSequence(definition, String.valueOf(statusCode.toInt())); } else { removeHeader(definition.getKey()); } return myself; }
/** * Sets the timestamp of the Message to build. * * @param timestamp the timestamp of the message. * @return this builder to allow method chaining. */ public MessageHeadersBuilder timestamp(@Nullable final OffsetDateTime timestamp) { final MessageHeaderDefinition definition = MessageHeaderDefinition.TIMESTAMP; if (null != timestamp) { putCharSequence(definition, timestamp.toString()); } else { removeHeader(definition.getKey()); } return myself; }
dittoHeadersBuilder.putHeader(THING_ID.getKey(), topicPath.getNamespace() + ":" + topicPath.getId()); dittoHeadersBuilder.putHeader(SUBJECT.getKey(), topicPath.getSubject().orElse("")); adaptable.getPayload().getPath().getDirection().ifPresent(direction -> dittoHeadersBuilder.putHeader(DIRECTION.getKey(), direction.name())); adaptable.getPayload().getPath().getFeatureId().ifPresent(featureId -> dittoHeadersBuilder.putHeader(FEATURE_ID.getKey(), featureId)); adaptable.getPayload().getStatus().ifPresent(statusCode -> dittoHeadersBuilder.putHeader(STATUS_CODE.getKey(), String.valueOf(statusCode.toInt()))); return dittoHeadersBuilder.build(); })
dittoHeadersBuilder.putHeader(THING_ID.getKey(), topicPath.getNamespace() + ":" + topicPath.getId()); dittoHeadersBuilder.putHeader(SUBJECT.getKey(), topicPath.getSubject().orElse("")); adaptable.getPayload().getPath().getDirection().ifPresent(direction -> dittoHeadersBuilder.putHeader(DIRECTION.getKey(), direction.name())); adaptable.getPayload().getPath().getFeatureId().ifPresent(featureId -> dittoHeadersBuilder.putHeader(FEATURE_ID.getKey(), featureId)); adaptable.getPayload().getStatus().ifPresent(statusCode -> dittoHeadersBuilder.putHeader(STATUS_CODE.getKey(), String.valueOf(statusCode.toInt()))); return dittoHeadersBuilder.build(); })