View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin.httpclient;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.http.HttpHost;
28  import org.apache.http.auth.AuthScope;
29  import org.apache.http.auth.UsernamePasswordCredentials;
30  import org.apache.http.client.CredentialsProvider;
31  import org.apache.http.client.HttpClient;
32  import org.apache.http.client.config.RequestConfig;
33  import org.apache.http.impl.client.BasicCredentialsProvider;
34  import org.apache.http.impl.client.HttpClientBuilder;
35  import org.apache.maven.settings.Proxy;
36  import org.eluder.coveralls.maven.plugin.util.UrlUtils;
37  import org.eluder.coveralls.maven.plugin.util.Wildcards;
38  
39  class HttpClientFactory {
40  
41      private static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
42      private static final int DEFAULT_SOCKET_TIMEOUT = 60000;
43  
44      private final String targetUrl;
45  
46      private final HttpClientBuilder hcb = HttpClientBuilder.create();
47      private final RequestConfig.Builder rcb = RequestConfig.custom()
48              .setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT)
49              .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT);
50  
51      HttpClientFactory(final String targetUrl) {
52          this.targetUrl = targetUrl;
53      }
54  
55      public HttpClientFactory proxy(final Proxy proxy) {
56          if (proxy != null && isProxied(targetUrl, proxy)) {
57              rcb.setProxy(new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol()));
58              if (StringUtils.isNotBlank(proxy.getUsername())) {
59                  CredentialsProvider cp = new BasicCredentialsProvider();
60                  cp.setCredentials(
61                          new AuthScope(proxy.getHost(), proxy.getPort()),
62                          new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword())
63                  );
64                  hcb.setDefaultCredentialsProvider(cp);
65              }
66          }
67          return this;
68      }
69  
70      public HttpClient create() {
71          return hcb.setDefaultRequestConfig(rcb.build()).build();
72      }
73  
74      private boolean isProxied(final String url, final Proxy proxy) {
75          if (StringUtils.isNotBlank(proxy.getNonProxyHosts())) {
76              String host = UrlUtils.create(url).getHost();
77              String[] excludes = proxy.getNonProxyHosts().split("\\|", -1);
78              for (String exclude : excludes) {
79                  if (StringUtils.isNotBlank(exclude) && Wildcards.matches(host, exclude.trim())) {
80                      return false;
81                  }
82              }
83          }
84          return true;
85      }
86  }