Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 1: Add support for headers in HttpTransport
  • Loading branch information
vgv committed Sep 2, 2019
1 parent 531b89d commit fbf2fd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> headers) {
HttpDelete httpDelete = new HttpDelete(url);
addHeadersToRequest(httpDelete, headers);
return executeRequest(httpDelete);
}

Expand Down Expand Up @@ -111,4 +117,17 @@ private Boolean parseBoolean(Header header) {
return null;
}

private void addHeadersToRequest(HttpRequestBase request, Map<String, String> headers) {
if (headers == null) {
return;
}

for (Map.Entry<String, String> headerValue : headers.entrySet()) {
String name = headerValue.getKey();
String value = headerValue.getValue();

request.addHeader(name, value);
}
}

}
10 changes: 6 additions & 4 deletions src/main/java/com/ecwid/consul/transport/HttpTransport.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.ecwid.consul.transport;

import java.util.Map;

/**
* @author Vasily Vasilkov ([email protected])
*/
public interface HttpTransport {

public RawResponse makeGetRequest(String url);
public RawResponse makeGetRequest(String url, Map<String, String> headers);

public RawResponse makePutRequest(String url, String content);
public RawResponse makePutRequest(String url, String content, Map<String, String> headers);

public RawResponse makePutRequest(String url, byte[] content);
public RawResponse makePutRequest(String url, byte[] content, Map<String, String> headers);

public RawResponse makeDeleteRequest(String url);
public RawResponse makeDeleteRequest(String url, Map<String, String> headers);

}
8 changes: 4 additions & 4 deletions src/main/java/com/ecwid/consul/v1/ConsulRawClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,28 @@ public RawResponse makeGetRequest(String endpoint, List<UrlParameters> 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) {
Expand Down

0 comments on commit fbf2fd1

Please sign in to comment.