<commit_before>package com.snowplowanalytics.snowplow.network;

import android.net.Uri;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.snowplowanalytics.snowplow.internal.emitter.Executor;
import com.snowplowanalytics.snowplow.internal.emitter.TLSArguments;
import com.snowplowanalytics.snowplow.internal.emitter.TLSVersion;
import com.snowplowanalytics.snowplow.tracker.BuildConfig;
import com.snowplowanalytics.snowplow.internal.constants.TrackerConstants;
import com.snowplowanalytics.snowplow.internal.tracker.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.snowplowanalytics.snowplow.network.HttpMethod.GET;
import static com.snowplowanalytics.snowplow.network.HttpMethod.POST;
import static com.snowplowanalytics.snowplow.network.Protocol.HTTP;

/**
 * Components in charge to send events to the collector.
 * It uses OkHttp as Http client.
 */
public class OkHttpNetworkConnection implements NetworkConnection {
    private final String TAG = OkHttpNetworkConnection.class.getSimpleName();

    private static final String DEFAULT_USER_AGENT = String.format("snowplow/%s android/%s", BuildConfig.TRACKER_LABEL, Build.VERSION.RELEASE);
    private final MediaType JSON = MediaType.parse(TrackerConstants.POST_CONTENT_TYPE);

    private final String uri;
    private final Protocol protocol;
    private final HttpMethod httpMethod;
    private final int emitTimeout;
    private final String customPostPath;

    private OkHttpClient client;
    private Uri.Builder uriBuilder;

    /**
     * Builder for the OkHttpNetworkConnection.
     */
    public static class OkHttpNetworkConnectionBuilder {
        final String uri; // Required
        HttpMethod httpMethod = POST; // Optional
        Protocol protocol = Protocol.HTTP; // Optional
        EnumSet<TLSVersion> tlsVersions = EnumSet.of(TLSVersion.TLSv1_2); // Optional
        private int emitTimeout = 5; // Optional
        OkHttpClient client = null; //Optional
        String customPostPath = null; //Optional

        /**
         * @param uri The uri of the collector
         */
        public OkHttpNetworkConnectionBuilder(@NonNull String uri) {
            this.uri = uri;
        }

        /**
         * @param httpMethod The method by which requests are emitted
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder method(@NonNull HttpMethod httpMethod) {
            this.httpMethod = httpMethod;
            return this;
        }

        /**
         * @param protocol the security chosen for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder security(@NonNull Protocol protocol) {
            this.protocol = protocol;
            return this;
        }

        /**
         * @param version the TLS version allowed for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder tls(@NonNull TLSVersion version) {
            this.tlsVersions = EnumSet.of(version);
            return this;
        }

        /**
         * @param versions the TLS versions allowed for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder tls(@NonNull EnumSet<TLSVersion> versions) {
            this.tlsVersions = versions;
            return this;
        }

        /**
         * @param emitTimeout The maximum timeout for emitting events. If emit time exceeds this value
         *                    TimeOutException will be thrown
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder emitTimeout(int emitTimeout){
            this.emitTimeout = emitTimeout;
            return this;
        }

        /**
         * @param client An OkHttp client that will be used in the emitter, you can provide your
         *               own if you want to share your Singleton client's interceptors, connection pool etc..
         *               ,otherwise a new one is created.
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder client(@Nullable OkHttpClient client) {
            this.client = client;
            return this;
        }

        /**
         * @param customPostPath A custom path that is used on the endpoint to send requests.
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder customPostPath(@Nullable String customPostPath) {
            this.customPostPath = customPostPath;
            return this;
        }

        /**
         * Creates a new OkHttpNetworkConnection
         *
         * @return a new OkHttpNetworkConnection object
         */
        @NonNull
        public OkHttpNetworkConnection build() {
            return new OkHttpNetworkConnection(this);
        }
    }

    private OkHttpNetworkConnection(OkHttpNetworkConnectionBuilder builder) {
        this.uri = builder.uri;
        this.protocol = builder.protocol;
        this.httpMethod = builder.httpMethod;
        this.emitTimeout = builder.emitTimeout;
        this.customPostPath = builder.customPostPath;

        TLSArguments tlsArguments = new TLSArguments(builder.tlsVersions);
        buildUri();

        final OkHttpClient.Builder clientBuilder;
        if (builder.client == null) {
            clientBuilder = new OkHttpClient.Builder();
        } else {
            clientBuilder = builder.client.newBuilder();
        }

        this.client = clientBuilder.sslSocketFactory(tlsArguments.getSslSocketFactory(),
                tlsArguments.getTrustManager())
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .build();
    }

    @Override
    @NonNull
    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    @NonNull
    @Override
    public Uri getUri() {
        return uriBuilder.clearQuery().build();
    }

    @NonNull<commit_msg>Fix OkHttpClient sslSocketFactory always overriden (close #417)<commit_after>package com.snowplowanalytics.snowplow.network;

import android.net.Uri;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.snowplowanalytics.snowplow.internal.emitter.Executor;
import com.snowplowanalytics.snowplow.internal.emitter.TLSArguments;
import com.snowplowanalytics.snowplow.internal.emitter.TLSVersion;
import com.snowplowanalytics.snowplow.tracker.BuildConfig;
import com.snowplowanalytics.snowplow.internal.constants.TrackerConstants;
import com.snowplowanalytics.snowplow.internal.tracker.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.snowplowanalytics.snowplow.network.HttpMethod.GET;
import static com.snowplowanalytics.snowplow.network.HttpMethod.POST;
import static com.snowplowanalytics.snowplow.network.Protocol.HTTP;

/**
 * Components in charge to send events to the collector.
 * It uses OkHttp as Http client.
 */
public class OkHttpNetworkConnection implements NetworkConnection {
    private final String TAG = OkHttpNetworkConnection.class.getSimpleName();

    private static final String DEFAULT_USER_AGENT = String.format("snowplow/%s android/%s", BuildConfig.TRACKER_LABEL, Build.VERSION.RELEASE);
    private final MediaType JSON = MediaType.parse(TrackerConstants.POST_CONTENT_TYPE);

    private final String uri;
    private final Protocol protocol;
    private final HttpMethod httpMethod;
    private final int emitTimeout;
    private final String customPostPath;

    private OkHttpClient client;
    private Uri.Builder uriBuilder;

    /**
     * Builder for the OkHttpNetworkConnection.
     */
    public static class OkHttpNetworkConnectionBuilder {
        final String uri; // Required
        HttpMethod httpMethod = POST; // Optional
        Protocol protocol = Protocol.HTTP; // Optional
        EnumSet<TLSVersion> tlsVersions = EnumSet.of(TLSVersion.TLSv1_2); // Optional
        private int emitTimeout = 5; // Optional
        OkHttpClient client = null; //Optional
        String customPostPath = null; //Optional

        /**
         * @param uri The uri of the collector
         */
        public OkHttpNetworkConnectionBuilder(@NonNull String uri) {
            this.uri = uri;
        }

        /**
         * @param httpMethod The method by which requests are emitted
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder method(@NonNull HttpMethod httpMethod) {
            this.httpMethod = httpMethod;
            return this;
        }

        /**
         * @param protocol the security chosen for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder security(@NonNull Protocol protocol) {
            this.protocol = protocol;
            return this;
        }

        /**
         * @param version the TLS version allowed for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder tls(@NonNull TLSVersion version) {
            this.tlsVersions = EnumSet.of(version);
            return this;
        }

        /**
         * @param versions the TLS versions allowed for requests
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder tls(@NonNull EnumSet<TLSVersion> versions) {
            this.tlsVersions = versions;
            return this;
        }

        /**
         * @param emitTimeout The maximum timeout for emitting events. If emit time exceeds this value
         *                    TimeOutException will be thrown
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder emitTimeout(int emitTimeout){
            this.emitTimeout = emitTimeout;
            return this;
        }

        /**
         * @param client An OkHttp client that will be used in the emitter, you can provide your
         *               own if you want to share your Singleton client's interceptors, connection pool etc..
         *               ,otherwise a new one is created.
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder client(@Nullable OkHttpClient client) {
            this.client = client;
            return this;
        }

        /**
         * @param customPostPath A custom path that is used on the endpoint to send requests.
         * @return itself
         */
        @NonNull
        public OkHttpNetworkConnectionBuilder customPostPath(@Nullable String customPostPath) {
            this.customPostPath = customPostPath;
            return this;
        }

        /**
         * Creates a new OkHttpNetworkConnection
         *
         * @return a new OkHttpNetworkConnection object
         */
        @NonNull
        public OkHttpNetworkConnection build() {
            return new OkHttpNetworkConnection(this);
        }
    }

    private OkHttpNetworkConnection(OkHttpNetworkConnectionBuilder builder) {
        this.uri = builder.uri;
        this.protocol = builder.protocol;
        this.httpMethod = builder.httpMethod;
        this.emitTimeout = builder.emitTimeout;
        this.customPostPath = builder.customPostPath;

        TLSArguments tlsArguments = new TLSArguments(builder.tlsVersions);
        buildUri();

        if (builder.client == null) {
            client = new OkHttpClient.Builder()
                    .sslSocketFactory(tlsArguments.getSslSocketFactory(), tlsArguments.getTrustManager())
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .readTimeout(15, TimeUnit.SECONDS)
                    .build();
        } else {
            client = builder.client;
        }
    }

    @Override
    @NonNull
    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    @NonNull
    @Override
    public Uri getUri() {
        return uriBuilder.clearQuery().build();
    }

    @NonNull
{'fraction_non_alphanumeric': 0.07504268198044389, 'fraction_numerical': 0.0019400900201769363, 'mean_word_length': 2.411064055055585, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264838', 'n_tokens_mistral': 3306, 'n_tokens_neox': 3240, 'n_words': 851}