1. /*
  2. * @(#)Proxy.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. /**
  9. * This class represents a proxy setting, typically a type (http, socks) and
  10. * a socket address.
  11. * A <code>Proxy</code> is an immutable object.
  12. *
  13. * @version 1.3, 08/09/03
  14. * @see java.net.ProxySelector
  15. * @author Yingxian Wang
  16. * @author Jean-Christophe Collet
  17. * @since 1.5
  18. */
  19. public class Proxy {
  20. /**
  21. * Represents the proxy type.
  22. *
  23. * @since 1.5
  24. */
  25. public enum Type {
  26. /**
  27. * Represents a direct connection, or the absence of a proxy.
  28. */
  29. DIRECT,
  30. /**
  31. * Represents proxy for high level protocols such as HTTP or FTP.
  32. */
  33. HTTP,
  34. /**
  35. * Represents a SOCKS (V4 or V5) proxy.
  36. */
  37. SOCKS
  38. };
  39. private Type type;
  40. private SocketAddress sa;
  41. /**
  42. * A proxy setting that represents a <code>DIRECT</code> connection,
  43. * basically telling the protocol handler not to use any proxying.
  44. * Used, for instance, to create sockets bypassing any other global
  45. * proxy settings (like SOCKS):
  46. * <P>
  47. * <code>Socket s = new Socket(Proxy.NO_PROXY);</code><br>
  48. * <P>
  49. */
  50. public final static Proxy NO_PROXY = new Proxy();
  51. // Creates the proxy that represents a <code>DIRECT</code> connection.
  52. private Proxy() {
  53. type = type.DIRECT;
  54. sa = null;
  55. }
  56. /**
  57. * Creates an entry representing a PROXY connection.
  58. * Certain combinations are illegal. For instance, for types Http, and
  59. * Socks, a SocketAddress <b>must</b> be provided.
  60. * <P>
  61. * Use the <code>Proxy.NO_PROXY</code> constant
  62. * for representing a direct connection.
  63. *
  64. * @param type the <code>Type</code> of the proxy
  65. * @param sa the <code>SocketAddress</code> for that proxy
  66. * @throws IllegalArgumentException when the type and the address are
  67. * incompatible
  68. */
  69. public Proxy(Type type, SocketAddress sa) {
  70. if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
  71. throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);
  72. this.type = type;
  73. this.sa = sa;
  74. }
  75. /**
  76. * Returns the proxy type.
  77. *
  78. * @return a Type representing the proxy type
  79. */
  80. public Type type() {
  81. return type;
  82. }
  83. /**
  84. * Returns the socket address of the proxy, or
  85. * <code>null</code> if its a direct connection.
  86. *
  87. * @return a <code>SocketAddress</code> representing the socket end
  88. * point of the proxy
  89. */
  90. public SocketAddress address() {
  91. return sa;
  92. }
  93. /**
  94. * Constructs a string representation of this Proxy.
  95. * This String is constructed by calling toString() on its type
  96. * and concatenating the toString() result from its address if any.
  97. *
  98. * @return a string representation of this object.
  99. */
  100. public String toString() {
  101. if (type() == Type.DIRECT)
  102. return "DIRECT";
  103. return type() + " @ " + address();
  104. }
  105. /**
  106. * Compares this object against the specified object.
  107. * The result is <code>true</code> if and only if the argument is
  108. * not <code>null</code> and it represents the same proxy as
  109. * this object.
  110. * <p>
  111. * Two instances of <code>Proxy</code> represent the same
  112. * address if both the SocketAddresses and type are equal.
  113. *
  114. * @param obj the object to compare against.
  115. * @return <code>true</code> if the objects are the same;
  116. * <code>false</code> otherwise.
  117. * @see java.net.InetSocketAddress#equals(java.lang.Object)
  118. */
  119. public final boolean equals(Object obj) {
  120. if (obj == null || !(obj instanceof Proxy))
  121. return false;
  122. Proxy p = (Proxy) obj;
  123. if (p.type() == type()) {
  124. if (address() == null) {
  125. return (p.address() == null);
  126. } else
  127. return address().equals(p.address());
  128. }
  129. return false;
  130. }
  131. /**
  132. * Returns a hashcode for this Proxy.
  133. *
  134. * @return a hash code value for this Proxy.
  135. */
  136. public final int hashCode() {
  137. if (address() == null)
  138. return type().hashCode();
  139. return type().hashCode() + address().hashCode();
  140. }
  141. }