Skip to content

Commit

Permalink
feat: Support for new media types
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Jan 8, 2025
1 parent 0167908 commit 55f6cf4
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 260 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<RepresentationProducer> representationProducers = Arrays.asList(
RepresentationProducerDID.getInstance(),
RepresentationProducerDIDJSONLD.getInstance(),
RepresentationProducerDIDJSON.getInstance(),
RepresentationProducerDIDCBOR.getInstance()
);

public static final List<String> MEDIA_TYPES = Arrays.asList(
MEDIA_TYPE_JSONLD,
MEDIA_TYPE_JSON,
MEDIA_TYPE_CBOR
public static final List<RepresentationConsumer> representationConsumers = Arrays.asList(
RepresentationConsumerDID.getInstance(),
RepresentationConsumerDIDJSONLD.getInstance(),
RepresentationConsumerDIDJSON.getInstance(),
RepresentationConsumerDIDCBOR.getInstance()
);

public static final Map<String, RepresentationProducer> didDocumentProducers = new HashMap<>();
public static final Map<String, RepresentationConsumer> didDocumentConsumers = new HashMap<>();
public static final Map<String, RepresentationProducer> representationProducersByMediaType = new LinkedHashMap<>();
public static final Map<String, RepresentationConsumer> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,25 +12,4 @@ public AbstractRepresentationConsumer(String mediaType) {
public String getMediaType() {
return this.mediaType;
}

public RepresentationConsumer.Result detectRepresentationSpecificEntries(Map<String, Object> map) {

Map<String, Object> didDocument = new HashMap<>(map);
Map<String, Map<String, Object>> representationSpecificEntries = new HashMap<>();
for (String mediaType : RepresentationSpecificEntries.REPRESENTATION_SPECIFIC_ENTRY_NAMES.keySet()) {
representationSpecificEntries.put(mediaType, new HashMap<>());
}
for (Map.Entry<String, List<String>> 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<String, Object> representationSpecificMap = representationSpecificEntries.get(mediaType);
representationSpecificMap.put(representationSpecificEntryName, representationSpecificEntryValue);
}
}
}

return new RepresentationConsumer.Result(didDocument, representationSpecificEntries);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
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<String, Object> didDocument,
Map<String, Map<String, Object>> 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);
return didDocumentConsumer.consume(representation);
}

public String getMediaType();
public Result consume(byte[] representation) throws IOException;
public DIDDocument consume(byte[] representation) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> map = objectMapper.readValue(representation, LinkedHashMap.class);
return DIDDocument.fromMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> map = cborObject.ToObject(LinkedHashMap.class);
return this.detectRepresentationSpecificEntries(map);
return DIDDocument.fromMap(map);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> map = objectMapper.readValue(representation, LinkedHashMap.class);
return DIDDocument.fromMap(map);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> map = objectMapper.readValue(representation, LinkedHashMap.class);
return DIDDocument.fromMap(map);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String, Object> didDocument, Map<String, Object> 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<String, Object> didDocument, Map<String, Object> representationSpecificEntries) throws IOException;
public byte[] produce(DIDDocument didDocument) throws IOException;
}
Loading

0 comments on commit 55f6cf4

Please sign in to comment.