forked from eclipse-edc/Connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dataspace protocol API configuration (eclipse-edc#2670)
- Loading branch information
1 parent
fd0ee52
commit 1000488
Showing
7 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
* | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
} | ||
|
||
dependencies { | ||
api(project(":spi:common:core-spi")) | ||
api(project(":extensions:common:http")) | ||
api(project(":extensions:common:json-ld")) | ||
|
||
implementation(root.jakarta.rsApi) | ||
|
||
testImplementation(project(":core:common:junit")) | ||
} |
39 changes: 39 additions & 0 deletions
39
...ion/src/main/java/org/eclipse/edc/protocol/dsp/api/configuration/DspApiConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.api.configuration; | ||
|
||
/** | ||
* Holds configuration information for the Dataspace Protocol API context. This is includes the | ||
* context alias used for registering resources as well as the callback address. | ||
*/ | ||
public class DspApiConfiguration { | ||
|
||
private final String contextAlias; | ||
private final String dspCallbackAddress; | ||
|
||
public DspApiConfiguration(String contextAlias, String dspCallbackAddress) { | ||
this.contextAlias = contextAlias; | ||
this.dspCallbackAddress = dspCallbackAddress; | ||
} | ||
|
||
public String getContextAlias() { | ||
return contextAlias; | ||
} | ||
|
||
public String getDspCallbackAddress() { | ||
return dspCallbackAddress; | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
...ain/java/org/eclipse/edc/protocol/dsp/api/configuration/DspApiConfigurationExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.api.configuration; | ||
|
||
import org.eclipse.edc.jsonld.transformer.JsonLdTransformerRegistry; | ||
import org.eclipse.edc.protocol.dsp.api.configuration.serdes.ObjectMapperProvider; | ||
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.Requires; | ||
import org.eclipse.edc.spi.iam.IdentityService; | ||
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.web.spi.WebServer; | ||
import org.eclipse.edc.web.spi.WebService; | ||
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurer; | ||
import org.eclipse.edc.web.spi.configuration.WebServiceSettings; | ||
|
||
import static org.eclipse.edc.jsonld.JsonLdExtension.TYPE_MANAGER_CONTEXT_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. | ||
*/ | ||
@Extension(value = DspApiConfigurationExtension.NAME) | ||
@Provides({ DspApiConfiguration.class }) | ||
@Requires({ JsonLdTransformerRegistry.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"; | ||
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"; | ||
|
||
public static final WebServiceSettings SETTINGS = WebServiceSettings.Builder.newInstance() | ||
.apiConfigKey("web.http.protocol") | ||
.contextAlias(CONTEXT_ALIAS) | ||
.defaultPath(DEFAULT_PROTOCOL_API_PATH) | ||
.defaultPort(DEFAULT_PROTOCOL_PORT) | ||
.name("Protocol API") | ||
.build(); | ||
|
||
@Inject | ||
private TypeManager typeManager; | ||
@Inject | ||
private WebService webService; | ||
@Inject | ||
private WebServer webServer; | ||
@Inject | ||
private WebServiceConfigurer configurator; | ||
@Inject | ||
private IdentityService identityService; | ||
|
||
@Override | ||
public String name() { | ||
return 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 jsonLdMapper = typeManager.getMapper(TYPE_MANAGER_CONTEXT_JSON_LD); | ||
webService.registerResource(config.getContextAlias(), new ObjectMapperProvider(jsonLdMapper)); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...main/java/org/eclipse/edc/protocol/dsp/api/configuration/serdes/ObjectMapperProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.api.configuration.serdes; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.ws.rs.ext.ContextResolver; | ||
|
||
/** | ||
* Provides an ObjectMapper to be used for parsing incoming requests. A custom ObjectMapper that supports the | ||
* Jakarta JSON API is required to allow JsonObject as a controller parameter. | ||
*/ | ||
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public ObjectMapperProvider(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public ObjectMapper getContext(Class<?> type) { | ||
return objectMapper; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...guration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# | ||
# Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Apache License, Version 2.0 which is available at | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Contributors: | ||
# Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
# | ||
|
||
org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfigurationExtension |
101 changes: 101 additions & 0 deletions
101
...java/org/eclipse/edc/protocol/dsp/api/configuration/DspApiConfigurationExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Fraunhofer Institute for Software and Systems Engineering - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.api.configuration; | ||
|
||
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | ||
import org.eclipse.edc.spi.iam.IdentityService; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.spi.system.configuration.ConfigFactory; | ||
import org.eclipse.edc.spi.system.injection.ObjectFactory; | ||
import org.eclipse.edc.spi.types.TypeManager; | ||
import org.eclipse.edc.web.spi.WebServer; | ||
import org.eclipse.edc.web.spi.WebService; | ||
import org.eclipse.edc.web.spi.configuration.WebServiceConfiguration; | ||
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurer; | ||
import org.eclipse.edc.web.spi.configuration.WebServiceConfigurerImpl; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfigurationExtension.CONTEXT_ALIAS; | ||
import static org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfigurationExtension.DEFAULT_DSP_CALLBACK_ADDRESS; | ||
import static org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfigurationExtension.DSP_CALLBACK_ADDRESS; | ||
import static org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfigurationExtension.SETTINGS; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(DependencyInjectionExtension.class) | ||
class DspApiConfigurationExtensionTest { | ||
|
||
private DspApiConfigurationExtension extension; | ||
private WebServiceConfigurer configurer = mock(WebServiceConfigurerImpl.class); | ||
private WebServer webServer = mock(WebServer.class); | ||
|
||
@BeforeEach | ||
void setUp(ServiceExtensionContext context, ObjectFactory factory) { | ||
context.registerService(TypeManager.class, mock(TypeManager.class)); | ||
context.registerService(WebService.class, mock(WebService.class)); | ||
context.registerService(WebServer.class, webServer); | ||
context.registerService(WebServiceConfigurer.class, configurer); | ||
context.registerService(IdentityService.class, mock(IdentityService.class)); | ||
extension = factory.constructInstance(DspApiConfigurationExtension.class); | ||
|
||
var webServiceConfiguration = WebServiceConfiguration.Builder.newInstance() | ||
.contextAlias(CONTEXT_ALIAS) | ||
.path("/path") | ||
.port(1234) | ||
.build(); | ||
when(configurer.configure(any(), any(), any())).thenReturn(webServiceConfiguration); | ||
} | ||
|
||
@Test | ||
void initialize_noSettingsProvided_useDspDefault(ServiceExtensionContext context) { | ||
var spyContext = spy(context); | ||
when(spyContext.getConfig()).thenReturn(ConfigFactory.empty()); | ||
when(spyContext.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS)).thenReturn(DEFAULT_DSP_CALLBACK_ADDRESS); | ||
|
||
extension.initialize(spyContext); | ||
|
||
verify(configurer).configure(spyContext, webServer, SETTINGS); | ||
var apiConfig = spyContext.getService(DspApiConfiguration.class); | ||
assertThat(apiConfig.getContextAlias()).isEqualTo(CONTEXT_ALIAS); | ||
assertThat(apiConfig.getDspCallbackAddress()).isEqualTo(DEFAULT_DSP_CALLBACK_ADDRESS); | ||
} | ||
|
||
@Test | ||
void initialize_settingsProvided_useSettings(ServiceExtensionContext context) { | ||
var webhookAddress = "http://webhook"; | ||
|
||
var spyContext = spy(context); | ||
when(spyContext.getConfig()).thenReturn(ConfigFactory.fromMap(Map.of( | ||
"web.http.protocol.port", String.valueOf(1234), | ||
"web.http.protocol.path", "/path")) | ||
); | ||
when(spyContext.getSetting(DSP_CALLBACK_ADDRESS, DEFAULT_DSP_CALLBACK_ADDRESS)).thenReturn(webhookAddress); | ||
|
||
extension.initialize(spyContext); | ||
|
||
verify(configurer).configure(spyContext, webServer, SETTINGS); | ||
var apiConfig = spyContext.getService(DspApiConfiguration.class); | ||
assertThat(apiConfig.getContextAlias()).isEqualTo(CONTEXT_ALIAS); | ||
assertThat(apiConfig.getDspCallbackAddress()).isEqualTo(webhookAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters