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

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

package com.turbospaces.http;

import com.turbospaces.boot.AbstractBootstrapAware;
import com.turbospaces.cfg.ApplicationProperties;
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 MockHttpProxyConfiguration extends AbstractBootstrapAware {
    public static final Logger LOG = LoggerFactory.getLogger(MockHttpProxyConfiguration.class);

    public static Optional proxyRequest(ApplicationProperties props, String uriStr) {
        return proxyRequest(props.MOCK_HTTP_PROXY.get(), props.MOCK_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();
            }
            return Optional.of(new URIBuilder(proxy)
                    .setPath(uri.getPath())
                    .setQuery(uri.getRawQuery())
                    .build().toString());
        } catch (Exception e) {
            LOG.error("Error when proxying request {}", uriStr, e);
            return Optional.empty();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy