|
| 1 | +package io.github.primelib.primecodegenlib.java.feign.common.config; |
| 2 | + |
| 3 | +import lombok.AccessLevel; |
| 4 | +import lombok.Data; |
| 5 | +import lombok.NoArgsConstructor; |
| 6 | +import lombok.experimental.Accessors; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.jetbrains.annotations.ApiStatus; |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | + |
| 11 | +import java.net.Proxy; |
| 12 | +import java.net.URI; |
| 13 | +import java.net.URISyntaxException; |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.function.Consumer; |
| 18 | + |
| 19 | +@Data |
| 20 | +@Accessors(fluent = true) |
| 21 | +@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true) |
| 22 | +@Slf4j |
| 23 | +public class ProxySpec { |
| 24 | + /* |
| 25 | + * Only HTTP(S) proxies are supported due to library limitations |
| 26 | + */ |
| 27 | + @NotNull |
| 28 | + private Proxy.Type type = Proxy.Type.HTTP; |
| 29 | + |
| 30 | + /** |
| 31 | + * The host of the proxy server |
| 32 | + */ |
| 33 | + private String host; |
| 34 | + |
| 35 | + /** |
| 36 | + * The port of the proxy server |
| 37 | + */ |
| 38 | + private Integer port; |
| 39 | + |
| 40 | + /** |
| 41 | + * The username used to authenticate with the proxy, if applicable |
| 42 | + */ |
| 43 | + private String username; |
| 44 | + |
| 45 | + /** |
| 46 | + * The password used to authenticate with the proxy, if applicable |
| 47 | + */ |
| 48 | + private char[] password; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs a validated implementation of {@link ProxySpec}. |
| 52 | + * |
| 53 | + * @param spec the specification to process |
| 54 | + */ |
| 55 | + @ApiStatus.Internal |
| 56 | + public ProxySpec(Consumer<ProxySpec> spec) { |
| 57 | + spec.accept(this); |
| 58 | + validate(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Validates the Spec, will throw a exception if required parameters are missing |
| 63 | + * |
| 64 | + * @throws NullPointerException if a required parameter is missing |
| 65 | + * @throws IllegalArgumentException if a parameter has an invalid value |
| 66 | + */ |
| 67 | + public void validate() { |
| 68 | + Objects.requireNonNull(type, "type is a required parameter!"); |
| 69 | + if (type == Proxy.Type.HTTP) { |
| 70 | + Objects.requireNonNull(host, "host is a required parameter!"); |
| 71 | + Objects.requireNonNull(port, "port is a required parameter!"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a new {@link ProxySpec} with values taken from the proxy environment variables, if applicable. |
| 77 | + */ |
| 78 | + public static ProxySpec detect() { |
| 79 | + Set<String> proxies = new HashSet<>(2); |
| 80 | + proxies.add(System.getenv("http_proxy")); |
| 81 | + proxies.add(System.getenv("https_proxy")); |
| 82 | + |
| 83 | + // check if a proxy is configured |
| 84 | + for (String p : proxies) { |
| 85 | + try { |
| 86 | + URI proxyUri = new URI(p); |
| 87 | + return new ProxySpec(spec -> { |
| 88 | + spec.type(Proxy.Type.HTTP); |
| 89 | + spec.host(proxyUri.getHost()); |
| 90 | + spec.port(proxyUri.getPort() == -1 ? 80 : proxyUri.getPort()); |
| 91 | + }); |
| 92 | + } catch (URISyntaxException ignored) { |
| 93 | + // ignore invalid proxy settings |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // no proxy configured |
| 98 | + return new ProxySpec(spec -> { |
| 99 | + spec.type(Proxy.Type.DIRECT); |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments