This is an automated email from the ASF dual-hosted git repository.
jamesnetherton pushed a commit to branch camel-4.0.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.0.x by this push:
new 9ccf61d7fa9 CAMEL-19816: Add headers to camel-platform-http-vertx for
the connection local and remote address
9ccf61d7fa9 is described below
commit 9ccf61d7fa9625d4201da40ae4b3a623546639db
Author: James Netherton <[email protected]>
AuthorDate: Mon Sep 4 08:13:42 2023 +0100
CAMEL-19816: Add headers to camel-platform-http-vertx for the connection
local and remote address
---
.../src/main/docs/platform-http-vertx.adoc | 4 +-
.../http/vertx/VertxPlatformHttpConstants.java | 2 +
.../http/vertx/VertxPlatformHttpSupport.java | 11 ++++
.../http/vertx/VertxPlatformHttpEngineTest.java | 60 ++++++++++++++++++++++
4 files changed, 76 insertions(+), 1 deletion(-)
diff --git
a/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
b/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
index 6edafd6cf67..e4f7ac16dfb 100644
---
a/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
+++
b/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
@@ -27,7 +27,9 @@ and the platform http component should auto-detect this.
|=======================================================================
|Name |Type |Description
-|`CamelVertxPlatformHttpAuthenticatedUser` |`io.vertx.ext.auth.User` |If an
authenticated user is present on the Vert.x Web `RoutingContext`, this header
is populated with a `User` object continaing the `Principal`.
+|`CamelVertxPlatformHttpAuthenticatedUser` |`io.vertx.ext.auth.User` |If an
authenticated user is present on the Vert.x Web `RoutingContext`, this header
is populated with a `User` object containing the `Principal`.
+|`CamelVertxPlatformHttpLocalAddress` |`io.vertx.core.net.SocketAddress` |The
local address for the connection if present on the Vert.x Web `RoutingContext`.
+|`CamelVertxPlatformHttpRemoteAddress` |`io.vertx.core.net.SocketAddress` |The
remote address for the connection if present on the Vert.x Web `RoutingContext`.
|=======================================================================
Camel also populates *all* request.parameter and Camel also populates *all*
request.parameter and request.headers. For
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConstants.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConstants.java
index 15917eaa4ac..dc9a878ad6e 100644
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConstants.java
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConstants.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.platform.http.vertx;
public final class VertxPlatformHttpConstants {
public static final String AUTHENTICATED_USER =
"CamelVertxPlatformHttpAuthenticatedUser";
+ public static final String LOCAL_ADDRESS =
"CamelVertxPlatformHttpLocalAddress";
+ public static final String REMOTE_ADDRESS =
"CamelVertxPlatformHttpRemoteAddress";
private VertxPlatformHttpConstants() {
}
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
index cef371bd0bf..15049773b14 100644
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
@@ -31,6 +31,7 @@ import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
+import io.vertx.core.net.SocketAddress;
import io.vertx.ext.web.RoutingContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
@@ -254,6 +255,16 @@ public final class VertxPlatformHttpSupport {
appendHeader(headersMap, en.getKey(), en.getValue());
}
+ SocketAddress localAddress = request.localAddress();
+ if (localAddress != null) {
+ headersMap.put(VertxPlatformHttpConstants.LOCAL_ADDRESS,
localAddress);
+ }
+
+ SocketAddress remoteAddress = request.remoteAddress();
+ if (remoteAddress != null) {
+ headersMap.put(VertxPlatformHttpConstants.REMOTE_ADDRESS,
remoteAddress);
+ }
+
// NOTE: these headers is applied using the same logic as
camel-http/camel-jetty to be consistent
headersMap.put(Exchange.HTTP_METHOD, request.method().toString());
// strip query parameters from the uri
diff --git
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index 48835e304c0..683b2327e1d 100644
---
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -33,6 +33,7 @@ import io.restassured.specification.RequestSpecification;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;
+import io.vertx.core.net.SocketAddress;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
@@ -58,6 +59,7 @@ import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.emptyString;
@@ -786,6 +788,64 @@ public class VertxPlatformHttpEngineTest {
}
}
+ @Test
+ public void testLocalAddressHeader() throws Exception {
+ final CamelContext context = createCamelContext();
+
+ try {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("platform-http:/local/address")
+ .process(exchange -> {
+ Message message = exchange.getMessage();
+ SocketAddress address
+ =
message.getHeader(VertxPlatformHttpConstants.LOCAL_ADDRESS,
SocketAddress.class);
+ message.setBody(address.hostAddress());
+ });
+ }
+ });
+
+ context.start();
+
+ get("/local/address")
+ .then()
+ .statusCode(200)
+ .body(notNullValue());
+ } finally {
+ context.stop();
+ }
+ }
+
+ @Test
+ public void testRemoteAddressHeader() throws Exception {
+ final CamelContext context = createCamelContext();
+
+ try {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("platform-http:/remote/address")
+ .process(exchange -> {
+ Message message = exchange.getMessage();
+ SocketAddress address
+ =
message.getHeader(VertxPlatformHttpConstants.REMOTE_ADDRESS,
SocketAddress.class);
+ message.setBody(address.hostAddress());
+ });
+ }
+ });
+
+ context.start();
+
+ get("/remote/address")
+ .then()
+ .statusCode(200)
+ .body(notNullValue());
+ } finally {
+ context.stop();
+ }
+ }
+
static CamelContext createCamelContext() throws Exception {
return createCamelContext(null);
}