diff --git a/src/main/java/com/ecwid/consul/transport/AbstractHttpTransport.java b/src/main/java/com/ecwid/consul/transport/AbstractHttpTransport.java index 9ee0ba58..cb0c5470 100644 --- a/src/main/java/com/ecwid/consul/transport/AbstractHttpTransport.java +++ b/src/main/java/com/ecwid/consul/transport/AbstractHttpTransport.java @@ -5,16 +5,14 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.methods.*; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.nio.charset.Charset; +import java.util.Map; public abstract class AbstractHttpTransport implements HttpTransport { @@ -29,28 +27,36 @@ public abstract class AbstractHttpTransport implements HttpTransport { private static final Charset UTF_8 = Charset.forName("UTF-8"); @Override - public RawResponse makeGetRequest(String url) { + public RawResponse makeGetRequest(String url, Map headers) { HttpGet httpGet = new HttpGet(url); + + addHeadersToRequest(httpGet, headers); + return executeRequest(httpGet); } @Override - public RawResponse makePutRequest(String url, String content) { + public RawResponse makePutRequest(String url, String content, Map headers) { HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(content, UTF_8)); + + addHeadersToRequest(httpPut, headers); + return executeRequest(httpPut); } @Override - public RawResponse makePutRequest(String url, byte[] content) { + public RawResponse makePutRequest(String url, byte[] content, Map headers) { HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new ByteArrayEntity(content)); + addHeadersToRequest(httpPut, headers); return executeRequest(httpPut); } @Override - public RawResponse makeDeleteRequest(String url) { + public RawResponse makeDeleteRequest(String url, Map headers) { HttpDelete httpDelete = new HttpDelete(url); + addHeadersToRequest(httpDelete, headers); return executeRequest(httpDelete); } @@ -111,4 +117,17 @@ private Boolean parseBoolean(Header header) { return null; } + private void addHeadersToRequest(HttpRequestBase request, Map headers) { + if (headers == null) { + return; + } + + for (Map.Entry headerValue : headers.entrySet()) { + String name = headerValue.getKey(); + String value = headerValue.getValue(); + + request.addHeader(name, value); + } + } + } \ No newline at end of file diff --git a/src/main/java/com/ecwid/consul/transport/HttpTransport.java b/src/main/java/com/ecwid/consul/transport/HttpTransport.java index e2f8cd01..b8ae5b28 100644 --- a/src/main/java/com/ecwid/consul/transport/HttpTransport.java +++ b/src/main/java/com/ecwid/consul/transport/HttpTransport.java @@ -1,16 +1,18 @@ package com.ecwid.consul.transport; +import java.util.Map; + /** * @author Vasily Vasilkov (vgv@ecwid.com) */ public interface HttpTransport { - public RawResponse makeGetRequest(String url); + public RawResponse makeGetRequest(String url, Map headers); - public RawResponse makePutRequest(String url, String content); + public RawResponse makePutRequest(String url, String content, Map headers); - public RawResponse makePutRequest(String url, byte[] content); + public RawResponse makePutRequest(String url, byte[] content, Map headers); - public RawResponse makeDeleteRequest(String url); + public RawResponse makeDeleteRequest(String url, Map headers); } diff --git a/src/main/java/com/ecwid/consul/v1/ConsulRawClient.java b/src/main/java/com/ecwid/consul/v1/ConsulRawClient.java index 97fc1f24..7085a429 100644 --- a/src/main/java/com/ecwid/consul/v1/ConsulRawClient.java +++ b/src/main/java/com/ecwid/consul/v1/ConsulRawClient.java @@ -132,28 +132,28 @@ public RawResponse makeGetRequest(String endpoint, List urlParams String url = prepareUrl(agentAddress + endpoint); url = Utils.generateUrl(url, urlParams); - return httpTransport.makeGetRequest(url); + return httpTransport.makeGetRequest(url, null); } public RawResponse makePutRequest(String endpoint, String content, UrlParameters... urlParams) { String url = prepareUrl(agentAddress + endpoint); url = Utils.generateUrl(url, urlParams); - return httpTransport.makePutRequest(url, content); + return httpTransport.makePutRequest(url, content, null); } public RawResponse makePutRequest(String endpoint, byte[] content, UrlParameters... urlParams) { String url = prepareUrl(agentAddress + endpoint); url = Utils.generateUrl(url, urlParams); - return httpTransport.makePutRequest(url, content); + return httpTransport.makePutRequest(url, content, null); } public RawResponse makeDeleteRequest(String endpoint, UrlParameters... urlParams) { String url = prepareUrl(agentAddress + endpoint); url = Utils.generateUrl(url, urlParams); - return httpTransport.makeDeleteRequest(url); + return httpTransport.makeDeleteRequest(url, null); } private String prepareUrl(String url) {