From 389a72a04c28c93f6193ae6bc99c9720676552ad Mon Sep 17 00:00:00 2001 From: Bojan Momcilov Date: Mon, 28 Sep 2020 16:29:49 +0200 Subject: [PATCH 1/3] Adds ability to customize/mock HTTP client --- .../src/main/java/org/jpos/http/client/HttpQuery.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java index 8f1b7586ba..99741fd255 100644 --- a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java +++ b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java @@ -199,10 +199,13 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException { headers[i].substring(colonPos+1)); // header value } - buildClient(cfg); + client = buildClient(cfg); + if (!client.isRunning()) { + client.start(); + } } - protected void buildClient(Configuration cfg) throws ConfigurationException { + protected CloseableHttpAsyncClient buildClient(Configuration cfg) throws ConfigurationException { String redirProp = cfg.get("redirect-strategy", "default"); RedirectStrategy redirectStrategy; if ("default".equals(redirProp)) @@ -216,8 +219,7 @@ else if ("lax".equals(redirProp)) .useSystemProperties() .setRedirectStrategy(redirectStrategy); - client = builder.build(); - client.start(); + return builder.build(); } private String getURL (Context ctx) { From b4deaf6b39d6624b5832cf9be0d59dcdd576a406 Mon Sep 17 00:00:00 2001 From: Bojan Momcilov Date: Mon, 28 Sep 2020 20:14:00 +0200 Subject: [PATCH 2/3] Ability to customize HttpAsyncClientBuilder, lazy client start --- .../java/org/jpos/http/client/HttpQuery.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java index 99741fd255..feed0fe684 100644 --- a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java +++ b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java @@ -41,6 +41,7 @@ import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.message.BasicHeader; +import org.apache.http.nio.client.HttpAsyncClient; import org.apache.http.util.EntityUtils; import org.jpos.core.ConfigurationException; @@ -75,6 +76,7 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De private String statusName; private String contentTypeName; private String basicAuthenticationName; + private DefaultRedirectStrategy redirectStrategy; // A shared client for the instance. // Created at configuration time; destroyed when this participant is destroyed. @@ -116,7 +118,7 @@ public int prepare (long id, Serializable o) { // Ref: https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/authentication.html } - client.execute(httpRequest, httpCtx, new FutureCallback() { + getHttpClient().execute(httpRequest, httpCtx, new FutureCallback() { @Override public void completed(HttpResponse result) { ctx.log (result.getStatusLine()); @@ -199,27 +201,25 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException { headers[i].substring(colonPos+1)); // header value } - client = buildClient(cfg); - if (!client.isRunning()) { - client.start(); - } - } - - protected CloseableHttpAsyncClient buildClient(Configuration cfg) throws ConfigurationException { String redirProp = cfg.get("redirect-strategy", "default"); - RedirectStrategy redirectStrategy; if ("default".equals(redirProp)) redirectStrategy= DefaultRedirectStrategy.INSTANCE; else if ("lax".equals(redirProp)) redirectStrategy= LaxRedirectStrategy.INSTANCE; else throw new ConfigurationException("'redirect-strategy' must be 'lax' or 'default'"); + } - HttpAsyncClientBuilder builder = HttpAsyncClients.custom() - .useSystemProperties() - .setRedirectStrategy(redirectStrategy); + protected HttpAsyncClientBuilder getClientBuilder() { + return HttpAsyncClients.custom().useSystemProperties().setRedirectStrategy(redirectStrategy); + } - return builder.build(); + protected HttpAsyncClient getHttpClient() { + if (client == null) { + client = getClientBuilder().build(); + client.start(); + } + return client; } private String getURL (Context ctx) { From 3fd63553f1802bdeda465a194437f7b80a1f4e6b Mon Sep 17 00:00:00 2001 From: Bojan Momcilov Date: Mon, 28 Sep 2020 21:18:25 +0200 Subject: [PATCH 3/3] Add setter for trivial HTTP client mocking --- .../java/org/jpos/http/client/HttpQuery.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java index feed0fe684..eacd9f728d 100644 --- a/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java +++ b/modules/http-client/src/main/java/org/jpos/http/client/HttpQuery.java @@ -41,7 +41,6 @@ import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.message.BasicHeader; -import org.apache.http.nio.client.HttpAsyncClient; import org.apache.http.util.EntityUtils; import org.jpos.core.ConfigurationException; @@ -76,11 +75,11 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De private String statusName; private String contentTypeName; private String basicAuthenticationName; - private DefaultRedirectStrategy redirectStrategy; + private RedirectStrategy redirectStrategy; // A shared client for the instance. // Created at configuration time; destroyed when this participant is destroyed. - private CloseableHttpAsyncClient client = null; + private CloseableHttpAsyncClient httpClient = null; public HttpQuery () { super(); @@ -210,16 +209,20 @@ else if ("lax".equals(redirProp)) throw new ConfigurationException("'redirect-strategy' must be 'lax' or 'default'"); } - protected HttpAsyncClientBuilder getClientBuilder() { - return HttpAsyncClients.custom().useSystemProperties().setRedirectStrategy(redirectStrategy); + public CloseableHttpAsyncClient getHttpClient() { + if (httpClient == null) { + setHttpClient(getClientBuilder().build()); + httpClient.start(); + } + return httpClient; } - protected HttpAsyncClient getHttpClient() { - if (client == null) { - client = getClientBuilder().build(); - client.start(); - } - return client; + public void setHttpClient(CloseableHttpAsyncClient httpClient) { + this.httpClient = httpClient; + } + + protected HttpAsyncClientBuilder getClientBuilder() { + return HttpAsyncClients.custom().useSystemProperties().setRedirectStrategy(redirectStrategy); } private String getURL (Context ctx) { @@ -308,9 +311,9 @@ else if (hObj instanceof List) @Override public void destroy() { - if (client != null && client.isRunning()) { + if (httpClient != null && httpClient.isRunning()) { try { - client.close(); + httpClient.close(); } catch (IOException e) { warn(e); }