Skip to content

Commit

Permalink
refactor: supports multi protocols dsp-http (#4514)
Browse files Browse the repository at this point in the history
refactor: supports multi protocols in DspRequest and DspHttpRemoteMessageDispatcherImpl
  • Loading branch information
wolf4ood authored Oct 2, 2024
1 parent 8f5bff2 commit f449c33
Show file tree
Hide file tree
Showing 50 changed files with 807 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.eclipse.edc.protocol.dsp.catalog.http.api.decorator.Base64continuationTokenSerDes;
import org.eclipse.edc.protocol.dsp.catalog.http.api.decorator.ContinuationTokenManagerImpl;
import org.eclipse.edc.protocol.dsp.catalog.http.api.validation.CatalogRequestMessageValidator;
import org.eclipse.edc.protocol.dsp.http.spi.message.ContinuationTokenManager;
import org.eclipse.edc.protocol.dsp.http.spi.message.DspRequestHandler;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.protocol.ProtocolWebhook;
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
import org.eclipse.edc.spi.system.ServiceExtension;
Expand All @@ -37,6 +39,9 @@
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_2024_1;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_08;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_2024_1;

/**
Expand Down Expand Up @@ -64,7 +69,10 @@ public class DspCatalogApiExtension implements ServiceExtension {
@Inject
private ProtocolVersionRegistry versionRegistry;
@Inject
private TypeTransformerRegistry typeTransformerRegistry;
private TypeTransformerRegistry transformerRegistry;
@Inject
private Monitor monitor;

@Inject
private JsonLd jsonLd;

Expand All @@ -77,16 +85,21 @@ public String name() {
public void initialize(ServiceExtensionContext context) {
validatorRegistry.register(DSPACE_TYPE_CATALOG_REQUEST_MESSAGE, CatalogRequestMessageValidator.instance(criterionOperatorRegistry));

var continuationTokenSerDes = new Base64continuationTokenSerDes(typeTransformerRegistry.forContext("dsp-api"), jsonLd);
var catalogPaginationResponseDecoratorFactory = new ContinuationTokenManagerImpl(continuationTokenSerDes, context.getMonitor());
webService.registerResource(ApiContext.PROTOCOL, new DspCatalogApiController(service, dspRequestHandler, catalogPaginationResponseDecoratorFactory));
webService.registerResource(ApiContext.PROTOCOL, new DspCatalogApiController20241(service, dspRequestHandler, catalogPaginationResponseDecoratorFactory));

webService.registerResource(ApiContext.PROTOCOL, new DspCatalogApiController(service, dspRequestHandler, continuationTokenManager(monitor, DSP_TRANSFORMER_CONTEXT_V_08)));
webService.registerResource(ApiContext.PROTOCOL, new DspCatalogApiController20241(service, dspRequestHandler, continuationTokenManager(monitor, DSP_TRANSFORMER_CONTEXT_V_2024_1)));

dataServiceRegistry.register(DataService.Builder.newInstance()
.endpointDescription("dspace:connector")
.endpointUrl(protocolWebhook.url())
.build());

versionRegistry.register(V_2024_1);
versionRegistry.register(V_08);
}

private ContinuationTokenManager continuationTokenManager(Monitor monitor, String version) {
var continuationTokenSerDes = new Base64continuationTokenSerDes(transformerRegistry.forContext(version), jsonLd);
return new ContinuationTokenManagerImpl(continuationTokenSerDes, monitor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.DATASET_REQUEST;
import static org.eclipse.edc.protocol.dsp.http.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR;
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE;

Expand All @@ -55,11 +56,17 @@ public class DspCatalogApiController {
private final CatalogProtocolService service;
private final DspRequestHandler dspRequestHandler;
private final ContinuationTokenManager continuationTokenManager;
private final String protocol;

public DspCatalogApiController(CatalogProtocolService service, DspRequestHandler dspRequestHandler, ContinuationTokenManager continuationTokenManager) {
this(service, dspRequestHandler, continuationTokenManager, DATASPACE_PROTOCOL_HTTP);
}

public DspCatalogApiController(CatalogProtocolService service, DspRequestHandler dspRequestHandler, ContinuationTokenManager continuationTokenManager, String protocol) {
this.service = service;
this.dspRequestHandler = dspRequestHandler;
this.continuationTokenManager = continuationTokenManager;
this.protocol = protocol;
}

@POST
Expand All @@ -80,6 +87,7 @@ public Response requestCatalog(JsonObject jsonObject, @HeaderParam(AUTHORIZATION
.message(messageJson)
.serviceCall(service::getCatalog)
.errorType(DSPACE_TYPE_CATALOG_ERROR)
.protocol(protocol)
.build();

var responseDecorator = continuationTokenManager.createResponseDecorator(uriInfo.getAbsolutePath().toString());
Expand All @@ -94,6 +102,7 @@ public Response getDataset(@PathParam("id") String id, @HeaderParam(AUTHORIZATIO
.id(id)
.serviceCall(service::getDataset)
.errorType(DSPACE_TYPE_CATALOG_ERROR)
.protocol(protocol)
.build();

return dspRequestHandler.getResource(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
import org.eclipse.edc.connector.controlplane.services.spi.catalog.CatalogProtocolService;
import org.eclipse.edc.protocol.dsp.http.spi.message.ContinuationTokenManager;
import org.eclipse.edc.protocol.dsp.http.spi.message.DspRequestHandler;
import org.eclipse.edc.protocol.dsp.spi.version.DspVersions;

import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.eclipse.edc.protocol.dsp.catalog.http.api.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.http.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP_V_2024_1;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_2024_1_PATH;

/**
* Versioned Catalog endpoint, same as {@link DspCatalogApiController} but exposed on the /2024/1 path
*/
@Consumes({ APPLICATION_JSON })
@Produces({ APPLICATION_JSON })
@Path(DspVersions.V_2024_1_PATH + BASE_PATH)
@Path(V_2024_1_PATH + BASE_PATH)
public class DspCatalogApiController20241 extends DspCatalogApiController {

public DspCatalogApiController20241(CatalogProtocolService service, DspRequestHandler dspRequestHandler,
ContinuationTokenManager responseDecorator) {
super(service, dspRequestHandler, responseDecorator);
super(service, dspRequestHandler, responseDecorator, DATASPACE_PROTOCOL_HTTP_V_2024_1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,39 @@

package org.eclipse.edc.protocol.dsp.catalog.http.api;

import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersion;
import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.protocol.dsp.spi.transform.DspProtocolTypeTransformerRegistry;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_08;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_2024_1;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(DependencyInjectionExtension.class)
class DspCatalogApiExtensionTest {

private final JsonObjectValidatorRegistry validatorRegistry = mock();
private final ProtocolVersionRegistry versionRegistry = mock();
private final DspProtocolTypeTransformerRegistry dspTransformerRegistry = mock();

@BeforeEach
void setUp(ServiceExtensionContext context) {
context.registerService(JsonObjectValidatorRegistry.class, validatorRegistry);
context.registerService(ProtocolVersionRegistry.class, versionRegistry);
context.registerService(DspProtocolTypeTransformerRegistry.class, dspTransformerRegistry);

when(dspTransformerRegistry.forProtocol(any())).thenReturn(Result.success(mock()));
}

@Test
Expand All @@ -53,6 +60,7 @@ void shouldRegisterMessageValidator(DspCatalogApiExtension extension, ServiceExt
void shouldRegisterDspVersion(DspCatalogApiExtension extension, ServiceExtensionContext context) {
extension.initialize(context);

verify(versionRegistry).register(isA(ProtocolVersion.class));
verify(versionRegistry).register(V_08);
verify(versionRegistry).register(V_2024_1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.edc.protocol.dsp.catalog.http.dispatcher.delegate.ByteArrayBodyExtractor;
import org.eclipse.edc.protocol.dsp.http.dispatcher.GetDspHttpRequestFactory;
import org.eclipse.edc.protocol.dsp.http.dispatcher.PostDspHttpRequestFactory;
import org.eclipse.edc.protocol.dsp.http.spi.DspProtocolParser;
import org.eclipse.edc.protocol.dsp.http.spi.dispatcher.DspHttpRemoteMessageDispatcher;
import org.eclipse.edc.protocol.dsp.http.spi.serialization.JsonLdRemoteMessageSerializer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
Expand All @@ -43,6 +44,8 @@ public class DspCatalogHttpDispatcherExtension implements ServiceExtension {
private DspHttpRemoteMessageDispatcher messageDispatcher;
@Inject
private JsonLdRemoteMessageSerializer remoteMessageSerializer;
@Inject
private DspProtocolParser dspProtocolParser;

@Override
public String name() {
Expand All @@ -55,12 +58,12 @@ public void initialize(ServiceExtensionContext context) {

messageDispatcher.registerMessage(
CatalogRequestMessage.class,
new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + CATALOG_REQUEST),
new PostDspHttpRequestFactory<>(remoteMessageSerializer, dspProtocolParser, m -> BASE_PATH + CATALOG_REQUEST),
byteArrayBodyExtractor
);
messageDispatcher.registerMessage(
DatasetRequestMessage.class,
new GetDspHttpRequestFactory<>(m -> BASE_PATH + DATASET_REQUEST + "/" + m.getDatasetId()),
new GetDspHttpRequestFactory<>(dspProtocolParser, m -> BASE_PATH + DATASET_REQUEST + "/" + m.getDatasetId()),
byteArrayBodyExtractor
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class ByteArrayBodyExtractor implements DspHttpResponseBodyExtractor<byte[]> {
@Override
public byte[] extractBody(ResponseBody responseBody) {
public byte[] extractBody(ResponseBody responseBody, String protocol) {
try {
if (responseBody == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ void shouldReturnBodyAsBytes() throws IOException {
var bytes = "test".getBytes();
when(responseBody.bytes()).thenReturn(bytes);

var result = extractor.extractBody(responseBody);
var result = extractor.extractBody(responseBody, "protocol");

assertThat(result).isEqualTo(bytes);
}

@Test
void shouldReturnNull_whenBodyIsNull() {
var result = extractor.extractBody(null);
var result = extractor.extractBody(null, "protocol");

assertThat(result).isNull();
}
Expand All @@ -52,7 +52,7 @@ void shouldThrowException_whenCannotExtractBytes() throws IOException {
var responseBody = mock(ResponseBody.class);
when(responseBody.bytes()).thenThrow(new IOException());

assertThatThrownBy(() -> extractor.extractBody(responseBody)).isInstanceOf(EdcException.class);
assertThatThrownBy(() -> extractor.extractBody(responseBody, "protocol")).isInstanceOf(EdcException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.protocol.dsp.catalog.transform;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogRequestMessageTransformer;
import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogTransformer;
Expand All @@ -31,6 +32,8 @@

import java.util.Map;

import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_2024_1;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
Expand All @@ -57,10 +60,16 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var jsonFactory = Json.createBuilderFactory(Map.of());
var mapper = typeManager.getMapper(JSON_LD);

var dspApiTransformerRegistry = registry.forContext("dsp-api");
registerTransformers(DSP_TRANSFORMER_CONTEXT_V_08, mapper);
registerTransformers(DSP_TRANSFORMER_CONTEXT_V_2024_1, mapper);
}

private void registerTransformers(String version, ObjectMapper mapper) {
var jsonFactory = Json.createBuilderFactory(Map.of());

var dspApiTransformerRegistry = registry.forContext(version);
dspApiTransformerRegistry.register(new JsonObjectFromCatalogRequestMessageTransformer(jsonFactory));
dspApiTransformerRegistry.register(new JsonObjectToCatalogRequestMessageTransformer());

Expand All @@ -69,4 +78,4 @@ public void initialize(ServiceExtensionContext context) {
dspApiTransformerRegistry.register(new JsonObjectFromDistributionTransformer(jsonFactory));
dspApiTransformerRegistry.register(new JsonObjectFromDataServiceTransformer(jsonFactory));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.protocol.dsp.http.api.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import org.eclipse.edc.connector.controlplane.transform.edc.from.JsonObjectFromAssetTransformer;
import org.eclipse.edc.connector.controlplane.transform.edc.to.JsonObjectToAssetTransformer;
Expand Down Expand Up @@ -62,6 +63,8 @@
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_PREFIX;
import static org.eclipse.edc.policy.model.OdrlNamespace.ODRL_SCHEMA;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_TRANSFORMER_CONTEXT_V_2024_1;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;
Expand Down Expand Up @@ -132,17 +135,18 @@ public void initialize(ServiceExtensionContext context) {
webService.registerResource(ApiContext.PROTOCOL, new ObjectMapperProvider(jsonLdMapper));
webService.registerResource(ApiContext.PROTOCOL, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE));

registerTransformers();
}

private void registerTransformers() {
var mapper = typeManager.getMapper(JSON_LD);
mapper.registerSubtypes(AtomicConstraint.class, LiteralExpression.class);

registerTransformers(DSP_TRANSFORMER_CONTEXT_V_08, mapper);
registerTransformers(DSP_TRANSFORMER_CONTEXT_V_2024_1, mapper);
}

private void registerTransformers(String version, ObjectMapper mapper) {
var jsonBuilderFactory = Json.createBuilderFactory(Map.of());

// EDC model to JSON-LD transformers
var dspApiTransformerRegistry = transformerRegistry.forContext("dsp-api");
var dspApiTransformerRegistry = transformerRegistry.forContext(version);
dspApiTransformerRegistry.register(new JsonObjectFromPolicyTransformer(jsonBuilderFactory, participantIdMapper));
dspApiTransformerRegistry.register(new JsonObjectFromAssetTransformer(jsonBuilderFactory, mapper));
dspApiTransformerRegistry.register(new JsonObjectFromDataAddressDspaceTransformer(jsonBuilderFactory, mapper));
Expand Down
Loading

0 comments on commit f449c33

Please sign in to comment.