Example usage for java.net Proxy NO_PROXY

List of usage examples for java.net Proxy NO_PROXY

Introduction

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

Prototype

Proxy NO_PROXY

To view the source code for java.net Proxy NO_PROXY.

Click Source Link

Document

A proxy setting that represents a DIRECT connection, basically telling the protocol handler not to use any proxying.

Usage

From source file:de.jetwick.snacktory.HtmlFetcher.java

public Proxy getProxy() {
    return (proxy != null ? proxy : Proxy.NO_PROXY);
}

From source file:com.truebanana.http.HTTPRequest.java

private HttpURLConnection buildURLConnection() {
    try {//from   w w  w.  java  2  s  .c  o m
        Uri.Builder builder = Uri.parse(url).buildUpon();
        Iterator<Map.Entry<String, String>> iterator = queryParameters.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> pair = (Map.Entry) iterator.next();
            builder.appendQueryParameter(pair.getKey(), pair.getValue());
        }

        URL u = new URL(builder.build().toString());

        Proxy proxy = Proxy.NO_PROXY;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
            String host = System.getProperty("http.proxyHost");
            if (host != null && !host.isEmpty()) {
                String port = System.getProperty("http.proxyPort");
                if (port != null && !port.isEmpty()) {
                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, Integer.parseInt(port)));
                }
            }
        }

        HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection(proxy);
        urlConnection.setRequestMethod(requestMethod.name());
        urlConnection.setDoInput(true);
        urlConnection.setConnectTimeout(connectTimeout);
        urlConnection.setReadTimeout(readTimeout);

        switch (requestMethod) {
        case POST:
        case PUT:
            urlConnection.setDoOutput(true);
            break;
        default:
        case GET:
        case DELETE:
            urlConnection.setDoOutput(false);
            break;
        }
        return urlConnection;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Invalid URL.");
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Invalid URL.");
    }
}

From source file:com.marvelution.hudson.plugins.apiv2.client.connectors.HttpClient3Connector.java

/**
 * Get the first available {@link Proxy} using the {@link ProxySelector} implementation
 * /*from  w ww  . j  av  a  2  s  .co  m*/
 * @return the first {@link Type#HTTP} proxy available, or {@link Proxy#NO_PROXY} if no
 *         proxy is available
 */
private Proxy chooseProxy() {
    try {
        for (Proxy proxy : ProxySelector.getDefault().select(new URI(server.getHost()))) {
            if (proxy.type() == Type.HTTP) {
                return proxy;
            }
        }
    } catch (URISyntaxException e) {
        // Failed to parse the server host URI
        // Fallback to the default NO_PROXY
        log.error("Failed to parse the host URI", e);
    }
    return Proxy.NO_PROXY;
}

From source file:net.sf.keystore_explorer.utilities.net.PacProxySelector.java

private Proxy parsePacProxy(String pacProxy) {
    /*/*from  ww  w  .ja v a  2 s .  c  om*/
     * PAC formats:
     *
     * DIRECT Connections should be made directly, without any proxies.
     *
     * PROXY host:port The specified proxy should be used.
     *
     * SOCKS host:port The specified SOCKS server should be used.
     *
     * Where port is not supplied use port 80
     */

    if (pacProxy.equals("DIRECT")) {
        return Proxy.NO_PROXY;
    }

    String[] split = pacProxy.split(" ", 0);

    if (split.length != 2) {
        return null;
    }

    String proxyTypeStr = split[0];
    String address = split[1];

    Proxy.Type proxyType = null;

    if (proxyTypeStr.equals("PROXY")) {
        proxyType = Proxy.Type.HTTP;
    } else if (proxyTypeStr.equals("SOCKS")) {
        proxyType = Proxy.Type.SOCKS;
    }

    if (proxyType == null) {
        return null;
    }

    split = address.split(":", 0);
    String host = null;
    int port = 80;

    if (split.length == 1) {
        host = split[0];
    } else if (split.length == 2) {
        host = split[0];

        try {
            port = Integer.parseInt(split[1]);
        } catch (NumberFormatException ex) {
            return null;
        }
    } else {
        return null;
    }

    return new Proxy(proxyType, new InetSocketAddress(host, port));
}

From source file:com.pannous.es.reindex.MySearchResponseJson.java

protected HttpURLConnection createUrlConnection(String urlAsStr, int timeout)
        throws MalformedURLException, IOException {
    URL url = new URL(urlAsStr);
    //using proxy may increase latency
    HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
    hConn.setRequestProperty("User-Agent", "ElasticSearch reindex");
    hConn.setRequestProperty("Accept", "application/json");
    hConn.setRequestProperty("content-charset", "UTF-8");
    // hConn.setRequestProperty("Cache-Control", cacheControl);
    // suggest respond to be gzipped or deflated (which is just another compression)
    // http://stackoverflow.com/q/3932117
    hConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    hConn.setConnectTimeout(timeout);/*from   w w  w .j a v a  2  s . c om*/
    hConn.setReadTimeout(timeout);
    return hConn;
}

From source file:com.nike.cerberus.module.CerberusModule.java

@Provides
@Singleton//w w w.j av  a 2 s .com
public Proxy proxy() {
    final Proxy.Type type = proxyDelegate.getProxyType();

    if (type == Proxy.Type.DIRECT) {
        return Proxy.NO_PROXY;
    }

    final String host = proxyDelegate.getProxyHost();
    final Integer port = proxyDelegate.getProxyPort();

    if (StringUtils.isBlank(host) || port == null) {
        logger.warn("Invalid proxy settings, ignoring...");
        return Proxy.NO_PROXY;
    }

    return new Proxy(type, new InetSocketAddress(host, port));
}

From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java

@Override
public void setProxy(String proxyhost, int proxyport) {
    if (StringUtils.isNotBlank(proxyhost)) {
        okHttpClient.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyhost, proxyport)));
        okHttpClient.setSslSocketFactory(sslSocketFactory);
    } else {/*from   ww  w  . ja  va2s .c om*/
        // Set proxy to direct
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setProxy(Proxy.NO_PROXY);
    }
}

From source file:com.irccloud.android.GingerbreadImageProxy.java

private void processRequest(HttpRequest request, Socket client) throws IllegalStateException, IOException {
    if (request == null) {
        return;//from  w  ww .  j ava2 s.  c  o  m
    }
    URL url = new URL(request.getRequestLine().getUri());

    HttpURLConnection conn = null;

    Proxy proxy = null;
    String host = null;
    int port = -1;

    if (Build.VERSION.SDK_INT < 11) {
        Context ctx = IRCCloudApplication.getInstance().getApplicationContext();
        if (ctx != null) {
            host = android.net.Proxy.getHost(ctx);
            port = android.net.Proxy.getPort(ctx);
        }
    } else {
        host = System.getProperty("http.proxyHost", null);
        try {
            port = Integer.parseInt(System.getProperty("http.proxyPort", "8080"));
        } catch (NumberFormatException e) {
            port = -1;
        }
    }

    if (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost")
            && !host.equalsIgnoreCase("127.0.0.1") && port > 0) {
        InetSocketAddress proxyAddr = new InetSocketAddress(host, port);
        proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
    }

    if (url.getProtocol().toLowerCase().equals("https")) {
        conn = (HttpsURLConnection) ((proxy != null) ? url.openConnection(proxy)
                : url.openConnection(Proxy.NO_PROXY));
    } else {
        conn = (HttpURLConnection) ((proxy != null) ? url.openConnection(proxy)
                : url.openConnection(Proxy.NO_PROXY));
    }

    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setUseCaches(true);

    if (!isRunning)
        return;

    try {
        client.getOutputStream().write(
                ("HTTP/1.0 " + conn.getResponseCode() + " " + conn.getResponseMessage() + "\n\n").getBytes());

        if (conn.getResponseCode() == 200 && conn.getInputStream() != null) {
            byte[] buff = new byte[8192];
            int readBytes;
            while (isRunning && (readBytes = conn.getInputStream().read(buff, 0, buff.length)) != -1) {
                client.getOutputStream().write(buff, 0, readBytes);
            }
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        client.close();
    }

    conn.disconnect();
    stop();
}

From source file:org.xframium.integrations.alm.ALMRESTConnection.java

private ALMResponse doHttp(String type, String url, String queryString, byte[] data,
        Map<String, String> headers, Map<String, String> cookies) throws Exception {
    log.warn("1.0.4");

    if ((queryString != null) && !queryString.isEmpty())
        url += "?" + queryString;

    if (log.isInfoEnabled())
        log.info("Executing " + type + ": to " + url);

    HttpURLConnection con = null;

    if (Boolean.parseBoolean(System.getProperty("alm.bypassProxy", "false")))
        con = (HttpURLConnection) new URL(url).openConnection(Proxy.NO_PROXY);
    else/*  w  w  w .ja v  a2s.  co m*/
        con = (HttpURLConnection) new URL(url).openConnection();

    con.setRequestMethod(type);
    String cookieString = getCookieString();

    if (log.isInfoEnabled())
        log.info("Cookies: " + cookieString);

    prepareHttpRequest(con, headers, data, cookieString);
    con.connect();
    ALMResponse ret = retrieveHtmlResponse(con);

    if (log.isInfoEnabled())
        log.info("Return Value: " + ret);

    updateCookies(ret);

    return ret;
}

From source file:org.jenkinsci.plugins.github_branch_source.Connector.java

/**
 * Uses proxy if configured on pluginManager/advanced page
 *
 * @param host GitHub's hostname to build proxy to
 *
 * @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour
 *///from  w  w w  .  ja  v a 2s.c o m
@Nonnull
private static Proxy getProxy(@Nonnull String host) {
    Jenkins jenkins = GitHubWebHook.getJenkinsInstance();

    if (jenkins.proxy == null) {
        return Proxy.NO_PROXY;
    } else {
        return jenkins.proxy.createProxy(host);
    }
}