View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/HttpAuthRealm.java,v 1.8 2004/05/13 04:02:00 mbecke Exp $
3    * $Revision: 1.8 $
4    * $Date: 2004/05/13 04:02:00 $
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  
30  package org.apache.commons.httpclient.auth;
31  
32  /*** The key used to look up authentication credentials.
33   * 
34   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
35   * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
36   */
37  public class HttpAuthRealm {
38      
39      /*** The authentication scheme the credentials apply to. */
40      private String scheme = null;
41      
42      /*** The realm the credentials apply to. */
43      private String realm = null;
44      
45      /*** The host the credentials apply to. */
46      private String host = null;
47          
48      /*** The port the credentials apply to. */
49      private int port = -1;
50          
51      /*** Creates a new authentication realm token for the given 
52       * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and 
53       * <tt>authentication scheme</tt>.
54       * 
55       * @param host the host the credentials apply to. May be set
56       *   to <tt>null</tt> if credenticals are applicable to
57       *   any host. 
58       * @param port the port the credentials apply to. May be set
59       *   to negative value if credenticals are applicable to
60       *   any port. 
61       * @param realm the realm the credentials apply to. May be set 
62       *   to <tt>null</tt> if credenticals are applicable to
63       *   any realm. 
64       * @param scheme the authentication scheme the credentials apply to. 
65       *   May be set to <tt>null</tt> if credenticals are applicable to
66       *   any authentication scheme. 
67       * 
68       * @since 3.0
69       */
70      public HttpAuthRealm(final String host, int port, 
71          final String realm, final String scheme)
72      {
73          this.host = host;
74          this.port = port;
75          this.realm = realm;
76          this.scheme = scheme;
77      }
78      
79      /*** Creates a new authentication realm token  for the given 
80       * <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
81       * authentication scheme.
82       * 
83       * @param host the host the credentials apply to. May be set
84       *   to <tt>null</tt> if credenticals are applicable to
85       *   any host. 
86       * @param port the port the credentials apply to. May be set
87       *   to negative value if credenticals are applicable to
88       *   any port. 
89       * @param realm the realm the credentials apply to. May be set 
90       *   to <tt>null</tt> if credenticals are applicable to
91       *   any realm. 
92       * 
93       * @since 3.0
94       */
95      public HttpAuthRealm(final String host, int port, final String realm) {
96          this(host, port, realm, null);
97      }
98      
99      /*** Creates a new authentication realm token for the given 
100      * <tt>host</tt>, <tt>port</tt>, any realm name, 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      * 
110      * @since 3.0
111      */
112     public HttpAuthRealm(final String host, int port) {
113         this(host, port, null, null);
114     }
115     
116     /*** Creates a new authentication realm token  for the given 
117      * <tt>host</tt>, <tt>realm</tt>, any port, and any authentication
118      * scheme. 
119      * 
120      * @param host the host the credentials apply to. May be set
121      *   to <tt>null</tt> if credenticals are applicable to
122      *   any host. 
123      * @param realm the realm the credentials apply to. May be set 
124      *   to <tt>null</tt> if credenticals are applicable to
125      *   any realm. 
126      */
127     public HttpAuthRealm(final String host, final String realm) {
128         this(host, -1, realm, null);
129     }
130     
131     /*** 
132      * Creates a new authentication realm token that matches any 
133      * authentication realm.
134      * 
135      * @since 3.0
136      */
137     public HttpAuthRealm() {
138         this(null, -1, null, null);
139     }
140     
141     /*** 
142      * Creates a copy of the given authentication realm token.
143      * 
144      * @since 3.0
145      */
146     public HttpAuthRealm(final HttpAuthRealm token) {
147         this(token.host, token.port, token.realm, token.scheme);
148     }
149     
150     /***
151      * @return the host
152      * 
153      * @since 3.0
154      */
155     public String getHost() {
156         return this.host;
157     }
158 
159     /***
160      * @return the port
161      * 
162      * @since 3.0
163      */
164     public int getPort() {
165         return this.port;
166     }
167 
168     /***
169      * @return the realm name
170      * 
171      * @since 3.0
172      */
173     public String getRealm() {
174         return this.realm;
175     }
176 
177     /***
178      * @return the scheme type
179      * 
180      * @since 3.0
181      */
182     public String getScheme() {
183         return this.scheme;
184     }
185 
186     /*** Determines if the given parameters match.  Note that <tt>null</tt> acts as a
187      * wildcard so if either of the parameters are <tt>null</tt>, it is considered a match.
188      * 
189      * @param p1 the parameter
190      * @param p2 the other parameter
191      * @return boolean true if the parameters match, otherwise false.
192      */
193     private static boolean paramsMatchIgnoreCase(final String p1, final String p2) {
194         return p1 == null || p2 == null || p1.equalsIgnoreCase(p2);
195     }
196 
197     /*** Determines if the given parameters match.  Note that <tt>null</tt> acts as a
198      * wildcard so if either of the parameters are <tt>null</tt>, it is considered a match.
199      * 
200      * @param p1 the parameter
201      * @param p2 the other parameter
202      * @return boolean true if the parameters match, otherwise false.
203      */
204     private static boolean paramsMatch(final String p1, final String p2) {
205         return p1 == null || p2 == null || p1.equals(p2);
206     }
207 
208     /*** Determines if the given parameters match.  Note that negative value acts as a
209      * wildcard so if either of the parameters are negative, it is considered a match.
210      * 
211      * @param p1 the parameter
212      * @param p2 the other parameter
213      * @return boolean true if the parameters match, otherwise false.
214      */
215     private static boolean paramsMatch(int p1, int p2) {
216         return p1 < 0 || p2 < 0 || p1 == p2;
217     }
218 
219     /***
220      * @see java.lang.Object#equals(Object)
221      */
222     public boolean equals(Object o) {
223         if (o == null) {
224             return false;
225         }
226         if (o == this) {
227             return true;
228         }
229         if (!(o instanceof HttpAuthRealm)) {
230             return super.equals(o);
231         }
232         HttpAuthRealm that = (HttpAuthRealm) o;
233         return 
234         paramsMatchIgnoreCase(this.host, that.host) 
235           && paramsMatch(this.port, that.port)
236           && paramsMatch(this.realm, that.realm)
237           && paramsMatchIgnoreCase(this.scheme, that.scheme);
238     }
239 
240     /***
241      * @see java.lang.Object#toString()
242      */
243     public String toString() {
244         StringBuffer buffer = new StringBuffer();
245         if (this.scheme != null) {
246             buffer.append(this.scheme);
247             buffer.append(' ');
248         }
249         if (this.realm != null) {
250             buffer.append("authentication realm '");
251             buffer.append(this.realm);
252             buffer.append("'");
253         } else {
254             buffer.append("default authentication realm ");
255         }
256         if (this.host != null) {
257             buffer.append('@');
258             buffer.append(this.host);
259             if (this.port >= 0) {
260                 buffer.append(':');
261                 buffer.append(this.port);
262             }
263         }
264         return buffer.toString();
265     }
266     /***
267      * @see java.lang.Object#hashCode()
268      */
269     public int hashCode() {
270         return ((this.host != null) ? this.host.toLowerCase().hashCode() : 0) + 
271                ((this.port >= 0) ? this.port : -1) +
272                ((this.realm != null) ? this.realm.hashCode() : 0) +
273                ((this.scheme != null) ? this.scheme.toLowerCase().hashCode() : 0);
274     }
275 }