|
1 | 1 | package jenkins.plugins.googlechat;
|
2 | 2 |
|
| 3 | +import hudson.ProxyConfiguration; |
| 4 | +import io.cnaik.Messages; |
| 5 | +import io.cnaik.model.google.MessageReplyOption; |
| 6 | +import jenkins.model.Jenkins; |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | + |
3 | 9 | import java.io.IOException;
|
| 10 | +import java.net.Authenticator; |
| 11 | +import java.net.InetSocketAddress; |
| 12 | +import java.net.PasswordAuthentication; |
| 13 | +import java.net.Proxy; |
| 14 | +import java.net.ProxySelector; |
| 15 | +import java.net.SocketAddress; |
4 | 16 | import java.net.URI;
|
5 | 17 | import java.net.http.HttpClient;
|
6 | 18 | import java.net.http.HttpRequest;
|
7 | 19 | import java.net.http.HttpResponse;
|
8 | 20 | import java.time.Duration;
|
| 21 | +import java.util.List; |
9 | 22 | import java.util.logging.Level;
|
10 | 23 | import java.util.logging.Logger;
|
11 | 24 |
|
12 |
| -import org.apache.commons.lang3.StringUtils; |
13 |
| - |
14 |
| -import io.cnaik.Messages; |
15 |
| -import io.cnaik.model.google.MessageReplyOption; |
16 |
| - |
17 | 25 | public class StandardGoogleChatService implements GoogleChatService {
|
18 | 26 |
|
19 | 27 | private static final Logger logger = Logger.getLogger(StandardGoogleChatService.class.getName());
|
@@ -99,7 +107,47 @@ private boolean call(String urlDetail, GoogleChatRequest request) {
|
99 | 107 | }
|
100 | 108 |
|
101 | 109 | protected HttpClient getHttpClient() {
|
102 |
| - return HttpClient.newHttpClient(); |
| 110 | + HttpClient.Builder builder = HttpClient.newBuilder(); |
| 111 | + Jenkins jenkins = Jenkins.getInstanceOrNull(); |
| 112 | + if (jenkins != null) { |
| 113 | + ProxyConfiguration proxy = jenkins.proxy; |
| 114 | + if (proxy != null) { |
| 115 | + builder.proxy(getProxySelector(proxy)); |
| 116 | + if (proxy.getUserName() != null) { |
| 117 | + builder.authenticator(getProxyAuthenticator(proxy)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return builder.build(); |
| 122 | + } |
| 123 | + |
| 124 | + private static ProxySelector getProxySelector(ProxyConfiguration proxy) { |
| 125 | + return new ProxySelector() { |
| 126 | + @Override |
| 127 | + public List<Proxy> select(URI uri) { |
| 128 | + if (proxy.getNoProxyHostPatterns().stream().anyMatch(pattern -> pattern.matcher(uri.getHost()).matches())) { |
| 129 | + return List.of(Proxy.NO_PROXY); |
| 130 | + } |
| 131 | + return List.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port))); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 136 | + |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + private static Authenticator getProxyAuthenticator(ProxyConfiguration proxy) { |
| 142 | + return new Authenticator() { |
| 143 | + @Override |
| 144 | + protected PasswordAuthentication getPasswordAuthentication() { |
| 145 | + if (getRequestorType() == RequestorType.PROXY && getRequestingHost().equalsIgnoreCase(proxy.name) && getRequestingPort() == proxy.port) { |
| 146 | + return new PasswordAuthentication(proxy.getUserName(), proxy.getSecretPassword().getPlainText().toCharArray()); |
| 147 | + } |
| 148 | + return null; |
| 149 | + } |
| 150 | + }; |
103 | 151 | }
|
104 | 152 |
|
105 | 153 | private boolean checkIfValidURL(String url) {
|
|
0 commit comments