Skip to content

Commit

Permalink
feat: introduces dymanic feature binding in WebService
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 17, 2024
1 parent faf3c95 commit cf1eaff
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ dependencies {
api(project(":spi:common:json-ld-spi"))

api(project(":spi:control-plane:control-plane-spi"))


implementation(project(":extensions:common:http:lib:jersey-providers-lib"))
implementation(project(":data-protocols:dsp:dsp-catalog:lib:dsp-catalog-validation-lib"))

implementation(libs.jakarta.rsApi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_2024_1;
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;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
* Creates and registers the controller for dataspace protocol catalog requests.
Expand Down Expand Up @@ -72,7 +77,8 @@ public class DspCatalogApiExtension implements ServiceExtension {
private TypeTransformerRegistry transformerRegistry;
@Inject
private Monitor monitor;

@Inject
private TypeManager typeManager;
@Inject
private JsonLd jsonLd;

Expand All @@ -84,10 +90,12 @@ public String name() {
@Override
public void initialize(ServiceExtensionContext context) {
validatorRegistry.register(DSPACE_TYPE_CATALOG_REQUEST_MESSAGE_IRI, CatalogRequestMessageValidator.instance(criterionOperatorRegistry));

var jsonLdMapper = typeManager.getMapper(JSON_LD);

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)));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspCatalogApiController.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_08));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspCatalogApiController20241.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_2024_1));

dataServiceRegistry.register(DataService.Builder.newInstance()
.endpointDescription("dspace:connector")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToCriterionTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToQuerySpecTransformer;
import org.eclipse.edc.transform.transformer.edc.to.JsonValueToGenericTypeTransformer;
import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.jersey.providers.jsonld.ObjectMapperProvider;
import org.eclipse.edc.web.spi.WebServer;
import org.eclipse.edc.web.spi.WebService;
Expand All @@ -62,7 +61,8 @@
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
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_SCOPE_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_2024_1;
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;
Expand Down Expand Up @@ -124,16 +124,10 @@ public void initialize(ServiceExtensionContext context) {
var jsonLdMapper = typeManager.getMapper(JSON_LD);

// registers ns for DSP scope
jsonLd.registerNamespace(DCAT_PREFIX, DCAT_SCHEMA, DSP_SCOPE);
jsonLd.registerNamespace(DCT_PREFIX, DCT_SCHEMA, DSP_SCOPE);
jsonLd.registerNamespace(ODRL_PREFIX, ODRL_SCHEMA, DSP_SCOPE);
jsonLd.registerNamespace(DSPACE_PREFIX, DSPACE_SCHEMA, DSP_SCOPE);
jsonLd.registerNamespace(VOCAB, EDC_NAMESPACE, DSP_SCOPE);
jsonLd.registerNamespace(EDC_PREFIX, EDC_NAMESPACE, DSP_SCOPE);

registerNamespaces(DSP_SCOPE_V_08);
registerNamespaces(DSP_SCOPE_V_2024_1);

webService.registerResource(ApiContext.PROTOCOL, new ObjectMapperProvider(jsonLdMapper));
webService.registerResource(ApiContext.PROTOCOL, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE));

var mapper = typeManager.getMapper(JSON_LD);
mapper.registerSubtypes(AtomicConstraint.class, LiteralExpression.class);
Expand All @@ -142,6 +136,15 @@ public void initialize(ServiceExtensionContext context) {
registerTransformers(DSP_TRANSFORMER_CONTEXT_V_2024_1, mapper);
}

private void registerNamespaces(String scope) {
jsonLd.registerNamespace(DCAT_PREFIX, DCAT_SCHEMA, scope);
jsonLd.registerNamespace(DCT_PREFIX, DCT_SCHEMA, scope);
jsonLd.registerNamespace(ODRL_PREFIX, ODRL_SCHEMA, scope);
jsonLd.registerNamespace(DSPACE_PREFIX, DSPACE_SCHEMA, scope);
jsonLd.registerNamespace(VOCAB, EDC_NAMESPACE, scope);
jsonLd.registerNamespace(EDC_PREFIX, EDC_NAMESPACE, scope);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
api(project(":extensions:common:json-ld"))

implementation(project(":data-protocols:dsp:dsp-negotiation:lib:dsp-negotiation-validation-lib"))
implementation(project(":extensions:common:http:lib:jersey-providers-lib"))

implementation(libs.jakarta.rsApi)

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

import org.eclipse.edc.connector.controlplane.services.spi.contractnegotiation.ContractNegotiationProtocolService;
import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.http.spi.message.DspRequestHandler;
import org.eclipse.edc.protocol.dsp.negotiation.http.api.controller.DspNegotiationApiController;
import org.eclipse.edc.protocol.dsp.negotiation.http.api.controller.DspNegotiationApiController20241;
Expand All @@ -29,10 +30,14 @@
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_2024_1;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_IRI;
Expand All @@ -41,6 +46,7 @@
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_REQUEST_MESSAGE_IRI;
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.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
* Creates and registers the controller for dataspace protocol negotiation requests.
Expand All @@ -61,13 +67,21 @@ public class DspNegotiationApiExtension implements ServiceExtension {
@Inject
private ProtocolVersionRegistry versionRegistry;

@Inject
private JsonLd jsonLd;

@Inject
private TypeManager typeManager;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
var jsonLdMapper = typeManager.getMapper(JSON_LD);

validatorRegistry.register(DSPACE_TYPE_CONTRACT_REQUEST_MESSAGE_IRI, ContractRequestMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_CONTRACT_OFFER_MESSAGE_IRI, ContractOfferMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_CONTRACT_NEGOTIATION_EVENT_MESSAGE_IRI, ContractNegotiationEventMessageValidator.instance());
Expand All @@ -77,6 +91,8 @@ public void initialize(ServiceExtensionContext context) {

webService.registerResource(ApiContext.PROTOCOL, new DspNegotiationApiController(protocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspNegotiationApiController20241(protocolService, dspRequestHandler));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspNegotiationApiController.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_08));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspNegotiationApiController20241.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_2024_1));

versionRegistry.register(V_2024_1);
versionRegistry.register(V_08);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface DspConstants {

String DSP_CONTEXT_SEPARATOR = ":";
String DSP_SCOPE = "DSP";
String DSP_SCOPE_V_08 = DSP_SCOPE + DSP_CONTEXT_SEPARATOR + V_08_VERSION;
String DSP_SCOPE_V_2024_1 = DSP_SCOPE + DSP_CONTEXT_SEPARATOR + V_2024_1_VERSION;
String DSP_TRANSFORMER_CONTEXT = "dsp-api";
String DSP_TRANSFORMER_CONTEXT_V_08 = DSP_TRANSFORMER_CONTEXT + DSP_CONTEXT_SEPARATOR + V_08_VERSION;
String DSP_TRANSFORMER_CONTEXT_V_2024_1 = DSP_TRANSFORMER_CONTEXT + DSP_CONTEXT_SEPARATOR + V_2024_1_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {

implementation(project(":spi:common:json-ld-spi"))
implementation(project(":data-protocols:dsp:dsp-transfer-process:lib:dsp-transfer-process-validation-lib"))
implementation(project(":extensions:common:http:lib:jersey-providers-lib"))

implementation(libs.jakarta.rsApi)

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

import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry;
import org.eclipse.edc.connector.controlplane.services.spi.transferprocess.TransferProcessProtocolService;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.http.spi.message.DspRequestHandler;
import org.eclipse.edc.protocol.dsp.transferprocess.http.api.controller.DspTransferProcessApiController;
import org.eclipse.edc.protocol.dsp.transferprocess.http.api.controller.DspTransferProcessApiController20241;
Expand All @@ -27,16 +28,21 @@
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_08;
import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_2024_1;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_REQUEST_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_START_MESSAGE_IRI;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_IRI;
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.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
* Creates and registers the controller for dataspace protocol transfer process requests.
Expand All @@ -55,16 +61,24 @@ public class DspTransferProcessApiExtension implements ServiceExtension {
private JsonObjectValidatorRegistry validatorRegistry;
@Inject
private ProtocolVersionRegistry versionRegistry;
@Inject
private JsonLd jsonLd;
@Inject
private TypeManager typeManager;

@Override
public void initialize(ServiceExtensionContext context) {
var jsonLdMapper = typeManager.getMapper(JSON_LD);

validatorRegistry.register(DSPACE_TYPE_TRANSFER_REQUEST_MESSAGE_IRI, TransferRequestMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_TRANSFER_START_MESSAGE_IRI, TransferStartMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE_IRI, TransferCompletionMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE_IRI, TransferTerminationMessageValidator.instance());

webService.registerResource(ApiContext.PROTOCOL, new DspTransferProcessApiController(transferProcessProtocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspTransferProcessApiController20241(transferProcessProtocolService, dspRequestHandler));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspTransferProcessApiController.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_08));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspTransferProcessApiController20241.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_2024_1));

versionRegistry.register(V_2024_1);
versionRegistry.register(V_08);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api(project(":spi:common:transform-spi"))
api(project(":spi:common:web-spi"))
api(project(":data-protocols:dsp:dsp-http-spi"))
implementation(project(":extensions:common:http:lib:jersey-providers-lib"))

testImplementation(project(":core:common:junit"))
testImplementation(project(":extensions:common:json-ld"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@

import jakarta.json.Json;
import org.eclipse.edc.connector.controlplane.services.spi.protocol.VersionProtocolService;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.http.spi.message.DspRequestHandler;
import org.eclipse.edc.protocol.dsp.version.http.api.transformer.JsonObjectFromProtocolVersionsTransformer;
import org.eclipse.edc.protocol.dsp.version.http.api.transformer.JsonObjectFromVersionsError;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.web.jersey.providers.jsonld.JerseyJsonLdInterceptor;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import java.util.Map;

import static org.eclipse.edc.protocol.dsp.spi.type.DspConstants.DSP_SCOPE_V_08;
import static org.eclipse.edc.protocol.dsp.version.http.api.DspVersionApiExtension.NAME;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
* Provide API for the protocol versions.
Expand All @@ -51,17 +56,26 @@ public class DspVersionApiExtension implements ServiceExtension {
@Inject
private VersionProtocolService service;

@Inject
private JsonLd jsonLd;
@Inject
private TypeManager typeManager;


@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
var jsonLdMapper = typeManager.getMapper(JSON_LD);

transformerRegistry.register(new JsonObjectFromProtocolVersionsTransformer());
transformerRegistry.register(new JsonObjectFromVersionsError(Json.createBuilderFactory(Map.of())));

webService.registerResource(ApiContext.PROTOCOL, new DspVersionApiController(requestHandler, service));
webService.registerDynamicResource(ApiContext.PROTOCOL, DspVersionApiController.class, new JerseyJsonLdInterceptor(jsonLd, jsonLdMapper, DSP_SCOPE_V_08));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.web.jersey.feature.DynamicResourceFeature;
import org.eclipse.edc.web.jersey.mapper.EdcApiExceptionMapper;
import org.eclipse.edc.web.jersey.mapper.UnexpectedExceptionMapper;
import org.eclipse.edc.web.jersey.providers.jsonld.ObjectMapperProvider;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class JerseyRestService implements WebService {
private final Monitor monitor;

private final Map<String, List<Object>> controllers = new HashMap<>();
private final Map<String, Map<Class<?>, List<Object>>> dynamicResources = new HashMap<>();
private final JerseyConfiguration configuration;
private final List<Supplier<Object>> additionalInstances = new ArrayList<>();

Expand All @@ -67,6 +69,14 @@ public void registerResource(String contextAlias, Object resource) {
.add(resource);
}

@Override
public void registerDynamicResource(String contextAlias, Class<?> target, Object resource) {
dynamicResources
.computeIfAbsent(contextAlias, s -> new HashMap<>())
.computeIfAbsent(target, s -> new ArrayList<>())
.add(resource);
}

void registerInstance(Supplier<Object> instance) {
additionalInstances.add(instance);
}
Expand All @@ -82,6 +92,8 @@ public void start() {
private void registerContext(String contextAlias, List<Object> controllers) {
var resourceConfig = new ResourceConfig();

var dynamicResourcesForContext = dynamicResources.computeIfAbsent(contextAlias, s -> new HashMap<>());

// Disable WADL as it is not used and emits a warning message about JAXB (which is also not used)
resourceConfig.property(WADL_FEATURE_DISABLE, Boolean.TRUE);

Expand All @@ -92,6 +104,7 @@ private void registerContext(String contextAlias, List<Object> controllers) {
resourceConfig.registerInstances(new ObjectMapperProvider(typeManager.getMapper()));
resourceConfig.registerInstances(new EdcApiExceptionMapper());
resourceConfig.registerInstances(new UnexpectedExceptionMapper(monitor));
resourceConfig.registerInstances(new DynamicResourceFeature(dynamicResourcesForContext));

additionalInstances.forEach(supplier -> resourceConfig.registerInstances(supplier.get()));

Expand Down
Loading

0 comments on commit cf1eaff

Please sign in to comment.