Skip to content

Commit ec8784b

Browse files
committed
Extract HttpClient creation to factory method
1 parent 19c47a7 commit ec8784b

File tree

2 files changed

+89
-35
lines changed

2 files changed

+89
-35
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.simplelocalize.cli.client;
2+
3+
import io.simplelocalize.cli.client.dto.ProxyConfiguration;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.net.Authenticator;
8+
import java.net.InetSocketAddress;
9+
import java.net.PasswordAuthentication;
10+
import java.net.ProxySelector;
11+
import java.net.http.HttpClient;
12+
import java.time.Duration;
13+
14+
public class HttpClientFactory
15+
{
16+
private static final Logger log = LoggerFactory.getLogger(HttpClientFactory.class);
17+
18+
private HttpClientFactory()
19+
{
20+
}
21+
22+
public static HttpClient createHttpClient()
23+
{
24+
HttpClient.Builder builder = HttpClient
25+
.newBuilder()
26+
.connectTimeout(Duration.ofMinutes(5));
27+
28+
String httpProxyValue = getSystemProxyVariable();
29+
if (httpProxyValue == null || httpProxyValue.isEmpty())
30+
{
31+
return builder.build();
32+
}
33+
34+
ProxyConfiguration proxyConfigOptional = SystemProxySelector.getHttpProxyValueOrNull(httpProxyValue);
35+
if (proxyConfigOptional != null)
36+
{
37+
log.info("Using proxy: {}", proxyConfigOptional);
38+
ProxySelector proxySelector = getProxySelector(proxyConfigOptional);
39+
builder.proxy(proxySelector);
40+
41+
String proxyUsername = proxyConfigOptional.getUsername();
42+
String proxyPassword = proxyConfigOptional.getPassword();
43+
boolean hasProxyAuthentication = proxyUsername != null && proxyPassword != null;
44+
if (hasProxyAuthentication)
45+
{
46+
Authenticator authenticator = getAuthenticator(proxyUsername, proxyPassword);
47+
builder.authenticator(authenticator);
48+
}
49+
}
50+
51+
return builder.build();
52+
}
53+
54+
private static String getSystemProxyVariable()
55+
{
56+
String systemProxyVariable = System.getenv("http_proxy");
57+
if (systemProxyVariable == null)
58+
{
59+
systemProxyVariable = System.getenv("https_proxy");
60+
}
61+
return systemProxyVariable;
62+
}
63+
64+
private static ProxySelector getProxySelector(ProxyConfiguration proxyConfigOptional)
65+
{
66+
String host = proxyConfigOptional.getHost();
67+
Integer port = proxyConfigOptional.getPort();
68+
InetSocketAddress proxyAddress = new InetSocketAddress(host, port);
69+
return ProxySelector.of(proxyAddress);
70+
}
71+
72+
private static Authenticator getAuthenticator(String proxyUsername, String proxyPassword)
73+
{
74+
return new Authenticator()
75+
{
76+
@Override
77+
protected PasswordAuthentication getPasswordAuthentication()
78+
{
79+
return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
80+
}
81+
};
82+
}
83+
}

src/main/java/io/simplelocalize/cli/client/SimpleLocalizeClient.java

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.jayway.jsonpath.JsonPath;
55
import com.jayway.jsonpath.Option;
6-
import io.simplelocalize.cli.client.dto.*;
6+
import io.simplelocalize.cli.client.dto.DownloadRequest;
7+
import io.simplelocalize.cli.client.dto.DownloadableFile;
8+
import io.simplelocalize.cli.client.dto.ExportResponse;
9+
import io.simplelocalize.cli.client.dto.UploadRequest;
710
import io.simplelocalize.cli.exception.ApiRequestException;
811
import org.slf4j.Logger;
912
import org.slf4j.LoggerFactory;
1013

1114
import java.io.IOException;
12-
import java.net.*;
15+
import java.net.URI;
1316
import java.net.http.HttpClient;
1417
import java.net.http.HttpRequest;
1518
import java.net.http.HttpResponse;
1619
import java.nio.file.Files;
1720
import java.nio.file.Path;
1821
import java.nio.file.StandardOpenOption;
19-
import java.time.Duration;
2022
import java.util.Collection;
2123
import java.util.List;
2224
import java.util.Objects;
@@ -44,38 +46,7 @@ public SimpleLocalizeClient(String baseUrl, String apiKey)
4446
this.uriFactory = new SimpleLocalizeUriFactory(baseUrl);
4547
this.httpRequestFactory = new SimpleLocalizeHttpRequestFactory(apiKey);
4648
this.objectMapper = new ObjectMapper();
47-
HttpClient.Builder builder = HttpClient
48-
.newBuilder()
49-
.connectTimeout(Duration.ofMinutes(5));
50-
51-
String httpProxyValue = System.getenv("http_proxy");
52-
ProxyConfiguration proxyConfigOptional = SystemProxySelector.getHttpProxyValueOrNull(httpProxyValue);
53-
if (proxyConfigOptional != null)
54-
{
55-
log.info("Using proxy: {}", proxyConfigOptional);
56-
String host = proxyConfigOptional.getHost();
57-
Integer port = proxyConfigOptional.getPort();
58-
InetSocketAddress proxyAddress = new InetSocketAddress(host, port);
59-
builder.proxy(ProxySelector.of(proxyAddress));
60-
61-
String proxyUsername = proxyConfigOptional.getUsername();
62-
String proxyPassword = proxyConfigOptional.getPassword();
63-
boolean hasAuthentication = proxyUsername != null && proxyPassword != null;
64-
if (hasAuthentication)
65-
{
66-
Authenticator authenticator = new Authenticator()
67-
{
68-
@Override
69-
protected PasswordAuthentication getPasswordAuthentication()
70-
{
71-
return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
72-
}
73-
};
74-
builder.authenticator(authenticator);
75-
}
76-
}
77-
78-
this.httpClient = builder.build();
49+
this.httpClient = HttpClientFactory.createHttpClient();
7950
}
8051

8152
public static SimpleLocalizeClient create(String baseUrl, String apiKey)

0 commit comments

Comments
 (0)