RSS Feed for This PostCurrent Article

Java: Detect and Use System Proxy

Here is a Java class that you use to detect and use the system proxy, or direct connection is available to the Internet.

Basically, it uses java.net.useSystemProxies property which is available starting JDK 5.

   1: package com.cdp.proxy.plugins;
   2:  
   3:  
   4: import java.net.InetSocketAddress;
   5: import java.net.Proxy;
   6: import java.net.ProxySelector;
   7: import java.net.SocketAddress;
   8: import java.util.List;
   9:  
  10: /**
  11:  * Detecting and selecting a proxy
  12:  *
  13:  */
  14: public class ProxyDetector {
  15:  
  16:     private static final String PROXY_PROPERTY = "java.net.useSystemProxies";
  17:     private final List<Proxy> proxies;
  18:     private final Proxy proxyToUse;
  19:  
  20:     /**
  21:      * No instances
  22:      */
  23:     private ProxyDetector() {
  24:         this.proxies = initProxies();
  25:         this.proxyToUse = determineProxy();
  26:     }
  27:  
  28:     /**
  29:      * ProxyDetectorHolder is loaded on the first execution of ProxyDetector.getInstance()
  30:      * or the first access to ProxyDetectorHolder.INSTANCE, not before.
  31:      */
  32:     private static class ProxyDetectorHolder {
  33:         private static final ProxyDetector INSTANCE = new ProxyDetector();
  34:     }
  35:  
  36:     /**
  37:      * @return the instance
  38:      */
  39:     public static ProxyDetector getInstance() {
  40:         return ProxyDetectorHolder.INSTANCE;
  41:     }
  42:  
  43:     /**
  44:      * Find the proxy, use the property <code>java.net.useSystemProxies</code> to force
  45:      * the usage of the system proxy. The value of this setting is restored afterwards.
  46:      *
  47:      * @return a list of found proxies
  48:      */
  49:     private List<Proxy> initProxies() {
  50:         final String valuePropertyBefore = System.getProperty(PROXY_PROPERTY);
  51:         try {
  52:             System.setProperty(PROXY_PROPERTY, "true");
  53:             return ProxySelector.getDefault().select(new java.net.URI("http://www.google.com"));
  54:         } catch (Exception e) {
  55:             // As ProxyDetector is the initial code being executed in main,
  56:             // we cannot afford any failure. This will make our entire JStock
  57:             // application crash.
  58:             // throw new RuntimeException(e);
  59:             System.out.println(e.getMessage());
  60:         } finally {
  61:             if (valuePropertyBefore != null) {
  62:                 System.setProperty(PROXY_PROPERTY, valuePropertyBefore);
  63:             }
  64:         }
  65:         return java.util.Collections.EMPTY_LIST;
  66:     }
  67:  
  68:     /**
  69:      * Is there a direct connection available? If I return <tt>true</tt> it is not
  70:      * necessary to detect a proxy address.
  71:      *
  72:      * @return <tt>true</tt> if the is a direct connection to the internet
  73:      */
  74:     public boolean directConnectionAvailable() {
  75:         for (Proxy proxy : this.proxies) {
  76:             if (Proxy.NO_PROXY.equals(proxy)) {
  77:                 return true;
  78:             }
  79:         }
  80:         return false;
  81:     }
  82:  
  83:     /**
  84:      * @return did we detect a proxy?
  85:      */
  86:     public boolean proxyDetected() {
  87:         return this.proxyToUse != null;
  88:     }
  89:  
  90:     /**
  91:      * I will determine the right proxy, there might be several proxies
  92:      * available, but some might not support the HTTP protocol.
  93:      *
  94:      * @return a proxy which can be used to access the given url, <tt>null</tt>
  95:      * if there is no proxy which supports HTTP.
  96:      */
  97:     private Proxy determineProxy() {
  98:         if (!directConnectionAvailable()) {
  99:             for (Proxy proxy : this.proxies) {
 100:                 if (proxy.type().equals(Proxy.Type.HTTP)) {
 101:                     return proxy;
 102:                 }
 103:             }
 104:         }
 105:         return null;
 106:     }
 107:  
 108:     /**
 109:      * @return a String representing the hostname of the proxy, <tt>null</tt> if there is no proxy
 110:      */
 111:     public String getHostname() {
 112:         if (this.proxyToUse != null) {
 113:             final SocketAddress socketAddress = this.proxyToUse.address();
 114:             if (socketAddress instanceof InetSocketAddress) {
 115:                 InetSocketAddress address = (InetSocketAddress) socketAddress;
 116:                 return address.getHostName();
 117:             }
 118:         }
 119:         return null;
 120:     }
 121:  
 122:     /**
 123:      * @return the port of the proxy, <tt>-1</tt> if there is no proxy
 124:      */
 125:     public int getPort() {
 126:         if (this.proxyToUse != null) {
 127:             final SocketAddress socketAddress = this.proxyToUse.address();
 128:             if (socketAddress instanceof InetSocketAddress) {
 129:                 InetSocketAddress address = (InetSocketAddress) socketAddress;
 130:                 return address.getPort();
 131:             }
 132:         }
 133:         return -1;
 134:     }
 135: }


Trackback URL


RSS Feed for This Post1 Comment(s)

  1. Filipe | Dec 17, 2009 | Reply

    Is it fail to detect domains where proxy shoud not be used? I tried something like this a time ago and it does not work for exceptions.

    Proxy exceptions example: “localhost|127.0.0.1|*.mycompany.com”

    It looks like O.S. does not share this values, or JVM is not able to read them.

    Do you have a solution for this issue?

    Tks!

Sorry, comments for this entry are closed at this time.