/** * Returns a new instance of {@code MessageDirection} which is based on the specified map. * * @param headers the header key-value-pairs. * @return the instance. * @throws NullPointerException if {@code headers} is {@code null}. * @throws IllegalArgumentException if {@code headers} contains a value that did not represent its appropriate Java * type or if {@code headers} did lack a mandatory header. * @throws SubjectInvalidException if {@code headers} contains an invalid value for * {@link MessageHeaderDefinition#SUBJECT}. */ static MessageHeaders of(final Map<String, String> headers) { return MessageHeadersBuilder.of(headers).build(); }
/** * Sets the timeout of the Message to build. * * @param timeoutInSeconds the seconds of the Message to time out. * @return this builder to allow method chaining. */ public MessageHeadersBuilder timeout(final long timeoutInSeconds) { return timeout(Duration.ofSeconds(timeoutInSeconds)); }
/** * Returns a new instance of {@code MessageHeadersBuilder} initialized with the the properties of the given map. * * @param headers the header map which provides the initial properties of the builder. * @return a builder for creating {@code MessageHeaders} object. * @throws NullPointerException if {@code headers} is {@code null}. * @throws IllegalArgumentException if {@code headers} contains a value that did not represent its appropriate Java * type or if {@code headers} did lack a mandatory header. * @throws SubjectInvalidException if {@code headers} contains an invalid value for * {@link MessageHeaderDefinition#SUBJECT}. */ public static MessageHeadersBuilder of(final Map<String, String> headers) { validateMandatoryHeaders(headers); return new MessageHeadersBuilder(headers); }
/** * Returns a new instance of {@code MessageHeadersBuilder} initialized with the the properties of the given {@code * jsonObject}. * * @param jsonObject the JSON object which provides the initial properties of the builder. * @return a builder for creating {@code MessageHeaders} object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws IllegalArgumentException if {@code jsonObject} contains a value that did not represent its appropriate * Java type or if {@code jsonObject} did lack a mandatory header. * @throws SubjectInvalidException if {@code jsonObject} contains an invalid value for * {@link MessageHeaderDefinition#SUBJECT}. */ public static MessageHeadersBuilder of(final JsonObject jsonObject) { return of(toMap(jsonObject)); }
private Function<ByteBuffer, MessageCommand<?, ?>> buildSendFeatureMessage(final MessageDirection direction, final RequestContext ctx, final DittoHeaders dittoHeaders, final String thingId, final String featureId, final String msgSubject, final Duration timeout) { final HttpRequest httpRequest = ctx.getRequest(); return payload -> { final ContentType contentType = httpRequest.entity() .getContentType(); final MessageHeaders headers = MessageHeaders.newBuilder(direction, thingId, normalizeSubject(msgSubject)) .featureId(featureId) .correlationId(dittoHeaders.getCorrelationId().orElse(null)) .contentType(contentType .toString()) .timeout(timeout) .timestamp(OffsetDateTime.now()) .validationUrl(httpRequest.getHeader(X_DITTO_VALIDATION_URL) .map(HttpHeader::value) .orElse(null)) .putHeaders(dittoHeaders) .build(); final MessageBuilder<Object> messageBuilder = initMessageBuilder(payload, contentType, headers); return SendFeatureMessage.of(thingId, featureId, messageBuilder.build(), enhanceHeaders(dittoHeaders)); }; }
private Function<ByteBuffer, MessageCommand<?, ?>> buildSendClaimMessage(final RequestContext ctx, final DittoHeaders dittoHeaders, final String thingId, final Duration timeout) { return payload -> { final ContentType contentType = ctx.getRequest() .entity() .getContentType(); final MessageHeaders headers = MessageHeaders.newBuilderForClaiming(thingId) .correlationId(dittoHeaders.getCorrelationId().orElse(null)) .contentType(contentType.toString()) .timeout(timeout) .timestamp(OffsetDateTime.now()) .putHeaders(dittoHeaders) .build(); final MessageBuilder<Object> messageBuilder = initMessageBuilder(payload, contentType, headers); return SendClaimMessage.of(thingId, messageBuilder.build(), enhanceHeaders(dittoHeaders)); }; }
/** * 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 timestamp of the Message to build. * * @param timestampISO8601 the timestamp of the message in IS0-8601 format. * @return this builder to allow method chaining. * @throws java.time.format.DateTimeParseException if the timestamp is not in ISO-8601 format. */ public MessageHeadersBuilder timestamp(@Nullable final CharSequence timestampISO8601) { return timestamp((null != timestampISO8601) ? OffsetDateTime.parse(timestampISO8601) : null); }
@Override default MessageHeadersBuilder toBuilder() { return MessageHeadersBuilder.of(this); }
/** * Sets the ID of the {@code Feature} from/to which the message will be sent. * * @param featureId the ID of the Feature from/to which the Message will be sent. * @return this builder to allow method chaining. * @throws IllegalArgumentException if {@code featureId} is empty. */ public MessageHeadersBuilder featureId(@Nullable final CharSequence featureId) { putCharSequence(MessageHeaderDefinition.FEATURE_ID, featureId); return myself; }
/** * Returns a new builder for {@link MessageHeaders}. * * @param direction the direction of the message. * @param thingId the thing ID of the message. * @param subject the subject of the message. * @return the builder. * @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 newHeadersBuilder(final MessageDirection direction, final CharSequence thingId, final CharSequence subject) { return MessageHeadersBuilder.newInstance(direction, thingId, subject); }
private Function<ByteBuffer, MessageCommand<?, ?>> buildSendThingMessage(final MessageDirection direction, final RequestContext ctx, final DittoHeaders dittoHeaders, final String thingId, final String msgSubject, final Duration timeout) { return payload -> { final HttpRequest httpRequest = ctx.getRequest(); final ContentType contentType = httpRequest.entity().getContentType(); final MessageHeaders headers = MessageHeaders.newBuilder(direction, thingId, normalizeSubject(msgSubject)) .correlationId(dittoHeaders.getCorrelationId().orElse(null)) .contentType(contentType.toString()) .timeout(timeout) .timestamp(OffsetDateTime.now()) .putHeaders(dittoHeaders) .build(); final MessageBuilder<Object> messageBuilder = initMessageBuilder(payload, contentType, headers); return SendThingMessage.of(thingId, messageBuilder.build(), enhanceHeaders(dittoHeaders)); }; }
/** * 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; }
/** * Returns a new builder for {@link MessageHeaders}. * * @return the builder. * @throws NullPointerException if {@code initialHeaders} is {@code null}. * @throws IllegalArgumentException if {@code initialHeaders} contains a value that did not represent its * appropriate Java type or if {@code initialHeaders} did lack a mandatory header. * @throws SubjectInvalidException if {@code initialHeaders} contains an invalid value for * {@link MessageHeaderDefinition#SUBJECT}. */ public static MessageHeadersBuilder newHeadersBuilder(final Map<String, String> initialHeaders) { return MessageHeadersBuilder.of(initialHeaders); }
/** * Sets the MIME contentType of the payload of the Message. * * @param contentType the MIME contentType of the payload of the message. * @return this builder to allow method chaining. * @throws IllegalArgumentException if {@code contentType} is empty. */ public MessageHeadersBuilder contentType(@Nullable final CharSequence contentType) { putCharSequence(DittoHeaderDefinition.CONTENT_TYPE, contentType); return myself; }
/** * Returns a new instance of {@code MessageHeaders} which is based on the specified JSON object. * * @param jsonObject the JSON object representation of message headers. * @return the instance. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws IllegalArgumentException if {@code jsonObject} contains a value that did not represent its appropriate * Java type or if {@code jsonObject} did lack a mandatory header. * @throws SubjectInvalidException if {@code jsonObject} contains an invalid value for * {@link MessageHeaderDefinition#SUBJECT}. */ static MessageHeaders of(final JsonObject jsonObject) { return MessageHeadersBuilder.of(jsonObject).build(); }
/** * 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; }
/** * 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); }
/** * 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; }