This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CJSCommonPlatform/messaging-classes
Add basic message classes and JsonObject utils
- Loading branch information
Showing
11 changed files
with
982 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
32 changes: 32 additions & 0 deletions
32
...aging/messaging-core/src/main/java/uk/gov/justice/services/messaging/DefaultEnvelope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
messaging/messaging-core/src/main/java/uk/gov/justice/services/messaging/Envelope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
93 changes: 93 additions & 0 deletions
93
...ng/messaging-core/src/main/java/uk/gov/justice/services/messaging/JsonObjectMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.