| 1 | # JSON-LD Processing Architecture |
| 2 | |
| 3 | The Dataspace Protocol requires all protocol message types to be serialized as JSON-LD. The IDS REST binding specifications further define how those serialized message types are bound to |
| 4 | endpoints over HTTPS. Controller endpoints will be added to the EDC that support marshalling and unmarshalling JSON-LD messages. These controllers will operate on native JSON |
| 5 | structures (since JSON-LD is valid JSON) as opposed to typed Java representations. This will allow the EDC to remove its dependency on the IDS InfoModel Java Library and to provide |
| 6 | full support for JSON-LD message exchanges. |
| 7 | |
| 8 | A common JSON-LD processing architecture is required by these controller endpoints, which may be deployed to individual runtimes such as the control plane and catalog systems. In |
| 9 | addition, the Management API will adopt support for JSON-LD. This document lays out the common processing architecture that will support those requirements. |
| 10 | |
| 11 | ## JSON-LD Support |
| 12 | |
| 13 | JSON-LD support can be added to the existing `TypeManager` infrastructure by creating a __serializer context__ that is configured with JSONP support in a JSON-LD EDC extension: |
| 14 | |
| 15 | ``` |
| 16 | var mapper = new ObjectMapper(); |
| 17 | |
| 18 | mapper.registerModule(new JSONPModule()); |
| 19 | |
| 20 | var module = new SimpleModule() { |
| 21 | |
| 22 | @Override |
| 23 | public void setupModule(SetupContext context){ |
| 24 | super.setupModule(context); |
| 25 | } |
| 26 | |
| 27 | }; |
| 28 | |
| 29 | mapper.registerModule(module); |
| 30 | |
| 31 | typeManager.registerContext("json-ld",mapper) |
| 32 | ``` |
| 33 | |
| 34 | This context can then be used in conjunction with the [Titanium JSON-LD Library](https://github.com/filip26/titanium-json-ld) when processing messages: |
| 35 | |
| 36 | ``` |
| 37 | // message is de-serialized as Map<String, Object> by Jersey |
| 38 | var document = JsonDocument.of(mapper.convertValue(message, JsonObject.class)); |
| 39 | |
| 40 | try { |
| 41 | |
| 42 | var compacted = JsonLd.compact(document,EMPTY_CONTEXT).get(); |
| 43 | var convertedDocument = mapper.convertValue(compacted,Map.class); |
| 44 | |
| 45 | // process converted document |
| 46 | |
| 47 | } catch(JsonLdError e) { |
| 48 | throw new RuntimeException(e); |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | ### JSON-LD Document Loaders |
| 53 | |
| 54 | JSON-LD allows references to external documents that contain context information. EDC should provide a default loader and extensibility point for resolving and caching external |
| 55 | document contexts, `JsonLdDocumentResolverRegistry`. This extensibility point should be provided by a generic JSON-LD extension, as it may be reused outside of IDS. |
| 56 | |
| 57 | The Titanium interface for loading referenced documents is `DocumentLoader`: |
| 58 | |
| 59 | ``` |
| 60 | public interface DocumentLoader { |
| 61 | |
| 62 | /** |
| 63 | * Retrieve a remote document. |
| 64 | * |
| 65 | * @param url of the remote document to fetch |
| 66 | * @param options to set the behavior of the loader |
| 67 | * @return {@link Document} representing a remote document |
| 68 | * @throws JsonLdError |
| 69 | */ |
| 70 | Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError; |
| 71 | } |
| 72 | |
| 73 | ``` |
| 74 | |
| 75 | EDC will provide an implementation that: |
| 76 | |
| 77 | 1. Resolves documents from a local file system directory (which can be a read-only docker mount) |
| 78 | 2. Delegates to `JsonLdDocumentResolver` implementations via the `JsonLdDocumentResolverRegistry`. |
| 79 | |
| 80 | `JsonLdDocumentResolver` should not expose Titanium classes. It should return a `JsonStructure` which can be wrapped in a Titanium `Document` by the EDC `DocumentLoader` |
| 81 | implementation: |
| 82 | |
| 83 | ``` |
| 84 | public interface JsonLdDocumentResolver { |
| 85 | |
| 86 | boolean canResolve(URI url); |
| 87 | |
| 88 | JsonStructure resolve(URI url); |
| 89 | } |
| 90 | ``` |
| 91 | |
| 92 | > __NOTE__: The EDC should refrain from resolving URLs that are not mapped to the local file system. For example, HTTP URLs, due to security considerations. If end-users want this |
| 93 | > capability, they can implement and register a `JsonLdDocumentResolver`. |
| 94 | |
| 95 | |