Skip to content

Commit a4bb6a2

Browse files
coder-hugoRyudo302
authored andcommitted
Send notifications via proxy if globally configured
1 parent b4ec00e commit a4bb6a2

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/main/java/jenkins/plugins/googlechat/StandardGoogleChatService.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package jenkins.plugins.googlechat;
22

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+
39
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;
416
import java.net.URI;
517
import java.net.http.HttpClient;
618
import java.net.http.HttpRequest;
719
import java.net.http.HttpResponse;
820
import java.time.Duration;
21+
import java.util.List;
922
import java.util.logging.Level;
1023
import java.util.logging.Logger;
1124

12-
import org.apache.commons.lang3.StringUtils;
13-
14-
import io.cnaik.Messages;
15-
import io.cnaik.model.google.MessageReplyOption;
16-
1725
public class StandardGoogleChatService implements GoogleChatService {
1826

1927
private static final Logger logger = Logger.getLogger(StandardGoogleChatService.class.getName());
@@ -99,7 +107,47 @@ private boolean call(String urlDetail, GoogleChatRequest request) {
99107
}
100108

101109
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+
};
103151
}
104152

105153
private boolean checkIfValidURL(String url) {

0 commit comments

Comments
 (0)