Skip to content

Commit 65579bd

Browse files
author
Stephane Landelle
committed
Merge pull request AsyncHttpClient#577 from salilsurendran/master
Pick up default config values from asynchttpclient.properties file
2 parents 6233657 + e89c646 commit 65579bd

File tree

7 files changed

+294
-35
lines changed

7 files changed

+294
-35
lines changed

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,126 +12,131 @@
1212
*/
1313
package org.asynchttpclient;
1414

15-
import static org.asynchttpclient.util.MiscUtil.getBoolean;
1615

16+
17+
import static org.asynchttpclient.util.MiscUtil.getBooleanValue;
18+
import static org.asynchttpclient.util.MiscUtil.getIntValue;
1719
import org.asynchttpclient.util.DefaultHostnameVerifier;
1820

1921
import javax.net.ssl.HostnameVerifier;
2022

23+
24+
2125
public final class AsyncHttpClientConfigDefaults {
2226

27+
2328
private AsyncHttpClientConfigDefaults() {
2429
}
2530

2631
public static final String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + ".";
2732

2833
public static int defaultMaxTotalConnections() {
29-
return Integer.getInteger(ASYNC_CLIENT + "maxTotalConnections", -1);
34+
return getIntValue(ASYNC_CLIENT + "maxTotalConnections", -1);
3035
}
3136

3237
public static int defaultMaxConnectionPerHost() {
33-
return Integer.getInteger(ASYNC_CLIENT + "maxConnectionsPerHost", -1);
38+
return getIntValue(ASYNC_CLIENT + "maxConnectionsPerHost", -1);
3439
}
3540

3641
public static int defaultConnectionTimeOutInMs() {
37-
return Integer.getInteger(ASYNC_CLIENT + "connectionTimeoutInMs", 60 * 1000);
42+
return getIntValue(ASYNC_CLIENT + "connectionTimeoutInMs", 60 * 1000);
3843
}
3944

4045
public static int defaultIdleConnectionInPoolTimeoutInMs() {
41-
return Integer.getInteger(ASYNC_CLIENT + "idleConnectionInPoolTimeoutInMs", 60 * 1000);
46+
return getIntValue(ASYNC_CLIENT + "idleConnectionInPoolTimeoutInMs", 60 * 1000);
4247
}
4348

4449
public static int defaultIdleConnectionTimeoutInMs() {
45-
return Integer.getInteger(ASYNC_CLIENT + "idleConnectionTimeoutInMs", 60 * 1000);
50+
return getIntValue(ASYNC_CLIENT + "idleConnectionTimeoutInMs", 60 * 1000);
4651
}
4752

4853
public static int defaultRequestTimeoutInMs() {
49-
return Integer.getInteger(ASYNC_CLIENT + "requestTimeoutInMs", 60 * 1000);
54+
return getIntValue(ASYNC_CLIENT + "requestTimeoutInMs", 60 * 1000);
5055
}
5156

5257
public static int defaultWebSocketIdleTimeoutInMs() {
53-
return Integer.getInteger(ASYNC_CLIENT + "webSocketTimoutInMS", 15 * 60 * 1000);
58+
return getIntValue(ASYNC_CLIENT + "webSocketTimoutInMS", 15 * 60 * 1000);
5459
}
5560

5661
public static int defaultMaxConnectionLifeTimeInMs() {
57-
return Integer.getInteger(ASYNC_CLIENT + "maxConnectionLifeTimeInMs", -1);
62+
return getIntValue(ASYNC_CLIENT + "maxConnectionLifeTimeInMs", -1);
5863
}
5964

6065
public static boolean defaultRedirectEnabled() {
61-
return Boolean.getBoolean(ASYNC_CLIENT + "redirectsEnabled");
66+
return getBooleanValue(ASYNC_CLIENT + "redirectsEnabled",false);
6267
}
6368

6469
public static int defaultMaxRedirects() {
65-
return Integer.getInteger(ASYNC_CLIENT + "maxRedirects", 5);
70+
return getIntValue(ASYNC_CLIENT + "maxRedirects", 5);
6671
}
6772

6873
public static boolean defaultCompressionEnabled() {
69-
return Boolean.getBoolean(ASYNC_CLIENT + "compressionEnabled");
74+
return getBooleanValue(ASYNC_CLIENT + "compressionEnabled",false);
7075
}
7176

7277
public static String defaultUserAgent() {
7378
return System.getProperty(ASYNC_CLIENT + "userAgent", "NING/1.0");
7479
}
7580

7681
public static int defaultIoThreadMultiplier() {
77-
return Integer.getInteger(ASYNC_CLIENT + "ioThreadMultiplier", 2);
82+
return getIntValue(ASYNC_CLIENT + "ioThreadMultiplier", 2);
7883
}
7984

8085
public static boolean defaultUseProxySelector() {
81-
return Boolean.getBoolean(ASYNC_CLIENT + "useProxySelector");
86+
return getBooleanValue(ASYNC_CLIENT + "useProxySelector",false);
8287
}
8388

8489
public static boolean defaultUseProxyProperties() {
85-
return Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
90+
return getBooleanValue(ASYNC_CLIENT + "useProxyProperties",false);
8691
}
8792

8893
public static boolean defaultStrict302Handling() {
89-
return Boolean.getBoolean(ASYNC_CLIENT + "strict302Handling");
94+
return getBooleanValue(ASYNC_CLIENT + "strict302Handling",false);
9095
}
9196

9297
public static boolean defaultAllowPoolingConnection() {
93-
return getBoolean(ASYNC_CLIENT + "allowPoolingConnection", true);
98+
return getBooleanValue(ASYNC_CLIENT + "allowPoolingConnection", true);
9499
}
95100

96101
public static boolean defaultUseRelativeURIsWithSSLProxies() {
97-
return getBoolean(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies", true);
102+
return getBooleanValue(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies", true);
98103
}
99104

100105
// unused/broken, left there for compatibility, fixed in Netty 4
101106
public static int defaultRequestCompressionLevel() {
102-
return Integer.getInteger(ASYNC_CLIENT + "requestCompressionLevel", -1);
107+
return getIntValue(ASYNC_CLIENT + "requestCompressionLevel", -1);
103108
}
104109

105110
public static int defaultMaxRequestRetry() {
106-
return Integer.getInteger(ASYNC_CLIENT + "maxRequestRetry", 5);
111+
return getIntValue(ASYNC_CLIENT + "maxRequestRetry", 5);
107112
}
108113

109114
public static boolean defaultAllowSslConnectionPool() {
110-
return getBoolean(ASYNC_CLIENT + "allowSslConnectionPool", true);
115+
return getBooleanValue(ASYNC_CLIENT + "allowSslConnectionPool", true);
111116
}
112117

113118
public static boolean defaultUseRawUrl() {
114-
return Boolean.getBoolean(ASYNC_CLIENT + "useRawUrl");
119+
return getBooleanValue(ASYNC_CLIENT + "useRawUrl",false);
115120
}
116121

117122
public static boolean defaultRemoveQueryParamOnRedirect() {
118-
return getBoolean(ASYNC_CLIENT + "removeQueryParamOnRedirect", true);
123+
return getBooleanValue(ASYNC_CLIENT + "removeQueryParamOnRedirect", true);
119124
}
120125

121126
public static HostnameVerifier defaultHostnameVerifier() {
122127
return new DefaultHostnameVerifier();
123128
}
124129

125130
public static boolean defaultSpdyEnabled() {
126-
return Boolean.getBoolean(ASYNC_CLIENT + "spdyEnabled");
131+
return getBooleanValue(ASYNC_CLIENT + "spdyEnabled",false);
127132
}
128133

129134
public static int defaultSpdyInitialWindowSize() {
130-
return Integer.getInteger(ASYNC_CLIENT + "spdyInitialWindowSize", 10 * 1024 * 1024);
135+
return getIntValue(ASYNC_CLIENT + "spdyInitialWindowSize", 10 * 1024 * 1024);
131136
}
132137

133138
public static int defaultSpdyMaxConcurrentStreams() {
134-
return Integer.getInteger(ASYNC_CLIENT + "spdyMaxConcurrentStreams", 100);
139+
return getIntValue(ASYNC_CLIENT + "spdyMaxConcurrentStreams", 100);
135140
}
136141

137142
public static boolean defaultAcceptAnyCertificate() {

api/src/main/java/org/asynchttpclient/util/AsyncImplHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static Class<AsyncHttpClient> getAsyncImplClass(String propertyName) {
5959
return asyncHttpClientImplClass;
6060
}
6161

62-
private static Properties getAsyncImplProperties() {
62+
public static Properties getAsyncImplProperties() {
6363
try {
6464
return AccessController.doPrivileged(new PrivilegedExceptionAction<Properties>() {
6565
public Properties run() throws IOException {

api/src/main/java/org/asynchttpclient/util/MiscUtil.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
*/
1313
package org.asynchttpclient.util;
1414

15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1517
import java.util.Collection;
1618
import java.util.Map;
19+
import java.util.Properties;
1720

1821
public class MiscUtil {
1922

23+
public final static Logger logger = LoggerFactory.getLogger(MiscUtil.class);
24+
2025
private MiscUtil() {
2126
}
2227

@@ -40,12 +45,69 @@ public static boolean isNonEmpty(Map<?, ?> map) {
4045
return map != null && !map.isEmpty();
4146
}
4247

43-
public static boolean getBoolean(String systemPropName, boolean defaultValue) {
44-
String systemPropValue = System.getProperty(systemPropName);
45-
return systemPropValue != null ? systemPropValue.equalsIgnoreCase("true") : defaultValue;
48+
// The getBooleanValue() method replaces this and reads the property from
49+
// properties file
50+
// too. Plus has a better check for invalid boolean values.
51+
/*
52+
* public static boolean getBoolean(String systemPropName, boolean
53+
* defaultValue) { String systemPropValue =
54+
* System.getProperty(systemPropName); return systemPropValue != null ?
55+
* systemPropValue.equalsIgnoreCase("true") : defaultValue; }
56+
*/
57+
58+
public static Integer getIntValue(String property, int defaultValue) {
59+
// Read system property and if not null return that.
60+
Integer value = Integer.getInteger(property);
61+
if (value != null)
62+
return value;
63+
Properties asyncHttpClientConfigProperties = AsyncImplHelper.getAsyncImplProperties();
64+
if (asyncHttpClientConfigProperties != null) {
65+
String valueString = asyncHttpClientConfigProperties.getProperty(property);
66+
try {
67+
// If property is present and is non null parse it.
68+
if (valueString != null)
69+
return Integer.parseInt(valueString);
70+
} catch (NumberFormatException e) {
71+
// If property couldn't be parsed log the error message and
72+
// return default value.
73+
logger.error("Property : " + property + " has value = " + valueString
74+
+ " which couldn't be parsed to an int value. Returning default value: " + defaultValue, e);
75+
}
76+
}
77+
return defaultValue;
78+
}
79+
80+
private static boolean isValidBooleanValue(String value) {
81+
return value != null && ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value));
4682
}
4783

84+
85+
public static Boolean getBooleanValue(String property, boolean defaultValue) {
86+
87+
// get from System property first
88+
String value = System.getProperty(property);
89+
if (isValidBooleanValue(value))
90+
return Boolean.parseBoolean(value);
91+
92+
// get from property file
93+
Properties asyncHttpClientConfigProperties = AsyncImplHelper.getAsyncImplProperties();
94+
if (asyncHttpClientConfigProperties != null) {
95+
value = asyncHttpClientConfigProperties.getProperty(property);
96+
if (isValidBooleanValue(value))
97+
return Boolean.parseBoolean(value);
98+
}
99+
100+
// have to use the default value now
101+
if (value != null)
102+
logger.error("Property : " + property + " has value = " + value
103+
+ " which couldn't be parsed to an boolean value. Returning default value: " + defaultValue);
104+
105+
return defaultValue;
106+
}
107+
108+
48109
public static <T> T withDefault(T value, T defaults) {
49110
return value != null? value : value;
50111
}
112+
51113
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.asynchttpclient;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class AsyncHttpClientConfigBuilderTest {
7+
8+
@Test
9+
public void testDefaultConfigValues() {
10+
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
11+
Assert.assertEquals(config.getConnectionTimeoutInMs(), 60000);
12+
Assert.assertEquals(config.getRequestTimeoutInMs(), 60000);
13+
Assert.assertEquals(config.getIdleConnectionTimeoutInMs(), 60000);
14+
Assert.assertFalse(config.isCompressionEnabled());
15+
Assert.assertFalse(config.isRedirectEnabled());
16+
System.setProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "connectionTimeoutInMs", "1000");
17+
System.setProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "requestTimeoutInMs", "500");
18+
System.setProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "compressionEnabled", "true");
19+
System.setProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "redirectsEnabled", "true");
20+
config = new AsyncHttpClientConfig.Builder().build();
21+
Assert.assertEquals(config.getConnectionTimeoutInMs(), 1000);
22+
Assert.assertEquals(config.getRequestTimeoutInMs(), 500);
23+
Assert.assertTrue(config.isCompressionEnabled());
24+
Assert.assertTrue(config.isRedirectEnabled());
25+
System.clearProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "connectionTimeoutInMs");
26+
System.clearProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "requestTimeoutInMs");
27+
System.clearProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "compressionEnabled");
28+
System.clearProperty(AsyncHttpClientConfig.ASYNC_CLIENT + "defaultRedirectsEnabled");
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.asynchttpclient;
2+
3+
import java.util.Properties;
4+
5+
import mockit.Mock;
6+
import mockit.MockUp;
7+
8+
import org.asynchttpclient.util.AsyncImplHelper;
9+
10+
public class AsyncImplHelperMock extends MockUp<AsyncImplHelper> {
11+
12+
private static Properties properties;
13+
14+
public AsyncImplHelperMock(Properties properties) {
15+
this.properties = properties;
16+
}
17+
18+
@Mock
19+
public Properties getAsyncImplProperties() {
20+
return properties;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)