Skip to content

Commit

Permalink
Allow custom configuration for the CustomApiRoutesProvider extension
Browse files Browse the repository at this point in the history
Signed-off-by: Yannic Klem <yannic.klem@bosch.io>
  • Loading branch information
Yannic92 committed Jul 12, 2022
1 parent 92a3a71 commit 8945fc7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
Expand Up @@ -16,7 +16,11 @@

import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.service.DittoExtensionIds;
import org.eclipse.ditto.base.service.DittoExtensionPoint;
import org.eclipse.ditto.base.service.RootChildActorStarter;

import com.typesafe.config.Config;

import akka.actor.ActorSystem;
import akka.http.javadsl.server.Route;
Expand Down Expand Up @@ -50,22 +54,31 @@ public interface CustomApiRoutesProvider extends DittoExtensionPoint {
* Loads the implementation of {@code CustomApiRoutesProvider} which is configured for the {@code ActorSystem}.
*
* @param actorSystem the actorSystem in which the {@code CustomApiRoutesProvider} should be loaded.
* @param config the configuration for this extension.
* @return the {@code CustomApiRoutesProvider} implementation.
* @throws NullPointerException if {@code actorSystem} is {@code null}.
* @since 3.0.0
*/
static CustomApiRoutesProvider get(final ActorSystem actorSystem) {
static CustomApiRoutesProvider get(final ActorSystem actorSystem, final Config config) {
checkNotNull(actorSystem, "actorSystem");
return ExtensionId.INSTANCE.get(actorSystem);
checkNotNull(config, "config");
final var extensionIdConfig = ExtensionId.computeConfig(config);
return DittoExtensionIds.get(actorSystem)
.computeIfAbsent(extensionIdConfig, ExtensionId::new)
.get(actorSystem);
}

final class ExtensionId extends DittoExtensionPoint.ExtensionId<CustomApiRoutesProvider> {

private static final String CONFIG_PATH = "ditto.gateway.http.custom-api-routes-provider";
private static final ExtensionId INSTANCE = new ExtensionId(CustomApiRoutesProvider.class);
private static final String CONFIG_KEY = "custom-api-routes-provider";
private static final String CONFIG_PATH = "ditto.extensions." + CONFIG_KEY;

private ExtensionId(final ExtensionIdConfig<CustomApiRoutesProvider> extensionIdConfig) {
super(extensionIdConfig);
}

private ExtensionId(final Class<CustomApiRoutesProvider> parentClass) {
super(parentClass);
static ExtensionIdConfig<CustomApiRoutesProvider> computeConfig(final Config config) {
return ExtensionIdConfig.of(CustomApiRoutesProvider.class, config, CONFIG_KEY);
}

@Override
Expand Down
Expand Up @@ -251,7 +251,7 @@ private static Route createRoute(final ActorSystem actorSystem,
.headerTranslator(headerTranslator)
.build();

final var customApiRoutesProvider = CustomApiRoutesProvider.get(actorSystem);
final var customApiRoutesProvider = CustomApiRoutesProvider.get(actorSystem, dittoExtensionConfig);

return RootRoute.getBuilder(httpConfig)
.statsRoute(new StatsRoute(routeBaseProperties, devopsAuthenticationDirective))
Expand Down
6 changes: 2 additions & 4 deletions gateway/service/src/main/resources/gateway.conf
Expand Up @@ -53,6 +53,8 @@ ditto {
incoming-websocket-event-sniffer = org.eclipse.ditto.gateway.service.endpoints.routes.websocket.NoOpIncomingWebSocketEventSniffer
# The listener for outgoing WebSocket events
outgoing-websocket-event-sniffer = org.eclipse.ditto.gateway.service.endpoints.routes.websocket.NoOpOutgoingWebSocketEventSniffer
# The provider for custom API routes.
custom-api-routes-provider = org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider
}

service-name = "gateway"
Expand Down Expand Up @@ -106,10 +108,6 @@ ditto {
"condition",
"live-channel-condition"
]

# The provider for custom API routes.
custom-api-routes-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider"
custom-api-routes-provider = ${?HTTP_CUSTOM_API_ROUTES_PROVIDER}
}

streaming {
Expand Down
Expand Up @@ -53,6 +53,7 @@
import org.eclipse.ditto.gateway.service.health.DittoStatusAndHealthProviderFactory;
import org.eclipse.ditto.gateway.service.security.HttpHeader;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationFactory;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterStatus;
import org.eclipse.ditto.internal.utils.health.routes.StatusRoute;
import org.eclipse.ditto.internal.utils.http.HttpClientFacade;
Expand Down Expand Up @@ -128,7 +129,8 @@ public void setUp() {
httpClientFacade,
ACTOR_SYSTEM);
final GatewayAuthenticationDirectiveFactory authenticationDirectiveFactory =
new DittoGatewayAuthenticationDirectiveFactory(routeBaseProperties.getActorSystem(), ConfigFactory.empty());
new DittoGatewayAuthenticationDirectiveFactory(routeBaseProperties.getActorSystem(),
ConfigFactory.empty());

final Supplier<ClusterStatus> clusterStatusSupplier = createClusterStatusSupplierMock();
final var statusAndHealthProvider = DittoStatusAndHealthProviderFactory.of(routeBaseProperties.getActorSystem(),
Expand All @@ -138,7 +140,8 @@ public void setUp() {
DevopsAuthenticationDirectiveFactory.newInstance(jwtAuthenticationFactory,
authConfig.getDevOpsConfig());
final var devOpsAuthenticationDirective = devopsAuthenticationDirectiveFactory.devops();

final var dittoExtensionConfig =
ScopedConfig.dittoExtension(routeBaseProperties.getActorSystem().settings().config());
final var rootRoute = RootRoute.getBuilder(httpConfig)
.statsRoute(new StatsRoute(routeBaseProperties, devOpsAuthenticationDirective))
.statusRoute(new StatusRoute(clusterStatusSupplier,
Expand Down Expand Up @@ -172,7 +175,8 @@ public void setUp() {
.wsAuthenticationDirective(
authenticationDirectiveFactory.buildWsAuthentication(jwtAuthenticationFactory))
.dittoHeadersSizeChecker(DittoHeadersSizeChecker.of(4096, 20))
.customApiRoutesProvider(CustomApiRoutesProvider.get(routeBaseProperties.getActorSystem()),
.customApiRoutesProvider(
CustomApiRoutesProvider.get(routeBaseProperties.getActorSystem(), dittoExtensionConfig),
routeBaseProperties)
.build();

Expand Down
2 changes: 1 addition & 1 deletion gateway/service/src/test/resources/test.conf
Expand Up @@ -145,6 +145,7 @@ ditto {
sse-event-sniffer = org.eclipse.ditto.gateway.service.endpoints.routes.sse.NoOpSseEventSniffer
incoming-websocket-event-sniffer = org.eclipse.ditto.gateway.service.endpoints.routes.websocket.NoOpIncomingWebSocketEventSniffer
outgoing-websocket-event-sniffer = org.eclipse.ditto.gateway.service.endpoints.routes.websocket.NoOpOutgoingWebSocketEventSniffer
custom-api-routes-provider = org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider
}

mapping-strategy.implementation = "org.eclipse.ditto.gateway.service.util.GatewayMappingStrategies"
Expand Down Expand Up @@ -261,7 +262,6 @@ ditto {

http {
request-timeout = 5s
custom-api-routes-provider = "org.eclipse.ditto.gateway.service.endpoints.routes.NoopCustomApiRoutesProvider"
}
}
}
Expand Down

0 comments on commit 8945fc7

Please sign in to comment.