Skip to content

Commit

Permalink
refactor: cleanup API config (#4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Jun 3, 2024
1 parent 968f138 commit 564d627
Show file tree
Hide file tree
Showing 103 changed files with 502 additions and 735 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
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.configuration.DspApiConfiguration;
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.protocol.ProtocolWebhook;
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.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
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;
import static org.eclipse.edc.protocol.dsp.spi.version.DspVersions.V_2024_1;
Expand All @@ -49,7 +50,7 @@ public class DspCatalogApiExtension implements ServiceExtension {
@Inject
private WebService webService;
@Inject
private DspApiConfiguration apiConfiguration;
private ProtocolWebhook protocolWebhook;
@Inject
private CatalogProtocolService service;
@Inject
Expand Down Expand Up @@ -78,12 +79,12 @@ public void initialize(ServiceExtensionContext context) {

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

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

versionRegistry.register(V_2024_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.policy.model.AtomicConstraint;
import org.eclipse.edc.policy.model.LiteralExpression;
import org.eclipse.edc.protocol.dsp.http.spi.configuration.DspApiConfiguration;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provides;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.runtime.metamodel.annotation.SettingContext;
import org.eclipse.edc.spi.agent.ParticipantIdMapper;
import org.eclipse.edc.spi.protocol.ProtocolWebhook;
import org.eclipse.edc.spi.system.Hostname;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
Expand All @@ -43,11 +45,13 @@
import org.eclipse.edc.web.jersey.providers.jsonld.ObjectMapperProvider;
import org.eclipse.edc.web.spi.WebServer;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurer;
import org.eclipse.edc.web.spi.configuration.WebServiceSettings;

import java.util.Map;

import static java.lang.String.format;
import static org.eclipse.edc.jsonld.spi.Namespaces.DCAT_PREFIX;
import static org.eclipse.edc.jsonld.spi.Namespaces.DCAT_SCHEMA;
import static org.eclipse.edc.jsonld.spi.Namespaces.DCT_PREFIX;
Expand All @@ -60,32 +64,28 @@
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

/**
* Provides the configuration for the Dataspace Protocol API context. Creates the API context
* using {@link #DEFAULT_PROTOCOL_PORT} and {@link #DEFAULT_PROTOCOL_API_PATH}, if no respective
* settings are provided. Configures the API context to allow Jakarta JSON-API types as endpoint
* parameters.
* Configure 'protocol' api context.
*/
@Extension(value = DspApiConfigurationExtension.NAME)
@Provides({ DspApiConfiguration.class, ProtocolWebhook.class })
@Provides(ProtocolWebhook.class)
public class DspApiConfigurationExtension implements ServiceExtension {

public static final String NAME = "Dataspace Protocol API Configuration Extension";

public static final String CONTEXT_ALIAS = "protocol";

public static final String DEFAULT_DSP_CALLBACK_ADDRESS = "http://localhost:8282/api/v1/dsp";
@Setting(value = "Configures endpoint for reaching the Protocol API.", defaultValue = "<hostname:protocol.port/protocol.path>")
public static final String DSP_CALLBACK_ADDRESS = "edc.dsp.callback.address";

public static final int DEFAULT_PROTOCOL_PORT = 8282;
public static final String DEFAULT_PROTOCOL_API_PATH = "/api/v1/dsp";
@SettingContext("Protocol API context setting key")
private static final String PROTOCOL_CONFIG_KEY = "web.http." + ApiContext.PROTOCOL;

public static final WebServiceSettings SETTINGS = WebServiceSettings.Builder.newInstance()
.apiConfigKey("web.http.protocol")
.contextAlias(CONTEXT_ALIAS)
.defaultPath(DEFAULT_PROTOCOL_API_PATH)
.defaultPort(DEFAULT_PROTOCOL_PORT)
.apiConfigKey(PROTOCOL_CONFIG_KEY)
.contextAlias(ApiContext.PROTOCOL)
.defaultPath("/api/v1/dsp")
.defaultPort(8282)
.name("Protocol API")
.build();

@Inject
private TypeManager typeManager;
@Inject
Expand All @@ -100,6 +100,8 @@ public class DspApiConfigurationExtension implements ServiceExtension {
private TypeTransformerRegistry transformerRegistry;
@Inject
private ParticipantIdMapper participantIdMapper;
@Inject
private Hostname hostname;

@Override
public String name() {
Expand All @@ -108,22 +110,21 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var config = configurator.configure(context, webServer, SETTINGS);
var dspWebhookAddress = context.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS);
context.registerService(DspApiConfiguration.class, new DspApiConfiguration(config.getContextAlias(), dspWebhookAddress));
var contextConfig = context.getConfig(PROTOCOL_CONFIG_KEY);
var apiConfiguration = configurator.configure(contextConfig, webServer, SETTINGS);
var dspWebhookAddress = context.getSetting(DSP_CALLBACK_ADDRESS, format("http://%s:%s%s", hostname.get(), apiConfiguration.getPort(), apiConfiguration.getPath()));
context.registerService(ProtocolWebhook.class, () -> dspWebhookAddress);

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);

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

registerTransformers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
package org.eclipse.edc.protocol.dsp.http.api.configuration;

import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.protocol.dsp.http.spi.configuration.DspApiConfiguration;
import org.eclipse.edc.spi.protocol.ProtocolWebhook;
import org.eclipse.edc.spi.system.Hostname;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.configuration.ConfigFactory;
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.jersey.providers.jsonld.ObjectMapperProvider;
import org.eclipse.edc.web.spi.WebServer;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;
import org.eclipse.edc.web.spi.configuration.WebServiceConfiguration;
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurer;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -33,8 +36,6 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.protocol.dsp.http.api.configuration.DspApiConfigurationExtension.CONTEXT_ALIAS;
import static org.eclipse.edc.protocol.dsp.http.api.configuration.DspApiConfigurationExtension.DEFAULT_DSP_CALLBACK_ADDRESS;
import static org.eclipse.edc.protocol.dsp.http.api.configuration.DspApiConfigurationExtension.DSP_CALLBACK_ADDRESS;
import static org.eclipse.edc.protocol.dsp.http.api.configuration.DspApiConfigurationExtension.SETTINGS;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -58,55 +59,50 @@ void setUp(ServiceExtensionContext context) {
context.registerService(WebService.class, webService);
context.registerService(WebServiceConfigurer.class, configurer);
context.registerService(TypeManager.class, typeManager);
context.registerService(Hostname.class, () -> "hostname");
TypeTransformerRegistry typeTransformerRegistry = mock();
when(typeTransformerRegistry.forContext(any())).thenReturn(mock());
context.registerService(TypeTransformerRegistry.class, typeTransformerRegistry);

var webServiceConfiguration = WebServiceConfiguration.Builder.newInstance()
.contextAlias(CONTEXT_ALIAS)
.path("/path")
.port(1234)
.build();
when(configurer.configure(any(), any(), any())).thenReturn(webServiceConfiguration);
when(configurer.configure(any(Config.class), any(), any())).thenReturn(webServiceConfiguration);
when(typeManager.getMapper(any())).thenReturn(mock());
}

@Test
void initialize_noSettingsProvided_useDspDefault(DspApiConfigurationExtension extension, ServiceExtensionContext context) {
void shouldComposeProtocolWebhook_whenNotConfigured(DspApiConfigurationExtension extension, ServiceExtensionContext context) {
when(context.getConfig()).thenReturn(ConfigFactory.empty());
when(context.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS)).thenReturn(DEFAULT_DSP_CALLBACK_ADDRESS);

extension.initialize(context);

verify(configurer).configure(context, webServer, SETTINGS);
var apiConfig = context.getService(DspApiConfiguration.class);
assertThat(apiConfig.getContextAlias()).isEqualTo(CONTEXT_ALIAS);
assertThat(apiConfig.getDspCallbackAddress()).isEqualTo(DEFAULT_DSP_CALLBACK_ADDRESS);
verify(configurer).configure(any(Config.class), eq(webServer), eq(SETTINGS));
assertThat(context.getService(ProtocolWebhook.class).url()).isEqualTo("http://hostname:1234/path");
}

@Test
void initialize_settingsProvided_useSettings(DspApiConfigurationExtension extension, ServiceExtensionContext context) {
void shouldUseConfiguredProtocolWebhook(DspApiConfigurationExtension extension, ServiceExtensionContext context) {
var webhookAddress = "http://webhook";
when(context.getConfig()).thenReturn(ConfigFactory.fromMap(Map.of(
"web.http.protocol.port", String.valueOf(1234),
"web.http.protocol.path", "/path"))
);
when(context.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS)).thenReturn(webhookAddress);
when(context.getSetting(eq(DSP_CALLBACK_ADDRESS), any())).thenReturn(webhookAddress);

extension.initialize(context);

verify(configurer).configure(context, webServer, SETTINGS);
var apiConfig = context.getService(DspApiConfiguration.class);
assertThat(apiConfig.getContextAlias()).isEqualTo(CONTEXT_ALIAS);
assertThat(apiConfig.getDspCallbackAddress()).isEqualTo(webhookAddress);
verify(configurer).configure(any(Config.class), eq(webServer), eq(SETTINGS));
assertThat(context.getService(ProtocolWebhook.class).url()).isEqualTo("http://webhook");
}

@Test
void initialize_shouldRegisterWebServiceProviders(DspApiConfigurationExtension extension, ServiceExtensionContext context) {
extension.initialize(context);

verify(webService).registerResource(eq(CONTEXT_ALIAS), isA(ObjectMapperProvider.class));
verify(webService).registerResource(eq(CONTEXT_ALIAS), isA(JerseyJsonLdInterceptor.class));
verify(webService).registerResource(eq(ApiContext.PROTOCOL), isA(ObjectMapperProvider.class));
verify(webService).registerResource(eq(ApiContext.PROTOCOL), isA(JerseyJsonLdInterceptor.class));
}

}

This file was deleted.

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

import org.eclipse.edc.connector.controlplane.services.spi.contractnegotiation.ContractNegotiationProtocolService;
import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry;
import org.eclipse.edc.protocol.dsp.http.spi.configuration.DspApiConfiguration;
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 @@ -32,6 +31,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE;
Expand All @@ -52,8 +52,6 @@ public class DspNegotiationApiExtension implements ServiceExtension {
@Inject
private WebService webService;
@Inject
private DspApiConfiguration apiConfiguration;
@Inject
private ContractNegotiationProtocolService protocolService;
@Inject
private JsonObjectValidatorRegistry validatorRegistry;
Expand All @@ -76,8 +74,8 @@ public void initialize(ServiceExtensionContext context) {
validatorRegistry.register(DSPACE_TYPE_CONTRACT_AGREEMENT_VERIFICATION_MESSAGE, ContractAgreementVerificationMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_CONTRACT_NEGOTIATION_TERMINATION_MESSAGE, ContractNegotiationTerminationMessageValidator.instance());

webService.registerResource(apiConfiguration.getContextAlias(), new DspNegotiationApiController(protocolService, dspRequestHandler));
webService.registerResource(apiConfiguration.getContextAlias(), new DspNegotiationApiController20241(protocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspNegotiationApiController(protocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspNegotiationApiController20241(protocolService, dspRequestHandler));

versionRegistry.register(V_2024_1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.eclipse.edc.connector.controlplane.services.spi.protocol.ProtocolVersionRegistry;
import org.eclipse.edc.connector.controlplane.services.spi.transferprocess.TransferProcessProtocolService;
import org.eclipse.edc.protocol.dsp.http.spi.configuration.DspApiConfiguration;
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 @@ -30,6 +29,7 @@
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;
import org.eclipse.edc.web.spi.configuration.ApiContext;

import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE;
import static org.eclipse.edc.protocol.dsp.spi.type.DspTransferProcessPropertyAndTypeNames.DSPACE_TYPE_TRANSFER_REQUEST_MESSAGE;
Expand All @@ -45,8 +45,6 @@ public class DspTransferProcessApiExtension implements ServiceExtension {

public static final String NAME = "Dataspace Protocol: TransferProcess API Extension";
@Inject
private DspApiConfiguration config;
@Inject
private WebService webService;
@Inject
private TransferProcessProtocolService transferProcessProtocolService;
Expand All @@ -64,8 +62,8 @@ public void initialize(ServiceExtensionContext context) {
validatorRegistry.register(DSPACE_TYPE_TRANSFER_COMPLETION_MESSAGE, TransferCompletionMessageValidator.instance());
validatorRegistry.register(DSPACE_TYPE_TRANSFER_TERMINATION_MESSAGE, TransferTerminationMessageValidator.instance());

webService.registerResource(config.getContextAlias(), new DspTransferProcessApiController(transferProcessProtocolService, dspRequestHandler));
webService.registerResource(config.getContextAlias(), new DspTransferProcessApiController20241(transferProcessProtocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspTransferProcessApiController(transferProcessProtocolService, dspRequestHandler));
webService.registerResource(ApiContext.PROTOCOL, new DspTransferProcessApiController20241(transferProcessProtocolService, dspRequestHandler));

versionRegistry.register(V_2024_1);
}
Expand Down
Loading

0 comments on commit 564d627

Please sign in to comment.