Skip to content

Commit 4ed2faa

Browse files
committed
fix: get connection timeout values from config
1 parent ec26162 commit 4ed2faa

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

common/src/main/java/com/mx/path/core/common/connect/ConnectionSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ default void describe(ObjectMap description) {
5454
Duration getRequestTimeout();
5555

5656
/**
57-
* @return true, if host name should be checked against the certificate
57+
* @return true if host name should NOT be checked against the certificate, false otherwise
5858
*/
5959
boolean getSkipHostNameVerify();
6060

common/src/main/java/com/mx/path/core/common/reflection/Fields.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public static Object coerceValueType(Class<?> targetType, Object value) {
105105
return null;
106106
}
107107

108+
if (targetType == value.getClass()) {
109+
return value;
110+
}
111+
108112
if (targetType == byte.class || targetType == Byte.class) {
109113
return coerceToByte(value);
110114
}

gateway/src/main/java/com/mx/path/gateway/configuration/ConnectionBinder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.mx.path.core.common.collection.ObjectMap;
1010
import com.mx.path.core.common.connect.AccessorConnectionSettings;
1111
import com.mx.path.core.common.gateway.GatewayException;
12+
import com.mx.path.core.common.lang.Durations;
1213
import com.mx.path.core.common.lang.Strings;
1314
import com.mx.path.gateway.connect.filter.CallbacksFilter;
1415
import com.mx.path.gateway.connect.filter.ErrorHandlerFilter;
@@ -64,7 +65,15 @@ public static AccessorConnectionSettings buildConnection(ObjectMap map, String c
6465
if (passwordString != null) {
6566
connection.keystorePassword(passwordString.toCharArray());
6667
}
67-
connection.skipHostNameVerify(Boolean.parseBoolean(String.valueOf(map.getMap(connectionName).get("skipHostNameVerify"))));
68+
String connectTimeoutString = map.getMap(connectionName).getAsString("connectTimeout");
69+
if (connectTimeoutString != null) {
70+
connection.connectTimeout(Durations.fromCompactString(connectTimeoutString));
71+
}
72+
String requestTimeoutString = map.getMap(connectionName).getAsString("requestTimeout");
73+
if (requestTimeoutString != null) {
74+
connection.requestTimeout(Durations.fromCompactString(requestTimeoutString));
75+
}
76+
connection.skipHostNameVerify(map.getMap(connectionName).getAsBoolean("skipHostNameVerify", false));
6877

6978
// Default request filters
7079
// todo: Provide way to configure the request filters in connection block

gateway/src/test/groovy/com/mx/path/gateway/configuration/ConnectionBinderTest.groovy

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.mx.path.gateway.configuration
22

3+
import java.time.Duration
4+
35
import com.mx.path.core.common.accessor.PathResponseStatus
46
import com.mx.path.core.common.collection.ObjectMap
7+
import com.mx.path.core.common.configuration.ConfigurationException
58
import com.mx.path.core.common.gateway.GatewayException
69
import com.mx.testing.binding.ConnectionWithBoundConfiguration
710

811
import spock.lang.Specification
12+
import spock.lang.Unroll
913

1014
class ConnectionBinderTest extends Specification {
1115

@@ -40,6 +44,9 @@ class ConnectionBinderTest extends Specification {
4044
certificateAlias == "alias"
4145
keystorePath == "path"
4246
keystorePassword.toString() == "password"
47+
connectTimeout == null
48+
requestTimeout == null
49+
!skipHostNameVerify
4350
}
4451
}
4552

@@ -73,6 +80,9 @@ class ConnectionBinderTest extends Specification {
7380
put("certificateAlias", "alias")
7481
put("keystorePath", "path")
7582
put("keystorePassword", "password")
83+
put("connectTimeout", "500ms")
84+
put("requestTimeout", "20s")
85+
put("skipHostNameVerify", true)
7686
}
7787
}
7888

@@ -85,7 +95,34 @@ class ConnectionBinderTest extends Specification {
8595
certificateAlias == "alias"
8696
keystorePath == "path"
8797
keystorePassword.toString() == "password"
98+
connectTimeout == Duration.ofMillis(500)
99+
requestTimeout == Duration.ofSeconds(20)
100+
skipHostNameVerify
101+
}
102+
}
103+
104+
@Unroll
105+
def "build connection throws on invalid #fieldName format"(String fieldName) {
106+
given:
107+
def configuration = new ObjectMap().tap {
108+
createMap("TestConnection").tap {
109+
put("baseUrl", "url")
110+
put(fieldName, "not-a-valid-duration")
111+
}
88112
}
113+
114+
when:
115+
subject.buildConnection(configuration, "TestConnection")
116+
117+
then:
118+
def e = thrown(ConfigurationException)
119+
e.message == "Invalid duration string: not-a-valid-duration"
120+
121+
where:
122+
fieldName << [
123+
"connectTimeout",
124+
"requestTimeout"
125+
]
89126
}
90127

91128
def "build connection and fail validation"() {

0 commit comments

Comments
 (0)