|
| 1 | +package org.jfrog.build.client; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.apache.http.HttpException; |
| 5 | +import org.apache.http.HttpHost; |
| 6 | +import org.apache.http.HttpRequest; |
| 7 | +import org.apache.http.HttpRequestInterceptor; |
| 8 | +import org.apache.http.auth.AuthScope; |
| 9 | +import org.apache.http.auth.AuthState; |
| 10 | +import org.apache.http.auth.Credentials; |
| 11 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 12 | +import org.apache.http.client.CredentialsProvider; |
| 13 | +import org.apache.http.client.config.RequestConfig; |
| 14 | +import org.apache.http.client.protocol.HttpClientContext; |
| 15 | +import org.apache.http.conn.routing.HttpRoute; |
| 16 | +import org.apache.http.impl.auth.BasicScheme; |
| 17 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 20 | +import org.apache.http.impl.client.HttpClients; |
| 21 | +import org.apache.http.impl.conn.DefaultRoutePlanner; |
| 22 | +import org.apache.http.impl.conn.DefaultSchemePortResolver; |
| 23 | +import org.apache.http.protocol.HttpContext; |
| 24 | + |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.net.MalformedURLException; |
| 29 | +import java.net.URL; |
| 30 | +import java.util.Properties; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Lior Hasson |
| 34 | + */ |
| 35 | +public class HttpClientConfigurator { |
| 36 | + private final static String CLIENT_VERSION; |
| 37 | + private HttpClientBuilder builder = HttpClients.custom(); |
| 38 | + private RequestConfig.Builder config = RequestConfig.custom(); |
| 39 | + private String baseUrl; |
| 40 | + private BasicCredentialsProvider credsProvider; |
| 41 | + |
| 42 | + static { |
| 43 | + // initialize client version |
| 44 | + Properties properties = new Properties(); |
| 45 | + InputStream is = HttpClientConfigurator.class.getResourceAsStream("/bi.client.properties"); |
| 46 | + if (is != null) { |
| 47 | + try { |
| 48 | + properties.load(is); |
| 49 | + is.close(); |
| 50 | + } catch (IOException e) { |
| 51 | + // ignore, use the default value |
| 52 | + } |
| 53 | + } |
| 54 | + CLIENT_VERSION = properties.getProperty("client.version", "unknown"); |
| 55 | + } |
| 56 | + |
| 57 | + public HttpClientConfigurator() { |
| 58 | + String userAgent = "ArtifactoryBuildClient/" + CLIENT_VERSION; |
| 59 | + builder.setUserAgent(userAgent); |
| 60 | + |
| 61 | + /** |
| 62 | + Support system properties setup |
| 63 | + */ |
| 64 | + builder.useSystemProperties(); |
| 65 | + credsProvider = new BasicCredentialsProvider(); |
| 66 | + } |
| 67 | + |
| 68 | + public HttpClientConfigurator defaultMaxConnectionsPerRoute(int maxConnectionsPerRoute) { |
| 69 | + builder.setMaxConnPerRoute(maxConnectionsPerRoute); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public HttpClientConfigurator maxTotalConnections(int maxTotalConnections) { |
| 74 | + builder.setMaxConnTotal(maxTotalConnections); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public HttpClientConfigurator connectionTimeout(int connectionTimeout) { |
| 79 | + config.setConnectTimeout(connectionTimeout); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public HttpClientConfigurator soTimeout(int soTimeout) { |
| 84 | + config.setSocketTimeout(soTimeout); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public CloseableHttpClient getClient() { |
| 89 | + if (hasCredentials()) { |
| 90 | + builder.setDefaultCredentialsProvider(credsProvider); |
| 91 | + } |
| 92 | + return builder.setDefaultRequestConfig(config.build()).build(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * May throw a runtime exception when the given URL is invalid. |
| 97 | + */ |
| 98 | + public HttpClientConfigurator hostFromUrl(String urlStr) throws IllegalArgumentException { |
| 99 | + if (StringUtils.isNotBlank(urlStr)) { |
| 100 | + try { |
| 101 | + URL url = new URL(urlStr); |
| 102 | + host(url.getHost()); |
| 103 | + } catch (MalformedURLException e) { |
| 104 | + throw new IllegalArgumentException("Cannot parse the url " + urlStr, e); |
| 105 | + } |
| 106 | + } |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Ignores blank getValues |
| 112 | + */ |
| 113 | + public HttpClientConfigurator host(String host) { |
| 114 | + if (StringUtils.isNotBlank(host)) { |
| 115 | + this.baseUrl = host; |
| 116 | + builder.setRoutePlanner(new DefaultHostRoutePlanner(host)); |
| 117 | + } |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + private boolean hasCredentials() { |
| 122 | + return credsProvider.getCredentials(AuthScope.ANY) != null; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Ignores blank username input |
| 127 | + */ |
| 128 | + public HttpClientConfigurator authentication(String username, String password) { |
| 129 | + if (StringUtils.isNotBlank(username)) { |
| 130 | + if (StringUtils.isBlank(baseUrl)) { |
| 131 | + throw new IllegalStateException("Cannot configure authentication when host is not set."); |
| 132 | + } |
| 133 | + |
| 134 | + credsProvider.setCredentials( |
| 135 | + new AuthScope(baseUrl, AuthScope.ANY_PORT, AuthScope.ANY_REALM), |
| 136 | + new UsernamePasswordCredentials(username, password)); |
| 137 | + |
| 138 | + builder.addInterceptorFirst(new PreemptiveAuthInterceptor()); |
| 139 | + } |
| 140 | + |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public HttpClientConfigurator proxy(@Nullable ProxyConfiguration proxyConfiguration) { |
| 145 | + configureProxy(proxyConfiguration); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + private void configureProxy(ProxyConfiguration proxy) { |
| 150 | + if (proxy != null) { |
| 151 | + config.setProxy(new HttpHost(proxy.host, proxy.port)); |
| 152 | + if (proxy.username != null) { |
| 153 | + Credentials creds = new UsernamePasswordCredentials(proxy.username, proxy.password); |
| 154 | + //TODO NTLM? |
| 155 | + credsProvider.setCredentials(new AuthScope(proxy.host, proxy.port, AuthScope.ANY_REALM), creds); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + static class PreemptiveAuthInterceptor implements HttpRequestInterceptor { |
| 161 | + @Override |
| 162 | + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { |
| 163 | + HttpClientContext clientContext = HttpClientContext.adapt(context); |
| 164 | + AuthState authState = clientContext.getTargetAuthState(); |
| 165 | + |
| 166 | + // If there's no auth scheme available yet, try to initialize it preemptively |
| 167 | + if (authState.getAuthScheme() == null) { |
| 168 | + CredentialsProvider credsProvider = clientContext.getCredentialsProvider(); |
| 169 | + HttpHost targetHost = clientContext.getTargetHost(); |
| 170 | + Credentials creds = credsProvider.getCredentials( |
| 171 | + new AuthScope(targetHost.getHostName(), targetHost.getPort())); |
| 172 | + if (creds == null) { |
| 173 | + throw new HttpException("No credentials for preemptive authentication"); |
| 174 | + } |
| 175 | + authState.update(new BasicScheme(), creds); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + static class DefaultHostRoutePlanner extends DefaultRoutePlanner { |
| 181 | + private final HttpHost defaultHost; |
| 182 | + |
| 183 | + public DefaultHostRoutePlanner(String defaultHost) { |
| 184 | + super(DefaultSchemePortResolver.INSTANCE); |
| 185 | + this.defaultHost = new HttpHost(defaultHost); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public HttpRoute determineRoute(HttpHost host, HttpRequest request, HttpContext context) throws HttpException { |
| 190 | + if (host == null) { |
| 191 | + host = defaultHost; |
| 192 | + } |
| 193 | + |
| 194 | + return super.determineRoute(host, request, context); |
| 195 | + } |
| 196 | + |
| 197 | + public HttpHost getDefaultHost() { |
| 198 | + return defaultHost; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments