1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthScope.java,v 1.2 2004/06/23 06:50:25 olegk Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2004/06/23 06:50:25 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.auth;
  30. /**
  31. * The class represents an authentication scope consisting of a host name,
  32. * a port number, a realm name and an authentication scheme name which
  33. * {@link org.apache.commons.httpclient.Credentials} apply to.
  34. *
  35. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  36. * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
  37. *
  38. * @since 3.0
  39. */
  40. public class AuthScope {
  41. /**
  42. * The <tt>null</tt> value represents any host. In the future versions of
  43. * HttpClient the use of this parameter will be discontinued.
  44. */
  45. public static final String ANY_HOST = null;
  46. /**
  47. * The <tt>-1</tt> value represents any port.
  48. */
  49. public static final int ANY_PORT = -1;
  50. /**
  51. * The <tt>null</tt> value represents any realm.
  52. */
  53. public static final String ANY_REALM = null;
  54. /**
  55. * The <tt>null</tt> value represents any authentication scheme.
  56. */
  57. public static final String ANY_SCHEME = null;
  58. /**
  59. * Default scope matching any host, port, realm and authentication scheme.
  60. * In the future versions of HttpClient the use of this parameter will be
  61. * discontinued.
  62. */
  63. public static AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
  64. /** The authentication scheme the credentials apply to. */
  65. private String scheme = null;
  66. /** The realm the credentials apply to. */
  67. private String realm = null;
  68. /** The host the credentials apply to. */
  69. private String host = null;
  70. /** The port the credentials apply to. */
  71. private int port = -1;
  72. /** Creates a new credentials scope for the given
  73. * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
  74. * <tt>authentication scheme</tt>.
  75. *
  76. * @param host the host the credentials apply to. May be set
  77. * to <tt>null</tt> if credenticals are applicable to
  78. * any host.
  79. * @param port the port the credentials apply to. May be set
  80. * to negative value if credenticals are applicable to
  81. * any port.
  82. * @param realm the realm the credentials apply to. May be set
  83. * to <tt>null</tt> if credenticals are applicable to
  84. * any realm.
  85. * @param scheme the authentication scheme the credentials apply to.
  86. * May be set to <tt>null</tt> if credenticals are applicable to
  87. * any authentication scheme.
  88. *
  89. * @since 3.0
  90. */
  91. public AuthScope(final String host, int port,
  92. final String realm, final String scheme)
  93. {
  94. this.host = (host == null) ? ANY_HOST: host.toLowerCase();
  95. this.port = (port < 0) ? ANY_PORT: port;
  96. this.realm = (realm == null) ? ANY_REALM: realm;
  97. this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase();;
  98. }
  99. /** Creates a new credentials scope for the given
  100. * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
  101. * authentication scheme.
  102. *
  103. * @param host the host the credentials apply to. May be set
  104. * to <tt>null</tt> if credenticals are applicable to
  105. * any host.
  106. * @param port the port the credentials apply to. May be set
  107. * to negative value if credenticals are applicable to
  108. * any port.
  109. * @param realm the realm the credentials apply to. May be set
  110. * to <tt>null</tt> if credenticals are applicable to
  111. * any realm.
  112. *
  113. * @since 3.0
  114. */
  115. public AuthScope(final String host, int port, final String realm) {
  116. this(host, port, realm, ANY_SCHEME);
  117. }
  118. /** Creates a new credentials scope for the given
  119. * <tt>host</tt>, <tt>port</tt>, any realm name, and any
  120. * authentication scheme.
  121. *
  122. * @param host the host the credentials apply to. May be set
  123. * to <tt>null</tt> if credenticals are applicable to
  124. * any host.
  125. * @param port the port the credentials apply to. May be set
  126. * to negative value if credenticals are applicable to
  127. * any port.
  128. *
  129. * @since 3.0
  130. */
  131. public AuthScope(final String host, int port) {
  132. this(host, port, ANY_REALM, ANY_SCHEME);
  133. }
  134. /**
  135. * Creates a copy of the given credentials scope.
  136. *
  137. * @since 3.0
  138. */
  139. public AuthScope(final AuthScope authscope) {
  140. super();
  141. if (authscope == null) {
  142. throw new IllegalArgumentException("Scope may not be null");
  143. }
  144. this.host = authscope.getHost();
  145. this.port = authscope.getPort();
  146. this.realm = authscope.getRealm();
  147. this.scheme = authscope.getScheme();
  148. }
  149. /**
  150. * @return the host
  151. *
  152. * @since 3.0
  153. */
  154. public String getHost() {
  155. return this.host;
  156. }
  157. /**
  158. * @return the port
  159. *
  160. * @since 3.0
  161. */
  162. public int getPort() {
  163. return this.port;
  164. }
  165. /**
  166. * @return the realm name
  167. *
  168. * @since 3.0
  169. */
  170. public String getRealm() {
  171. return this.realm;
  172. }
  173. /**
  174. * @return the scheme type
  175. *
  176. * @since 3.0
  177. */
  178. public String getScheme() {
  179. return this.scheme;
  180. }
  181. /** Determines if the given parameters are equal.
  182. *
  183. * @param p1 the parameter
  184. * @param p2 the other parameter
  185. * @return boolean true if the parameters are equal, otherwise false.
  186. */
  187. private static boolean paramsEqual(final String p1, final String p2) {
  188. if (p1 == null) {
  189. return p1 == p2;
  190. } else {
  191. return p1.equals(p2);
  192. }
  193. }
  194. /** Determines if the given parameters are equal.
  195. *
  196. * @param p1 the parameter
  197. * @param p2 the other parameter
  198. * @return boolean true if the parameters are equal, otherwise false.
  199. */
  200. private static boolean paramsEqual(int p1, int p2) {
  201. return p1 == p2;
  202. }
  203. /**
  204. * Tests if the authentication scopes match.
  205. *
  206. * @return the match factor. Negative value signifies no match.
  207. * Non-negative signifies a match. The greater the returned value
  208. * the closer the match.
  209. *
  210. * @since 3.0
  211. */
  212. public int match(final AuthScope that) {
  213. int factor = 0;
  214. if (paramsEqual(this.scheme, that.scheme)) {
  215. factor += 1;
  216. } else {
  217. if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
  218. return -1;
  219. }
  220. }
  221. if (paramsEqual(this.realm, that.realm)) {
  222. factor += 2;
  223. } else {
  224. if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
  225. return -1;
  226. }
  227. }
  228. if (paramsEqual(this.port, that.port)) {
  229. factor += 4;
  230. } else {
  231. if (this.port != ANY_PORT && that.port != ANY_PORT) {
  232. return -1;
  233. }
  234. }
  235. if (paramsEqual(this.host, that.host)) {
  236. factor += 8;
  237. } else {
  238. if (this.host != ANY_HOST && that.host != ANY_HOST) {
  239. return -1;
  240. }
  241. }
  242. return factor;
  243. }
  244. /**
  245. * @see java.lang.Object#equals(Object)
  246. */
  247. public boolean equals(Object o) {
  248. if (o == null) {
  249. return false;
  250. }
  251. if (o == this) {
  252. return true;
  253. }
  254. if (!(o instanceof AuthScope)) {
  255. return super.equals(o);
  256. }
  257. AuthScope that = (AuthScope) o;
  258. return
  259. paramsEqual(this.host, that.host)
  260. && paramsEqual(this.port, that.port)
  261. && paramsEqual(this.realm, that.realm)
  262. && paramsEqual(this.scheme, that.scheme);
  263. }
  264. /**
  265. * @see java.lang.Object#toString()
  266. */
  267. public String toString() {
  268. StringBuffer buffer = new StringBuffer();
  269. if (this.scheme != null) {
  270. buffer.append(this.scheme.toUpperCase());
  271. buffer.append(' ');
  272. }
  273. if (this.realm != null) {
  274. buffer.append('\'');
  275. buffer.append(this.realm);
  276. buffer.append('\'');
  277. } else {
  278. buffer.append("<any realm>");
  279. }
  280. if (this.host != null) {
  281. buffer.append('@');
  282. buffer.append(this.host);
  283. if (this.port >= 0) {
  284. buffer.append(':');
  285. buffer.append(this.port);
  286. }
  287. }
  288. return buffer.toString();
  289. }
  290. /**
  291. * @see java.lang.Object#hashCode()
  292. */
  293. public int hashCode() {
  294. return ((this.host != null) ? this.host.toLowerCase().hashCode() : 0) +
  295. ((this.port >= 0) ? this.port : -1) +
  296. ((this.realm != null) ? this.realm.hashCode() : 0) +
  297. ((this.scheme != null) ? this.scheme.toLowerCase().hashCode() : 0);
  298. }
  299. }