All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mike10004.seleniumhelp.ProxyDefinitionBuilder Maven / Gradle / Ivy

There is a newer version: 0.58
Show newest version
package com.github.mike10004.seleniumhelp;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.http.client.utils.URIBuilder;

import javax.annotation.Nullable;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

public class ProxyDefinitionBuilder {

    private final FullSocketAddress socketAddress;
    private final List proxyBypasses;
    @Nullable
    private ProxyProtocol proxyProtocol;

    protected ProxyDefinitionBuilder(FullSocketAddress socketAddress) {
        this.socketAddress = requireNonNull(socketAddress, "socketAddress");
        proxyBypasses = new ArrayList<>();
    }

    /**
     * Designed to align with {@link org.littleshoot.proxy.ChainedProxy}.
     */
    public enum ProxyProtocol {
        http, socks4, socks5;

        String toScheme() {
            return name().toLowerCase();
        }
    }

    public ProxyDefinitionBuilder protocol(ProxyProtocol proxyType) {
        this.proxyProtocol = proxyType;
        return this;
    }

    public ProxyDefinitionBuilder http() {
        return protocol(ProxyProtocol.http);
    }

    public ProxyDefinitionBuilder socks5() {
        return protocol(ProxyProtocol.socks5);
    }

    public static ProxyDefinitionBuilder through(String host, int port) {
        return through(FullSocketAddress.define(host, port));
    }

    public static ProxyDefinitionBuilder through(FullSocketAddress socketAddress) {
        return new ProxyDefinitionBuilder(socketAddress);
    }

    public ProxyDefinitionBuilder addProxyBypass(String bypassPattern) {
        requireNonNull(bypassPattern);
        Preconditions.checkArgument(!bypassPattern.trim().isEmpty(), "bypass pattern must be non-whitespace, non-empty string");
        proxyBypasses.add(bypassPattern);
        return this;
    }

    public ProxyDefinitionBuilder addProxyBypasses(List bypassPatterns) {
        for (String p : bypassPatterns) {
            addProxyBypass(p);
        }
        return this;
    }

    @VisibleForTesting
    UriProxySpecification buildUriSpec() {
        try {
            URIBuilder b = new URIBuilder();
            if (proxyProtocol != null) {
                b.setScheme(proxyProtocol.toScheme());
            }
            b.setHost(socketAddress.getHost())
                    .setPort(socketAddress.getPort());
            for (String bypass : proxyBypasses) {
                b.addParameter(UriProxySpecification.PARAM_BYPASS, bypass);
            }
            return new UriProxySpecification(b.build());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public WebdrivingProxyDefinition buildWebdrivingProxyDefinition() {
        return buildUriSpec().toWebdrivingProxyDefinition();
    }

    public UpstreamProxyDefinition buildUpstreamProxyDefinition() {
        return buildUriSpec().toUpstreamProxyDefinition();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy