use of com.facebook.presto.client.ClientException in project presto by prestodb.
the class PrestoStatement method internalExecute.
final boolean internalExecute(String sql) throws SQLException {
clearCurrentResults();
checkOpen();
StatementClient client = null;
PrestoResultSet resultSet = null;
boolean intercepted = false;
try {
WarningsManager warningsManager = new WarningsManager();
currentWarningsManager.set(Optional.of(warningsManager));
int statementDepth = this.statementDepth.incrementAndGet();
boolean shouldIntercept = !connection().getQueryInterceptorInstances().isEmpty() && statementDepth == 1;
if (shouldIntercept) {
Optional<PrestoResultSet> newResultSet = connection().invokeQueryInterceptorsPre(sql, this);
if (newResultSet.isPresent()) {
resultSet = newResultSet.get();
}
}
// Check if no resultSet is returned from an interceptor
if (resultSet != null) {
currentResult.set(resultSet);
intercepted = true;
} else {
client = connection().startQuery(sql, getStatementSessionProperties());
if (client.isFinished()) {
QueryStatusInfo finalStatusInfo = client.finalStatusInfo();
if (finalStatusInfo.getError() != null) {
throw resultsException(finalStatusInfo);
}
}
executingClient.set(client);
resultSet = new PrestoResultSet(this, client, maxRows.get(), progressConsumer, warningsManager);
for (Map.Entry<String, SelectedRole> entry : client.getSetRoles().entrySet()) {
connection.get().setRole(entry.getKey(), entry.getValue());
}
}
// check if this is a query
if (intercepted || client.currentStatusInfo().getUpdateType() == null) {
currentResult.set(resultSet);
if (shouldIntercept) {
resultSet = connection().invokeQueryInterceptorsPost(sql, this, resultSet);
verifyNotNull(resultSet, "invokeQueryInterceptorsPost should never return a null ResultSet");
currentResult.set(resultSet);
}
return true;
}
// this is an update, not a query
while (resultSet.next()) {
// ignore rows
}
connection().updateSession(client);
Long updateCount = client.finalStatusInfo().getUpdateCount();
currentUpdateCount.set((updateCount != null) ? updateCount : 0);
currentUpdateType.set(client.finalStatusInfo().getUpdateType());
warningsManager.addWarnings(client.finalStatusInfo().getWarnings());
return false;
} catch (ClientException e) {
throw new SQLException(e.getMessage(), e);
} catch (RuntimeException e) {
throw new SQLException("Error executing query", e);
} finally {
this.statementDepth.decrementAndGet();
executingClient.set(null);
if (currentResult.get() == null) {
if (resultSet != null) {
resultSet.close();
}
if (client != null) {
client.close();
}
}
}
}
use of com.facebook.presto.client.ClientException in project presto by prestodb.
the class QueryExecutor method getServerInfo.
public ServerInfo getServerInfo(URI server) {
HttpUrl url = HttpUrl.get(server);
if (url == null) {
throw new ClientException("Invalid server URL: " + server);
}
url = url.newBuilder().encodedPath("/v1/info").build();
Request request = new Request.Builder().url(url).build();
JsonResponse<ServerInfo> response = JsonResponse.execute(SERVER_INFO_CODEC, httpClient, request);
if (!response.hasValue()) {
throw new RuntimeException(format("Request to %s failed: %s [Error: %s]", server, response, response.getResponseBody()));
}
return response.getValue();
}
use of com.facebook.presto.client.ClientException in project presto by prestodb.
the class PrestoDriverUri method setupClient.
public void setupClient(OkHttpClient.Builder builder) throws SQLException {
try {
setupCookieJar(builder);
setupSocksProxy(builder, SOCKS_PROXY.getValue(properties));
setupHttpProxy(builder, HTTP_PROXY.getValue(properties));
// add user specified protocols to okhttp3 client if specified
getProtocols().ifPresent(builder::protocols);
// TODO: fix Tempto to allow empty passwords
String password = PASSWORD.getValue(properties).orElse("");
if (!password.isEmpty() && !password.equals("***empty***")) {
if (!useSecureConnection) {
throw new SQLException("Authentication using username/password requires SSL to be enabled");
}
builder.addInterceptor(basicAuth(getUser(), password));
}
if (useSecureConnection) {
setupSsl(builder, SSL_KEY_STORE_PATH.getValue(properties), SSL_KEY_STORE_PASSWORD.getValue(properties), SSL_TRUST_STORE_PATH.getValue(properties), SSL_TRUST_STORE_PASSWORD.getValue(properties));
}
if (KERBEROS_REMOTE_SERVICE_NAME.getValue(properties).isPresent()) {
if (!useSecureConnection) {
throw new SQLException("Authentication using Kerberos requires SSL to be enabled");
}
setupKerberos(builder, KERBEROS_REMOTE_SERVICE_NAME.getRequiredValue(properties), KERBEROS_USE_CANONICAL_HOSTNAME.getRequiredValue(properties), KERBEROS_PRINCIPAL.getValue(properties), KERBEROS_CONFIG_PATH.getValue(properties), KERBEROS_KEYTAB_PATH.getValue(properties), Optional.ofNullable(KERBEROS_CREDENTIAL_CACHE_PATH.getValue(properties).orElseGet(() -> defaultCredentialCachePath().map(File::new).orElse(null))));
}
Map<String, String> extraCredentials = EXTRA_CREDENTIALS.getValue(properties).orElse(ImmutableMap.of());
Optional.ofNullable(extraCredentials.get(GCS_CREDENTIALS_PATH_KEY)).ifPresent(credentialPath -> OkHttpUtil.setupGCSOauth(builder, credentialPath, Optional.ofNullable(extraCredentials.get(GCS_OAUTH_SCOPES_KEY))));
if (ACCESS_TOKEN.getValue(properties).isPresent()) {
if (!useSecureConnection) {
throw new SQLException("Authentication using an access token requires SSL to be enabled");
}
builder.addInterceptor(tokenAuth(ACCESS_TOKEN.getValue(properties).get()));
}
} catch (ClientException e) {
throw new SQLException(e.getMessage(), e);
} catch (RuntimeException e) {
throw new SQLException("Error setting up connection", e);
}
}
Aggregations