|
| 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 | +} |
0 commit comments