Загрузка данных


src
└── main
    └── java
        └── ...
            └── auth
                ├── AuthClient.java
                ├── AuthConfiguration.java
                ├── AuthService.java
                ├── CertificateLoader.java
                └── AuthException.java

package ...auth;

public final class AuthConfiguration {

    private AuthConfiguration() {
    }

    public static final String AUTH_URL =
            System.getenv("AUTH_URL");

    public static final String CERTIFICATE_PATH =
            System.getenv("KEYSTORE_FILE");

    public static final String CERTIFICATE_PASSWORD =
            System.getenv("KEYSTORE_STRING");
}




package ...auth;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;

public final class CertificateLoader {

    private CertificateLoader() {
    }

    public static KeyStore load() {
        try (InputStream is = Files.newInputStream(
                Paths.get(AuthConfiguration.CERTIFICATE_PATH))) {

            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(
                    is,
                    AuthConfiguration.CERTIFICATE_PASSWORD.toCharArray());

            return keyStore;

        } catch (Exception e) {
            throw new AuthException("Не удалось загрузить клиентский сертификат", e);
        }
    }
}




package ...auth;

import io.restassured.RestAssured;
import io.restassured.config.RedirectConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.config.SSLConfig;
import io.restassured.response.Response;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class AuthClient {

    private final RestAssuredConfig config;

    public AuthClient() {
        this.config = RestAssuredConfig.config()
                .sslConfig(new SSLConfig()
                        .keyStore(
                                AuthConfiguration.CERTIFICATE_PATH,
                                AuthConfiguration.CERTIFICATE_PASSWORD)
                        .keystoreType("PKCS12")
                        .relaxedHTTPSValidation())
                .redirect(RedirectConfig.redirectConfig()
                        .followRedirects(false)
                        .allowCircularRedirects(true)
                        .maxRedirects(1));
    }

    public Map<String, String> authenticate() {

        Map<String, String> cookies = new HashMap<>();

        String currentUrl = AuthConfiguration.AUTH_URL;

        for (int i = 0; i < 10; i++) {

            Response response = RestAssured
                    .given()
                    .config(config)
                    .cookies(cookies)
                    .redirects().follow(false)
                    .get(currentUrl);

            response.getDetailedCookies()
                    .forEach(c ->
                            cookies.put(c.getName(), c.getValue()));

            String location = response.getHeader("Location");

            if (location == null) {
                break;
            }

            currentUrl = toAbsoluteUrl(location, currentUrl);
        }

        return cookies;
    }

    private String toAbsoluteUrl(String location, String base) {

        if (location.startsWith("http://")
                || location.startsWith("https://")) {
            return location;
        }

        return URI.create(base)
                .resolve(location)
                .toString();
    }
}




package ...auth;

import java.util.Collections;
import java.util.Map;

public final class AuthService {

    private static final AuthClient CLIENT = new AuthClient();

    private static volatile Map<String, String> cookies;

    private AuthService() {
    }

    public static synchronized void authorize() {

        if (cookies != null) {
            return;
        }

        cookies = Collections.unmodifiableMap(
                CLIENT.authenticate());
    }

    public static Map<String, String> getCookies() {

        if (cookies == null) {
            throw new AuthException(
                    "Авторизация еще не выполнена");
        }

        return cookies;
    }
}




package ...auth;

public class AuthException extends RuntimeException {

    public AuthException(String message) {
        super(message);
    }

    public AuthException(String message, Throwable cause) {
        super(message, cause);
    }
}