Example usage for java.net Proxy Proxy

List of usage examples for java.net Proxy Proxy

Introduction

In this page you can find the example usage for java.net Proxy Proxy.

Prototype

public Proxy(Type type, SocketAddress sa) 

Source Link

Document

Creates an entry representing a PROXY connection.

Usage

From source file:jails.http.client.support.ProxyFactoryBean.java

public void afterPropertiesSet() throws IllegalArgumentException {
    Assert.notNull(type, "'type' must not be null");
    Assert.hasLength(hostname, "'hostname' must not be empty");
    Assert.isTrue(port >= 0 && port <= 65535, "'port' out of range: " + port);

    SocketAddress socketAddress = new InetSocketAddress(hostname, port);
    this.proxy = new Proxy(type, socketAddress);

}

From source file:com.tda.sitefilm.allocine.AbstractAllocineAPI.java

/**
 *  bla/*w w w  . j a  v  a 2s. c  o m*/
 *  @param proxyHost bla
 *  @param proxyPort bla
 */
public final void setProxy(String proxyHost, int proxyPort) {
    if (StringUtils.isNotBlank(proxyHost) && (proxyPort > -1)) {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    }
}

From source file:org.finra.herd.dao.helper.UrlHelper.java

/**
 * Reads JSON from a specified URL./*  w  w  w. j a v a  2  s .co m*/
 *
 * @param url the url
 *
 * @return the JSON object
 */
public JSONObject parseJsonObjectFromUrl(String url) {
    try {
        // Get proxy information.
        Proxy proxy;
        String httpProxyHost = configurationHelper.getProperty(ConfigurationValue.HTTP_PROXY_HOST);
        Integer httpProxyPort = configurationHelper.getProperty(ConfigurationValue.HTTP_PROXY_PORT,
                Integer.class);
        if (StringUtils.isNotBlank(httpProxyHost) && httpProxyPort != null) {
            proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyHost, httpProxyPort));
        } else {
            proxy = Proxy.NO_PROXY;
        }

        // Open an input stream as per specified URL.
        InputStream inputStream = urlOperations.openStream(new URL(url), proxy);

        try {
            // Parse the JSON object from the input stream.
            JSONParser jsonParser = new JSONParser();
            return (JSONObject) jsonParser.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        } catch (ParseException e) {
            throw new IllegalArgumentException(
                    String.format("Failed to parse JSON object from the URL: url=\"%s\"", url), e);
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new IllegalArgumentException(String.format("Failed to read JSON from the URL: url=\"%s\"", url),
                e);
    }
}

From source file:org.tinymediamanager.scraper.http.TmmHttpClient.java

private static void setProxy(OkHttpClient.Builder builder) {
    Proxy proxyHost;/*from w  ww.java2 s. c om*/

    if (ProxySettings.INSTANCE.getPort() > 0) {
        proxyHost = new Proxy(Proxy.Type.HTTP, InetSocketAddress
                .createUnresolved(ProxySettings.INSTANCE.getHost(), ProxySettings.INSTANCE.getPort()));
    } else if (StringUtils.isNotBlank(ProxySettings.INSTANCE.getHost())) {
        proxyHost = new Proxy(Proxy.Type.HTTP,
                InetSocketAddress.createUnresolved(ProxySettings.INSTANCE.getHost(), 80));
    } else {
        // no proxy settings found. return
        return;
    }

    builder.proxy(proxyHost);
    // authenticate
    if (StringUtils.isNotBlank(ProxySettings.INSTANCE.getUsername())
            && StringUtils.isNotBlank(ProxySettings.INSTANCE.getPassword())) {
        builder.authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                String credential = Credentials.basic(ProxySettings.INSTANCE.getUsername(),
                        ProxySettings.INSTANCE.getPassword());
                return response.request().newBuilder().header("Proxy-Authorization", credential).build();
            }
        });
    }
}

From source file:org.owasp.dependencycheck.utils.URLConnectionFactory.java

/**
 * Utility method to create an HttpURLConnection. If the application is configured to use a proxy this method will retrieve
 * the proxy settings and use them when setting up the connection.
 *
 * @param url the url to connect to//from   w  w w. java 2  s . c  o m
 * @return an HttpURLConnection
 * @throws URLConnectionFailureException thrown if there is an exception
 */
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Just being extra safe")
public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException {
    HttpURLConnection conn = null;
    final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER);

    try {
        if (proxyUrl != null && !matchNonProxy(url)) {
            final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
            final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort);

            final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME);
            final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD);

            if (username != null && password != null) {
                final Authenticator auth = new Authenticator() {
                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType().equals(Authenticator.RequestorType.PROXY)) {
                            return new PasswordAuthentication(username, password.toCharArray());
                        }
                        return super.getPasswordAuthentication();
                    }
                };
                Authenticator.setDefault(auth);
            }

            final Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
            conn = (HttpURLConnection) url.openConnection(proxy);
        } else {
            conn = (HttpURLConnection) url.openConnection();
        }
        final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 10000);
        conn.setConnectTimeout(timeout);
        conn.setInstanceFollowRedirects(true);
    } catch (IOException ex) {
        if (conn != null) {
            try {
                conn.disconnect();
            } finally {
                conn = null;
            }
        }
        throw new URLConnectionFailureException("Error getting connection.", ex);
    }
    return conn;
}

From source file:de.mpg.escidoc.services.common.util.ProxyHelper.java

/**
 * Returns <code>java.net.Proxy</code> class for <code>java.net.URL.openConnection</code>
 * creation/* w w  w . j  av a2s .  c om*/
 *
 * @param url url
 *
 * @throws Exception
 */
public static Proxy getProxy(final String url) {
    Proxy proxy = Proxy.NO_PROXY;

    getProxyProperties();

    if (proxyHost != null) {
        if (!findUrlInNonProxyHosts(url)) {
            SocketAddress proxyAddress = new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort));
            proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
        }
    }

    return proxy;
}

From source file:ch.bfh.abcvote.util.helpers.SocksConnectionSocketFactory.java

/**
 * ConnectionSocketFactory needed for Socks Proxy
 * @param context the http context/*  ww  w  .java  2s. co m*/
 * @return returns a proxy socket
 */
public Socket createSocket(final HttpContext context) {
    InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
    return new Socket(proxy);
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java

public static String fetchContent(final String stateUrl, final String ipStr, final int port, final int timeout)
        throws IOException {
    final URLConnection conn = new URL(stateUrl)
            .openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipStr, port)));
    if (timeout != 0) {
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);//from www. j a  va 2 s  . com
    }
    conn.connect();

    return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR));
}

From source file:org.elasticsearch.hadoop.rest.commonshttp.SocksSocketFactory.java

public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {

    InetSocketAddress socksAddr = new InetSocketAddress(socksHost, socksPort);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
    int timeout = params.getConnectionTimeout();

    Socket socket = new Socket(proxy);
    socket.setSoTimeout(timeout);/*from   w w w. ja  v  a  2 s  . c o  m*/

    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    return socket;
}

From source file:com.blackducksoftware.integration.hub.global.HubProxyInfo.java

public URLConnection openConnection(final URL url) throws IOException {
    if (shouldUseProxyForUrl(url)) {
        final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
        setDefaultAuthenticator();/*  w  ww .j a  va  2  s .  c om*/

        return url.openConnection(proxy);
    }

    return url.openConnection();
}