use of org.apache.drill.exec.store.http.HttpApiConfig in project drill by apache.
the class SimpleHttp method setupHttpClient.
/**
* Configures the OkHTTP3 server object with configuration info from the user.
*
* @return OkHttpClient configured server
*/
private OkHttpClient setupHttpClient() {
Builder builder = new OkHttpClient.Builder();
// Set up the HTTP Cache. Future possibilities include making the cache size and retention configurable but
// right now it is on or off. The writer will write to the Drill temp directory if it is accessible and
// output a warning if not.
HttpStoragePluginConfig config = scanDefn.tableSpec().config();
if (config.cacheResults()) {
setupCache(builder);
}
HttpApiConfig apiConfig = scanDefn.tableSpec().connectionConfig();
// If OAuth information is provided, we will assume that the user does not want to use
// basic authentication
HttpOAuthConfig oAuthConfig = scanDefn.tableSpec().config().oAuthConfig();
if (oAuthConfig != null) {
// Add interceptors for OAuth2
logger.debug("Adding OAuth2 Interceptor");
AccessTokenRepository repository = new AccessTokenRepository(proxyConfig, config, tokenTable);
builder.authenticator(new AccessTokenAuthenticator(repository));
builder.addInterceptor(new AccessTokenInterceptor(repository));
} else if (apiConfig.authType().equalsIgnoreCase("basic")) {
// If the API uses basic authentication add the authentication code.
logger.debug("Adding Interceptor");
UsernamePasswordCredentials credentials = apiConfig.getUsernamePasswordCredentials();
builder.addInterceptor(new BasicAuthInterceptor(credentials.getUsername(), credentials.getPassword()));
}
// Set timeouts
int timeout = Math.max(1, config.timeout());
builder.connectTimeout(timeout, TimeUnit.SECONDS);
builder.writeTimeout(timeout, TimeUnit.SECONDS);
builder.readTimeout(timeout, TimeUnit.SECONDS);
// Sourced from https://stackoverflow.com/questions/60110848/how-to-disable-ssl-verification
if (!apiConfig.verifySSLCert()) {
try {
TrustManager[] trustAllCerts = getAllTrustingTrustManager();
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
HostnameVerifier verifier = (hostname, session) -> true;
builder.hostnameVerifier(verifier);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error("Error when configuring Drill not to verify SSL certs. {}", e.getMessage());
}
}
// Set the proxy configuration
addProxyInfo(builder, proxyConfig);
return builder.build();
}
use of org.apache.drill.exec.store.http.HttpApiConfig in project drill by apache.
the class SimpleHttp method getInputStream.
/**
* Returns an InputStream based on the URL and config in the scanSpec. If anything goes wrong
* the method throws a UserException.
* @return An Inputstream of the data from the URL call.
*/
public InputStream getInputStream() {
Request.Builder requestBuilder = new Request.Builder().url(url);
// The configuration does not allow for any other request types other than POST and GET.
HttpApiConfig apiConfig = scanDefn.tableSpec().connectionConfig();
if (apiConfig.getMethodType() == HttpMethod.POST) {
// Handle POST requests
FormBody.Builder formBodyBuilder = buildPostBody(apiConfig.postBody());
requestBuilder.post(formBodyBuilder.build());
}
// Log the URL and method to aid in debugging user issues.
logger.info("Connection: {}, Method {}, URL: {}", scanDefn.tableSpec().connection(), apiConfig.getMethodType().name(), url());
// Add headers to request
if (apiConfig.headers() != null) {
for (Map.Entry<String, String> entry : apiConfig.headers().entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
// Build the request object
Request request = requestBuilder.build();
try {
logger.debug("Executing request: {}", request);
logger.debug("Headers: {}", request.headers());
// Execute the request
Response response = client.newCall(request).execute();
// Preserve the response
responseMessage = response.message();
responseCode = response.code();
responseProtocol = response.protocol().toString();
responseURL = response.request().url().toString();
// Case for pagination without limit
if (paginator != null && (response.code() != 200 || response.body() == null || response.body().contentLength() == 0)) {
paginator.notifyPartialPage();
}
// If the request is unsuccessful, throw a UserException
if (!isSuccessful(responseCode)) {
throw UserException.dataReadError().message("HTTP request failed").addContext("Response code", response.code()).addContext("Response message", response.message()).addContext(errorContext).build(logger);
}
logger.debug("HTTP Request for {} successful.", url());
logger.debug("Response Headers: {} ", response.headers());
// Return the InputStream of the response
return Objects.requireNonNull(response.body()).byteStream();
} catch (IOException e) {
throw UserException.dataReadError(e).message("Failed to read the HTTP response body").addContext("Error message", e.getMessage()).addContext(errorContext).build(logger);
}
}
Aggregations