Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Add basic message classes and JsonObject utils #1

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions messaging/messaging-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>messaging</artifactId>
<groupId>uk.gov.justice.services</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>messaging-core</artifactId>

<dependencies>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.trajano.commons</groupId>
<artifactId>commons-testing</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.justice.services.messaging;

import javax.json.JsonObject;

/**
* Default implementation of an envelope.
*/
public class DefaultEnvelope implements Envelope {

private Metadata metadata;

private JsonObject payload;

private DefaultEnvelope(final Metadata metadata, final JsonObject payload) {
this.metadata = metadata;
this.payload = payload;
}

@Override
public Metadata metadata() {
return metadata;
}

@Override
public JsonObject payload() {
return payload;
}

public static Envelope envelopeFrom(final Metadata metadata, final JsonObject payload) {
return new DefaultEnvelope(metadata, payload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.justice.services.messaging;

import javax.json.JsonObject;

/**
* Interface for a messaging envelope containing metadata and a payload.
*/
public interface Envelope {

Metadata metadata();

JsonObject payload();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package uk.gov.justice.services.messaging;

import javax.json.JsonObject;
import javax.json.JsonString;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static uk.gov.justice.services.messaging.JsonObjects.getJsonString;
import static uk.gov.justice.services.messaging.JsonObjects.getLong;
import static uk.gov.justice.services.messaging.JsonObjects.getString;
import static uk.gov.justice.services.messaging.JsonObjects.getUUID;
import static uk.gov.justice.services.messaging.JsonObjects.getUUIDs;

/**
* Implementation of metadata that uses a JsonObject internally to store the metadata.
*/
public class JsonObjectMetadata implements Metadata {

private final JsonObject metadata;

private JsonObjectMetadata(final JsonObject metadata) {
this.metadata = metadata;
}

@Override
public UUID id() {
return getUUID(metadata, ID)
.orElseThrow(() -> new IllegalStateException("Missing id field"));
}

@Override
public String name() {
return getString(metadata, NAME)
.orElseThrow(() -> new IllegalStateException("Missing name field"));
}

@Override
public Optional<String> clientCorrelationId() {
return getString(metadata, CLIENT_CORRELATION);
}

@Override
public List<UUID> causation() {
return getUUIDs(metadata, CAUSATION);
}

@Override
public Optional<String> userId() {
return getString(metadata, USER_ID);
}

@Override
public Optional<String> sessionId() {
return getString(metadata, SESSION_ID);
}

@Override
public Optional<UUID> streamId() {
return getUUID(metadata, STREAM_ID);
}

@Override
public Optional<Long> version() {
return getLong(metadata, VERSION);
}

@Override
public JsonObject asJsonObject() {
return metadata;
}

/**
* Instantiate a {@link JsonObjectMetadata} object from a {@link JsonObject}.
*
* @param jsonObject the {@link JsonObject} to build the metadata from
* @return the {@link JsonObjectMetadata}
*/
public static Metadata metadataFrom(final JsonObject jsonObject) {

JsonString id = getJsonString(jsonObject, Metadata.ID)
.orElseThrow(() -> new IllegalArgumentException("Missing id field"));
UUID.fromString(id.getString());

JsonString name = getJsonString(jsonObject, Metadata.NAME)
.orElseThrow(() -> new IllegalArgumentException("Missing name field"));
if (name.getString().isEmpty()) {
throw new IllegalArgumentException("Name field cannot be empty");
}

return new JsonObjectMetadata(jsonObject);
}
}
Loading