forked from gpedro/slack-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackApi.java
103 lines (83 loc) · 2.38 KB
/
SlackApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package net.gpedro.integrations.slack;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
public class SlackApi {
private static final String POST = "POST";
private static final String PAYLOAD = "payload=";
private static final String UTF_8 = "UTF-8";
private final String service;
private final int timeout;
private final Proxy proxy;
public SlackApi(String service) {
this(service, 5000);
}
public SlackApi(String service, Proxy proxy) {
this(service, 5000, proxy);
}
public SlackApi(String service, int timeout) {
this(service, timeout, Proxy.NO_PROXY);
}
public SlackApi(String service, int timeout, Proxy proxy) {
this.timeout = timeout;
if (service == null) {
throw new IllegalArgumentException("Missing WebHook URL Configuration @ SlackApi");
}
if (proxy == null) {
this.proxy = Proxy.NO_PROXY;
} else {
this.proxy = proxy;
}
this.service = service;
}
/**
* Prepare Message and send to Slack
*/
public void call(SlackMessage message) {
if (message != null) {
this.send(message.prepare());
}
}
private String send(JsonObject message) {
HttpURLConnection connection = null;
try {
// Create connection
final URL url = new URL(this.service);
connection = (HttpURLConnection) url.openConnection(proxy);
connection.setRequestMethod(POST);
connection.setConnectTimeout(timeout);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
final String payload = PAYLOAD + URLEncoder.encode(message.toString(), UTF_8);
// Send request
final DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(payload);
wr.flush();
wr.close();
// Get Response
final InputStream is = connection.getInputStream();
final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\n');
}
rd.close();
return response.toString();
} catch (Exception e) {
throw new SlackException(e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}