use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.
the class LoggerRequestDataFilter method mergeHeadersWithPayloadMetadataIfPresent.
private Optional<JsonObject> mergeHeadersWithPayloadMetadataIfPresent(final ContainerRequestContext requestContext, final MultivaluedMap<String, String> headers) throws IOException {
final Optional<Metadata> payloadMetadata = payloadMetadataIfPresent(requestContext);
final Optional<String> id = idFromHeaderOrMetadataIfPresent(headers, payloadMetadata);
final Optional<String> name = nameFromHeaderOrMetadataIfPresent(headers, payloadMetadata);
final Optional<String> correlationId = correlationIdFromHeaderOrMetadataIfPresent(headers, payloadMetadata);
final Optional<String> sessionId = sessionIdFromHeaderOrMetadataIfPresent(headers, payloadMetadata);
final Optional<String> userId = userIdFromHeaderOrMetadataIfPresent(headers, payloadMetadata);
if (id.isPresent() || name.isPresent() || correlationId.isPresent() || sessionId.isPresent() || userId.isPresent()) {
final JsonObjectBuilder metadataBuilder = createObjectBuilder();
id.ifPresent(value -> metadataBuilder.add("id", value));
name.ifPresent(value -> metadataBuilder.add("name", value));
correlationId.ifPresent(value -> metadataBuilder.add("correlation", createObjectBuilder().add("client", value)));
if (sessionId.isPresent() || userId.isPresent()) {
final JsonObjectBuilder contextBuilder = createObjectBuilder();
sessionId.ifPresent(value -> contextBuilder.add("session", value));
userId.ifPresent(value -> contextBuilder.add("user", value));
metadataBuilder.add("context", contextBuilder);
}
return Optional.of(metadataBuilder.build());
}
return Optional.empty();
}
use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.
the class RestEnvelopeBuilder method buildMetadata.
private Metadata buildMetadata() {
final Optional<Metadata> payloadMetadata = metadataFromPayloadIfPresent();
final MetadataBuilder metadataBuilder = payloadMetadata.map(JsonEnvelope::metadataFrom).orElse(JsonEnvelope.metadataBuilder().withId(id).withName(this.action));
return mergeHeadersIntoMetadata(payloadMetadata, metadataBuilder).build();
}
use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.
the class RestEnvelopeBuilder method mergeHeadersIntoMetadata.
private MetadataBuilder mergeHeadersIntoMetadata(final Optional<Metadata> metadata, final MetadataBuilder metadataBuilder) {
final Optional<String> correlationId;
final Optional<String> sessionId;
final Optional<String> userId;
final List<UUID> causation;
if (metadata.isPresent()) {
final Metadata metadataValue = metadata.get();
correlationId = metadataValue.clientCorrelationId();
sessionId = metadataValue.sessionId();
userId = metadataValue.userId();
causation = metadataValue.causation();
} else {
correlationId = Optional.empty();
sessionId = Optional.empty();
userId = Optional.empty();
causation = emptyList();
}
final boolean requestHeadersArePresent = headers.isPresent() && headers.get().getRequestHeaders() != null;
if (requestHeadersArePresent) {
final HttpHeaders httpHeaders = this.headers.get();
setMetaDataIfNotSet(correlationId, httpHeaders.getHeaderString(CLIENT_CORRELATION_ID), metadataBuilder::withClientCorrelationId, "Client Correlation Id");
setMetaDataIfNotSet(userId, httpHeaders.getHeaderString(USER_ID), metadataBuilder::withUserId, "User Id");
setMetaDataIfNotSet(sessionId, httpHeaders.getHeaderString(SESSION_ID), metadataBuilder::withSessionId, "Session Id");
setCausationMetaDataIfNotSet(causation, httpHeaders.getHeaderString(CAUSATION), metadataBuilder::withCausation);
}
return metadataBuilder;
}
use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.
the class RestEnvelopeBuilderTest method shouldNotFailIfClientCorrelationIdIsSetInBothHeaderAndPayloadAndAreEqual.
@Test
public void shouldNotFailIfClientCorrelationIdIsSetInBothHeaderAndPayloadAndAreEqual() throws Exception {
final Optional<JsonObject> initialPayload = Optional.of(createObjectBuilder().add(METADATA, createObjectBuilder().add(ID, randomUUID().toString()).add(NAME, PAYLOAD_NAME).add(CORRELATION, createObjectBuilder().add(CLIENT_ID, UUID_CLIENT_CORRELATION_ID.toString()))).build());
final JsonEnvelope envelope = builderWithDefaultAction().withInitialPayload(initialPayload).withHeaders(httpHeadersOf(ImmutableMap.of(CLIENT_CORRELATION_ID, UUID_CLIENT_CORRELATION_ID.toString()))).build();
final Metadata metadata = envelope.metadata();
assertThat(metadata.clientCorrelationId(), is(Optional.of(UUID_CLIENT_CORRELATION_ID.toString())));
}
use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.
the class RestEnvelopeBuilderTest method shouldPassNonFrameworkMetadataThroughIntoJsonEnvelope.
@Test
public void shouldPassNonFrameworkMetadataThroughIntoJsonEnvelope() throws Exception {
final String external_1 = "External_1";
final String external_2 = "External_2";
final String value_1 = "external value 1";
final String value_2 = "external value 2";
final Optional<JsonObject> initialPayload = Optional.of(createObjectBuilder().add(METADATA, createObjectBuilder().add(ID, PAYLOAD_METADATA_ID.toString()).add(NAME, PAYLOAD_NAME).add(STREAM, createObjectBuilder().add(STREAM_ID, UUID_STREAM_ID.toString())).add(external_1, value_1).add(external_2, value_2)).add("test", "value").build());
final JsonEnvelope envelope = builderWithDefaultAction().withInitialPayload(initialPayload).build();
final Metadata metadata = envelope.metadata();
final JsonObject metadataJson = metadata.asJsonObject();
assertThat(metadataJson.getString(external_1), is(value_1));
assertThat(metadataJson.getString(external_2), is(value_2));
assertThat(metadata.id(), is(PAYLOAD_METADATA_ID));
assertThat(metadata.name(), is(PAYLOAD_NAME));
assertThat(metadata.streamId(), is(Optional.of(UUID_STREAM_ID)));
}
Aggregations