|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Think-it GmbH |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Think-it GmbH - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.jsonld; |
| 16 | + |
| 17 | +import org.eclipse.edc.jsonld.spi.JsonLdContext; |
| 18 | +import org.eclipse.edc.spi.result.Result; |
| 19 | + |
| 20 | +import java.net.URI; |
| 21 | +import java.net.URISyntaxException; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import static java.lang.String.format; |
| 26 | +import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_CONTEXT_2025_1; |
| 27 | +import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_ODRL_PROFILE_2025_1; |
| 28 | +import static org.eclipse.edc.jsonld.spi.Namespaces.EDC_DSPACE_CONTEXT; |
| 29 | +import static org.eclipse.edc.spi.constants.CoreConstants.EDC_CONNECTOR_MANAGEMENT_CONTEXT; |
| 30 | +import static org.eclipse.edc.spi.constants.CoreConstants.EDC_CONNECTOR_MANAGEMENT_CONTEXT_V2; |
| 31 | + |
| 32 | +/** |
| 33 | + * Manages the hardcoded json-ld documents cached |
| 34 | + */ |
| 35 | +public class CachedDocumentRegistry { |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the cached documents |
| 39 | + * |
| 40 | + * @return the cached documents |
| 41 | + */ |
| 42 | + public static Stream<Result<JsonLdContext>> getDocuments() { |
| 43 | + return Map.of( |
| 44 | + "odrl.jsonld", "http://www.w3.org/ns/odrl.jsonld", |
| 45 | + "dspace.jsonld", "https://w3id.org/dspace/2024/1/context.json", |
| 46 | + "management-context-v1.jsonld", EDC_CONNECTOR_MANAGEMENT_CONTEXT, |
| 47 | + "management-context-v2.jsonld", EDC_CONNECTOR_MANAGEMENT_CONTEXT_V2, |
| 48 | + "dspace-edc-context-v1.jsonld", EDC_DSPACE_CONTEXT, |
| 49 | + "dspace-v2025-1.jsonld", DSPACE_CONTEXT_2025_1, |
| 50 | + "dspace-v2025-1-odrl.jsonld", DSPACE_ODRL_PROFILE_2025_1 |
| 51 | + ).entrySet().stream() |
| 52 | + .map(entry -> getResourceUri("document/" + entry.getKey()) |
| 53 | + .map(uri -> new JsonLdContext(uri, entry.getValue()))); |
| 54 | + } |
| 55 | + |
| 56 | + static Result<URI> getResourceUri(String name) { |
| 57 | + var uri = CachedDocumentRegistry.class.getClassLoader().getResource(name); |
| 58 | + if (uri == null) { |
| 59 | + return Result.failure(format("Cannot find resource %s", name)); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + return Result.success(uri.toURI()); |
| 64 | + } catch (URISyntaxException e) { |
| 65 | + return Result.failure(format("Cannot read resource %s: %s", name, e.getMessage())); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments