Skip to content

Commit

Permalink
Merge branch 'develop' into prep-2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Vihas Splunk committed Nov 21, 2023
2 parents 379c223 + 4cf0e85 commit a89d8a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/splunk/hecclient/HecURIBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.splunk.hecclient;

import org.apache.http.Consts;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;

import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -18,9 +20,18 @@ public HecURIBuilder(String baseUrl, HecConfig hecConfig) {

public URI getURI(String endpoint) {
try {
URIBuilder uriBuilder = new URIBuilder(baseUrl)
.setPath(endpoint);

URIBuilder uriBuilder = new URIBuilder(baseUrl);
int idx = endpoint.indexOf('?');
if (idx == -1) {
// json endpoint
uriBuilder = uriBuilder.setPath(endpoint);
} else {
// in case of raw endpoint, the endpoint will be in form "/services/collector/raw?index=xxx&source=xxx"
// extract the path and params via a split on '?'
uriBuilder = uriBuilder.setPath(endpoint.substring(0, idx));
uriBuilder = uriBuilder.setParameters(URLEncodedUtils.parse(endpoint.substring(idx+1), Consts.UTF_8));
}

if (hecConfig.getAutoExtractTimestamp() != null) {
uriBuilder.addParameter(AUTO_EXTRACT_TIMESTAMP_PARAMETER, hecConfig.getAutoExtractTimestamp().toString());
}
Expand Down
41 changes: 37 additions & 4 deletions src/test/java/com/splunk/hecclient/HecURIBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
import static com.splunk.hecclient.JsonEventBatch.ENDPOINT;

public class HecURIBuilderTest {
private static final String RAW_ENDPOINT = "/services/collector/raw?index=main&source=source";
private static final String BASE_URL = "https://localhost:8088";
private static final String TOKEN = "mytoken";

@Test
public void testDefaultValues() {
HecConfig hecConfig = new HecConfig(Collections.emptyList(), TOKEN);
HecURIBuilder builder = new HecURIBuilder(BASE_URL, hecConfig);
{
HecConfig hecConfig = new HecConfig(Collections.emptyList(), TOKEN);
HecURIBuilder builder = new HecURIBuilder(BASE_URL, hecConfig);

URI uri = builder.getURI(ENDPOINT);

Assert.assertEquals("https://localhost:8088/services/collector/event", uri.toString());
}
{
HecConfig hecConfig = new HecConfig(Collections.emptyList(), TOKEN);
HecURIBuilder builder = new HecURIBuilder(BASE_URL, hecConfig);

URI uri = builder.getURI(ENDPOINT);
URI uri = builder.getURI(RAW_ENDPOINT);

Assert.assertEquals("https://localhost:8088/services/collector/event", uri.toString());
Assert.assertEquals("https://localhost:8088/services/collector/raw?index=main&source=source", uri.toString());
}
}

@Test
Expand All @@ -46,5 +57,27 @@ public void testAutoExtractTimestamp() {
HecURIBuilder.AUTO_EXTRACT_TIMESTAMP_PARAMETER + "=false",
uri.toString());
}
{
HecConfig hecConfig = new HecConfig(Collections.emptyList(), TOKEN)
.setAutoExtractTimestamp(false);
HecURIBuilder builder = new HecURIBuilder(BASE_URL, hecConfig);

URI uri = builder.getURI(RAW_ENDPOINT);

Assert.assertEquals("https://localhost:8088/services/collector/raw?index=main&source=source&" +
HecURIBuilder.AUTO_EXTRACT_TIMESTAMP_PARAMETER + "=false",
uri.toString());
}
{
HecConfig hecConfig = new HecConfig(Collections.emptyList(), TOKEN)
.setAutoExtractTimestamp(true);
HecURIBuilder builder = new HecURIBuilder(BASE_URL, hecConfig);

URI uri = builder.getURI(RAW_ENDPOINT);

Assert.assertEquals("https://localhost:8088/services/collector/raw?index=main&source=source&" +
HecURIBuilder.AUTO_EXTRACT_TIMESTAMP_PARAMETER + "=true",
uri.toString());
}
}
}

0 comments on commit a89d8a2

Please sign in to comment.