Skip to content

Commit 2371c6c

Browse files
committed
Adding support for Proxy in Slack / SlackService
Requirement for ashwanthkumar/gocd-slack-build-notifier#2
1 parent 5382e6e commit 2371c6c

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/main/java/in/ashwanthkumar/slack/webhook/Slack.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import in.ashwanthkumar.utils.collections.Lists;
55

66
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.net.Proxy;
79
import java.util.List;
810

911
import static in.ashwanthkumar.utils.lang.StringUtils.isEmpty;
@@ -17,13 +19,22 @@ public class Slack {
1719
private String channel;
1820
private String user;
1921
private String icon;
20-
private SlackService slackService = new SlackService();
22+
private SlackService slackService;
2123

22-
public Slack(String webhookUrl) {
24+
public Slack(String webhookUrl, Proxy proxy) {
2325
if (isEmpty(webhookUrl)) {
2426
throw new IllegalArgumentException("Webhook url is not provided");
2527
}
2628
this.webhookUrl = webhookUrl;
29+
this.slackService = new SlackService(proxy);
30+
}
31+
32+
public Slack(String webhookUrl) {
33+
this(webhookUrl, (Proxy) null);
34+
}
35+
36+
public Slack(String webhookUrl, String hostname, int port) {
37+
this(webhookUrl, new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port)));
2738
}
2839

2940
/**

src/main/java/in/ashwanthkumar/slack/webhook/service/SlackService.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.api.client.http.GenericUrl;
44
import com.google.api.client.http.HttpRequestFactory;
5-
import com.google.api.client.http.HttpTransport;
65
import com.google.api.client.http.UrlEncodedContent;
76
import com.google.api.client.http.javanet.NetHttpTransport;
87
import com.google.api.client.util.Maps;
@@ -11,6 +10,7 @@
1110
import in.ashwanthkumar.slack.webhook.SlackMessage;
1211

1312
import java.io.IOException;
13+
import java.net.Proxy;
1414
import java.util.ArrayList;
1515
import java.util.HashMap;
1616
import java.util.List;
@@ -21,8 +21,17 @@
2121

2222

2323
public class SlackService {
24-
private final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
25-
private final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
24+
private final HttpRequestFactory requestFactory;
25+
26+
public SlackService(Proxy proxy) {
27+
NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
28+
builder.setProxy(proxy);
29+
requestFactory = builder.build().createRequestFactory();
30+
}
31+
32+
public SlackService() {
33+
this(null);
34+
}
2635

2736
public void push(String webHookUrl, SlackMessage text, String username, String imageOrIcon, String destination, List<SlackAttachment> attachments) throws IOException {
2837
Map<String, Object> payload = new HashMap<String, Object>();

0 commit comments

Comments
 (0)