[ 
https://issues.apache.org/jira/browse/CAMEL-12287?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16376549#comment-16376549
 ] 

ASF GitHub Bot commented on CAMEL-12287:
----------------------------------------

oscerd closed pull request #2237: CAMEL-12287: Allow overriding the endpoint 
host
URL: https://github.com/apache/camel/pull/2237
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/components/camel-milo/src/main/docs/milo-client-component.adoc 
b/components/camel-milo/src/main/docs/milo-client-component.adoc
index eb3e2b5db37..ea1f48a3320 100644
--- a/components/camel-milo/src/main/docs/milo-client-component.adoc
+++ b/components/camel-milo/src/main/docs/milo-client-component.adoc
@@ -95,7 +95,7 @@ with the following path and query parameters:
 | *endpointUri* | *Required* The OPC UA server endpoint |  | String
 |===
 
-==== Query Parameters (23 parameters):
+==== Query Parameters (24 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
@@ -119,6 +119,7 @@ with the following path and query parameters:
 | *keyStoreUrl* (client) | The URL where the key should be loaded from |  | URL
 | *maxPendingPublishRequests* (client) | The maximum number of pending publish 
requests |  | Long
 | *maxResponseMessageSize* (client) | The maximum number of bytes a response 
message may have |  | Long
+| *overrideHost* (client) | Override the server reported endpoint host with 
the host from the endpoint URI. | false | boolean
 | *productUri* (client) | The product URI | 
http://camel.apache.org/EclipseMilo | String
 | *requestTimeout* (client) | Request timeout in milliseconds |  | Long
 | *sessionName* (client) | Session name |  | String
diff --git 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
index 1a78648a93c..75743ea4009 100644
--- 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
+++ 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/MiloClientConfiguration.java
@@ -85,6 +85,9 @@
     @UriParam(label = "client", javaType = "java.lang.String")
     private Set<String> allowedSecurityPolicies = new HashSet<>();
 
+    @UriParam(label = "client")
+    private boolean overrideHost;
+
     public MiloClientConfiguration() {
     }
 
@@ -95,6 +98,7 @@ public MiloClientConfiguration(final MiloClientConfiguration 
other) {
         this.productUri = other.productUri;
         this.requestTimeout = other.requestTimeout;
         this.allowedSecurityPolicies = allowedSecurityPolicies != null ? new 
HashSet<>(other.allowedSecurityPolicies) : null;
+        this.overrideHost = other.overrideHost;
     }
 
     public void setEndpointUri(final String endpointUri) {
@@ -316,7 +320,19 @@ public void setAllowedSecurityPolicies(final String 
allowedSecurityPolicies) {
     }
 
     public Set<String> getAllowedSecurityPolicies() {
-        return allowedSecurityPolicies;
+        return this.allowedSecurityPolicies;
+    }
+
+    /**
+     * Override the server reported endpoint host with the host from the
+     * endpoint URI.
+     */
+    public void setOverrideHost(boolean overrideHost) {
+        this.overrideHost = overrideHost;
+    }
+
+    public boolean isOverrideHost() {
+        return overrideHost;
     }
 
     @Override
diff --git 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
index f2d31025a4b..904217ddc19 100644
--- 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
+++ 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.milo.client.internal;
 
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -416,7 +417,11 @@ private Connected performConnect() throws Exception {
                 }
             }
 
-            return findEndpoint(endpoints);
+            try {
+                return findEndpoint(endpoints);
+            } catch (final URISyntaxException e) {
+                throw new RuntimeException("Failed to find endpoints", e);
+            }
         }).get();
 
         LOG.debug("Selected endpoint: {}", endpoint);
@@ -495,7 +500,7 @@ private synchronized void triggerReconnect(final boolean 
immediate) {
         }
     }
 
-    private EndpointDescription findEndpoint(final EndpointDescription[] 
endpoints) {
+    private EndpointDescription findEndpoint(final EndpointDescription[] 
endpoints) throws URISyntaxException {
 
         final Predicate<String> allowed;
         final Set<String> uris = 
this.configuration.getAllowedSecurityPolicies();
@@ -517,7 +522,55 @@ private EndpointDescription findEndpoint(final 
EndpointDescription[] endpoints)
                 best = ep;
             }
         }
-        return best;
+
+        // return result, might override the host part
+
+        return overrideHost(best);
+    }
+
+    /**
+     * Optionally override the host of the endpoint URL with the configured 
one.
+     * <br>
+     * The method will call {@link #overrideHost(String)} if the endpoint is 
not
+     * {@code null} and {@link MiloClientConfiguration#isOverrideHost()} 
returns
+     * {@code true}.
+     * 
+     * @param desc The endpoint descriptor to work on
+     * @return Either the provided or updated endpoint descriptor. Only returns
+     *         {@code null} when the input was {@code null}.
+     * @throws URISyntaxException on case the URI is malformed
+     */
+    private EndpointDescription overrideHost(final EndpointDescription desc) 
throws URISyntaxException {
+        if (desc == null) {
+            return null;
+        }
+
+        if (!this.configuration.isOverrideHost()) {
+            return desc;
+        }
+
+        return new EndpointDescription(overrideHost(desc.getEndpointUrl()), 
desc.getServer(), desc.getServerCertificate(), desc.getSecurityMode(), 
desc.getSecurityPolicyUri(),
+                                       desc.getUserIdentityTokens(), 
desc.getTransportProfileUri(), desc.getSecurityLevel());
+    }
+
+    /**
+     * Override host part of the endpoint URL with the configured one.
+     * 
+     * @param endpointUrl the server provided endpoint URL
+     * @return A new endpoint URL with the host part exchanged by the 
configured
+     *         host. Will be {@code null} when the input is {@code null}.
+     * @throws URISyntaxException on case the URI is malformed
+     */
+    private String overrideHost(final String endpointUrl) throws 
URISyntaxException {
+
+        if (endpointUrl == null) {
+            return null;
+        }
+
+        final URI uri = URI.create(endpointUrl);
+        final URI originalUri = URI.create(configuration.getEndpointUri());
+
+        return new URI(uri.getScheme(), uri.getUserInfo(), 
originalUri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), 
uri.getFragment()).toString();
     }
 
     protected synchronized void whenConnected(final Worker<Connected> worker) {
diff --git 
a/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
index cd194aa1384..2abe2102d3e 100644
--- 
a/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
+++ 
b/platforms/spring-boot/components-starter/camel-milo-starter/src/main/java/org/apache/camel/component/milo/client/springboot/MiloClientComponentConfiguration.java
@@ -175,6 +175,11 @@ public void setResolvePropertyPlaceholders(
          * use the highest.
          */
         private Set allowedSecurityPolicies;
+        /**
+         * Override the server reported endpoint host with the host from the
+         * endpoint URI.
+         */
+        private Boolean overrideHost = false;
 
         public String getEndpointUri() {
             return endpointUri;
@@ -303,5 +308,13 @@ public Set getAllowedSecurityPolicies() {
         public void setAllowedSecurityPolicies(Set allowedSecurityPolicies) {
             this.allowedSecurityPolicies = allowedSecurityPolicies;
         }
+
+        public Boolean getOverrideHost() {
+            return overrideHost;
+        }
+
+        public void setOverrideHost(Boolean overrideHost) {
+            this.overrideHost = overrideHost;
+        }
     }
 }
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Allow overriding the server reported endpoint
> ---------------------------------------------
>
>                 Key: CAMEL-12287
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12287
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-milo
>    Affects Versions: 2.19.0, 2.20.2
>            Reporter: Jens Reimann
>            Assignee: Jens Reimann
>            Priority: Minor
>             Fix For: 2.21.0
>
>
> When the client connects to the server it asks the server first for available 
> endpoints. The server should report back with a set of endpoints, which again 
> contain the hostname to connect to.
> If the server reports back with a hostname which the client cannot look up, 
> it is currently not possible to connect to the server with camel-milo.
> I did prepare a PR which adds a boolean flag to the endpoint, allowing to 
> override the host of reported endpoint with the one originally provided. This 
> is disabled by default, but can be enabled when necessary.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to