1. /*
  2. * @(#)ProxySelector.java 1.3 03/08/09
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.IOException;
  9. import java.util.List;
  10. import sun.security.util.SecurityConstants;
  11. /**
  12. * Selects the proxy server to use, if any, when connecting to the
  13. * network resource referenced by a URL. A proxy selector is a
  14. * concrete sub-class of this class and is registered by invoking the
  15. * {@link java.net.ProxySelector#setDefault setDefault} method. The
  16. * currently registered proxy selector can be retrieved by calling
  17. * {@link java.net.ProxySelector#getDefault getDefault} method.
  18. *
  19. * <p> When a proxy selector is registered, for instance, a subclass
  20. * of URLConnection class should call the {@link #select select}
  21. * method for each URL request so that the proxy selector can decide
  22. * if a direct, or proxied connection should be used. The {@link
  23. * #select select} method returns an iterator over a collection with
  24. * the preferred connection approach.
  25. *
  26. * <p> If a connection cannot be established to a proxy (PROXY or
  27. * SOCKS) servers then the caller should call the proxy selector's
  28. * {@link #connectFailed connectFailed} method to notify the proxy
  29. * selector that the proxy server is unavailable. </p>
  30. *
  31. * @version 1.3, 03/08/09
  32. * @author Yingxian Wang
  33. * @author Jean-Christophe Collet
  34. * @since 1.5
  35. */
  36. public abstract class ProxySelector {
  37. /**
  38. * The system wide proxy selector that selects the proxy server to
  39. * use, if any, when connecting to a remote object referenced by
  40. * an URL.
  41. *
  42. * @see #setDefault(ProxySelector)
  43. */
  44. private static ProxySelector theProxySelector;
  45. static {
  46. try {
  47. Class c = Class.forName("sun.net.spi.DefaultProxySelector");
  48. if (c != null && ProxySelector.class.isAssignableFrom(c)) {
  49. theProxySelector = (ProxySelector) c.newInstance();
  50. }
  51. } catch (Exception e) {
  52. theProxySelector = null;
  53. }
  54. }
  55. /**
  56. * Gets the system-wide proxy selector.
  57. *
  58. * @throws SecurityException
  59. * If a security manager has been installed and it denies
  60. * {@link NetPermission}<tt>("getProxySelector")</tt>
  61. * @see #setDefault(ProxySelector)
  62. * @return the system-wide <code>ProxySelector</code>
  63. * @since 1.5
  64. */
  65. public static ProxySelector getDefault() {
  66. SecurityManager sm = System.getSecurityManager();
  67. if (sm != null) {
  68. sm.checkPermission(SecurityConstants.GET_PROXYSELECTOR_PERMISSION);
  69. }
  70. return theProxySelector;
  71. }
  72. /**
  73. * Sets (or unsets) the system-wide proxy selector.
  74. *
  75. * Note: non-standard procotol handlers may ignore this setting.
  76. *
  77. * @param ps The HTTP proxy selector, or
  78. * <code>null</code> to unset the proxy selector.
  79. *
  80. * @throws SecurityException
  81. * If a security manager has been installed and it denies
  82. * {@link NetPermission}<tt>("setProxySelector")</tt>
  83. *
  84. * @see #getDefault()
  85. * @since 1.5
  86. */
  87. public static void setDefault(ProxySelector ps) {
  88. SecurityManager sm = System.getSecurityManager();
  89. if (sm != null) {
  90. sm.checkPermission(SecurityConstants.SET_PROXYSELECTOR_PERMISSION);
  91. }
  92. theProxySelector = ps;
  93. }
  94. /**
  95. * Selects all the applicable proxies based on the protocol to
  96. * access the resource with and a destination address to access
  97. * the resource at.
  98. * The format of the URI is defined as follow:
  99. * <UL>
  100. * <LI>http URI for http connections</LI>
  101. * <LI>https URI for https connections
  102. * <LI>ftp URI for ftp connections</LI>
  103. * <LI><code>socket://host:port</code><br>
  104. * for tcp client sockets connections</LI>
  105. * </UL>
  106. *
  107. * @param uri
  108. * The URI that a connection is required to
  109. *
  110. * @return a List of Proxies. Each element in the
  111. * the List is of type
  112. * {@link java.net.Proxy Proxy};
  113. * when no proxy is available, the list will
  114. * contain one element of type
  115. * {@link java.net.Proxy Proxy}
  116. * that represents a direct connection.
  117. * @throws IllegalArgumentException if either argument is null
  118. */
  119. public abstract List<Proxy> select(URI uri);
  120. /**
  121. * Called to indicate that a connection could not be established
  122. * to a proxy/socks server. An implementation of this method can
  123. * temporarily remove the proxies or reorder the sequence of
  124. * proxies returned by select(String, String), using the address
  125. * and they kind of IOException given.
  126. *
  127. * @param uri
  128. * The URI that the proxy at sa failed to serve.
  129. * @param sa
  130. * The socket address of the proxy/SOCKS server
  131. *
  132. * @param ioe
  133. * The I/O exception thrown when the connect failed.
  134. * @throws IllegalArgumentException if either argument is null
  135. */
  136. public abstract void connectFailed(URI uri, SocketAddress sa, IOException ioe);
  137. }