From 2299c2f831d637bf944e36c707d59153fac0c24f Mon Sep 17 00:00:00 2001 From: jamesloveday Date: Tue, 11 May 2021 12:11:57 +0100 Subject: [PATCH] Set Http protocol version Change allows for setting the highest http version protocol. Defaults to version 1.1 --- .../src/dist/deploy/30_http_client_txnmgr.xml | 3 ++- .../java/org/jpos/http/client/HttpQuery.java | 19 +++++++++++++- .../org/jpos/http/client/HttpClientTest.java | 25 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/modules/http-client/src/dist/deploy/30_http_client_txnmgr.xml b/modules/http-client/src/dist/deploy/30_http_client_txnmgr.xml index f6941aeede..d3aad71dc2 100644 --- a/modules/http-client/src/dist/deploy/30_http_client_txnmgr.xml +++ b/modules/http-client/src/dist/deploy/30_http_client_txnmgr.xml @@ -18,6 +18,7 @@ + - + \ No newline at end of file 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 062394c8d8..03f0f65e13 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 @@ -38,6 +38,7 @@ import javax.net.ssl.X509TrustManager; import javax.security.cert.CertificateExpiredException; +import org.apache.commons.codec.binary.StringUtils; import org.apache.http.*; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; @@ -101,6 +102,7 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De private String basicAuthenticationName; private RedirectStrategy redirectStrategy; private boolean ignoreNullRequest; + private String httpVersion; // Shared clients for the instance. // Created at configuration time; destroyed when this participant is destroyed. @@ -121,6 +123,16 @@ public int prepare (long id, Serializable o) { addHeaders(ctx, httpRequest); + //set the http protocol version, default to version 1.1 + //config example + HttpVersion definedVersion = null; + httpVersion = getVersion(ctx); + if(httpVersion != null && httpVersion.length() > 0) { + String[] versions = httpVersion.split(","); + definedVersion = new HttpVersion(Integer.parseInt(versions[0]), Integer.parseInt(versions[1])); + } + httpRequest.setProtocolVersion((definedVersion != null) ? definedVersion : HttpVersion.HTTP_1_1); + httpRequest.setConfig(RequestConfig.custom(). setConnectTimeout(connectTimeout). setSocketTimeout(timeout). @@ -208,6 +220,7 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException { timeout= cfg.getInt("timeout", DEFAULT_TIMEOUT); urlName = cfg.get("urlName", "HTTP_URL"); + httpVersion = cfg.get("httpVersion", "HTTP_VERSION"); methodName = cfg.get("methodName", "HTTP_METHOD"); paramsName = cfg.get ("paramsName", "HTTP_PARAMS"); requestName = cfg.get ("requestName", "HTTP_REQUEST"); @@ -342,6 +355,10 @@ private HttpRequestBase getHttpRequest(Context ctx) { return null; } + private String getVersion(Context ctx) { + return ctx.getString(httpVersion); + } + private ContentType getContentType (Context ctx) { return cfg.getBoolean("no-charset") ? ContentType.create(ctx.get(contentTypeName, contentType)) : @@ -406,4 +423,4 @@ private CloseableHttpAsyncClient destroyClient (CloseableHttpAsyncClient client) } return null; } -} +} \ No newline at end of file diff --git a/modules/http-client/src/test/java/org/jpos/http/client/HttpClientTest.java b/modules/http-client/src/test/java/org/jpos/http/client/HttpClientTest.java index 4c99cd801f..075735005f 100644 --- a/modules/http-client/src/test/java/org/jpos/http/client/HttpClientTest.java +++ b/modules/http-client/src/test/java/org/jpos/http/client/HttpClientTest.java @@ -192,4 +192,27 @@ public void testTrustWrongHostCertificate() { Integer sc = ctx.get ("HTTP_STATUS", 10000L); assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200"); } -} + + @Test + public void testHttpVersionDefault() { + Context ctx = new Context(); + ctx.put("HTTP_URL", "https://untrusted-root.badssl.com/"); + ctx.put("HTTP_METHOD", "GET"); + ctx.put("HTTP_TRUST_ALL_CERTS", "true"); + mgr.queue(ctx); + Integer sc = ctx.get ("HTTP_STATUS", 10000L); + assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200"); + } + + @Test + public void testHttpVersionSetValue() { + Context ctx = new Context(); + ctx.put("HTTP_URL", "https://untrusted-root.badssl.com/"); + ctx.put("HTTP_VERSION", "2,0"); + ctx.put("HTTP_METHOD", "GET"); + ctx.put("HTTP_TRUST_ALL_CERTS", "true"); + mgr.queue(ctx); + Integer sc = ctx.get ("HTTP_STATUS", 10000L); + assertEquals (Integer.valueOf(HttpStatus.SC_OK), sc, "Status code should be 200"); + } +} \ No newline at end of file