Skip to content

Commit 6b88eab

Browse files
Added feature to add custom headers to Service
1 parent 636b3c1 commit 6b88eab

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

splunk/src/main/java/com/splunk/HttpService.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
import java.io.OutputStreamWriter;
2424
import java.net.*;
2525
import java.security.cert.X509Certificate;
26+
import java.util.Arrays;
27+
import java.util.Collections;
2628
import java.util.HashMap;
29+
import java.util.HashSet;
2730
import java.util.List;
2831
import java.util.Map;
2932
import java.util.Map.Entry;
33+
import java.util.Objects;
34+
import java.util.stream.Collectors;
35+
import java.util.Set;
3036

3137
/**
3238
* The {@code HttpService} class represents a generic HTTP service at a given
@@ -79,11 +85,20 @@ public boolean verify(String s, SSLSession sslSession) {
7985

8086
private String prefix = null;
8187

88+
private static final Set<String> filtertHttpHeaderKeys = Collections.unmodifiableSet(
89+
new HashSet<String>(Arrays.asList(
90+
"User-Agent",
91+
"Accept"
92+
))
93+
);
94+
8295
static Map<String, String> defaultHeader = new HashMap<String, String>() {{
8396
put("User-Agent", "splunk-sdk-java/1.7.1");
8497
put("Accept", "*/*");
8598
}};
86-
99+
100+
protected Map<String, String> customHeaders = new HashMap<>();
101+
87102
protected SimpleCookieStore cookieStore = new SimpleCookieStore();
88103

89104
/**
@@ -193,6 +208,21 @@ public String getHost() {
193208
public int getPort() {
194209
return this.port;
195210
}
211+
212+
/**
213+
* Sets Custom Headers of this service
214+
*
215+
* @param headers
216+
*/
217+
public void setCustomHeaders(Map<String, String> headers) {
218+
if (Objects.nonNull(headers) && !headers.isEmpty()) {
219+
Map<String, String> fitleredCustomHeaders = headers.entrySet()
220+
.stream()
221+
.filter(e -> !filtertHttpHeaderKeys.contains(e.getKey()))
222+
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));
223+
customHeaders.putAll(fitleredCustomHeaders);
224+
}
225+
}
196226

197227
/**
198228
* Returns the SSL security protocol of this service.
@@ -258,6 +288,15 @@ public URL getUrl(String path) {
258288
throw new RuntimeException(e.getMessage(), e);
259289
}
260290
}
291+
292+
/**
293+
* Returns all the stored custom headers
294+
*
295+
* @return customHeaders The custom headers
296+
*/
297+
public Map<String, String> getCustomHeaders() {
298+
return customHeaders;
299+
}
261300

262301
/**
263302
* Returns all the stored cookies
@@ -441,6 +480,13 @@ public ResponseMessage send(String path, RequestMessage request) {
441480
if (header.containsKey(key)) continue;
442481
cn.setRequestProperty(key, entry.getValue());
443482
}
483+
// Add Custom Headers
484+
for (Entry<String, String> entry: customHeaders.entrySet()) {
485+
String key = entry.getKey();
486+
if (!header.containsKey(key)) {
487+
cn.setRequestProperty(key, entry.getValue());
488+
}
489+
}
444490

445491
// Add cookies to header
446492
cn.setRequestProperty("Cookie", cookieStore.getCookies());

splunk/src/main/java/com/splunk/Service.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public Service(ServiceArgs args) {
152152
this.httpsHandler = Args.<URLStreamHandler>get(args, "httpsHandler", null);
153153
this.setSslSecurityProtocol(Args.get(args, "SSLSecurityProtocol", Service.getSslSecurityProtocol()));
154154
this.addCookie((String)args.get("cookie"));
155+
this.setCustomHeaders((Map<String, String>) args.get("customHeaders"));
155156
}
156157

157158
/**

splunk/src/main/java/com/splunk/ServiceArgs.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.splunk;
1818

1919
import java.net.URLStreamHandler;
20+
import java.util.Map;
2021

2122
/**
2223
* The {@code ServiceArgs} class contains a collection of arguments that are
@@ -164,4 +165,12 @@ public void setUsername(String username) {
164165
public void setCookie(String cookie) {
165166
this.put("cookie", cookie);
166167
}
168+
169+
/**
170+
* @param httpHeaders
171+
* A map of customHeaders.
172+
*/
173+
public void setHttpHeaders(Map<String, String> httpHeaders) {
174+
this.put("customHeaders", httpHeaders);
175+
}
167176
}

splunk/src/test/java/com/splunk/ServiceTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,23 @@ public void testLogin() {
146146
service.logout();
147147
checkNotLoggedIn(service);
148148
}
149-
149+
150+
@Test
151+
public void testServiceWithCustomHeaders() {
152+
ServiceArgs args = new ServiceArgs();
153+
args.setHost((String) command.opts.get("host"));
154+
args.setPort((Integer) command.opts.get("port"));
155+
args.setScheme((String) command.opts.get("scheme"));
156+
args.setUsername((String) command.opts.get("username"));
157+
args.setPassword((String) command.opts.get("password"));
158+
args.setHttpHeaders(new HashMap<String, String>() {{
159+
put("some header key", "some value");
160+
}});
161+
Service service = new Service(args);
162+
Map<String, String> customHeaders = service.getCustomHeaders();
163+
Assert.assertEquals(customHeaders.get("some header key"), "some value");
164+
}
165+
150166
@Test
151167
public void testLoginWithoutArguments() {
152168
ServiceArgs args = new ServiceArgs();

0 commit comments

Comments
 (0)