Skip to content
Merged
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
32 changes: 31 additions & 1 deletion splunk/src/main/java/com/splunk/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

/**
* The {@code HttpService} class represents a generic HTTP service at a given
Expand Down Expand Up @@ -83,7 +84,9 @@ public boolean verify(String s, SSLSession sslSession) {
put("User-Agent", "splunk-sdk-java/1.7.1");
put("Accept", "*/*");
}};


protected Map<String, String> customHeaders = new HashMap<>();

protected SimpleCookieStore cookieStore = new SimpleCookieStore();

/**
Expand Down Expand Up @@ -193,6 +196,17 @@ public String getHost() {
public int getPort() {
return this.port;
}

/**
* Sets Custom Headers of this service
*
* @param headers
*/
public void setCustomHeaders(Map<String, String> headers) {
if (Objects.nonNull(headers)) {
customHeaders = headers;
}
}

/**
* Returns the SSL security protocol of this service.
Expand Down Expand Up @@ -258,6 +272,15 @@ public URL getUrl(String path) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* Returns all the stored custom headers
*
* @return customHeaders The custom headers
*/
public Map<String, String> getCustomHeaders() {
return customHeaders;
}

/**
* Returns all the stored cookies
Expand Down Expand Up @@ -441,6 +464,13 @@ public ResponseMessage send(String path, RequestMessage request) {
if (header.containsKey(key)) continue;
cn.setRequestProperty(key, entry.getValue());
}
// Add Custom Headers
for (Entry<String, String> entry: customHeaders.entrySet()) {
String key = entry.getKey();
if (!header.containsKey(key)) {
cn.setRequestProperty(key, entry.getValue());
}
}

// Add cookies to header
cn.setRequestProperty("Cookie", cookieStore.getCookies());
Expand Down
1 change: 1 addition & 0 deletions splunk/src/main/java/com/splunk/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public Service(ServiceArgs args) {
this.httpsHandler = Args.<URLStreamHandler>get(args, "httpsHandler", null);
this.setSslSecurityProtocol(Args.get(args, "SSLSecurityProtocol", Service.getSslSecurityProtocol()));
this.addCookie((String)args.get("cookie"));
this.setCustomHeaders((Map<String, String>) args.get("customHeaders"));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions splunk/src/main/java/com/splunk/ServiceArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.splunk;

import java.net.URLStreamHandler;
import java.util.Map;

/**
* The {@code ServiceArgs} class contains a collection of arguments that are
Expand Down Expand Up @@ -164,4 +165,12 @@ public void setUsername(String username) {
public void setCookie(String cookie) {
this.put("cookie", cookie);
}

/**
* @param httpHeaders
* A map of customHeaders.
*/
public void setHttpHeaders(Map<String, String> httpHeaders) {
this.put("customHeaders", httpHeaders);
}
}
18 changes: 17 additions & 1 deletion splunk/src/test/java/com/splunk/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,23 @@ public void testLogin() {
service.logout();
checkNotLoggedIn(service);
}


@Test
public void testServiceWithCustomHeaders() {
ServiceArgs args = new ServiceArgs();
args.setHost((String) command.opts.get("host"));
args.setPort((Integer) command.opts.get("port"));
args.setScheme((String) command.opts.get("scheme"));
args.setUsername((String) command.opts.get("username"));
args.setPassword((String) command.opts.get("password"));
args.setHttpHeaders(new HashMap<String, String>() {{
put("some header key", "some value");
}});
Service service = new Service(args);
Map<String, String> customHeaders = service.getCustomHeaders();
Assert.assertEquals(customHeaders.get("some header key"), "some value");
}

@Test
public void testLoginWithoutArguments() {
ServiceArgs args = new ServiceArgs();
Expand Down