From 7d4b2b468bc3ecc4d898c23f0a2a0176b24c58b9 Mon Sep 17 00:00:00 2001 From: dimitrisstaratzis Date: Fri, 29 Sep 2023 14:24:48 +0300 Subject: [PATCH] adding host as a constructor parameter in TileDBLogin --- .../java/io/tiledb/cloud/TileDBClient.java | 26 +++++++++-- .../java/io/tiledb/cloud/TileDBLogin.java | 45 ++++++++++++++++++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/tiledb/cloud/TileDBClient.java b/src/main/java/io/tiledb/cloud/TileDBClient.java index decab44..d4fd21b 100644 --- a/src/main/java/io/tiledb/cloud/TileDBClient.java +++ b/src/main/java/io/tiledb/cloud/TileDBClient.java @@ -22,7 +22,7 @@ public class TileDBClient{ private static String password; private static String basePath; - private static boolean verifyingSsl = true; + private static boolean verifyingSsl; private static boolean loginInfoIsInJSONFile; @@ -42,8 +42,9 @@ public class TileDBClient{ apiKey = ""; username = ""; password = ""; - basePath = "https://api.tiledb.com/v1"; + basePath = "https://api.tiledb.com/v1"; //default is TileDB loginInfoIsInJSONFile = true; + verifyingSsl = true; // set path according to OS if (System.getProperty("os.name").toLowerCase().contains("windows")){ @@ -99,6 +100,16 @@ private static boolean loadCloudJSONFileFromHome() throws IOException { logger.log(Level.INFO, "Found verifySSL from disk"); } + if (object.has("host")){ + String host = object.getString("host"); + if (host.equals("https://api.tiledb.com")){ + basePath = host + "/v1"; + } else { + 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, "")))))){ @@ -132,6 +143,11 @@ private void findCredentials(TileDBLogin tileDBLogin) { * @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(); @@ -155,7 +171,11 @@ private void writeAuthJSONFileToHome() { jsonObject.put("api_key", apiKeyObject); jsonObject.put("username", username); jsonObject.put("password", password); - jsonObject.put("host", "https://api.tiledb.com"); + 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); diff --git a/src/main/java/io/tiledb/cloud/TileDBLogin.java b/src/main/java/io/tiledb/cloud/TileDBLogin.java index 8ebb832..478a89c 100644 --- a/src/main/java/io/tiledb/cloud/TileDBLogin.java +++ b/src/main/java/io/tiledb/cloud/TileDBLogin.java @@ -1,5 +1,7 @@ package io.tiledb.cloud; +import io.tiledb.cloud.rest_api.ApiException; + public class TileDBLogin { /** The client password */ private String password; @@ -22,13 +24,54 @@ public class TileDBLogin { /** True if the user wants their current login input to overwrite the last one */ private boolean overwritePrevious; - public TileDBLogin(String username, String password, String apiKey, boolean verifySSL, boolean rememberMe, boolean overwritePrevious) { + /** + * Constructor + * + * @param username The username + * @param password The password + * @param apiKey The User's api token. Can be found in the TileDB console at https://www.console.tiledb.com + * @param verifySSL True to verify SSL + * @param rememberMe True if the user wants their login credentials to be remembered + * @param overwritePrevious True if the user wants their current login input to overwrite the last one + */ + public TileDBLogin(String username, String password, String apiKey, boolean verifySSL, + boolean rememberMe, boolean overwritePrevious) { this.password = password; this.username = username; this.apiKey = apiKey; this.verifySSL = verifySSL; this.rememberMe = rememberMe; this.overwritePrevious = overwritePrevious; + this.host = ""; + } + + /** + * Constructor + * + * @param username The username + * @param password The password + * @param apiKey The User's api token. Can be found in the TileDB console at https://www.console.tiledb.com + * @param verifySSL True to verify SSL + * @param rememberMe True if the user wants their login credentials to be remembered + * @param overwritePrevious True if the user wants their current login input to overwrite the last one + * @param host The host name + */ + public TileDBLogin(String username, String password, String apiKey, boolean verifySSL, + boolean rememberMe, boolean overwritePrevious, String host) throws ApiException { + if (host.equals("https://api.tiledb.com/v2")){ + throw new ApiException("https://api.tiledb.com/v2 is not yet supported"); + } + this.password = password; + this.username = username; + this.apiKey = apiKey; + this.verifySSL = verifySSL; + this.rememberMe = rememberMe; + this.host = host; + this.overwritePrevious = overwritePrevious; + } + + public String getHost() { + return host; } public TileDBLogin() {