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

io.qameta.allure.maven.ProxyUtils Maven / Gradle / Ivy

There is a newer version: 2.15.2
Show newest version
package io.qameta.allure.maven;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.Socket;
import java.util.List;

/**
 * Created by bvo2002 on 25.05.17.
 */
final class ProxyUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProxyUtils.class);

    private ProxyUtils(){
        throw new IllegalStateException("Do not instance");
    }

    /* default */ static Proxy getProxy(MavenSession mavenSession, SettingsDecrypter decrypter) {
        if (mavenSession == null ||
                mavenSession.getSettings() == null ||
                mavenSession.getSettings().getProxies() == null ||
                mavenSession.getSettings().getProxies().isEmpty()) {
            LOGGER.info("Proxy is not specified.");
            return null;
        } else {
            final List mavenProxies = mavenSession.getSettings().getProxies();
            for (Proxy proxy : mavenProxies) {
                if (proxy.isActive()) {
                    proxy = decryptProxy(proxy, decrypter);
                    try (Socket socket = new Socket(proxy.getHost(), proxy.getPort())) {
                    } catch (IOException e) {
                        LOGGER.info(String.format("Proxy: %s:%s is not available", proxy.getHost(), proxy.getPort()));
                        continue;
                    }
                    LOGGER.info(String.format("Found proxy: %s:%s", proxy.getHost(), proxy.getPort()));
                    return proxy;
                }
            }
            LOGGER.info("No active proxies found.");
            return null;
        }
    }

    private static Proxy decryptProxy(Proxy proxy, SettingsDecrypter decrypter) {
        final DefaultSettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(proxy);
        SettingsDecryptionResult decryptedResult = decrypter.decrypt(decryptionRequest);
        return decryptedResult.getProxy();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy