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

com.github.robozonky.internal.remote.ProxyFactory Maven / Gradle / Ivy

There is a newer version: 6.4.1
Show newest version
/*
 * Copyright 2021 The RoboZonky Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.robozonky.internal.remote;

import java.util.concurrent.TimeUnit;

import javax.ws.rs.client.ClientBuilder;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;

import com.github.robozonky.internal.Settings;

final class ProxyFactory {

    private static final Logger LOGGER = LogManager.getLogger(ProxyFactory.class);

    private ProxyFactory() {
        // no instances
    }

    public static ResteasyClient newResteasyClient() {
        LOGGER.debug("Creating RESTEasy client.");
        var settings = Settings.INSTANCE;
        var socketTimeout = settings.getSocketTimeout()
            .toMillis();
        LOGGER.debug("Set socket timeout to {} ms.", socketTimeout);
        var connectionTimeout = settings.getConnectionTimeout()
            .toMillis();
        LOGGER.debug("Set connection timeout to {} ms.", connectionTimeout);
        var builder = ClientBuilder.newBuilder()
            .readTimeout(socketTimeout, TimeUnit.MILLISECONDS)
            .connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS);
        /*
         * setup HTTP proxy when required (see
         * http://docs.jboss.org/resteasy/docs/4.0.0.Final/userguide/html/RESTEasy_Client_Framework.html#http_proxy)
         */
        settings.getHttpsProxyHostname()
            .ifPresent(host -> {
                var port = settings.getHttpsProxyPort();
                builder.property("org.jboss.resteasy.jaxrs.client.proxy.host", host)
                    .property("org.jboss.resteasy.jaxrs.client.proxy.port", port);
                LOGGER.debug("Set HTTP proxy to {}:{}.", host, port);
            });
        return (ResteasyClient) builder.build();
    }

    public static  T newProxy(final ResteasyClient client, final RoboZonkyFilter filter, final Class api,
            final String url) {
        return client.target(url)
            .register(filter)
            .proxy(api);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy