Skip to content

Commit c961d13

Browse files
authored
Merge pull request #178 from splunk/master
Master -> Develop
2 parents 8ea97d7 + c6f3008 commit c961d13

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828
import java.util.Map;
2929
import java.util.Map.Entry;
30+
import java.util.Objects;
3031

3132
/**
3233
* The {@code HttpService} class represents a generic HTTP service at a given
@@ -83,7 +84,9 @@ public boolean verify(String s, SSLSession sslSession) {
8384
put("User-Agent", "splunk-sdk-java/1.7.1");
8485
put("Accept", "*/*");
8586
}};
86-
87+
88+
protected Map<String, String> customHeaders = new HashMap<>();
89+
8790
protected SimpleCookieStore cookieStore = new SimpleCookieStore();
8891

8992
/**
@@ -193,6 +196,17 @@ public String getHost() {
193196
public int getPort() {
194197
return this.port;
195198
}
199+
200+
/**
201+
* Sets Custom Headers of this service
202+
*
203+
* @param headers
204+
*/
205+
public void setCustomHeaders(Map<String, String> headers) {
206+
if (Objects.nonNull(headers)) {
207+
customHeaders = headers;
208+
}
209+
}
196210

197211
/**
198212
* Returns the SSL security protocol of this service.
@@ -258,6 +272,15 @@ public URL getUrl(String path) {
258272
throw new RuntimeException(e.getMessage(), e);
259273
}
260274
}
275+
276+
/**
277+
* Returns all the stored custom headers
278+
*
279+
* @return customHeaders The custom headers
280+
*/
281+
public Map<String, String> getCustomHeaders() {
282+
return customHeaders;
283+
}
261284

262285
/**
263286
* Returns all the stored cookies
@@ -441,6 +464,13 @@ public ResponseMessage send(String path, RequestMessage request) {
441464
if (header.containsKey(key)) continue;
442465
cn.setRequestProperty(key, entry.getValue());
443466
}
467+
// Add Custom Headers
468+
for (Entry<String, String> entry: customHeaders.entrySet()) {
469+
String key = entry.getKey();
470+
if (!header.containsKey(key)) {
471+
cn.setRequestProperty(key, entry.getValue());
472+
}
473+
}
444474

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

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

+1
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

+9
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

+17-1
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)