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:org.mariotaku.twidere.util.Utils.java

public static Proxy getProxy(final Context context) {
    if (context == null)
        return null;
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final boolean enable_proxy = prefs.getBoolean(PREFERENCE_KEY_ENABLE_PROXY, false);
    if (!enable_proxy)
        return Proxy.NO_PROXY;
    final String proxy_host = prefs.getString(PREFERENCE_KEY_PROXY_HOST, null);
    final int proxy_port = parseInt(prefs.getString(PREFERENCE_KEY_PROXY_PORT, "-1"));
    if (!isEmpty(proxy_host) && proxy_port > 0) {
        final SocketAddress addr = InetSocketAddress.createUnresolved(proxy_host, proxy_port);
        return new Proxy(Proxy.Type.HTTP, addr);
    }/*from  w  w w.  j ava  2 s.com*/
    return Proxy.NO_PROXY;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static HttpClientWrapper getHttpClient(final Context context, final int timeoutMillis,
        final boolean ignoreSslError, final Proxy proxy, final HostAddressResolverFactory resolverFactory,
        final String userAgent, final boolean twitterClientHeader) {
    final ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setHttpConnectionTimeout(timeoutMillis);
    cb.setIgnoreSSLError(ignoreSslError);
    cb.setIncludeTwitterClientHeader(twitterClientHeader);
    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        final SocketAddress address = proxy.address();
        if (address instanceof InetSocketAddress) {
            cb.setHttpProxyHost(((InetSocketAddress) address).getHostName());
            cb.setHttpProxyPort(((InetSocketAddress) address).getPort());
        }/*  ww w  .j  a  va 2 s.c  o m*/
    }
    cb.setHostAddressResolverFactory(resolverFactory);
    if (userAgent != null) {
        cb.setHttpUserAgent(userAgent);
    }
    cb.setHttpClientFactory(new TwidereHttpClientFactory(context));
    return new HttpClientWrapper(cb.build());
}

From source file:org.getlantern.firetweet.util.Utils.java

public static HttpClientWrapper getHttpClient(final Context context, final int timeoutMillis,
        final boolean ignoreSslError, final Proxy proxy, final HostAddressResolverFactory resolverFactory,
        final String userAgent, final boolean twitterClientHeader) {
    final ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setHttpConnectionTimeout(timeoutMillis);
    cb.setIgnoreSSLError(ignoreSslError);
    cb.setIncludeTwitterClientHeader(twitterClientHeader);
    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        final SocketAddress address = proxy.address();
        if (address instanceof InetSocketAddress) {
            cb.setHttpProxyHost(((InetSocketAddress) address).getHostName());
            cb.setHttpProxyPort(((InetSocketAddress) address).getPort());
        }//  ww  w  . j  a va2s .c  om
    }
    cb.setHostAddressResolverFactory(resolverFactory);
    if (userAgent != null) {
        cb.setHttpUserAgent(userAgent);
    }
    cb.setHttpClientFactory(new OkHttpClientFactory(context));
    return new HttpClientWrapper(cb.build());
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static Proxy getProxy(final Context context) {
    if (context == null)
        return null;
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final boolean enable_proxy = prefs.getBoolean(KEY_ENABLE_PROXY, false);
    if (!enable_proxy)
        return Proxy.NO_PROXY;
    final String proxyHost = prefs.getString(KEY_PROXY_HOST, null);
    final int proxyPort = ParseUtils.parseInt(prefs.getString(KEY_PROXY_PORT, "-1"));
    if (!isEmpty(proxyHost) && proxyPort >= 0 && proxyPort < 65535) {
        final SocketAddress addr = InetSocketAddress.createUnresolved(proxyHost, proxyPort);
        return new Proxy(Proxy.Type.HTTP, addr);
    }//from   w  ww .j  av a  2  s .  c om
    return Proxy.NO_PROXY;
}

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

public String fetch(URL url, String postdata, String sk, String token, HashMap<String, String> headers)
        throws Exception {
    HttpURLConnection conn = null;

    Proxy proxy = null;//from  w  w w  .j  ava2s.co  m
    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 (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost")
            && !host.equalsIgnoreCase("127.0.0.1") && port > 0) {
        Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url + " via proxy: " + host);
    } else {
        Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url);
    }

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

    conn.setConnectTimeout(5000);
    conn.setReadTimeout(5000);
    conn.setUseCaches(false);
    conn.setRequestProperty("User-Agent", useragent);
    conn.setRequestProperty("Accept", "application/json");
    if (headers != null) {
        for (String key : headers.keySet()) {
            conn.setRequestProperty(key, headers.get(key));
        }
    }
    if (sk != null)
        conn.setRequestProperty("Cookie", "session=" + sk);
    if (token != null)
        conn.setRequestProperty("x-auth-formtoken", token);
    if (postdata != null) {
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        OutputStream ostr = null;
        try {
            ostr = conn.getOutputStream();
            ostr.write(postdata.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ostr != null)
                ostr.close();
        }
    }
    BufferedReader reader = null;
    String response = "";

    try {
        ConnectivityManager cm = (ConnectivityManager) IRCCloudApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) {
            Crashlytics.log(Log.DEBUG, TAG, "Loading via WiFi");
        } else {
            Crashlytics.log(Log.DEBUG, TAG, "Loading via mobile");
        }
    } catch (Exception e) {
    }

    try {
        if (conn.getInputStream() != null) {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()), 512);
        }
    } catch (IOException e) {
        if (conn.getErrorStream() != null) {
            reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()), 512);
        }
    }

    if (reader != null) {
        response = toString(reader);
        reader.close();
    }
    conn.disconnect();
    return response;
}

From source file:com.atlauncher.data.Settings.java

public Proxy getProxyForAuth() {
    if (!this.enableProxy) {
        return Proxy.NO_PROXY;
    }/* w  w w  .  j  a v  a2s .c  o m*/
    if (this.proxy == null) {
        Type type;
        if (this.proxyType.equals("HTTP")) {
            type = Proxy.Type.HTTP;
        } else if (this.proxyType.equals("SOCKS")) {
            type = Proxy.Type.SOCKS;
        } else if (this.proxyType.equals("DIRECT")) {
            type = Proxy.Type.DIRECT;
        } else {
            // Oh noes, problem!
            LogManager.warn("Tried to set proxy type to " + this.proxyType
                    + " which is not valid! Proxy support " + "disabled!");
            this.enableProxy = false;
            return Proxy.NO_PROXY;
        }
        this.proxy = new Proxy(type, new InetSocketAddress(this.proxyHost, this.proxyPort));
    }
    return this.proxy;
}

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

public Bitmap fetchImage(URL url, boolean cacheOnly) throws Exception {
    HttpURLConnection conn = null;

    Proxy proxy = null;/*  ww w .  j ava2s .  c  om*/
    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 (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost")
            && !host.equalsIgnoreCase("127.0.0.1") && port > 0) {
        Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url + " via proxy: " + host);
    } else {
        Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url);
    }

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

    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setUseCaches(true);
    conn.setRequestProperty("User-Agent", useragent);
    if (cacheOnly)
        conn.addRequestProperty("Cache-Control", "only-if-cached");
    Bitmap bitmap = null;

    try {
        ConnectivityManager cm = (ConnectivityManager) IRCCloudApplication.getInstance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) {
            Crashlytics.log(Log.DEBUG, TAG, "Loading via WiFi");
        } else {
            Crashlytics.log(Log.DEBUG, TAG, "Loading via mobile");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (conn.getInputStream() != null) {
            bitmap = BitmapFactory.decodeStream(conn.getInputStream());
        }
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    }

    conn.disconnect();
    return bitmap;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static HttpClientWrapper getHttpClient(final int timeout_millis, final boolean ignore_ssl_error,
        final Proxy proxy, final HostAddressResolver resolver, final String user_agent) {
    final ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setHttpConnectionTimeout(timeout_millis);
    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        final SocketAddress address = proxy.address();
        if (address instanceof InetSocketAddress) {
            cb.setHttpProxyHost(((InetSocketAddress) address).getHostName());
            cb.setHttpProxyPort(((InetSocketAddress) address).getPort());
        }//from  w  ww. j  a  v  a 2s.c om
    }
    // cb.setHttpClientImplementation(HttpClientImpl.class);
    return new HttpClientWrapper(cb.build());
}