Search in sources :

Example 61 with Metadata

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();
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 62 with Metadata

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();
}
Also used : MetadataBuilder(uk.gov.justice.services.messaging.MetadataBuilder) Metadata(uk.gov.justice.services.messaging.Metadata)

Example 63 with Metadata

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;
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) Metadata(uk.gov.justice.services.messaging.Metadata) UUID(java.util.UUID)

Example 64 with Metadata

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())));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObject(javax.json.JsonObject) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 65 with Metadata

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)));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObject(javax.json.JsonObject) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) JsonObjects.getString(uk.gov.justice.services.messaging.JsonObjects.getString) Test(org.junit.Test)

Aggregations

Metadata (uk.gov.justice.services.messaging.Metadata)67 Test (org.junit.Test)58 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)19 UUID (java.util.UUID)17 UUID.randomUUID (java.util.UUID.randomUUID)11 JsonObject (javax.json.JsonObject)10 JsonObjects.getJsonObject (uk.gov.justice.services.messaging.JsonObjects.getJsonObject)8 MetadataBuilder (uk.gov.justice.services.messaging.MetadataBuilder)6 JsonObjectBuilder (javax.json.JsonObjectBuilder)5 JsonValue (javax.json.JsonValue)3 JsonObjects.getString (uk.gov.justice.services.messaging.JsonObjects.getString)3 Json.createObjectBuilder (javax.json.Json.createObjectBuilder)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 UtcClock (uk.gov.justice.services.common.util.UtcClock)2 Interceptor (uk.gov.justice.services.core.interceptor.Interceptor)2 InterceptorChain (uk.gov.justice.services.core.interceptor.InterceptorChain)2 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)2 JsonEnvelope.metadataBuilder (uk.gov.justice.services.messaging.JsonEnvelope.metadataBuilder)2 EqualsTester (com.google.common.testing.EqualsTester)1 String.format (java.lang.String.format)1