forked from edouardswiac/zerotier-api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZTServiceImpl.java
386 lines (335 loc) · 12.9 KB
/
ZTServiceImpl.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package com.github.edouardswiac.zerotier;
import com.github.edouardswiac.zerotier.api.ZTCMember;
import com.github.edouardswiac.zerotier.api.ZTCNetwork;
import com.github.edouardswiac.zerotier.api.ZTController;
import com.github.edouardswiac.zerotier.api.ZTNet;
import com.github.edouardswiac.zerotier.api.ZTNetwork;
import com.github.edouardswiac.zerotier.api.ZTNetworkMember;
import com.github.edouardswiac.zerotier.api.ZTStatus;
import com.github.edouardswiac.zerotier.exceptions.ZTClientException;
import com.github.edouardswiac.zerotier.exceptions.ZTServiceException;
import com.github.edouardswiac.zerotier.interceptors.AuthenticationInterceptor;
import com.github.edouardswiac.zerotier.interceptors.LoggingInterceptor;
import com.github.edouardswiac.zerotier.interceptors.UserAgentInterceptor;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
public final class ZTServiceImpl implements ZTService {
private final OkHttpClient client;
private final Gson gson;
private final HttpUrl baseApiUrl;
private final static Logger LOG = LoggerFactory.getLogger("HTTP Client");
private final static String USER_AGENT = String.format("ZeroTier API Java Client v%s <https://github.com/edouardswiac/zerotier-api-java>", API_VERSION);
private final static MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
public ZTServiceImpl(String apiAccessToken) {
this(ZT_COM_CENTRAL_URL, apiAccessToken);
}
public ZTServiceImpl(String ztCentralUrl, String apiAccessToken) {
this.gson = new Gson();
this.client = new OkHttpClient();
// user agent
client.interceptors().add(new UserAgentInterceptor(USER_AGENT));
// auth
client.interceptors().add(new AuthenticationInterceptor(apiAccessToken));
// logging
client.interceptors().add(new LoggingInterceptor(LOG));
this.baseApiUrl = HttpUrl.parse(ztCentralUrl);
}
@Override
public void createNetwork(ZTNetwork network) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.post(RequestBody.create(MEDIA_TYPE_JSON, gson.toJson(network)))
.build();
execute(request);
}
@Override
public List<ZTNetwork> getNetworks() {
final HttpUrl targetUrl = baseApiUrl.newBuilder().addPathSegment("network").build();
final Request request = new Request.Builder().url(targetUrl).build();
return Arrays.asList(get(request, ZTNetwork[].class));
}
@Override
public ZTNetwork getNetwork(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkId)
.build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTNetwork.class);
}
@Override
public void updateNetwork(@NotNull ZTNetwork network) {
if (network.getId() == null)
throw new IllegalArgumentException("network ID must not be null");
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(network.getId())
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.post(RequestBody.create(MEDIA_TYPE_JSON, gson.toJson(network)))
.build();
execute(request);
}
@Override
public void deleteNetwork(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkId)
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.delete()
.build();
try {
execute(request);
} catch (ZTServiceException ex) {
// catch the 500 that's returned when deleting :/
if (!ex.getCause().getLocalizedMessage().contains("code=500")) {
throw ex;
}
}
}
@Override
public Map<String, Integer> getNetworkMembers(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.build();
final Type entityType = new TypeToken<HashMap<String, Integer>>(){}.getType();
final Request request = new Request.Builder().url(targetUrl).build();
final Map<String, Integer> membersWithVersion = get(request, entityType);
return Collections.unmodifiableMap(membersWithVersion);
}
@Override
public void createNetworkMember(@NotNull ZTNetworkMember networkMember) {
// zt api treats it the same
updateNetworkMember(networkMember);
}
@Override
public ZTNetworkMember getNetworkMember(@NotNull String networkId, @NotNull String address) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.addPathSegment(address)
.build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTNetworkMember.class);
}
@Override
public void updateNetworkMember(@NotNull ZTNetworkMember networkMember) {
if (networkMember.getNetworkId() == null)
throw new IllegalArgumentException("network ID must not be null");
if (networkMember.getNodeId() == null)
throw new IllegalArgumentException("node ID must not be null");
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkMember.getNetworkId())
.addPathSegment("member")
.addPathSegment(networkMember.getNodeId())
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.post(RequestBody.create(MEDIA_TYPE_JSON, gson.toJson(networkMember)))
.build();
execute(request);
}
@Override
public void deleteNetworkMember(@NotNull String networkId, String address) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.addPathSegment(address)
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.delete()
.build();
execute(request);
}
//=======================================
@Override
public ZTStatus status() {
final HttpUrl targetUrl = baseApiUrl.newBuilder().addPathSegment("status").build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTStatus.class);
}
@Override
public List<ZTNet> getNets() {
final HttpUrl targetUrl = baseApiUrl.newBuilder().addPathSegment("network").build();
final Request request = new Request.Builder().url(targetUrl).build();
return Arrays.asList(get(request, ZTNet[].class));
}
@Override
public ZTController controller() {
final HttpUrl targetUrl = baseApiUrl.newBuilder().addPathSegment("controller").build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTController.class);
}
@Override
public List<String> getCNetworks() {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network").build();
final Request request = new Request.Builder().url(targetUrl).build();
return Arrays.asList(get(request, String[].class));
}
@Override
public ZTCNetwork getCNetwork(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(networkId)
.build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTCNetwork.class);
}
@Override
public ZTCNetwork updateCNetwork(@NotNull ZTCNetwork network) {
if (network.getNwid() == null)
throw new IllegalArgumentException("network ID must not be null");
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(network.getNwid())
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.post(RequestBody.create(MEDIA_TYPE_JSON, gson.toJson(network)))
.build();
return get(request, ZTCNetwork.class);
}
@Override
public void deleteCNetwork(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(networkId)
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.delete()
.build();
try {
execute(request);
} catch (ZTServiceException ex) {
// catch the 500 that's returned when deleting :/
if (!ex.getCause().getLocalizedMessage().contains("code=500")) {
throw ex;
}
}
}
@Override
public Map<String, Integer> getCMembers(@NotNull String networkId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.build();
final Type entityType = new TypeToken<HashMap<String, Integer>>(){}.getType();
final Request request = new Request.Builder().url(targetUrl).build();
final Map<String, Integer> membersWithVersion = get(request, entityType);
return Collections.unmodifiableMap(membersWithVersion);
}
@Override
public ZTCMember getCMember(@NotNull String networkId, @NotNull String memberId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.addPathSegment(memberId)
.build();
final Request request = new Request.Builder().url(targetUrl).build();
return get(request, ZTCMember.class);
}
@Override
public ZTCMember updateCMember(@NotNull ZTCMember member) {
if (member.getNwid() == null)
throw new IllegalArgumentException("network ID must not be null");
if (member.getAddress() == null)
throw new IllegalArgumentException("address must not be null");
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(member.getNwid())
.addPathSegment("member")
.addPathSegment(member.getAddress())
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.post(RequestBody.create(MEDIA_TYPE_JSON, gson.toJson(member)))
.build();
return get(request, ZTCMember.class);
}
@Override
public void deleteCMember(@NotNull ZTCMember member) {
if (member.getNwid() == null)
throw new IllegalArgumentException("network ID must not be null");
if (member.getAddress() == null)
throw new IllegalArgumentException("address must not be null");
deleteCMember(member.getNwid(), member.getAddress());
}
@Override
public void deleteCMember(@NotNull String networkId, @NotNull String memberId) {
final HttpUrl targetUrl = baseApiUrl.newBuilder()
.addPathSegment("controller")
.addPathSegment("network")
.addPathSegment(networkId)
.addPathSegment("member")
.addPathSegment(memberId)
.build();
final Request request = new Request.Builder()
.url(targetUrl)
.delete()
.build();
try {
execute(request);
} catch (ZTServiceException ex) {
// catch the 500 that's returned when deleting :/
if (!ex.getCause().getLocalizedMessage().contains("code=500")) {
throw ex;
}
}
}
//=======================================
Response execute(Request request) {
try {
final Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response;
} catch(IOException e) {
throw new ZTServiceException(e);
}
}
<T> T get(Request request, Class<T> entityClass) {
final Response response = execute(request);
try {
return gson.fromJson(response.body().charStream(), entityClass);
} catch (IOException e) {
throw new ZTClientException("cannot deserialize resource into its corresponding bean", e);
}
}
<T> T get(Request request, Type entityClass) {
final Response response = execute(request);
try {
return gson.fromJson(response.body().charStream(), entityClass);
} catch (IOException e) {
throw new ZTClientException("cannot deserialize resource into its corresponding bean", e);
}
}
}