-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0167908
commit 55f6cf4
Showing
18 changed files
with
250 additions
and
260 deletions.
There are no files selected for viewing
31 changes: 0 additions & 31 deletions
31
src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java
This file was deleted.
Oops, something went wrong.
55 changes: 26 additions & 29 deletions
55
src/main/java/foundation/identity/did/representations/Representations.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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
11 changes: 3 additions & 8 deletions
11
...main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.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 |
---|---|---|
@@ -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; | ||
} |
30 changes: 30 additions & 0 deletions
30
...n/java/foundation/identity/did/representations/consumption/RepresentationConsumerDID.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,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); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...va/foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSON.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,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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../foundation/identity/did/representations/consumption/RepresentationConsumerDIDJSONLD.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,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); | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
.../java/foundation/identity/did/representations/consumption/RepresentationConsumerJSON.java
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
...ava/foundation/identity/did/representations/consumption/RepresentationConsumerJSONLD.java
This file was deleted.
Oops, something went wrong.
13 changes: 4 additions & 9 deletions
13
src/main/java/foundation/identity/did/representations/production/RepresentationProducer.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 |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.