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

io.mstream.trader.commons.http.client.HttpClientSupplier Maven / Gradle / Ivy

The newest version!
package io.mstream.trader.commons.http.client;


import io.mstream.trader.commons.config.model.DownstreamService;
import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.client.HttpClient;

import javax.inject.Inject;
import javax.net.ssl.SSLEngine;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;


public abstract class HttpClientSupplier
        implements Supplier> {
    
    private final Supplier configSupplier;
    
    private final Supplier sslEngineSupplier;
    
    private final int timeoutInSeconds;
    
    @Inject
    public HttpClientSupplier(
            Supplier configSupplier,
            Supplier sslEngineSupplier,
            int timeoutInSeconds
    ) {
        
        this.configSupplier = configSupplier;
        this.sslEngineSupplier = sslEngineSupplier;
        this.timeoutInSeconds = timeoutInSeconds;
    }
    
    @Override
    public HttpClient get() {
        
        DownstreamService dateFeed = configSupplier.get();
        
        URI uri =
                URI.create(
                        dateFeed
                                .getUrl()
                                .url()
                );
        
        String host = uri.getHost();
        int port = uri.getPort();
        
        SocketAddress serviceAddress =
                new InetSocketAddress(
                        host,
                        port
                );
    
        HttpClient httpClient =
                HttpClient
                        .newClient(serviceAddress)
                        .readTimeOut(
                                timeoutInSeconds,
                                TimeUnit.SECONDS
                        );
        
        if ("https".equals(uri.getScheme())) {
            httpClient = httpClient.secure(sslEngineSupplier.get());
        }
        
        return httpClient;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy