-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathTwilio.java
282 lines (243 loc) · 8.45 KB
/
Twilio.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.twilio;
import com.twilio.exception.ApiException;
import com.twilio.exception.AuthenticationException;
import com.twilio.exception.CertificateValidationException;
import com.twilio.http.HttpMethod;
import com.twilio.http.NetworkHttpClient;
import com.twilio.http.Request;
import com.twilio.http.Response;
import com.twilio.http.TwilioRestClient;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.io.File;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.LogManager;
/**
* Singleton class to initialize Twilio environment.
*/
public class Twilio {
public static final String VERSION = "8.3.0";
public static final String JAVA_VERSION = System.getProperty("java.version");
private static String username = System.getenv("TWILIO_ACCOUNT_SID");
private static String password = System.getenv("TWILIO_AUTH_TOKEN");
private static String accountSid; // username used if this is null
private static String region = System.getenv("TWILIO_REGION");
private static String edge = System.getenv("TWILIO_EDGE");
private static volatile TwilioRestClient restClient;
private static volatile ExecutorService executorService;
private Twilio() {
}
/*
* Ensures that the ExecutorService is shutdown when the JVM exits.
*/
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (executorService != null) {
executorService.shutdownNow();
}
}
});
}
/**
* Initialize the Twilio environment.
*
* @param username account to use
* @param password auth token for the account
*/
public static synchronized void init(final String username, final String password) {
Twilio.setUsername(username);
Twilio.setPassword(password);
}
/**
* Initialize the Twilio environment.
*
* @param username account to use
* @param password auth token for the account
* @param accountSid account sid to use
*/
public static synchronized void init(final String username, final String password, final String accountSid) {
Twilio.setUsername(username);
Twilio.setPassword(password);
Twilio.setAccountSid(accountSid);
}
/**
* Set the username.
*
* @param username account to use
* @throws AuthenticationException if username is null
*/
public static synchronized void setUsername(final String username) {
if (username == null) {
throw new AuthenticationException("Username can not be null");
}
if (!username.equals(Twilio.username)) {
Twilio.invalidate();
}
Twilio.username = username;
}
/**
* Set the auth token.
*
* @param password auth token to use
* @throws AuthenticationException if password is null
*/
public static synchronized void setPassword(final String password) {
if (password == null) {
throw new AuthenticationException("Password can not be null");
}
if (!password.equals(Twilio.password)) {
Twilio.invalidate();
}
Twilio.password = password;
}
/**
* Set the account sid.
*
* @param accountSid account sid to use
* @throws AuthenticationException if account sid is null
*/
public static synchronized void setAccountSid(final String accountSid) {
if (accountSid == null) {
throw new AuthenticationException("AccountSid can not be null");
}
if (!accountSid.equals(Twilio.accountSid)) {
Twilio.invalidate();
}
Twilio.accountSid = accountSid;
}
/**
* Set the region.
*
* @param region region to make request
*/
public static synchronized void setRegion(final String region) {
if (!Objects.equals(region, Twilio.region)) {
Twilio.invalidate();
}
Twilio.region = region;
}
/**
* Set the edge.
*
* @param edge edge to make request
*/
public static synchronized void setEdge(final String edge) {
if (!Objects.equals(edge, Twilio.edge)) {
Twilio.invalidate();
}
Twilio.edge = edge;
}
/**
* Set the logger configuration file path.
*
* @param filePath path to logging configuration file
* @param loggerContext defaults to false to get the appropriate logger context for the caller.
*/
public static synchronized void setLoggerConfiguration(final String filePath, final boolean... loggerContext) {
boolean logContext = (loggerContext.length >= 1) ? loggerContext[0] : false;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(logContext);
File file = new File(filePath);
context.setConfigLocation(file.toURI());
}
/**
* Returns (and initializes if not initialized) the Twilio Rest Client.
*
* @return the Twilio Rest Client
* @throws AuthenticationException if initialization required and either accountSid or authToken is null
*/
public static TwilioRestClient getRestClient() {
if (Twilio.restClient == null) {
synchronized (Twilio.class) {
if (Twilio.restClient == null) {
Twilio.restClient = buildRestClient();
}
}
}
return Twilio.restClient;
}
private static TwilioRestClient buildRestClient() {
if (Twilio.username == null || Twilio.password == null) {
throw new AuthenticationException(
"TwilioRestClient was used before AccountSid and AuthToken were set, please call Twilio.init()"
);
}
TwilioRestClient.Builder builder = new TwilioRestClient.Builder(Twilio.username, Twilio.password);
if (Twilio.accountSid != null) {
builder.accountSid(Twilio.accountSid);
}
builder.region(Twilio.region);
builder.edge(Twilio.edge);
return builder.build();
}
/**
* Use a custom rest client.
*
* @param restClient rest client to use
*/
public static void setRestClient(final TwilioRestClient restClient) {
synchronized (Twilio.class) {
Twilio.restClient = restClient;
}
}
/**
* Returns the Twilio executor service.
*
* @return the Twilio executor service
*/
public static ExecutorService getExecutorService() {
if (Twilio.executorService == null) {
synchronized (Twilio.class) {
if (Twilio.executorService == null) {
Twilio.executorService = Executors.newCachedThreadPool();
}
}
}
return Twilio.executorService;
}
/**
* Use a custom executor service.
*
* @param executorService executor service to use
*/
public static void setExecutorService(final ExecutorService executorService) {
synchronized (Twilio.class) {
Twilio.executorService = executorService;
}
}
/**
* Validate that we can connect to the new SSL certificate posted on api.twilio.com.
*
* @throws CertificateValidationException if the connection fails
*/
public static void validateSslCertificate() {
final NetworkHttpClient client = new NetworkHttpClient();
final Request request = new Request(HttpMethod.GET, "https://api.twilio.com:8443");
try {
final Response response = client.makeRequest(request);
if (!TwilioRestClient.SUCCESS.test(response.getStatusCode())) {
throw new CertificateValidationException(
"Unexpected response from certificate endpoint", request, response
);
}
} catch (final ApiException e) {
throw new CertificateValidationException("Could not get response from certificate endpoint", request);
}
}
/**
* Invalidates the volatile state held in the Twilio singleton.
*/
private static void invalidate() {
Twilio.restClient = null;
}
/**
* Attempts to gracefully shutdown the ExecutorService if it is present.
*/
public static synchronized void destroy() {
if (executorService != null) {
executorService.shutdown();
}
}
}