diff --git a/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java b/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java index ab274c545..d0cb02477 100644 --- a/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java +++ b/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java @@ -9,8 +9,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Map; +import java.util.Map.Entry; -import org.apache.hc.core5.net.URIBuilder; import org.opensearch.core.common.Strings; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; @@ -84,19 +84,28 @@ protected URI buildUri(String endpoint, String scheme, String host, int port, St if (Strings.isNullOrEmpty(scheme)) { scheme = "https"; } - URIBuilder uriBuilder = new URIBuilder(); - if (queryParams != null) { - for (Map.Entry e : queryParams.entrySet()) - uriBuilder.addParameter(e.getKey(), e.getValue()); - } - return uriBuilder.setScheme(scheme).setHost(host).setPort(port).setPath(path).build(); + return new URI(scheme, null, host, port, path, buildQueryString(queryParams), null); } - return new URIBuilder(endpoint).build(); + return new URI(endpoint); } catch (URISyntaxException exception) { throw new IllegalStateException("Error creating URI"); } } + private static String buildQueryString(Map queryParams) { + if (queryParams == null || queryParams.isEmpty()) { + return null; + } + StringBuilder query = new StringBuilder(); + for (Entry param : queryParams.entrySet()) { + if (query.length() > 0) { + query.append('&'); + } + query.append(param.getKey()).append('=').append(param.getValue()); + } + return query.toString(); + } + @Override public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeEnum(destinationType); diff --git a/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java b/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java index dba1b65d4..49bf1cbcb 100644 --- a/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java +++ b/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java @@ -33,6 +33,32 @@ public void testBuildingLegacyCustomWebhookMessage() { assertEquals("https://amazon.com", message.getUrl()); } + @Test + public void testGetUriWithFullUrl() { + LegacyCustomWebhookMessage message = new LegacyCustomWebhookMessage.Builder("custom_webhook") + .withMessage("Hello world") + .withUrl("https://httpbin.org/post") + .build(); + + assertEquals("https://httpbin.org/post", message.getUri().toString()); + } + + @Test + public void testGetUriWithHostAndQueryParams() { + Map queryParams = new HashMap(); + queryParams.put("token", "sometoken"); + LegacyCustomWebhookMessage message = new LegacyCustomWebhookMessage.Builder("custom_webhook") + .withMessage("Hello world") + .withHost("hooks.example.com") + .withPath("incoming") + .withQueryParams(queryParams) + .withPort(443) + .withScheme("https") + .build(); + + assertEquals("https://hooks.example.com:443/incoming?token=sometoken", message.getUri().toString()); + } + @Test public void testRoundTrippingLegacyCustomWebhookMessageWithUrl() throws IOException { LegacyCustomWebhookMessage message = new LegacyCustomWebhookMessage.Builder("custom_webhook")