Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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<String, String> queryParams) {
if (queryParams == null || queryParams.isEmpty()) {
return null;
}
StringBuilder query = new StringBuilder();
for (Entry<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> queryParams = new HashMap<String, String>();
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")
Expand Down
Loading