diff --git a/splunk/src/main/java/com/splunk/HttpService.java b/splunk/src/main/java/com/splunk/HttpService.java index 9ce22781..41d50a64 100644 --- a/splunk/src/main/java/com/splunk/HttpService.java +++ b/splunk/src/main/java/com/splunk/HttpService.java @@ -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 @@ -83,7 +84,9 @@ public boolean verify(String s, SSLSession sslSession) { put("User-Agent", "splunk-sdk-java/1.7.1"); put("Accept", "*/*"); }}; - + + protected Map customHeaders = new HashMap<>(); + protected SimpleCookieStore cookieStore = new SimpleCookieStore(); /** @@ -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 headers) { + if (Objects.nonNull(headers)) { + customHeaders = headers; + } + } /** * Returns the SSL security protocol of this service. @@ -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 getCustomHeaders() { + return customHeaders; + } /** * Returns all the stored cookies @@ -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 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()); diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index 8856c1e2..a85e9d17 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -152,6 +152,7 @@ public Service(ServiceArgs args) { this.httpsHandler = Args.get(args, "httpsHandler", null); this.setSslSecurityProtocol(Args.get(args, "SSLSecurityProtocol", Service.getSslSecurityProtocol())); this.addCookie((String)args.get("cookie")); + this.setCustomHeaders((Map) args.get("customHeaders")); } /** diff --git a/splunk/src/main/java/com/splunk/ServiceArgs.java b/splunk/src/main/java/com/splunk/ServiceArgs.java index 38670741..bf8f7f03 100644 --- a/splunk/src/main/java/com/splunk/ServiceArgs.java +++ b/splunk/src/main/java/com/splunk/ServiceArgs.java @@ -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 @@ -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 httpHeaders) { + this.put("customHeaders", httpHeaders); + } } diff --git a/splunk/src/test/java/com/splunk/ServiceTest.java b/splunk/src/test/java/com/splunk/ServiceTest.java index b1681afe..727434f5 100644 --- a/splunk/src/test/java/com/splunk/ServiceTest.java +++ b/splunk/src/test/java/com/splunk/ServiceTest.java @@ -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() {{ + put("some header key", "some value"); + }}); + Service service = new Service(args); + Map customHeaders = service.getCustomHeaders(); + Assert.assertEquals(customHeaders.get("some header key"), "some value"); + } + @Test public void testLoginWithoutArguments() { ServiceArgs args = new ServiceArgs();