Skip to content

Commit 4cdd85a

Browse files
committed
move secure connection to http client which is a better fit for managing it
1 parent b29008b commit 4cdd85a

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/main/java/com/postmarkapp/postmark/client/HttpClient.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public enum DEFAULTS {
3131
private final MultivaluedMap<String,Object> headers;
3232
private final Client client;
3333

34+
private boolean secureConnection = true;
35+
3436
public HttpClient(MultivaluedMap<String,Object> headers, int connectTimeoutSeconds, int readTimeoutSeconds) {
3537
this(headers);
3638
setConnectTimeoutSeconds(connectTimeoutSeconds);
@@ -54,8 +56,9 @@ public HttpClient(MultivaluedMap<String,Object> headers) {
5456
* @return response from HTTP request
5557
*/
5658
public ClientResponse execute(REQUEST_TYPES requestType, String url, String data) {
59+
String httpUrl = getSecureUrl(url);
5760
Response response;
58-
WebTarget target = client.target(url);
61+
WebTarget target = client.target(httpUrl);
5962

6063
switch (requestType) {
6164
case POST:
@@ -113,6 +116,15 @@ public void setReadTimeoutSeconds(int readTimeoutSeconds) {
113116
client.property(ClientProperties.READ_TIMEOUT, readTimeoutSeconds * 1000);
114117
}
115118

119+
public void setSecureConnection(boolean secureConnection) {
120+
this.secureConnection = secureConnection;
121+
}
122+
123+
private String getSecureUrl(String url) {
124+
String urlPrefix = this.secureConnection ? "https://" : "http://";
125+
return urlPrefix + url;
126+
}
127+
116128
/**
117129
* Access to original HTTP client used for requests
118130
*

src/main/java/com/postmarkapp/postmark/client/HttpClientHandler.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class HttpClientHandler {
1515
private final HttpClient httpClient;
1616
protected final DataHandler dataHandler;
1717
private final HttpClientErrorHandler httpClientErrorHandler;
18-
private boolean secureConnection = true;
1918

2019
protected HttpClientHandler(MultivaluedMap<String,Object> headers) {
2120
this.dataHandler = new DataHandler(false);
@@ -25,7 +24,7 @@ protected HttpClientHandler(MultivaluedMap<String,Object> headers) {
2524

2625
protected HttpClientHandler(MultivaluedMap<String,Object> headers, boolean secureConnection) {
2726
this(headers);
28-
this.secureConnection = secureConnection;
27+
this.getHttpClient().setSecureConnection(secureConnection);
2928
}
3029

3130
/**
@@ -59,7 +58,7 @@ protected String execute(HttpClient.REQUEST_TYPES request_type, String url) thro
5958
* @return HTTP response message
6059
*/
6160
protected String execute(HttpClient.REQUEST_TYPES request_type, String url, Object data) throws PostmarkException, IOException {
62-
HttpClient.ClientResponse response = httpClient.execute(request_type, getSecureUrl(url), dataHandler.toJson(data));
61+
HttpClient.ClientResponse response = httpClient.execute(request_type, url, dataHandler.toJson(data));
6362

6463
if (response.getCode() == 200) {
6564
return response.getMessage();
@@ -77,10 +76,6 @@ public void setDebugMode() {
7776
this.dataHandler.setStrictMapper();
7877
}
7978

80-
public void setSecureConnection(boolean secureConnection) {
81-
this.secureConnection = secureConnection;
82-
}
83-
8479
/**
8580
* @return HTTP client which processes HTTP requests
8681
*/
@@ -89,7 +84,7 @@ public HttpClient getHttpClient() {
8984
}
9085

9186
/**
92-
* Delegation method for HTTP client connection setttings
87+
* Delegation method for HTTP client connection settings
9388
*
9489
* @param connectTimeoutSeconds HTTP client connection timeout
9590
*/
@@ -98,21 +93,25 @@ public void setConnectTimeoutSeconds(int connectTimeoutSeconds) {
9893
}
9994

10095
/**
101-
* Delegation method for HTTP client connection setttings
96+
* Delegation method for HTTP client connection settings
10297
*
10398
* @param readTimeoutSeconds HTTP client read timeout
10499
*/
105100
public void setReadTimeoutSeconds(int readTimeoutSeconds) {
106101
getHttpClient().setReadTimeoutSeconds(readTimeoutSeconds);
107102
}
108103

104+
/**
105+
* Delegation method for HTTP client connection settings
106+
*
107+
* @param secureConnection - choose http or https
108+
*/
109+
public void setSecureConnection(boolean secureConnection) {
110+
getHttpClient().setSecureConnection(secureConnection);
111+
}
112+
109113
/**
110114
* @return DataHandler which processes HTTP requests sent, and HTTP request responses
111115
*/
112116
public DataHandler getDataHandler() { return dataHandler; }
113-
114-
private String getSecureUrl(String url) {
115-
String urlPrefix = this.secureConnection ? "https://" : "http://";
116-
return urlPrefix + url;
117-
}
118117
}

src/test/java/unit/client/HttpClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public class HttpClientTest {
1717
@Test
1818
void execute() throws IOException, PostmarkException {
1919
HttpClient client = new HttpClient(new MultivaluedHashMap<>());
20-
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, "https://" + Postmark.DEFAULTS.API_URL.value);
20+
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, Postmark.DEFAULTS.API_URL.value);
2121

2222
assertNotNull(response.getMessage());
2323
}
2424

2525
@Test
2626
void executeIncorrectLink() throws IOException, PostmarkException {
2727
HttpClient client = new HttpClient(new MultivaluedHashMap<>());
28-
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, "https://" + Postmark.DEFAULTS.API_URL.value + "/someweirdlink");
28+
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, Postmark.DEFAULTS.API_URL.value + "/someweirdlink");
2929

3030
assertEquals(response.getCode(),404);
3131
}

0 commit comments

Comments
 (0)