Search in sources :

Example 31 with Builder

use of okhttp3.MultipartBody.Builder 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();
}
Also used : HttpSubScan(org.apache.drill.exec.store.http.HttpSubScan) X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) Cache(okhttp3.Cache) URLDecoder(java.net.URLDecoder) UserException(org.apache.drill.common.exceptions.UserException) LoggerFactory(org.slf4j.LoggerFactory) TrustManager(javax.net.ssl.TrustManager) HttpOAuthConfig(org.apache.drill.exec.store.http.HttpOAuthConfig) StringUtils(org.apache.commons.lang3.StringUtils) FormBody(okhttp3.FormBody) PersistentTokenTable(org.apache.drill.exec.oauth.PersistentTokenTable) Matcher(java.util.regex.Matcher) Proxy(java.net.Proxy) Map(java.util.Map) HostnameVerifier(javax.net.ssl.HostnameVerifier) Interceptor(okhttp3.Interceptor) Request(okhttp3.Request) HttpMethod(org.apache.drill.exec.store.http.HttpApiConfig.HttpMethod) HttpApiConfig(org.apache.drill.exec.store.http.HttpApiConfig) KeyManagementException(java.security.KeyManagementException) Credentials(okhttp3.Credentials) InetSocketAddress(java.net.InetSocketAddress) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) AccessTokenAuthenticator(org.apache.drill.exec.store.http.oauth.AccessTokenAuthenticator) Objects(java.util.Objects) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AccessTokenInterceptor(org.apache.drill.exec.store.http.oauth.AccessTokenInterceptor) Pattern(java.util.regex.Pattern) HttpUrl(okhttp3.HttpUrl) NotNull(org.jetbrains.annotations.NotNull) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Builder(okhttp3.OkHttpClient.Builder) StoragePluginRegistry(org.apache.drill.exec.store.StoragePluginRegistry) CustomErrorContext(org.apache.drill.common.exceptions.CustomErrorContext) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(org.apache.drill.exec.store.security.UsernamePasswordCredentials) AccessTokenRepository(org.apache.drill.exec.store.http.oauth.AccessTokenRepository) Response(okhttp3.Response) Logger(org.slf4j.Logger) IOException(java.io.IOException) CaseInsensitiveMap(org.apache.drill.common.map.CaseInsensitiveMap) Paginator(org.apache.drill.exec.store.http.paginator.Paginator) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) HttpStoragePluginConfig(org.apache.drill.exec.store.http.HttpStoragePluginConfig) InputStream(java.io.InputStream) AccessTokenInterceptor(org.apache.drill.exec.store.http.oauth.AccessTokenInterceptor) HttpApiConfig(org.apache.drill.exec.store.http.HttpApiConfig) Builder(okhttp3.OkHttpClient.Builder) AccessTokenRepository(org.apache.drill.exec.store.http.oauth.AccessTokenRepository) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) UsernamePasswordCredentials(org.apache.drill.exec.store.security.UsernamePasswordCredentials) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpOAuthConfig(org.apache.drill.exec.store.http.HttpOAuthConfig) AccessTokenAuthenticator(org.apache.drill.exec.store.http.oauth.AccessTokenAuthenticator) HttpStoragePluginConfig(org.apache.drill.exec.store.http.HttpStoragePluginConfig) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 32 with Builder

use of okhttp3.MultipartBody.Builder in project drill by apache.

the class SimpleHttp method setupCache.

/**
 * Configures response caching using a provided temp directory.
 *
 * @param builder Builder the Builder object to which the caching is to be
 *                configured
 */
private void setupCache(Builder builder) {
    // TODO Add cache size in MB to config
    int cacheSize = 10 * 1024 * 1024;
    File cacheDirectory = new File(tempDir, "http-cache");
    if (!cacheDirectory.exists()) {
        if (!cacheDirectory.mkdirs()) {
            throw UserException.dataWriteError().message("Could not create the HTTP cache directory").addContext("Path", cacheDirectory.getAbsolutePath()).addContext("Please check the temp directory or disable HTTP caching.").addContext(errorContext).build(logger);
        }
    }
    try {
        Cache cache = new Cache(cacheDirectory, cacheSize);
        logger.debug("Caching HTTP Query Results at: {}", cacheDirectory);
        builder.cache(cache);
    } catch (Exception e) {
        throw UserException.dataWriteError(e).message("Could not create the HTTP cache").addContext("Path", cacheDirectory.getAbsolutePath()).addContext("Please check the temp directory or disable HTTP caching.").addContext(errorContext).build(logger);
    }
}
Also used : File(java.io.File) UserException(org.apache.drill.common.exceptions.UserException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Cache(okhttp3.Cache)

Example 33 with Builder

use of okhttp3.MultipartBody.Builder in project drill by apache.

the class SimpleHttp method buildPostBody.

/**
 * Accepts text from a post body in the format:<br>
 * {@code key1=value1}<br>
 * {@code key2=value2}
 * <p>
 * and creates the appropriate headers.
 *
 * @return FormBody.Builder The populated formbody builder
 */
private FormBody.Builder buildPostBody(String postBody) {
    FormBody.Builder formBodyBuilder = new FormBody.Builder();
    if (StringUtils.isEmpty(postBody)) {
        return formBodyBuilder;
    }
    final Pattern postBodyPattern = Pattern.compile("^.+=.+$");
    String[] lines = postBody.split("\\r?\\n");
    for (String line : lines) {
        // Otherwise ignore
        if (postBodyPattern.matcher(line).find()) {
            // Split into key/value
            String[] parts = line.split("=");
            formBodyBuilder.add(parts[0], parts[1]);
        }
    }
    return formBodyBuilder;
}
Also used : Pattern(java.util.regex.Pattern) Builder(okhttp3.OkHttpClient.Builder) FormBody(okhttp3.FormBody)

Example 34 with Builder

use of okhttp3.MultipartBody.Builder in project drill by apache.

the class HttpBatchReader method open.

@Override
public boolean open(SchemaNegotiator negotiator) {
    // Result set loader setup
    String tempDirPath = negotiator.drillConfig().getString(ExecConstants.DRILL_TMP_DIR);
    HttpUrl url = buildUrl();
    logger.debug("Final URL: {}", url);
    CustomErrorContext errorContext = new ChildErrorContext(negotiator.parentErrorContext()) {

        @Override
        public void addContext(UserException.Builder builder) {
            super.addContext(builder);
            builder.addContext("URL", url.toString());
        }
    };
    negotiator.setErrorContext(errorContext);
    // Http client setup
    SimpleHttp http = SimpleHttp.builder().scanDefn(subScan).url(url).tempDir(new File(tempDirPath)).proxyConfig(proxySettings(negotiator.drillConfig(), url)).errorContext(errorContext).build();
    // JSON loader setup
    resultSetLoader = negotiator.build();
    if (implicitColumnsAreProjected()) {
        implicitColumns = new ImplicitColumns(resultSetLoader.writer());
        buildImplicitColumns();
    }
    InputStream inStream = http.getInputStream();
    populateImplicitFieldMap(http);
    try {
        JsonLoaderBuilder jsonBuilder = new JsonLoaderBuilder().implicitFields(implicitColumns).resultSetLoader(resultSetLoader).standardOptions(negotiator.queryOptions()).maxRows(maxRecords).dataPath(subScan.tableSpec().connectionConfig().dataPath()).errorContext(errorContext).fromStream(inStream);
        if (subScan.tableSpec().connectionConfig().jsonOptions() != null) {
            JsonLoaderOptions jsonOptions = subScan.tableSpec().connectionConfig().jsonOptions().getJsonOptions(negotiator.queryOptions());
            jsonBuilder.options(jsonOptions);
        } else {
            jsonBuilder.standardOptions(negotiator.queryOptions());
        }
        jsonLoader = jsonBuilder.build();
    } catch (Throwable t) {
        // Paranoia: ensure stream is closed if anything goes wrong.
        // After this, the JSON loader will close the stream.
        AutoCloseables.closeSilently(inStream);
        throw t;
    }
    return true;
}
Also used : SimpleHttp(org.apache.drill.exec.store.http.util.SimpleHttp) InputStream(java.io.InputStream) ChildErrorContext(org.apache.drill.common.exceptions.ChildErrorContext) Builder(okhttp3.HttpUrl.Builder) JsonLoaderBuilder(org.apache.drill.exec.store.easy.json.loader.JsonLoaderImpl.JsonLoaderBuilder) ProxyBuilder(org.apache.drill.exec.store.http.util.HttpProxyConfig.ProxyBuilder) JsonLoaderBuilder(org.apache.drill.exec.store.easy.json.loader.JsonLoaderImpl.JsonLoaderBuilder) JsonLoaderOptions(org.apache.drill.exec.store.easy.json.loader.JsonLoaderOptions) File(java.io.File) HttpUrl(okhttp3.HttpUrl) ImplicitColumns(org.apache.drill.exec.store.ImplicitColumnUtils.ImplicitColumns) CustomErrorContext(org.apache.drill.common.exceptions.CustomErrorContext)

Example 35 with Builder

use of okhttp3.MultipartBody.Builder in project HL4A by HL4A.

the class DNSAmendNetwork method getIPByHostSync.

public static String getIPByHostSync(String host) throws Exception {
    HttpUrl httpUrl = new HttpUrl.Builder().scheme("http").host("119.29.29.29").addPathSegment("d").addQueryParameter("dn", host).build();
    OkHttpClient.Builder builder = AVHttpClient.clientInstance().getOkHttpClientBuilder();
    builder.connectTimeout(DNS_REQUEST_TIME_OUT, TimeUnit.MILLISECONDS);
    builder.dns(Dns.SYSTEM);
    OkHttpClient okHttpClient = builder.build();
    Request request = new Request.Builder().url(httpUrl).get().build();
    try {
        Response response = okHttpClient.newCall(request).execute();
        if (null != response && response.isSuccessful()) {
            return response.body().string();
        } else {
            return "";
        }
    } catch (IOException e) {
        if (AVOSCloud.isDebugLogEnabled()) {
            LogUtil.avlog.e("getIPByHostSync error", e);
        }
        return "";
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Aggregations

Request (okhttp3.Request)412 OkHttpClient (okhttp3.OkHttpClient)360 Response (okhttp3.Response)287 IOException (java.io.IOException)221 RequestBody (okhttp3.RequestBody)173 HttpUrl (okhttp3.HttpUrl)109 MultipartBody (okhttp3.MultipartBody)102 File (java.io.File)93 Map (java.util.Map)92 Test (org.junit.Test)92 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)87 Call (okhttp3.Call)77 HashMap (java.util.HashMap)64 AndroidFlipperClient (com.facebook.flipper.android.AndroidFlipperClient)59 FlipperClient (com.facebook.flipper.core.FlipperClient)59 DatabasesFlipperPlugin (com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin)59 FrescoFlipperPlugin (com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin)59 InspectorFlipperPlugin (com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin)59 NetworkFlipperPlugin (com.facebook.flipper.plugins.network.NetworkFlipperPlugin)59 ReactFlipperPlugin (com.facebook.flipper.plugins.react.ReactFlipperPlugin)59