Skip to content

Commit 8c223c7

Browse files
committed
Fixed transformer ambiguity. Allowed 'id' field in asset as it is created by EDC internally. First version of getAsset endpoint for CBM (WIP).
1 parent 00dfd5a commit 8c223c7

7 files changed

Lines changed: 207 additions & 13 deletions

File tree

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/AssetApiExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public void initialize(ServiceExtensionContext context) {
7171

7272
var managementTypeTransformerRegistry = transformerRegistry.forContext("management-api");
7373

74-
managementTypeTransformerRegistry.register(new CbmJsonObjectToJsonObjectAssetTransformer());
74+
managementTypeTransformerRegistry.register(new CbmJsonObjectToAssetJsonObjectTransformer());
75+
managementTypeTransformerRegistry.register(new JsonObjectAssetToCbmJsonObjectTransformer());
7576

7677
webService.registerResource(ApiContext.MANAGEMENT, new ContentBasedAssetApiController(assetService,
7778
managementTypeTransformerRegistry, monitor, validator));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2025 Universidad de Alicante
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+
* LdE - Universidad de Alicante - initial implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.heleade.provider.extension.content.based.api.asset;
16+
17+
import jakarta.json.JsonObject;
18+
19+
/**
20+
* Helper class to apply proper transformations from the registry
21+
* and distinguish among different JsonObject types
22+
*/
23+
24+
public class AssetJsonObject {
25+
26+
private JsonObject jsonObject;
27+
28+
/**
29+
* Constructor for wrapper class
30+
*/
31+
public AssetJsonObject(JsonObject jsonObject) {
32+
this.jsonObject = jsonObject;
33+
}
34+
35+
public JsonObject getJsonObject() {
36+
return jsonObject;
37+
}
38+
39+
public void setJsonObject(JsonObject jsonObject) {
40+
this.jsonObject = jsonObject;
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025 Universidad de Alicante
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+
* LdE - Universidad de Alicante - initial implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.heleade.provider.extension.content.based.api.asset;
16+
17+
import jakarta.json.JsonObject;
18+
19+
/**
20+
* Helper class to apply proper transformations from the registry
21+
* and distinguish among different JsonObject types
22+
*/
23+
public class CbmJsonObject {
24+
25+
private JsonObject jsonObject;
26+
27+
/**
28+
* Constructor for wrapper class
29+
*/
30+
public CbmJsonObject(JsonObject jsonObject) {
31+
this.jsonObject = jsonObject;
32+
}
33+
34+
public JsonObject getJsonObject() {
35+
return jsonObject;
36+
}
37+
38+
public void setJsonObject(JsonObject jsonObject) {
39+
this.jsonObject = jsonObject;
40+
}
41+
}

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/CbmJsonObjectToJsonObjectAssetTransformer.java renamed to providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/CbmJsonObjectToAssetJsonObjectTransformer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@
4343
/**
4444
* Converts from an {@link Dataset} as a {@link JsonObject} in JSON-LD expanded form of an {@link Asset}.
4545
*/
46-
public class CbmJsonObjectToJsonObjectAssetTransformer extends AbstractJsonLdTransformer<JsonObject, JsonObject> {
46+
public class CbmJsonObjectToAssetJsonObjectTransformer extends AbstractJsonLdTransformer<CbmJsonObject, JsonObject> {
4747
/**
48-
* Constructor for CbmJsonObjectToJsonObjectAssetTransformer.
48+
* Constructor for CbmJsonObjectToAssetJsonObjectTransformer.
4949
* Configures the transformer to handle transformations from a JsonObject
5050
* to another JsonObject, leveraging the base functionality provided
5151
* by the AbstractJsonLdTransformer.
5252
*/
53-
public CbmJsonObjectToJsonObjectAssetTransformer() {
54-
super(JsonObject.class, JsonObject.class);
53+
public CbmJsonObjectToAssetJsonObjectTransformer() {
54+
super(CbmJsonObject.class, JsonObject.class);
5555
}
5656

5757
@Override
58-
public @Nullable JsonObject transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
58+
public @Nullable JsonObject transform(@NotNull CbmJsonObject cbmJsonObject, @NotNull TransformerContext context) {
59+
60+
JsonObject jsonObject = cbmJsonObject.getJsonObject();
61+
5962
// Check if we have a valid Dataset
6063
if (!DCAT_DATASET_TYPE.equals(nodeType(jsonObject)) && !CBM_SAMPLE_TYPE.equals(nodeType(jsonObject))) {
6164
context.problem()

providers/provider/src/main/java/org/eclipse/edc/heleade/provider/extension/content/based/api/asset/ContentBasedAssetApiController.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
import jakarta.json.JsonObject;
1818
import jakarta.ws.rs.Consumes;
19+
import jakarta.ws.rs.DELETE;
20+
import jakarta.ws.rs.GET;
1921
import jakarta.ws.rs.POST;
2022
import jakarta.ws.rs.PUT;
2123
import jakarta.ws.rs.Path;
24+
import jakarta.ws.rs.PathParam;
2225
import jakarta.ws.rs.Produces;
2326
import org.eclipse.edc.connector.controlplane.api.management.asset.v3.AssetApi;
2427
import org.eclipse.edc.connector.controlplane.api.management.asset.v3.AssetApiController;
@@ -69,26 +72,48 @@ public ContentBasedAssetApiController(AssetService service, TypeTransformerRegis
6972
@Override
7073
public JsonObject createAssetV3(JsonObject assetJson) {
7174
monitor.info("Received CBM asset creation request");
72-
JsonObject edcAssetJson = transform(assetJson);
75+
JsonObject edcAssetJson = transformCbmToAsset(assetJson);
7376
return super.createAssetV3(edcAssetJson);
7477
}
7578

7679
@PUT
7780
@Override
7881
public void updateAssetV3(JsonObject assetJson) {
7982
monitor.info("Received CBM asset modification request");
80-
JsonObject edcAssetJson = transform(assetJson);
83+
JsonObject edcAssetJson = transformCbmToAsset(assetJson);
8184
super.updateAssetV3(edcAssetJson);
8285
}
8386

84-
private JsonObject transform(JsonObject assetJson) {
85-
if (assetJson.containsKey("@type") && CBM_SAMPLE_TYPE.equals(assetJson.getJsonArray("@type").getString(0))) {
86-
validator.validate(CBM_SAMPLE_TYPE, assetJson).orElseThrow(ValidationFailureException::new);
87+
@GET
88+
@Path("{id}")
89+
@Override
90+
public JsonObject getAssetV3(@PathParam("id") String id) {
91+
return transformAssetToCbm(super.getAssetV3(id));
92+
93+
}
94+
95+
@DELETE
96+
@Path("{id}")
97+
@Override
98+
public void removeAssetV3(@PathParam("id") String id) {
99+
super.removeAssetV3(id);
100+
}
101+
102+
private JsonObject transformCbmToAsset(JsonObject cbmJson) {
103+
if (cbmJson.containsKey("@type") && CBM_SAMPLE_TYPE.equals(cbmJson.getJsonArray("@type").getString(0))) {
104+
validator.validate(CBM_SAMPLE_TYPE, cbmJson).orElseThrow(ValidationFailureException::new);
87105
} else {
88-
validator.validate(DCAT_DATASET_TYPE, assetJson).orElseThrow(ValidationFailureException::new);
106+
validator.validate(DCAT_DATASET_TYPE, cbmJson).orElseThrow(ValidationFailureException::new);
89107
}
90-
JsonObject edcAssetJson = transformerRegistry.transform(assetJson, JsonObject.class)
108+
JsonObject edcAssetJson = transformerRegistry.transform(new CbmJsonObject(cbmJson), JsonObject.class)
91109
.orElseThrow(f -> new EdcException(f.getFailureDetail()));
92110
return edcAssetJson;
93111
}
112+
113+
private JsonObject transformAssetToCbm(JsonObject edcAssetJson) {
114+
JsonObject cbmAssetJson = transformerRegistry.transform(new AssetJsonObject(edcAssetJson), JsonObject.class)
115+
.orElseThrow(f -> new EdcException(f.getFailureDetail()));
116+
return cbmAssetJson;
117+
}
118+
94119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2025 Universidad de Alicante
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+
* LdE - Universidad de Alicante - initial implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.heleade.provider.extension.content.based.api.asset;
16+
17+
import jakarta.json.Json;
18+
import jakarta.json.JsonObject;
19+
import org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset;
20+
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
21+
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
22+
import org.eclipse.edc.transform.spi.TransformerContext;
23+
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
25+
26+
import static org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset.EDC_ASSET_TYPE;
27+
import static org.eclipse.edc.jsonld.spi.TypeUtil.nodeType;
28+
29+
/**
30+
* Converts from an {@link Asset} as a {@link JsonObject} in JSON-LD expanded form of a {@link Dataset}.
31+
*/
32+
public class JsonObjectAssetToCbmJsonObjectTransformer extends AbstractJsonLdTransformer<AssetJsonObject, JsonObject> {
33+
/**
34+
* Constructor for JsonObjectAssetToCbmJsonObjectTransformer.
35+
* Configures the transformer to handle transformations from a JsonObject
36+
* to another JsonObject, leveraging the base functionality provided
37+
* by the AbstractJsonLdTransformer.
38+
*/
39+
public JsonObjectAssetToCbmJsonObjectTransformer() {
40+
super(AssetJsonObject.class, JsonObject.class);
41+
}
42+
43+
@Override
44+
public @Nullable JsonObject transform(@NotNull AssetJsonObject assetJsonObject, @NotNull TransformerContext context) {
45+
JsonObject jsonObject = assetJsonObject.getJsonObject();
46+
47+
// Check if we have a valid Dataset
48+
if (!EDC_ASSET_TYPE.equals(nodeType(jsonObject))) {
49+
context.problem()
50+
.unexpectedType()
51+
.expected(EDC_ASSET_TYPE)
52+
.actual(nodeType(jsonObject))
53+
.report();
54+
return null;
55+
}
56+
57+
// Extract the ID
58+
var id = nodeId(jsonObject);
59+
if (id == null) {
60+
context.problem()
61+
.missingProperty()
62+
.property("@id")
63+
.report();
64+
return null;
65+
}
66+
67+
// Start building the Asset object
68+
var assetBuilder = Json.createObjectBuilder()
69+
.add("@id", id)
70+
.add("@type", EDC_ASSET_TYPE);
71+
72+
// Add context from original object
73+
if (jsonObject.containsKey("@context")) {
74+
assetBuilder.add("@context", jsonObject.get("@context"));
75+
}
76+
return assetBuilder.build();
77+
}
78+
79+
}

providers/provider/src/main/resources/asset-schema-cbm.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"http://purl.org/dc/terms/description": {
2121
"type": "string"
2222
},
23+
"id": {
24+
"type": "string"
25+
},
2326
"contenttype": {
2427
"type": "string"
2528
},

0 commit comments

Comments
 (0)