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

com.turbospaces.http.ProxyConfiguration Maven / Gradle / Ivy

package com.turbospaces.http;

import com.turbospaces.boot.AbstractBootstrapAware;
import com.turbospaces.cfg.ApplicationProperties;
import com.turbospaces.common.HttpProto;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;


public class ProxyConfiguration extends AbstractBootstrapAware {
    public static final Logger LOG = LoggerFactory.getLogger(ProxyConfiguration.class);

    public static Optional proxyRequest(ApplicationProperties props, String uriStr) {
        return proxyRequest(props.HTTP_PROXY.get(), props.HTTP_REQUEST_TO_PROXY_PATTERNS.get(), uriStr);
    }

    public static Optional proxyRequest(String proxy, List applyTo, String uriStr) {
        try {
            URI uri = new URI(uriStr);
            if (StringUtils.isEmpty(proxy)) {
                return Optional.empty();
            }
            boolean apply = applyTo.stream().anyMatch(p -> p.asPredicate().test(uriStr));
            if (!apply) {
                return Optional.empty();
            }
            String original = new URIBuilder()
                    .setScheme(uri.getScheme())
                    .setHost(uri.getHost())
                    .setPort(uri.getPort()).toString();
            return Optional.of(new URIBuilder(proxy)
                    .setPath(uri.getPath())
                    .setQuery(uri.getRawQuery())
                    .addParameter(HttpProto.PARAM_PROXY_REF, original)
                    .build()
                    .toString());
        } catch (Exception e) {
            LOG.error("Error when proxying request {}", uriStr, e);
            return Optional.empty();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy