Skip to content

Commit

Permalink
camel-jbang: Automatically load main http server for Knative component
Browse files Browse the repository at this point in the history
- KnativeHttpConsumer needs the main http server in order to provide a Http endpoint route that is supposed to be called by the Knative broker subscription
- Initialize platform main http server when Knative component is used in Camel JBang
- Use default port 8080 only if camel.jbang.platform-http.port setting has not been set
- Add KnativeHttpConsumer and KnativeHttpProducer factory as a service to the Camel context so init() and start() methods are called properly
  • Loading branch information
christophd committed Feb 14, 2024
1 parent 10d9e95 commit 1609edf
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Endpoint;
import org.apache.camel.component.knative.spi.Knative;
import org.apache.camel.component.knative.spi.KnativeConsumerFactory;
Expand Down Expand Up @@ -312,7 +311,7 @@ private void setUpProducerFactory() throws Exception {

this.managedProducer = true;

CamelContextAware.trySetCamelContext(this.producerFactory, getCamelContext());
getCamelContext().addService(this.producerFactory);
}

LOGGER.info("Using Knative producer factory: {} for protocol: {}", producerFactory, protocol.name());
Expand All @@ -339,7 +338,7 @@ private void setUpConsumerFactory() throws Exception {

this.managedConsumer = true;

CamelContextAware.trySetCamelContext(this.consumerFactory, getCamelContext());
getCamelContext().addService(this.consumerFactory);
}

LOGGER.info("Using Knative consumer factory: {} for protocol: {}", consumerFactory, protocol.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class KnativeHttpConsumer extends DefaultConsumer {
private final KnativeTransportConfiguration configuration;
private final Predicate<HttpServerRequest> filter;
private final KnativeResource resource;
private final Router router;
private final Supplier<Router> router;
private final HeaderFilterStrategy headerFilterStrategy;

private String basePath;
Expand All @@ -68,7 +69,7 @@ public class KnativeHttpConsumer extends DefaultConsumer {
public KnativeHttpConsumer(KnativeTransportConfiguration configuration,
Endpoint endpoint,
KnativeResource resource,
Router router,
Supplier<Router> router,
Processor processor) {
super(endpoint, processor);
this.configuration = configuration;
Expand Down Expand Up @@ -116,7 +117,7 @@ protected void doStart() throws Exception {

LOGGER.debug("Creating route for path: {}", path);

route = router.route(
route = router.get().route(
HttpMethod.POST,
path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.component.knative.spi.KnativeConsumerFactory;
import org.apache.camel.component.knative.spi.KnativeResource;
import org.apache.camel.component.knative.spi.KnativeTransportConfiguration;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;

public class KnativeHttpConsumerFactory extends ServiceSupport implements CamelContextAware, KnativeConsumerFactory {
Expand All @@ -38,6 +39,10 @@ public Router getRouter() {
}

public KnativeHttpConsumerFactory setRouter(Router router) {
if (ServiceHelper.isStarted(this)) {
throw new IllegalArgumentException("Can't set the Router instance after the service has been started");
}

this.router = router;
return this;
}
Expand All @@ -52,24 +57,31 @@ public CamelContext getCamelContext() {
return camelContext;
}

@Override
protected void doInit() throws Exception {
if (router == null) {
router = KnativeHttpSupport.lookupRouter(camelContext);
}
}

@Override
public Consumer createConsumer(
Endpoint endpoint, KnativeTransportConfiguration config, KnativeResource service, Processor processor) {
Objects.requireNonNull(this.router, "router");

return new KnativeHttpConsumer(
config,
endpoint,
service,
this.router,
this::lookupRouter,
processor);
}

/**
* Resolve router from given Camel context if not explicitly set. KnativeHttpConsumer implementation usually calls
* this method to retrieve the router during service startup phase.
*
* @return
*/
private Router lookupRouter() {
if (router == null) {
router = KnativeHttpSupport.lookupRouter(camelContext);
}

Objects.requireNonNull(router, "router");

return router;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.component.knative.spi.KnativeProducerFactory;
import org.apache.camel.component.knative.spi.KnativeResource;
import org.apache.camel.component.knative.spi.KnativeTransportConfiguration;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;

public class KnativeHttpProducerFactory extends ServiceSupport implements CamelContextAware, KnativeProducerFactory {
Expand All @@ -39,6 +40,10 @@ public Vertx getVertx() {
}

public KnativeHttpProducerFactory setVertx(Vertx vertx) {
if (ServiceHelper.isStarted(this)) {
throw new IllegalArgumentException("Can't set the Vertx instance after the service has been started");
}

this.vertx = vertx;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void remapCloudEventHeaders(CloudEvent ce, Message message) {
}

/**
* Retrieve router from given CamelContext or create new instance.
* Retrieve router from given CamelContext using the default platform http router name.
*
* @param camelContext the current context.
* @return router
Expand All @@ -114,15 +114,10 @@ public static Router lookupRouter(CamelContext camelContext) {
return router;
}

router = CamelContextHelper.lookup(
return CamelContextHelper.lookup(
camelContext,
PLATFORM_HTTP_ROUTER_NAME,
Router.class);
if (router != null) {
return router;
}

return Router.router(lookupVertxInstance(camelContext));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ public VertxPlatformHttpRouter getRouter() {
}

@Override
protected void doStart() throws Exception {
protected void doInit() throws Exception {
ObjectHelper.notNull(camelContext, "CamelContext");

server = new VertxPlatformHttpServer(configuration);
camelContext.addService(server);
}

@Override
protected void doStart() throws Exception {
ObjectHelper.notNull(camelContext, "CamelContext");

ServiceHelper.startService(server);
router = VertxPlatformHttpRouter.lookup(camelContext);
platformHttpComponent = camelContext.getComponent("platform-http", PlatformHttpComponent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ public Component resolveComponent(String name, CamelContext context) {
sc.setShadow(true);
sc.setShadowPattern(stubPattern);
}
if (answer instanceof PlatformHttpComponent) {
// setup a default http server on port 8080 if not already done
if (answer instanceof PlatformHttpComponent || name.equals("knative")) {
// set up a default http server on configured port if not already done
MainHttpServer server = camelContext.hasService(MainHttpServer.class);
if (server == null) {
// need to capture we use http-server
// need to capture that we use a http-server
HttpServerConfigurationProperties config = new HttpServerConfigurationProperties(null);
CamelJBangSettingsHelper.writeSettings("camel.jbang.platform-http.port", String.valueOf(config.getPort()));
CamelJBangSettingsHelper.writeSettingsIfNotExists("camel.jbang.platform-http.port",
String.valueOf(config.getPort()));
if (!silent) {
try {
// enable http server if not silent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public final class CamelJBangSettingsHelper {
private CamelJBangSettingsHelper() {
}

public static void writeSettings(String key, String value) {
public static void writeSettingsIfNotExists(String key, String value) {
if (FILE.exists()) {
try {
String line = key + "=" + value;

String context;
try (FileInputStream fis = new FileInputStream(FILE)) {
context = IOHelper.loadText(fis);
}

if (!context.contains(line)) {
// append line as it was not already present
if (!context.contains(key + "=")) {
// append line as key has not been set before
String line = key + "=" + value;
try (FileOutputStream fos = new FileOutputStream(FILE, true)) {
fos.write(line.getBytes(StandardCharsets.UTF_8));
fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
Expand Down

0 comments on commit 1609edf

Please sign in to comment.