From 55f6cf4a3ec2f9d0fcbab31cbc11a8554ca83698 Mon Sep 17 00:00:00 2001 From: Markus Sabadello Date: Wed, 8 Jan 2025 14:06:51 +0100 Subject: [PATCH] feat: Support for new media types --- .../RepresentationSpecificEntries.java | 31 ----------- .../did/representations/Representations.java | 55 +++++++++---------- .../AbstractRepresentationConsumer.java | 27 --------- .../consumption/RepresentationConsumer.java | 11 +--- .../RepresentationConsumerDID.java | 30 ++++++++++ ...ava => RepresentationConsumerDIDCBOR.java} | 17 +++--- .../RepresentationConsumerDIDJSON.java | 31 +++++++++++ .../RepresentationConsumerDIDJSONLD.java | 30 ++++++++++ .../RepresentationConsumerJSON.java | 28 ---------- .../RepresentationConsumerJSONLD.java | 28 ---------- .../production/RepresentationProducer.java | 13 ++--- .../RepresentationProducerCBOR.java | 33 ----------- .../production/RepresentationProducerDID.java | 28 ++++++++++ .../RepresentationProducerDIDCBOR.java | 31 +++++++++++ .../RepresentationProducerDIDJSON.java | 28 ++++++++++ .../RepresentationProducerDIDJSONLD.java | 29 ++++++++++ .../RepresentationProducerJSON.java | 30 ---------- .../RepresentationProducerJSONLD.java | 30 ---------- 18 files changed, 250 insertions(+), 260 deletions(-) delete mode 100644 src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java create mode 100644 src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDID.java rename src/main/java/foundation/identity/did/representations/consumption/{RepresentationConsumerCBOR.java => RepresentationConsumerDIDCBOR.java} (50%) create mode 100644 src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSON.java create mode 100644 src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSONLD.java delete mode 100644 src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSON.java delete mode 100644 src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSONLD.java delete mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java create mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerDID.java create mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDCBOR.java create mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSON.java create mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSONLD.java delete mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSON.java delete mode 100644 src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSONLD.java diff --git a/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java b/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java deleted file mode 100644 index a6bb261..0000000 --- a/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java +++ /dev/null @@ -1,31 +0,0 @@ -package foundation.identity.did.representations; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class RepresentationSpecificEntries { - - public static final List JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of( - "@context" - ); - - public static final List JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of(); - - public static final List CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of(); - - public static final Map> REPRESENTATION_SPECIFIC_ENTRY_NAMES = new HashMap<>(); - public static final List ALL_REPRESENTATION_SPECIFIC_ENTRY_NAMES = new ArrayList<>(); - - static { - - REPRESENTATION_SPECIFIC_ENTRY_NAMES.put(Representations.MEDIA_TYPE_JSONLD, JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES); - REPRESENTATION_SPECIFIC_ENTRY_NAMES.put(Representations.MEDIA_TYPE_JSON, JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES); - REPRESENTATION_SPECIFIC_ENTRY_NAMES.put(Representations.MEDIA_TYPE_CBOR, CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES); - - for (Map.Entry> entry : REPRESENTATION_SPECIFIC_ENTRY_NAMES.entrySet()) { - ALL_REPRESENTATION_SPECIFIC_ENTRY_NAMES.addAll(entry.getValue()); - } - } -} diff --git a/src/main/java/foundation/identity/did/representations/Representations.java b/src/main/java/foundation/identity/did/representations/Representations.java index d6f32f6..5602c67 100644 --- a/src/main/java/foundation/identity/did/representations/Representations.java +++ b/src/main/java/foundation/identity/did/representations/Representations.java @@ -1,54 +1,51 @@ package foundation.identity.did.representations; -import foundation.identity.did.representations.consumption.RepresentationConsumer; -import foundation.identity.did.representations.consumption.RepresentationConsumerCBOR; -import foundation.identity.did.representations.consumption.RepresentationConsumerJSON; -import foundation.identity.did.representations.consumption.RepresentationConsumerJSONLD; -import foundation.identity.did.representations.production.RepresentationProducer; -import foundation.identity.did.representations.production.RepresentationProducerCBOR; -import foundation.identity.did.representations.production.RepresentationProducerJSON; -import foundation.identity.did.representations.production.RepresentationProducerJSONLD; +import foundation.identity.did.representations.consumption.*; +import foundation.identity.did.representations.production.*; import java.util.Arrays; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Representations { - public static final String MEDIA_TYPE_JSONLD = "application/did+ld+json"; - public static final String MEDIA_TYPE_JSON = "application/did+json"; - public static final String MEDIA_TYPE_CBOR = "application/did+cbor"; + public static final String DEFAULT_MEDIA_TYPE = RepresentationProducerDID.MEDIA_TYPE; - public static final String DEFAULT_MEDIA_TYPE = MEDIA_TYPE_JSONLD; + public static final List representationProducers = Arrays.asList( + RepresentationProducerDID.getInstance(), + RepresentationProducerDIDJSONLD.getInstance(), + RepresentationProducerDIDJSON.getInstance(), + RepresentationProducerDIDCBOR.getInstance() + ); - public static final List MEDIA_TYPES = Arrays.asList( - MEDIA_TYPE_JSONLD, - MEDIA_TYPE_JSON, - MEDIA_TYPE_CBOR + public static final List representationConsumers = Arrays.asList( + RepresentationConsumerDID.getInstance(), + RepresentationConsumerDIDJSONLD.getInstance(), + RepresentationConsumerDIDJSON.getInstance(), + RepresentationConsumerDIDCBOR.getInstance() ); - public static final Map didDocumentProducers = new HashMap<>(); - public static final Map didDocumentConsumers = new HashMap<>(); + public static final Map representationProducersByMediaType = new LinkedHashMap<>(); + public static final Map representationConsumersByMediaType = new LinkedHashMap<>(); static { + representationProducers.forEach(x -> representationProducersByMediaType.put(x.getMediaType(), x)); + representationConsumers.forEach(x -> representationConsumersByMediaType.put(x.getMediaType(), x)); + } - didDocumentProducers.put(RepresentationProducerJSONLD.getInstance().getMediaType(), RepresentationProducerJSONLD.getInstance()); - didDocumentProducers.put(RepresentationProducerJSON.getInstance().getMediaType(), RepresentationProducerJSON.getInstance()); - didDocumentProducers.put(RepresentationProducerCBOR.getInstance().getMediaType(), RepresentationProducerCBOR.getInstance()); - didDocumentConsumers.put(RepresentationConsumerJSONLD.getInstance().getMediaType(), RepresentationConsumerJSONLD.getInstance()); - didDocumentConsumers.put(RepresentationConsumerJSON.getInstance().getMediaType(), RepresentationConsumerJSON.getInstance()); - didDocumentConsumers.put(RepresentationConsumerCBOR.getInstance().getMediaType(), RepresentationConsumerCBOR.getInstance()); + public static boolean isProducibleMediaType(String mediaType) { + return representationProducersByMediaType.containsKey(mediaType); } - public static boolean isRepresentationMediaType(String mediaType) { - return MEDIA_TYPES.contains(mediaType); + public static boolean isConsumableMediaType(String mediaType) { + return representationConsumersByMediaType.containsKey(mediaType); } public static RepresentationProducer getProducer(String mediaType) { - return didDocumentProducers.get(mediaType); + return representationProducersByMediaType.get(mediaType); } public static RepresentationConsumer getConsumer(String mediaType) { - return didDocumentConsumers.get(mediaType); + return representationConsumersByMediaType.get(mediaType); } } diff --git a/src/main/java/foundation/identity/did/representations/consumption/AbstractRepresentationConsumer.java b/src/main/java/foundation/identity/did/representations/consumption/AbstractRepresentationConsumer.java index 7b4b273..4a229e2 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/AbstractRepresentationConsumer.java +++ b/src/main/java/foundation/identity/did/representations/consumption/AbstractRepresentationConsumer.java @@ -1,11 +1,5 @@ package foundation.identity.did.representations.consumption; -import foundation.identity.did.representations.RepresentationSpecificEntries; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - public abstract class AbstractRepresentationConsumer implements RepresentationConsumer { private final String mediaType; @@ -18,25 +12,4 @@ public AbstractRepresentationConsumer(String mediaType) { public String getMediaType() { return this.mediaType; } - - public RepresentationConsumer.Result detectRepresentationSpecificEntries(Map map) { - - Map didDocument = new HashMap<>(map); - Map> representationSpecificEntries = new HashMap<>(); - for (String mediaType : RepresentationSpecificEntries.REPRESENTATION_SPECIFIC_ENTRY_NAMES.keySet()) { - representationSpecificEntries.put(mediaType, new HashMap<>()); - } - for (Map.Entry> representationSpecificEntryNames : RepresentationSpecificEntries.REPRESENTATION_SPECIFIC_ENTRY_NAMES.entrySet()) { - String mediaType = representationSpecificEntryNames.getKey(); - for (String representationSpecificEntryName : representationSpecificEntryNames.getValue()) { - if (didDocument.containsKey(representationSpecificEntryName)) { - Object representationSpecificEntryValue = didDocument.remove(representationSpecificEntryName); - Map representationSpecificMap = representationSpecificEntries.get(mediaType); - representationSpecificMap.put(representationSpecificEntryName, representationSpecificEntryValue); - } - } - } - - return new RepresentationConsumer.Result(didDocument, representationSpecificEntries); - } } diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java index 4b728ee..e34ec51 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java @@ -1,18 +1,13 @@ package foundation.identity.did.representations.consumption; +import foundation.identity.did.DIDDocument; import foundation.identity.did.representations.Representations; import java.io.IOException; -import java.util.Map; public interface RepresentationConsumer { - public record Result(Map didDocument, - Map> representationSpecificEntries) { - - } - - public static Result consume(byte[] representation, String mediaType) throws IOException { + public static DIDDocument consume(byte[] representation, String mediaType) throws IOException { RepresentationConsumer didDocumentConsumer = Representations.getConsumer(mediaType); if (didDocumentConsumer == null) throw new IllegalArgumentException("No consumer for media type " + mediaType); @@ -20,5 +15,5 @@ public static Result consume(byte[] representation, String mediaType) throws IOE } public String getMediaType(); - public Result consume(byte[] representation) throws IOException; + public DIDDocument consume(byte[] representation) throws IOException; } diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDID.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDID.java new file mode 100644 index 0000000..f77ba93 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDID.java @@ -0,0 +1,30 @@ +package foundation.identity.did.representations.consumption; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +public class RepresentationConsumerDID extends AbstractRepresentationConsumer implements RepresentationConsumer { + + public static final String MEDIA_TYPE = "application/did"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationConsumerDID instance = new RepresentationConsumerDID(); + + public static RepresentationConsumerDID getInstance() { + return instance; + } + + private RepresentationConsumerDID() { + super(MEDIA_TYPE); + } + + @Override + public DIDDocument consume(byte[] representation) throws IOException { + Map map = objectMapper.readValue(representation, LinkedHashMap.class); + return DIDDocument.fromMap(map); + } +} diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDCBOR.java similarity index 50% rename from src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java rename to src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDCBOR.java index 3d49531..d83f675 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDCBOR.java @@ -2,29 +2,32 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.upokecenter.cbor.CBORObject; +import foundation.identity.did.DIDDocument; import foundation.identity.did.representations.Representations; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; -public class RepresentationConsumerCBOR extends AbstractRepresentationConsumer implements RepresentationConsumer { +public class RepresentationConsumerDIDCBOR extends AbstractRepresentationConsumer implements RepresentationConsumer { + + public static final String MEDIA_TYPE = "application/did+cbor"; private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationConsumerCBOR instance = new RepresentationConsumerCBOR(); + private static final RepresentationConsumerDIDCBOR instance = new RepresentationConsumerDIDCBOR(); - public static RepresentationConsumerCBOR getInstance() { + public static RepresentationConsumerDIDCBOR getInstance() { return instance; } - private RepresentationConsumerCBOR() { - super(Representations.MEDIA_TYPE_CBOR); + private RepresentationConsumerDIDCBOR() { + super(MEDIA_TYPE); } @Override - public RepresentationConsumer.Result consume(byte[] representation) throws IOException { + public DIDDocument consume(byte[] representation) throws IOException { CBORObject cborObject = CBORObject.DecodeFromBytes(representation); Map map = cborObject.ToObject(LinkedHashMap.class); - return this.detectRepresentationSpecificEntries(map); + return DIDDocument.fromMap(map); } } diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSON.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSON.java new file mode 100644 index 0000000..8051c43 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSON.java @@ -0,0 +1,31 @@ +package foundation.identity.did.representations.consumption; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; +import foundation.identity.did.representations.Representations; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +public class RepresentationConsumerDIDJSON extends AbstractRepresentationConsumer implements RepresentationConsumer { + + public static final String MEDIA_TYPE = "application/did+json"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationConsumerDIDJSON instance = new RepresentationConsumerDIDJSON(); + + public static RepresentationConsumerDIDJSON getInstance() { + return instance; + } + + private RepresentationConsumerDIDJSON() { + super(MEDIA_TYPE); + } + + @Override + public DIDDocument consume(byte[] representation) throws IOException { + Map map = objectMapper.readValue(representation, LinkedHashMap.class); + return DIDDocument.fromMap(map); + } +} diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSONLD.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSONLD.java new file mode 100644 index 0000000..ec02738 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSONLD.java @@ -0,0 +1,30 @@ +package foundation.identity.did.representations.consumption; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +public class RepresentationConsumerDIDJSONLD extends AbstractRepresentationConsumer implements RepresentationConsumer { + + public static final String MEDIA_TYPE = "application/did+ld+json"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationConsumerDIDJSONLD instance = new RepresentationConsumerDIDJSONLD(); + + public static RepresentationConsumerDIDJSONLD getInstance() { + return instance; + } + + private RepresentationConsumerDIDJSONLD() { + super(MEDIA_TYPE); + } + + @Override + public DIDDocument consume(byte[] representation) throws IOException { + Map map = objectMapper.readValue(representation, LinkedHashMap.class); + return DIDDocument.fromMap(map); + } +} diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSON.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSON.java deleted file mode 100644 index 1a28811..0000000 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSON.java +++ /dev/null @@ -1,28 +0,0 @@ -package foundation.identity.did.representations.consumption; - -import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.did.representations.Representations; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; - -public class RepresentationConsumerJSON extends AbstractRepresentationConsumer implements RepresentationConsumer { - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationConsumerJSON instance = new RepresentationConsumerJSON(); - - public static RepresentationConsumerJSON getInstance() { - return instance; - } - - private RepresentationConsumerJSON() { - super(Representations.MEDIA_TYPE_JSON); - } - - @Override - public RepresentationConsumer.Result consume(byte[] representation) throws IOException { - Map map = objectMapper.readValue(representation, LinkedHashMap.class); - return this.detectRepresentationSpecificEntries(map); - } -} diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSONLD.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSONLD.java deleted file mode 100644 index af22fae..0000000 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerJSONLD.java +++ /dev/null @@ -1,28 +0,0 @@ -package foundation.identity.did.representations.consumption; - -import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.did.representations.Representations; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; - -public class RepresentationConsumerJSONLD extends AbstractRepresentationConsumer implements RepresentationConsumer { - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationConsumerJSONLD instance = new RepresentationConsumerJSONLD(); - - public static RepresentationConsumerJSONLD getInstance() { - return instance; - } - - private RepresentationConsumerJSONLD() { - super(Representations.MEDIA_TYPE_JSONLD); - } - - @Override - public RepresentationConsumer.Result consume(byte[] representation) throws IOException { - Map map = objectMapper.readValue(representation, LinkedHashMap.class); - return this.detectRepresentationSpecificEntries(map); - } -} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java index 99b5cf3..2097a95 100644 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java @@ -1,23 +1,18 @@ package foundation.identity.did.representations.production; +import foundation.identity.did.DIDDocument; import foundation.identity.did.representations.Representations; import java.io.IOException; -import java.util.Map; public interface RepresentationProducer { - public record Result(String mediaType, byte[] representation) { - - } - - public static RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries, String mediaType) throws IOException { - + public static byte[] produce(DIDDocument didDocument, String mediaType) throws IOException { RepresentationProducer didDocumentConsumer = Representations.getProducer(mediaType); if (didDocumentConsumer == null) throw new IllegalArgumentException("No producer for media type " + mediaType); - return didDocumentConsumer.produce(didDocument, representationSpecificEntries); + return didDocumentConsumer.produce(didDocument); } public String getMediaType(); - public Result produce(Map didDocument, Map representationSpecificEntries) throws IOException; + public byte[] produce(DIDDocument didDocument) throws IOException; } diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java deleted file mode 100644 index da86c43..0000000 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java +++ /dev/null @@ -1,33 +0,0 @@ -package foundation.identity.did.representations.production; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.upokecenter.cbor.CBORObject; -import foundation.identity.did.representations.Representations; - -import java.io.IOException; -import java.util.Map; - -public class RepresentationProducerCBOR extends AbstractRepresentationProducer implements RepresentationProducer { - - public static final String MEDIA_TYPE = "application/did+cbor"; - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationProducerCBOR instance = new RepresentationProducerCBOR(); - - public static RepresentationProducerCBOR getInstance() { - return instance; - } - - private RepresentationProducerCBOR() { - super(Representations.MEDIA_TYPE_CBOR); - } - - @Override - public RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries) throws IOException { - - RepresentationProducer.Result jsonResult = RepresentationProducerJSON.getInstance().produce(didDocument, representationSpecificEntries); - CBORObject cborObject = CBORObject.FromJSONBytes(jsonResult.representation()); - byte[] representation = cborObject.EncodeToBytes(); - return new RepresentationProducer.Result(MEDIA_TYPE, representation); - } -} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDID.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDID.java new file mode 100644 index 0000000..9b69bee --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDID.java @@ -0,0 +1,28 @@ +package foundation.identity.did.representations.production; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; + +import java.io.IOException; + +public class RepresentationProducerDID extends AbstractRepresentationProducer implements RepresentationProducer { + + public static final String MEDIA_TYPE = "application/did"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationProducerDID instance = new RepresentationProducerDID(); + + public static RepresentationProducerDID getInstance() { + return instance; + } + + private RepresentationProducerDID() { + super(MEDIA_TYPE); + } + + @Override + public byte[] produce(DIDDocument didDocument) throws IOException { + byte[] representation = objectMapper.writeValueAsBytes(didDocument.getJsonObject()); + return representation; + } +} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDCBOR.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDCBOR.java new file mode 100644 index 0000000..a897f98 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDCBOR.java @@ -0,0 +1,31 @@ +package foundation.identity.did.representations.production; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.upokecenter.cbor.CBORObject; +import foundation.identity.did.DIDDocument; + +import java.io.IOException; + +public class RepresentationProducerDIDCBOR extends AbstractRepresentationProducer implements RepresentationProducer { + + public static final String MEDIA_TYPE = "application/did+cbor"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationProducerDIDCBOR instance = new RepresentationProducerDIDCBOR(); + + public static RepresentationProducerDIDCBOR getInstance() { + return instance; + } + + private RepresentationProducerDIDCBOR() { + super(MEDIA_TYPE); + } + + @Override + public byte[] produce(DIDDocument didDocument) throws IOException { + byte[] jsonRepresentation = RepresentationProducerDIDJSON.getInstance().produce(didDocument); + CBORObject cborObject = CBORObject.FromJSONBytes(jsonRepresentation); + byte[] representation = cborObject.EncodeToBytes(); + return representation; + } +} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSON.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSON.java new file mode 100644 index 0000000..01b9b07 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSON.java @@ -0,0 +1,28 @@ +package foundation.identity.did.representations.production; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; + +import java.io.IOException; + +public class RepresentationProducerDIDJSON extends AbstractRepresentationProducer implements RepresentationProducer { + + public static final String MEDIA_TYPE = "application/did+json"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationProducerDIDJSON instance = new RepresentationProducerDIDJSON(); + + public static RepresentationProducerDIDJSON getInstance() { + return instance; + } + + private RepresentationProducerDIDJSON() { + super(MEDIA_TYPE); + } + + @Override + public byte[] produce(DIDDocument didDocument) throws IOException { + byte[] representation = objectMapper.writeValueAsBytes(didDocument.getJsonObject()); + return representation; + } +} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSONLD.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSONLD.java new file mode 100644 index 0000000..421c0b8 --- /dev/null +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerDIDJSONLD.java @@ -0,0 +1,29 @@ +package foundation.identity.did.representations.production; + +import com.fasterxml.jackson.databind.ObjectMapper; +import foundation.identity.did.DIDDocument; +import foundation.identity.did.representations.Representations; + +import java.io.IOException; + +public class RepresentationProducerDIDJSONLD extends AbstractRepresentationProducer implements RepresentationProducer { + + public static final String MEDIA_TYPE = "application/did+ld+json"; + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final RepresentationProducerDIDJSONLD instance = new RepresentationProducerDIDJSONLD(); + + public static RepresentationProducerDIDJSONLD getInstance() { + return instance; + } + + private RepresentationProducerDIDJSONLD() { + super(MEDIA_TYPE); + } + + @Override + public byte[] produce(DIDDocument didDocument) throws IOException { + byte[] representation = objectMapper.writeValueAsBytes(didDocument.getJsonObject()); + return representation; + } +} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSON.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSON.java deleted file mode 100644 index 3b8a218..0000000 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSON.java +++ /dev/null @@ -1,30 +0,0 @@ -package foundation.identity.did.representations.production; - -import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.did.representations.Representations; - -import java.io.IOException; -import java.util.Map; - -public class RepresentationProducerJSON extends AbstractRepresentationProducer implements RepresentationProducer { - - public static final String MEDIA_TYPE = "application/did+json"; - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationProducerJSON instance = new RepresentationProducerJSON(); - - public static RepresentationProducerJSON getInstance() { - return instance; - } - - private RepresentationProducerJSON() { - super(Representations.MEDIA_TYPE_JSON); - } - - @Override - public RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries) throws IOException { - - byte[] representation = objectMapper.writeValueAsBytes(didDocument); - return new RepresentationProducer.Result(MEDIA_TYPE, representation); - } -} diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSONLD.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSONLD.java deleted file mode 100644 index 5e8bda7..0000000 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerJSONLD.java +++ /dev/null @@ -1,30 +0,0 @@ -package foundation.identity.did.representations.production; - -import com.fasterxml.jackson.databind.ObjectMapper; -import foundation.identity.did.representations.Representations; - -import java.io.IOException; -import java.util.Map; - -public class RepresentationProducerJSONLD extends AbstractRepresentationProducer implements RepresentationProducer { - - public static final String MEDIA_TYPE = "application/did+ld+json"; - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final RepresentationProducerJSONLD instance = new RepresentationProducerJSONLD(); - - public static RepresentationProducerJSONLD getInstance() { - return instance; - } - - private RepresentationProducerJSONLD() { - super(Representations.MEDIA_TYPE_JSONLD); - } - - @Override - public RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries) throws IOException { - - byte[] representation = objectMapper.writeValueAsBytes(didDocument); - return new RepresentationProducer.Result(MEDIA_TYPE, representation); - } -}