Skip to content

Commit

Permalink
FIR-32846 replaced throws FireboltException to throws SQLException (#406
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Alexander Radzin authored May 8, 2024
1 parent 4b1aab3 commit 73c4223
Show file tree
Hide file tree
Showing 26 changed files with 109 additions and 97 deletions.
18 changes: 9 additions & 9 deletions src/main/java/com/firebolt/jdbc/client/FireboltClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,12 +56,11 @@ protected FireboltClient(OkHttpClient httpClient, FireboltConnection connection,
}

protected <T> T getResource(String uri, String accessToken, Class<T> valueType)
throws IOException, FireboltException {
throws IOException, SQLException {
return getResource(uri, uri, accessToken, valueType);
}

protected <T> T getResource(String uri, String host, String accessToken, Class<T> valueType)
throws IOException, FireboltException {
protected <T> T getResource(String uri, String host, String accessToken, Class<T> valueType) throws SQLException, IOException {
Request rq = createGetRequest(uri, accessToken);
try (Response response = execute(rq, host)) {
return jsonToObject(getResponseAsString(response), valueType);
Expand All @@ -85,12 +85,12 @@ private Request createGetRequest(String uri, String accessToken) {
return requestBuilder.build();
}

protected Response execute(@NonNull Request request, String host) throws IOException, FireboltException {
protected Response execute(@NonNull Request request, String host) throws IOException, SQLException {
return execute(request, host, false);
}

protected Response execute(@NonNull Request request, String host, boolean isCompress)
throws IOException, FireboltException {
throws IOException, SQLException {
Response response = null;
try {
OkHttpClient client = getClientWithTimeouts(connection.getConnectionTimeout(), connection.getNetworkTimeout());
Expand Down Expand Up @@ -132,7 +132,7 @@ protected Request createPostRequest(String uri, String label, String json, Strin
return createPostRequest(uri, label, requestBody, accessToken);
}

protected void validateResponse(String host, Response response, Boolean isCompress) throws FireboltException {
protected void validateResponse(String host, Response response, Boolean isCompress) throws SQLException {
int statusCode = response.code();
if (!isCallSuccessful(statusCode)) {
if (statusCode == HTTP_UNAVAILABLE) {
Expand All @@ -153,18 +153,18 @@ protected void validateResponse(String host, Response response, Boolean isCompre
}
}

protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws FireboltException {
protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws SQLException {
// empty implementation
}

protected String getResponseAsString(Response response) throws FireboltException, IOException {
protected String getResponseAsString(Response response) throws SQLException, IOException {
if (response.body() == null) {
throw new FireboltException("Cannot get resource: the response from the server is empty");
}
return response.body().string();
}

private String extractErrorMessage(Response response, boolean isCompress) throws FireboltException {
private String extractErrorMessage(Response response, boolean isCompress) throws SQLException {
byte[] entityBytes;
try {
entityBytes = response.body() != null ? response.body().bytes() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import okhttp3.OkHttpClient;

import java.io.IOException;
import java.sql.SQLException;

import static java.lang.String.format;

Expand All @@ -37,7 +38,7 @@ public FireboltAccountClient(OkHttpClient httpClient, FireboltConnection firebol
* @param accessToken the access token
* @return the account
*/
public FireboltAccountResponse getAccount(String host, String account, String accessToken) throws FireboltException, IOException {
public FireboltAccountResponse getAccount(String host, String account, String accessToken) throws SQLException, IOException {
String uri = format(GET_ACCOUNT_ID_URI, host, account);
return getResource(uri, host, accessToken, FireboltAccountResponse.class);
}
Expand All @@ -52,7 +53,7 @@ public FireboltAccountResponse getAccount(String host, String account, String ac
* @param accessToken the access token
* @return the engine
*/
public FireboltEngineResponse getEngine(String host, String accountId, String engineName, String engineId, String accessToken) throws FireboltException, IOException {
public FireboltEngineResponse getEngine(String host, String accountId, String engineName, String engineId, String accessToken) throws SQLException, IOException {
String uri = createAccountUri(accountId, host, URI_SUFFIX_ACCOUNT_ENGINE_INFO_BY_ENGINE_ID + engineId);
return getResource(uri, host, accessToken, FireboltEngineResponse.class, format("The address of the engine with name %s and id %s could not be found", engineName, engineId));
}
Expand All @@ -66,7 +67,7 @@ public FireboltEngineResponse getEngine(String host, String accountId, String en
* @param accessToken the access token
* @return the default engine for the database
*/
public FireboltDefaultDatabaseEngineResponse getDefaultEngineByDatabaseName(String host, String accountId, String dbName, String accessToken) throws FireboltException, IOException {
public FireboltDefaultDatabaseEngineResponse getDefaultEngineByDatabaseName(String host, String accountId, String dbName, String accessToken) throws SQLException, IOException {
String uri = createAccountUri(accountId, host, URI_SUFFIX_DATABASE_INFO_URL + dbName);
return getResource(uri, host, accessToken, FireboltDefaultDatabaseEngineResponse.class, format("The database with the name %s could not be found", dbName));
}
Expand All @@ -81,13 +82,13 @@ public FireboltDefaultDatabaseEngineResponse getDefaultEngineByDatabaseName(Stri
* @return the engine id
*/
public FireboltEngineIdResponse getEngineId(String host, String accountId, String engineName, String accessToken)
throws FireboltException, IOException {
throws SQLException, IOException {
String uri = createAccountUri(accountId, host, URI_SUFFIX_ENGINE_AND_ACCOUNT_ID_BY_ENGINE_NAME + engineName);
return getResource(uri, host, accessToken, FireboltEngineIdResponse.class, format("The engine %s could not be found", engineName));
}


private <R> R getResource(String uri, String host, String accessToken, Class<R> responseType, String notFoundErrorMessage) throws FireboltException, IOException {
private <R> R getResource(String uri, String host, String accessToken, Class<R> responseType, String notFoundErrorMessage) throws SQLException, IOException {
try {
return getResource(uri, host, accessToken, responseType);
} catch (FireboltException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import okhttp3.OkHttpClient;

import java.io.IOException;
import java.sql.SQLException;

import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
Expand All @@ -23,7 +24,7 @@ public FireboltAccountRetriever(OkHttpClient httpClient, FireboltConnection conn
this.type = type;
}

public T retrieve(String accessToken, String accountName) throws FireboltException {
public T retrieve(String accessToken, String accountName) throws SQLException {
try {
return getResource(format(URL, host, accountName, path), accessToken, type);
} catch (IOException e) {
Expand All @@ -32,7 +33,7 @@ public T retrieve(String accessToken, String accountName) throws FireboltExcepti
}

@Override
protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws FireboltException {
protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws SQLException {
if (statusCode == HTTP_NOT_FOUND) {
String[] fragments = host.split("/");
// Second to last because th last element presents action and the second to last is the account name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import okhttp3.Response;

import java.io.IOException;
import java.sql.SQLException;

@CustomLog
public abstract class FireboltAuthenticationClient extends FireboltClient {
Expand All @@ -29,7 +30,7 @@ protected FireboltAuthenticationClient(OkHttpClient httpClient,
* @return the connection tokens
*/
public FireboltConnectionTokens postConnectionTokens(String host, String user, String password, String environment)
throws IOException, FireboltException {
throws SQLException, IOException {
AuthenticationRequest authenticationRequest = getAuthenticationRequest(user, password, host, environment);
String uri = authenticationRequest.getUri();
log.debug("Creating connection with url {}", uri);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.firebolt.jdbc.client.query;

import java.io.InputStream;
import java.sql.SQLException;

import com.firebolt.jdbc.connection.settings.FireboltProperties;
import com.firebolt.jdbc.exception.FireboltException;
Expand All @@ -12,12 +13,12 @@ public interface StatementClient {
* Post SQL statement
*/
InputStream executeSqlStatement(StatementInfoWrapper statementInfoWrapper, FireboltProperties connectionProperties,
boolean systemEngine, int queryTimeout, boolean standardSql) throws FireboltException;
boolean systemEngine, int queryTimeout, boolean standardSql) throws SQLException;

/**
* Call endpoint to abort a running SQL statement
*/
void abortStatement(String label, FireboltProperties fireboltProperties) throws FireboltException;
void abortStatement(String label, FireboltProperties fireboltProperties) throws SQLException;

boolean isStatementRunning(String statementLabel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public StatementClientImpl(OkHttpClient httpClient, FireboltConnection connectio
@Override
public InputStream executeSqlStatement(@NonNull StatementInfoWrapper statementInfoWrapper,
@NonNull FireboltProperties connectionProperties, boolean systemEngine, int queryTimeout,
boolean standardSql) throws FireboltException {
boolean standardSql) throws SQLException {
QueryIdFetcher.getQueryFetcher(connection.getInfraVersion()).formatStatement(statementInfoWrapper);
String formattedStatement = QueryIdFetcher.getQueryFetcher(connection.getInfraVersion()).formatStatement(statementInfoWrapper);
Map<String, String> params = getAllParameters(connectionProperties, statementInfoWrapper, systemEngine, queryTimeout);
Expand All @@ -156,7 +156,7 @@ public InputStream executeSqlStatement(@NonNull StatementInfoWrapper statementIn
}

private InputStream executeSqlStatementWithRetryOnUnauthorized(String label, @NonNull FireboltProperties connectionProperties, String formattedStatement, String uri)
throws IOException, FireboltException {
throws SQLException, IOException {
try {
log.debug("Posting statement with label {} to URI: {}", label, uri);
return postSqlStatement(connectionProperties, formattedStatement, uri, label);
Expand All @@ -171,7 +171,7 @@ private InputStream executeSqlStatementWithRetryOnUnauthorized(String label, @No
}

private InputStream postSqlStatement(@NonNull FireboltProperties connectionProperties, String formattedStatement, String uri, String label)
throws FireboltException, IOException {
throws SQLException, IOException {
Request post = createPostRequest(uri, label, formattedStatement, getConnection().getAccessToken().orElse(null));
Response response = execute(post, connectionProperties.getHost(), connectionProperties.isCompress());
InputStream is = ofNullable(response.body()).map(ResponseBody::byteStream).orElse(null);
Expand All @@ -181,7 +181,7 @@ private InputStream postSqlStatement(@NonNull FireboltProperties connectionPrope
return is;
}

public void abortStatement(@NonNull String statementLabel, @NonNull FireboltProperties properties) throws FireboltException {
public void abortStatement(@NonNull String statementLabel, @NonNull FireboltProperties properties) throws SQLException {
boolean aborted = abortRunningHttpRequest(statementLabel);
if (properties.isSystemEngine()) {
throw new FireboltException("Cannot cancel a statement using a system engine", INVALID_REQUEST);
Expand All @@ -196,7 +196,7 @@ public void abortStatement(@NonNull String statementLabel, @NonNull FireboltProp
* @param label label of the statement
* @param fireboltProperties the properties
*/
private void abortRunningDbStatement(String label, FireboltProperties fireboltProperties, int getIdTimeout) throws FireboltException {
private void abortRunningDbStatement(String label, FireboltProperties fireboltProperties, int getIdTimeout) throws SQLException {
try {
String id;
int attempt = 0;
Expand Down Expand Up @@ -343,7 +343,7 @@ private Map<String, String> getCancelParameters(String statementId) {
}

@Override
protected void validateResponse(String host, Response response, Boolean isCompress) throws FireboltException {
protected void validateResponse(String host, Response response, Boolean isCompress) throws SQLException {
super.validateResponse(host, response, isCompress);
FireboltConnection connection = getConnection();
if (isCallSuccessful(response.code())) {
Expand All @@ -362,7 +362,7 @@ protected void validateResponse(String host, Response response, Boolean isCompre
}

@Override
protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws FireboltException {
protected void validateResponse(String host, int statusCode, String errorMessageFromServer) throws SQLException {
if (statusCode == HTTP_INTERNAL_ERROR) {
FireboltException ex = missConfigurationErrorMessages.entrySet().stream()
.filter(msg -> msg.getKey().matcher(errorMessageFromServer).find()).findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static int getUrlVersion(String url, Properties connectionSettings) {
return 2;
}

protected OkHttpClient getHttpClient(FireboltProperties fireboltProperties) throws FireboltException {
protected OkHttpClient getHttpClient(FireboltProperties fireboltProperties) throws SQLException {
try {
return HttpClientConfig.getInstance() == null ? HttpClientConfig.init(fireboltProperties) : HttpClientConfig.getInstance();
} catch (GeneralSecurityException | IOException e) {
Expand All @@ -168,15 +168,15 @@ protected void connect() throws SQLException {

protected abstract void assertDatabaseExisting(String database) throws SQLException;

public void removeExpiredTokens() throws FireboltException {
public void removeExpiredTokens() throws SQLException {
fireboltAuthenticationService.removeConnectionTokens(httpConnectionUrl, loginProperties);
}

public Optional<String> getAccessToken() throws FireboltException {
public Optional<String> getAccessToken() throws SQLException {
return getAccessToken(sessionProperties);
}

protected Optional<String> getAccessToken(FireboltProperties fireboltProperties) throws FireboltException {
protected Optional<String> getAccessToken(FireboltProperties fireboltProperties) throws SQLException {
String accessToken = fireboltProperties.getAccessToken();
if (accessToken != null) {
if (fireboltProperties.getPrincipal() != null || fireboltProperties.getSecret() != null) {
Expand Down Expand Up @@ -445,19 +445,19 @@ public void removeClosedStatement(FireboltStatement fireboltStatement) {
}
}

public void addProperty(@NonNull String key, String value) throws FireboltException {
public void addProperty(@NonNull String key, String value) throws SQLException {
changeProperty(p -> p.addProperty(key, value), () -> format("Could not set property %s=%s", key, value));
}

public void addProperty(Entry<String, String> property) throws FireboltException {
public void addProperty(Entry<String, String> property) throws SQLException {
changeProperty(p -> p.addProperty(property), () -> format("Could not set property %s=%s", property.getKey(), property.getValue()));
}

public void reset() throws FireboltException {
public void reset() throws SQLException {
changeProperty(FireboltProperties::clearAdditionalProperties, () -> "Could not reset connection");
}

private synchronized void changeProperty(Consumer<FireboltProperties> propertiesEditor, Supplier<String> errorMessageFactory) throws FireboltException {
private synchronized void changeProperty(Consumer<FireboltProperties> propertiesEditor, Supplier<String> errorMessageFactory) throws SQLException {
try {
FireboltProperties tmpProperties = FireboltProperties.copy(sessionProperties);
propertiesEditor.accept(tmpProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void assertDatabaseExisting(String database) throws SQLException {
}
}

private FireboltProperties getSessionPropertiesForSystemEngine(String accessToken, String accountName) throws FireboltException {
private FireboltProperties getSessionPropertiesForSystemEngine(String accessToken, String accountName) throws SQLException {
String systemEngineEndpoint = fireboltGatewayUrlService.getUrl(accessToken, accountName);
FireboltAccount account = fireboltAccountIdService.getValue(accessToken, accountName);
infraVersion = account.getInfraVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public class FieldTypeConverter {
});
}

private static <T> void verify(Class<T> toType, BaseType columnBaseType, BaseType... supportedTypes)
throws FireboltException {
private static <T> void verify(Class<T> toType, BaseType columnBaseType, BaseType... supportedTypes) throws SQLException {
if (Arrays.stream(supportedTypes).noneMatch(b -> b.equals(columnBaseType))) {
throw new FireboltException(
String.format(CONVERSION_NOT_SUPPORTED_EXCEPTION, toType, columnBaseType.getType().getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import com.firebolt.jdbc.client.account.FireboltAccountRetriever;
import com.firebolt.jdbc.exception.FireboltException;

import java.sql.SQLException;

public class FireboltAccountIdService {
private final FireboltAccountRetriever<FireboltAccount> firebolAccountClient;

public FireboltAccountIdService(FireboltAccountRetriever<FireboltAccount> firebolAccountClient) {
this.firebolAccountClient = firebolAccountClient;
}

public FireboltAccount getValue(String accessToken, String account) throws FireboltException {
public FireboltAccount getValue(String accessToken, String account) throws SQLException {
return firebolAccountClient.retrieve(accessToken, account);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

import static java.lang.String.format;
import static java.util.Optional.ofNullable;
Expand All @@ -30,7 +31,7 @@ public class FireboltAuthenticationService {
private static final String ERROR_MESSAGE_FROM_SERVER = "Failed to connect to Firebolt with the error from the server: %s, see logs for more info.";
private final FireboltAuthenticationClient fireboltAuthenticationClient;

public FireboltConnectionTokens getConnectionTokens(String host, FireboltProperties loginProperties) throws FireboltException {
public FireboltConnectionTokens getConnectionTokens(String host, FireboltProperties loginProperties) throws SQLException {
try {
ConnectParams connectionParams = new ConnectParams(host, loginProperties.getPrincipal(), loginProperties.getSecret());
synchronized (this) {
Expand Down Expand Up @@ -70,7 +71,7 @@ private long getCachingDurationInSeconds(long expireInSeconds) {
* @param host host
* @param loginProperties the login properties linked to the tokens
*/
public void removeConnectionTokens(String host, FireboltProperties loginProperties) throws FireboltException {
public void removeConnectionTokens(String host, FireboltProperties loginProperties) throws SQLException {
try {
log.debug("Removing connection token for host {}", host);
ConnectParams connectionParams = new ConnectParams(host, loginProperties.getPrincipal(), loginProperties.getSecret());
Expand Down
Loading

0 comments on commit 73c4223

Please sign in to comment.