Java Code Examples for java.net.Proxy#NO_PROXY

The following examples show how to use java.net.Proxy#NO_PROXY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyProxySelector.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public List<Proxy> select(URI uri) {
    if (uri == null) {
        throw new IllegalArgumentException(ResourceUtils.getString(
                MyProxySelector.class, 
                ERROR_URI_CANNOT_BE_NULL_KEY));
    }
    Proxy proxy = Proxy.NO_PROXY;
    if (byPassSet.contains(uri.getHost())) {
        return Collections.singletonList(proxy);
    }
    if (HTTP_SCHEME.equalsIgnoreCase(uri.getScheme()) || 
            HTTPS_SCHEME.equalsIgnoreCase(uri.getScheme())) {
        if (proxies.containsKey(MyProxyType.HTTP)) {
            proxy = proxies.get(MyProxyType.HTTP).getProxy();
        }
    } else if (FTP_SCHEME.equalsIgnoreCase(uri.getScheme())) {
        if (proxies.containsKey(MyProxyType.FTP)) {
            proxy = proxies.get(MyProxyType.FTP).getProxy();
        }
    } else {
        if (proxies.containsKey(MyProxyType.SOCKS)) {
            proxy = proxies.get(MyProxyType.SOCKS).getProxy();
        }
    }
    return Collections.singletonList(proxy);
}
 
Example 2
Source File: ProxyManager.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
private void logProxy(String protocol, Proxy proxy, PasswordAuthentication auth, String noProxyValue) {
    if (proxy != Proxy.NO_PROXY) {
        String authMessage = auth == null ?
                "" :
                " [" + auth.getUserName() + (auth.getPassword().length == 0 ? "" : ":***") + "]";
        if (Strings.isNullOrEmpty(noProxyValue)) {
            LOGGER.info("{} proxy configured to {}{} without exclusion",
                    protocol,
                    proxy.address().toString(),
                    authMessage);
        } else {
            LOGGER.info("{} proxy configured to {}{} excluding {}",
                    protocol,
                    proxy.address().toString(),
                    authMessage,
                    noProxyValue);
        }
    } else {
        LOGGER.info("No {} proxy configured", protocol);
    }
}
 
Example 3
Source File: EndpointAddress.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Proxy chooseProxy() {
    ProxySelector sel =
        java.security.AccessController.doPrivileged(
            new java.security.PrivilegedAction<ProxySelector>() {
                @Override
                public ProxySelector run() {
                    return ProxySelector.getDefault();
                }
            });

    if(sel==null)
        return Proxy.NO_PROXY;


    if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector"))
        // user-defined proxy. may return a different proxy for each invocation
        return null;

    Iterator<Proxy> it = sel.select(uri).iterator();
    if(it.hasNext())
        return it.next();

    return Proxy.NO_PROXY;
}
 
Example 4
Source File: RouteSelector.java    From wildfly-samples with MIT License 6 votes vote down vote up
/** Returns the next proxy to try. May be PROXY.NO_PROXY but never null. */
private Proxy nextProxy() {
  // If the user specifies a proxy, try that and only that.
  if (userSpecifiedProxy != null) {
    hasNextProxy = false;
    return userSpecifiedProxy;
  }

  // Try each of the ProxySelector choices until one connection succeeds. If none succeed
  // then we'll try a direct connection below.
  if (proxySelectorProxies != null) {
    while (proxySelectorProxies.hasNext()) {
      Proxy candidate = proxySelectorProxies.next();
      if (candidate.type() != Proxy.Type.DIRECT) {
        return candidate;
      }
    }
  }

  // Finally try a direct connection.
  hasNextProxy = false;
  return Proxy.NO_PROXY;
}
 
Example 5
Source File: RouteSelector.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
/** Returns the next proxy to try. May be PROXY.NO_PROXY but never null. */
private Proxy nextProxy() {
  // If the user specifies a proxy, try that and only that.
  if (userSpecifiedProxy != null) {
    hasNextProxy = false;
    return userSpecifiedProxy;
  }

  // Try each of the ProxySelector choices until one connection succeeds. If none succeed
  // then we'll try a direct connection below.
  if (proxySelectorProxies != null) {
    while (proxySelectorProxies.hasNext()) {
      Proxy candidate = proxySelectorProxies.next();
      if (candidate.type() != Proxy.Type.DIRECT) {
        return candidate;
      }
    }
  }

  // Finally try a direct connection.
  hasNextProxy = false;
  return Proxy.NO_PROXY;
}
 
Example 6
Source File: B6563259.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 7
Source File: B6563259.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 8
Source File: PreferencesDialog.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private Proxy getNewProxy() {
    if (proxyComboBox.getSelectedItem() == Proxy.Type.DIRECT) {
        return Proxy.NO_PROXY;
    } else {
        final Proxy.Type proxyType = (Proxy.Type) proxyComboBox.getSelectedItem();
        final String urlAddress = proxyAddressTextField.getText().trim();
        final String portString = proxyPortSpinner.getValue().toString();
        final int portNumber = portString.isEmpty() ? 0 : Integer.parseInt(portString);
        return new Proxy(proxyType, new InetSocketAddress(urlAddress, portNumber));
    }
}
 
Example 9
Source File: B6563259.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    System.setProperty("http.nonProxyHosts", "host1.*");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
    if (l.get(0) != Proxy.NO_PROXY) {
        throw new RuntimeException("ProxySelector returned the wrong proxy");
    }
}
 
Example 10
Source File: GoogleProxy.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
/** Creates an {@link GoogleProxy} instance based on proxy configuration. */
public static GoogleProxy fromConfiguration() {
  checkState(Configuration.isInitialized(), "configuration must be initialized");

  String userName = Configuration.getString(TRANSPORT_PROXY_USERNAME_KEY, "").get();
  String password = Configuration.getString(TRANSPORT_PROXY_PASSWORD_KEY, "").get();
  GoogleProxy.Builder builder = new GoogleProxy.Builder();
  if (!userName.isEmpty() && !password.isEmpty()) {
    builder.setUserNamePassword(userName, password);
  }

  Proxy.Type proxyType =
      Configuration.getValue(
          TRANSPORT_PROXY_TYPE_KEY,
          Proxy.Type.HTTP,
          value -> {
            try {
              return Proxy.Type.valueOf(value);
            } catch (IllegalArgumentException e) {
              throw new InvalidConfigurationException(e);
            }
          })
          .get();
  String hostname = Configuration.getString(TRANSPORT_PROXY_HOSTNAME_KEY, "").get();
  int port = Configuration.getInteger(TRANSPORT_PROXY_PORT_KEY, -1).get();

  if (!hostname.isEmpty()) {
    Configuration.checkConfiguration(port > 0, String.format("port %d is invalid", port));
  }

  Proxy proxy =
      hostname.isEmpty()
          ? Proxy.NO_PROXY
          : new Proxy(proxyType, new InetSocketAddress(hostname, port));

  return builder.setProxy(proxy).build();
}
 
Example 11
Source File: PreferencesDialog.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private JPanel getNetworkSettingsPanel() {
    final Proxy proxy = config.getProxy();
    //
    proxyComboBox.setModel(new DefaultComboBoxModel<>(Proxy.Type.values()));
    proxyComboBox.setFocusable(false);
    proxyComboBox.addActionListener(this);
    proxyComboBox.setSelectedItem(proxy.type());
    //
    // allow only numeric characters to be recognised.
    proxyPortSpinner.setEditor(new JSpinner.NumberEditor(proxyPortSpinner, "#"));
    final JFormattedTextField txt1 = ((JSpinner.NumberEditor) proxyPortSpinner.getEditor()).getTextField();
    ((NumberFormatter) txt1.getFormatter()).setAllowsInvalid(false);
    //
    if (proxy != Proxy.NO_PROXY) {
        proxyAddressTextField.setText(proxy.address().toString());
        proxyPortSpinner.setValue(((InetSocketAddress) proxy.address()).getPort());
    }
    // layout components
    final JPanel panel = new JPanel(new MigLayout("flowx, wrap 2, insets 16, gapy 4"));
    panel.add(getCaptionLabel(MText.get(_S12)));
    panel.add(proxyComboBox, "w 140!");
    panel.add(getCaptionLabel(MText.get(_S13)));
    panel.add(proxyAddressTextField, "w 100%");
    panel.add(getCaptionLabel(MText.get(_S14)));
    panel.add(proxyPortSpinner, "w 60!");
    return panel;
}
 
Example 12
Source File: ProxyConfigurationTest.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void testGetProxy(Proxy expected, String fileURL) {
	Proxy proxy = ProxyConfiguration.getProxy(fileURL);
	assertEquals(expected, proxy);
	if (expected == Proxy.NO_PROXY) {
		assertSame(expected, proxy);
	}
}
 
Example 13
Source File: HttpUtils.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This proxy will be used as default proxy.
 * To override default proxy usage use "Proxy.NO_PROXY"
 * 
 * It is set via JVM properties on startup:
 * java ... -Dhttp.proxyHost=proxy.url.local -Dhttp.proxyPort=8080 -Dhttp.nonProxyHosts='127.0.0.1|localhost'
 */
public static Proxy getProxyFromSystem(String url) {
	String proxyHost = System.getProperty("http.proxyHost");
	if (StringUtils.isNotBlank(proxyHost)) {
		String proxyPort = System.getProperty("http.proxyPort");
		String nonProxyHosts = System.getProperty("http.nonProxyHosts");
		
		if (StringUtils.isBlank(nonProxyHosts)) {
			if (StringUtils.isNotBlank(proxyHost)) {
				if (StringUtils.isNotBlank(proxyPort) && AgnUtils.isNumber(proxyPort)) {
					return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
				} else {
					return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, 8080));
				}
			}
		} else {
			boolean ignoreProxy = false;
			String urlDomain = getDomainFromUrl(url);
			for (String nonProxyHost : nonProxyHosts.split("\\|")) {
				nonProxyHost = nonProxyHost.trim();
				if (urlDomain == null || urlDomain.equalsIgnoreCase(url)) {
					ignoreProxy = true;
					break;
				}
			}
			if (!ignoreProxy) {
				if (StringUtils.isNotBlank(proxyHost)) {
					if (StringUtils.isNotBlank(proxyPort) && AgnUtils.isNumber(proxyPort)) {
						return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
					} else {
						return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, 8080));
					}
				}
			}
		}
	}
	
	return Proxy.NO_PROXY;
}
 
Example 14
Source File: SlackApi.java    From slack-webhook with MIT License 4 votes vote down vote up
public SlackApi(String service, int timeout) {
	this(service, timeout, Proxy.NO_PROXY);
}
 
Example 15
Source File: SocksSocketFactory.java    From hadoop-gpu with Apache License 2.0 4 votes vote down vote up
/**
 * Default empty constructor (for use with the reflection API).
 */
public SocksSocketFactory() {
  this.proxy = Proxy.NO_PROXY;
}
 
Example 16
Source File: ProxyConfiguration.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Create a Proxy instance based on proxy type, proxy server host and port.
 */
public Proxy createProxy() {
    return Proxy.Type.DIRECT.equals(proxyType) ? Proxy.NO_PROXY : new Proxy(proxyType, new InetSocketAddress(proxyServerHost, proxyServerPort));
}
 
Example 17
Source File: PriceDataGetterOnlineTest.java    From jeveassets with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Proxy getProxy() {
	return Proxy.NO_PROXY;
}
 
Example 18
Source File: SocksSocketFactory.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * Default empty constructor (for use with the reflection API).
 */
public SocksSocketFactory() {
  this.proxy = Proxy.NO_PROXY;
}
 
Example 19
Source File: Bot.java    From LambdaAttack with MIT License 4 votes vote down vote up
public Bot(Options options, UniversalProtocol account) {
    this(options, account, Proxy.NO_PROXY);
}
 
Example 20
Source File: ChartRepository.java    From microbean-helm with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link ChartRepository}.
 *
 * @param name the name of this {@link ChartRepository}; must not be
 * {@code null}
 *
 * @param uri the {@link URI} to the root of this {@link
 * ChartRepository}; must not be {@code null}
 *
 * @param archiveCacheDirectory an {@linkplain Path#isAbsolute()
 * absolute} {@link Path} representing a directory where Helm chart
 * archives may be stored; if {@code null} then a {@link Path}
 * beginning with the absolute directory represented by the value of
 * the {@code helm.home} system property, or the value of the {@code
 * HELM_HOME} environment variable, appended with {@code
 * cache/archive} will be used instead
 *
 * @param indexCacheDirectory an {@linkplain Path#isAbsolute()
 * absolute} {@link Path} representing a directory that the supplied
 * {@code cachedIndexPath} parameter value will be considered to be
 * relative to; <strong>will be ignored and hence may be {@code
 * null}</strong> if the supplied {@code cachedIndexPath} parameter
 * value {@linkplain Path#isAbsolute() is absolute}
 *
 * @param cachedIndexPath a {@link Path} naming the file that will
 * store a copy of the chart repository's {@code index.yaml} file;
 * if {@code null} then a {@link Path} relative to the absolute
 * directory represented by the value of the {@code helm.home}
 * system property, or the value of the {@code HELM_HOME}
 * environment variable, and bearing a name consisting of the
 * supplied {@code name} suffixed with {@code -index.yaml} will be
 * used instead
 *
 * @exception NullPointerException if either {@code name} or {@code
 * uri} is {@code null}
 *
 * @exception IllegalArgumentException if {@code uri} is {@linkplain
 * URI#isAbsolute() not absolute}, or if there is no existing "Helm
 * home" directory, or if {@code archiveCacheDirectory} is
 * non-{@code null} and either empty or not {@linkplain
 * Path#isAbsolute()}
 *
 * @see #ChartRepository(String, URI, Path, Path, Path, boolean,
 * Proxy)
 *
 * @see #getName()
 *
 * @see #getUri()
 *
 * @see #getCachedIndexPath()
 */
public ChartRepository(final String name,
                       final URI uri,
                       final Path archiveCacheDirectory,
                       final Path indexCacheDirectory,
                       final Path cachedIndexPath) {
  this(name, uri, archiveCacheDirectory, indexCacheDirectory, cachedIndexPath, false, Proxy.NO_PROXY);
}