-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileDBClient.java
More file actions
377 lines (334 loc) · 12.5 KB
/
TileDBClient.java
File metadata and controls
377 lines (334 loc) · 12.5 KB
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
package io.tiledb.cloud;
import okhttp3.OkHttpClient;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TileDBClient{
private static String apiKey;
private static String username;
private static String password;
private static String basePath;
private static boolean verifyingSsl;
private static boolean loginInfoIsInJSONFile;
private static final String homeDir = System.getProperty("user.home");
private static String cloudFilePath;
static Logger logger = Logger.getLogger(TileDBClient.class.getName());
private io.tiledb.cloud.rest_api.ApiClient apiClient;
// Lazy-initialized versioned clients (v4 is optional and created via reflection)
private io.tiledb.cloud.rest_api.ApiClient v1Client;
private io.tiledb.cloud.rest_api.v2.ApiClient v2Client;
private io.tiledb.cloud.rest_api.v4.ApiClient v4Client;
public enum ApiVersion { V1, V2, V4 }
/**
* Static initialization.
*/
static
{
apiKey = "";
username = "";
password = "";
basePath = "https://api.tiledb.com/v1"; //default is TileDB (v1 for backward compatibility)
loginInfoIsInJSONFile = true;
verifyingSsl = true;
// set path according to OS
if (System.getProperty("os.name").toLowerCase().contains("windows")){
cloudFilePath = "\\.tiledb\\cloud.json";
} else{
cloudFilePath = "/.tiledb/cloud.json";
}
boolean ok = false;
try {
ok = loadCloudJSONFileFromHome();
} catch (Exception e) {
loginInfoIsInJSONFile = false;
}
if (!ok) {
loginInfoIsInJSONFile = false;
}
}
/**
* If exists, it reads the cloud.json file which is stored in the home
* folder to look for stored credentials.
* @return true if found
* @throws IOException
*/
private static boolean loadCloudJSONFileFromHome() throws IOException {
String fileName = homeDir + cloudFilePath;
File initialFile = new File(fileName);
InputStream is = Files.newInputStream(initialFile.toPath());
JSONTokener tokener = new JSONTokener(is);
JSONObject object = new JSONObject(tokener);
if (object.has("api_key")){
JSONObject apiKeyObject = object.getJSONObject("api_key");
if (apiKeyObject.has("X-TILEDB-REST-API-KEY")) {
apiKey = apiKeyObject.getString("X-TILEDB-REST-API-KEY");
logger.log(Level.INFO, "Found apiKey from disk");
}
}
if (object.has("username")){
username = object.getString("username");
logger.log(Level.INFO, "Found username from disk");
}
if (object.has("password")){
password = object.getString("password");
logger.log(Level.INFO, "Found password from disk");
}
if (object.has("verify_ssl")){
boolean verifySSL = object.getBoolean("verify_ssl");
verifyingSsl = verifySSL;
logger.log(Level.INFO, "Found verifySSL from disk");
}
if (object.has("host")){
String host = object.getString("host");
basePath = host;
logger.log(Level.INFO, "Found host from disk");
}
// check if credentials are adequate for logging in
if ((Objects.equals(apiKey, "") && (Objects.equals(password, "") && !Objects.equals(username, ""))
|| (Objects.equals(apiKey, "") && ((Objects.equals(password, "") || Objects.equals(username, "")))))){
return false;
}
return true;
}
/**
* Enables debugging logs
* @param flag True to enable
*/
public void setDebugging(boolean flag){
this.apiClient.setDebugging(flag);
}
/**
* This method throws a warning if there is no login information in the json file or passed
* as a parameter. If the login information has data it calls another helper method to save it.
* @param tileDBLogin
*/
private void findCredentials(TileDBLogin tileDBLogin) {
if (!loginInfoIsInJSONFile) {
//requires login from user for the first time
if (tileDBLogin == null || !tileDBLogin.isValid()){
logger.warning("No login info was provided nor found. " +
"Use the Login class to login for the first time");
} else {
populateFieldsFromLogin(tileDBLogin);
}
} else if (tileDBLogin != null && tileDBLogin.overwritePrevious()){ //in this case the data in the json is overwritten.
populateFieldsFromLogin(tileDBLogin);
}
}
/**
* Saves the data from the Login object.
* @param tileDBLogin The Login object
*/
private void populateFieldsFromLogin(TileDBLogin tileDBLogin) {
if (!tileDBLogin.getHost().equals("")){
basePath = tileDBLogin.getHost();
} else {
basePath = "https://api.tiledb.com/v1";
}
apiKey = tileDBLogin.getApiKey();
username = tileDBLogin.getUsername();
password = tileDBLogin.getPassword();
verifyingSsl = tileDBLogin.isVerifySSL();
if (tileDBLogin.rememberMe()) { //save credentials
writeAuthJSONFileToHome();
}
}
/**
* Writes the json file to the home folder
*/
private void writeAuthJSONFileToHome() {
JSONObject jsonObject = new JSONObject();
//create nested object for apiKey
JSONObject apiKeyObject = new JSONObject();
apiKeyObject.put("X-TILEDB-REST-API-KEY", apiKey);
//Inserting key-value pairs into the json object
jsonObject.put("api_key", apiKeyObject);
jsonObject.put("username", username);
jsonObject.put("password", password);
if (basePath.equals("https://api.tiledb.com/v1")){
jsonObject.put("host", "https://api.tiledb.com");
}else{
jsonObject.put("host", basePath);
}
jsonObject.put("verify_ssl", verifyingSsl);
try {
File file = new File(homeDir + cloudFilePath);
file.getParentFile().mkdirs(); //create /.tiledb dir if not present
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(jsonObject.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Basic constructor with custom OkHttpClient
*
* @param client an okhttp3.OkHttpClient object
* @param tileDBLogin Login object with credentials
*/
public TileDBClient(OkHttpClient client, TileDBLogin tileDBLogin){
apiClient = new io.tiledb.cloud.rest_api.ApiClient(client);
setClientCredentials(tileDBLogin);
setReadTimeout(0);
}
/**
* Basic constructor with custom OkHttpClient
*
* @param client an okhttp3.OkHttpClient object
*/
public TileDBClient(OkHttpClient client){
apiClient = new io.tiledb.cloud.rest_api.ApiClient(client);
setClientCredentials(new TileDBLogin());
setReadTimeout(0);
}
/**
* Basic constructor
*
* @param tileDBLogin Login object with credentials
*/
public TileDBClient(TileDBLogin tileDBLogin){
apiClient = new io.tiledb.cloud.rest_api.ApiClient();
setClientCredentials(tileDBLogin);
setReadTimeout(0);
}
/**
* Basic constructor
*
*/
public TileDBClient(){
apiClient = new io.tiledb.cloud.rest_api.ApiClient();
setClientCredentials(new TileDBLogin());
setReadTimeout(0);
}
/**
* Constructor for TileDBClient to support access token retry on 401/403 configured with base path, client ID, secret, and additional parameters
*
* @param basePath base path
* @param clientId client ID
* @param clientSecret client secret
* @param parameters a java.util.Map of parameters
*/
public TileDBClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters){
apiClient = new io.tiledb.cloud.rest_api.ApiClient(basePath, clientId, clientSecret, parameters);
setClientCredentials(new TileDBLogin());
setReadTimeout(0);
}
/**
* Constructor for TileDBClient to support access token retry on 401/403 configured with base path, client ID, secret, and additional parameters
*
* @param basePath base path
* @param clientId client ID
* @param clientSecret client secret
* @param parameters a java.util.Map of parameters
* @param tileDBLogin Login object with credentials
*/
public TileDBClient(String basePath, String clientId, String clientSecret, Map<String, String> parameters, TileDBLogin tileDBLogin){
apiClient = new io.tiledb.cloud.rest_api.ApiClient(basePath, clientId, clientSecret, parameters);
setClientCredentials(tileDBLogin);
setReadTimeout(0);
}
/**
* Set timeout timer
* @param timeout timeout timer in milliseconds
*/
public void setReadTimeout(int timeout){
this.apiClient = this.apiClient.setReadTimeout(timeout);
}
/**
* Sets the credentials for the client. Can be called during runtime.
* @param tileDBLogin A TileDBLogin Object
*/
public void setClientCredentials(TileDBLogin tileDBLogin) {
findCredentials(tileDBLogin);
apiClient.setApiKey(apiKey);
apiClient.setUsername(username);
apiClient.setPassword(password);
apiClient.setBasePath(basePath);
}
@Deprecated
// Replaced by: getV1Client()
public io.tiledb.cloud.rest_api.ApiClient getApiClient() {
return apiClient;
}
private String normalizeBaseHost(String inputBase) {
if (inputBase == null || inputBase.isEmpty()) {
return "https://api.tiledb.com";
}
// strip trailing /v{digits}
String trimmed = inputBase.trim();
return trimmed.replaceFirst("/v\\d+$", "");
}
private String versionedBasePath(ApiVersion version) {
String hostOnly = normalizeBaseHost(basePath);
String suffix;
switch (version) {
case V2:
suffix = "/v2";
break;
case V4:
suffix = "/v4";
break;
case V1:
default:
suffix = "/v1";
break;
}
return hostOnly + suffix;
}
public io.tiledb.cloud.rest_api.ApiClient getV1Client() {
if (v1Client == null) {
v1Client = new io.tiledb.cloud.rest_api.ApiClient();
v1Client.setApiKey(apiKey);
v1Client.setUsername(username);
v1Client.setPassword(password);
v1Client.setVerifyingSsl(verifyingSsl);
v1Client.setBasePath(versionedBasePath(ApiVersion.V1));
v1Client = v1Client.setReadTimeout(this.apiClient.getReadTimeout());
}
return v1Client;
}
public io.tiledb.cloud.rest_api.v2.ApiClient getV2Client() {
if (v2Client == null) {
v2Client = new io.tiledb.cloud.rest_api.v2.ApiClient();
v2Client.setApiKey(apiKey);
v2Client.setUsername(username);
v2Client.setPassword(password);
v2Client.setVerifyingSsl(verifyingSsl);
v2Client.setBasePath(versionedBasePath(ApiVersion.V2));
v2Client = v2Client.setReadTimeout(this.apiClient.getReadTimeout());
}
return v2Client;
}
public io.tiledb.cloud.rest_api.v4.ApiClient getV4Client() {
if (v4Client == null) {
v4Client = new io.tiledb.cloud.rest_api.v4.ApiClient();
v4Client.setApiKey(apiKey);
v4Client.setUsername(username);
v4Client.setPassword(password);
v4Client.setVerifyingSsl(verifyingSsl);
v4Client.setBasePath(versionedBasePath(ApiVersion.V4));
v4Client = v4Client.setReadTimeout(this.apiClient.getReadTimeout());
}
return v4Client;
}
public Object getApiClient(ApiVersion version) {
switch (version) {
case V1:
return getV1Client();
case V2:
return getV2Client();
case V4:
default:
return getV4Client();
}
}
}