Skip to content

Commit

Permalink
feat: Move protocol setting from transformers to controllers (#2996)
Browse files Browse the repository at this point in the history
* Pull up protocol setting; align and simplify controllers

* Move up protocol and align controllers

* Checkstyle fixes
  • Loading branch information
jimmarino authored May 14, 2023
1 parent 2ba041d commit a7b21bb
Show file tree
Hide file tree
Showing 39 changed files with 600 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.eclipse.edc.connector.spi.catalog.CatalogProtocolService;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfiguration;
import org.eclipse.edc.protocol.dsp.catalog.api.controller.CatalogController;
import org.eclipse.edc.protocol.dsp.catalog.api.controller.DspCatalogApiController;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.iam.IdentityService;
Expand Down Expand Up @@ -66,7 +66,7 @@ public String name() {
public void initialize(ServiceExtensionContext context) {
var mapper = typeManager.getMapper(JSON_LD);
var dspCallbackAddress = apiConfiguration.getDspCallbackAddress();
var catalogController = new CatalogController(context.getMonitor(), mapper, identityService, transformerRegistry, dspCallbackAddress, service, jsonLdService);
var catalogController = new DspCatalogApiController(context.getMonitor(), mapper, identityService, transformerRegistry, dspCallbackAddress, service, jsonLdService);
webService.registerResource(apiConfiguration.getContextAlias(), catalogController);

dataServiceRegistry.register(DataService.Builder.newInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@

import static jakarta.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static java.lang.String.format;
import static org.eclipse.edc.jsonld.spi.TypeUtil.isOfExpectedType;
import static org.eclipse.edc.protocol.dsp.catalog.api.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.api.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_CATALOG_REQUEST_TYPE;
import static org.eclipse.edc.protocol.dsp.transform.util.TypeUtil.isOfExpectedType;
import static org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;
import static org.eclipse.edc.web.spi.exception.ServiceResultHandler.exceptionMapper;

/**
Expand All @@ -50,8 +51,8 @@
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path(BASE_PATH)
public class CatalogController {
public class DspCatalogApiController {

private final Monitor monitor;
private final ObjectMapper mapper;
private final IdentityService identityService;
Expand All @@ -60,9 +61,9 @@ public class CatalogController {
private final CatalogProtocolService service;
private final JsonLd jsonLdService;

public CatalogController(Monitor monitor, ObjectMapper mapper, IdentityService identityService,
TypeTransformerRegistry transformerRegistry, String dspCallbackAddress,
CatalogProtocolService service, JsonLd jsonLdService) {
public DspCatalogApiController(Monitor monitor, ObjectMapper mapper, IdentityService identityService,
TypeTransformerRegistry transformerRegistry, String dspCallbackAddress,
CatalogProtocolService service, JsonLd jsonLdService) {
this.monitor = monitor;
this.mapper = mapper;
this.identityService = identityService;
Expand All @@ -76,7 +77,7 @@ public CatalogController(Monitor monitor, ObjectMapper mapper, IdentityService i
@Path(CATALOG_REQUEST)
public Map<String, Object> getCatalog(JsonObject jsonObject, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(() -> "DSP: Incoming catalog request.");

var tokenRepresentation = TokenRepresentation.Builder.newInstance()
.token(token)
.build();
Expand All @@ -96,6 +97,9 @@ public Map<String, Object> getCatalog(JsonObject jsonObject, @HeaderParam(AUTHOR
var message = transformerRegistry.transform(expandedJson, CatalogRequestMessage.class)
.orElseThrow(failure -> new InvalidRequestException(format("Request body was malformed: %s", failure.getFailureDetail())));

// set protocol
message.setProtocol(DATASPACE_PROTOCOL_HTTP);

var catalog = service.getCatalog(message, claimToken)
.orElseThrow(exceptionMapper(Catalog.class));

Expand All @@ -106,6 +110,7 @@ public Map<String, Object> getCatalog(JsonObject jsonObject, @HeaderParam(AUTHOR
if (compacted.failed()) {
throw new InvalidRequestException(compacted.getFailureDetail());
}
//noinspection unchecked
return mapper.convertValue(compacted.getContent(), Map.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

@ApiTest
@ExtendWith(EdcExtension.class)
class CatalogControllerIntegrationTest {
class DspCatalogApiControllerIntegrationTest {

private final int dspApiPort = getFreePort();
private final String dspApiPath = "/api/v1/dsp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.eclipse.edc.jsonld.spi.Namespaces.ODRL_PREFIX;
import static org.eclipse.edc.jsonld.spi.Namespaces.ODRL_SCHEMA;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_CATALOG_REQUEST_TYPE;
import static org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;
import static org.eclipse.edc.service.spi.result.ServiceResult.badRequest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -57,7 +58,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class CatalogControllerTest {
class DspCatalogApiControllerTest {

private final Monitor monitor = mock(Monitor.class);
private final ObjectMapper mapper = mock(ObjectMapper.class);
Expand All @@ -69,7 +70,7 @@ class CatalogControllerTest {
private final JsonLd jsonLdService = new TitaniumJsonLd(mock(Monitor.class));
private JsonObject request;
private CatalogRequestMessage requestMessage;
private CatalogController controller;
private DspCatalogApiController controller;

private static ClaimToken createToken() {
return ClaimToken.Builder.newInstance().build();
Expand All @@ -81,7 +82,7 @@ void setUp() {
jsonLdService.registerNamespace(DCT_PREFIX, DCT_SCHEMA);
jsonLdService.registerNamespace(ODRL_PREFIX, ODRL_SCHEMA);
jsonLdService.registerNamespace(DSPACE_PREFIX, DSPACE_SCHEMA);
controller = new CatalogController(monitor, mapper, identityService, transformerRegistry, callbackAddress, service, jsonLdService);
controller = new DspCatalogApiController(monitor, mapper, identityService, transformerRegistry, callbackAddress, service, jsonLdService);

request = Json.createObjectBuilder()
.add(TYPE, DSPACE_CATALOG_REQUEST_TYPE)
Expand All @@ -107,6 +108,9 @@ void getCatalog_returnCatalog() {

assertThat(response).isEqualTo(responseMap);
verify(service).getCatalog(requestMessage, token);

// verify that the message protocol was set to the DSP protocol by the controller
assertThat(requestMessage.getProtocol()).isEqualTo(DATASPACE_PROTOCOL_HTTP);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,30 @@

import static java.lang.String.format;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_FILTER_PROPERTY;
import static org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;

/**
* Transforms a {@link JsonObject} in JSON-LD expanded form to a {@link CatalogRequestMessage}.
*/
public class JsonObjectToCatalogRequestMessageTransformer extends AbstractJsonLdTransformer<JsonObject, CatalogRequestMessage> {

private final ObjectMapper mapper;

public JsonObjectToCatalogRequestMessageTransformer(ObjectMapper mapper) {
super(JsonObject.class, CatalogRequestMessage.class);
this.mapper = mapper;
}

@Override
public @Nullable CatalogRequestMessage transform(@NotNull JsonObject object, @NotNull TransformerContext context) {
var builder = CatalogRequestMessage.Builder.newInstance();
builder.protocol(DATASPACE_PROTOCOL_HTTP);


if (object.get(DSPACE_FILTER_PROPERTY) != null) {
builder.querySpec(transformQuerySpec(object.get(DSPACE_FILTER_PROPERTY), context));
}

return builder.build();
}

private QuerySpec transformQuerySpec(JsonValue value, TransformerContext context) {
if (value instanceof JsonObject) {
return mapper.convertValue(value, QuerySpec.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ plugins {

dependencies {
api(project(":data-protocols:dsp:dsp-api-configuration"))

// TODO remove this dependency by moving type constants to another module
api(project(":data-protocols:dsp:dsp-negotiation:dsp-negotiation-transform"))
api(project(":data-protocols:dsp:dsp-transform"))

api(project(":spi:common:core-spi"))
api(project(":spi:control-plane:control-plane-spi"))
Expand All @@ -32,4 +33,4 @@ dependencies {
testImplementation(project(":core:common:junit"))
testImplementation(testFixtures(project(":extensions:common:http:jersey-core")))
testImplementation(libs.restAssured)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.edc.connector.spi.contractnegotiation.ContractNegotiationProtocolService;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfiguration;
import org.eclipse.edc.protocol.dsp.negotiation.api.controller.DspNegotiationController;
import org.eclipse.edc.protocol.dsp.negotiation.api.controller.DspNegotiationApiController;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.iam.IdentityService;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void initialize(ServiceExtensionContext context) {
var callbackAddress = apiConfiguration.getDspCallbackAddress();
var objectMapper = typeManager.getMapper(JSON_LD);

var controller = new DspNegotiationController(callbackAddress, identityService, transformerRegistry, protocolService, jsonLdService, objectMapper, monitor);
var controller = new DspNegotiationApiController(callbackAddress, identityService, transformerRegistry, protocolService, jsonLdService, objectMapper, monitor);

webService.registerResource(apiConfiguration.getContextAlias(), controller);
}
Expand Down
Loading

0 comments on commit a7b21bb

Please sign in to comment.