View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/HttpAuthRealm.java,v 1.3.2.2 2004/02/22 18:21:15 olegk Exp $
3    * $Revision: 1.3.2.2 $
4    * $Date: 2004/02/22 18:21:15 $
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   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.auth;
33  
34  /*** The key used to look up authentication credentials.
35   * 
36   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
37   * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
38   */
39  public class HttpAuthRealm {
40      
41      /*** The realm the credentials apply to. */
42      private String realm = null;
43      
44      /*** The domain the credentials apply to. */
45      private String domain = null;
46          
47      /*** Creates a new HttpAuthRealm for the given <tt>domain</tt> and 
48       * <tt>realm</tt>.
49       * 
50       * @param domain the domain the credentials apply to. May be set
51       *   to <tt>null</tt> if credenticals are applicable to
52       *   any domain. 
53       * @param realm the realm the credentials apply to. May be set 
54       *   to <tt>null</tt> if credenticals are applicable to
55       *   any realm. 
56       *   
57       */
58      public HttpAuthRealm(final String domain, final String realm) {
59          this.domain = domain;
60          this.realm = realm;
61      }
62      
63      /*** Determines if the given domains match.  Note that <tt>null</tt> acts as a
64       * wildcard so if either of the domains are <tt>null</tt>, it is considered a match.
65       * 
66       * @param d1 the domain
67       * @param d2 the other domain
68       * @return boolean true if the domains match, otherwise false.
69       */
70      private static boolean domainAttribMatch(final String d1, final String d2) {
71          return d1 == null || d2 == null || d1.equalsIgnoreCase(d2);
72      }
73  
74      /*** Determines if the given realms match.  Note that <tt>null</tt> acts as a
75       * wildcard so if either realm is <tt>null</tt>, this function will return <tt>true</tt>.
76       * 
77       * @param r1 the realm
78       * @param r2 the other realm
79       * @return boolean true if the realms match, otherwise false.
80       */ 
81      private static boolean realmAttribMatch(final String r1, final String r2) {
82          return r1 == null || r2 == null || r1.equals(r2);
83      }
84  
85  
86      /***
87       * @see java.lang.Object#equals(Object)
88       */
89      public boolean equals(Object o) {
90          if (o == null) {
91              return false;
92          }
93          if (o == this) {
94              return true;
95          }
96          if (!(o instanceof HttpAuthRealm)) {
97              return super.equals(o);
98          }
99          HttpAuthRealm that = (HttpAuthRealm) o;
100         return 
101           domainAttribMatch(this.domain, that.domain) 
102           && realmAttribMatch(this.realm, that.realm);
103     }
104 
105     /***
106      * @see java.lang.Object#toString()
107      */
108     public String toString() {
109         StringBuffer buffer = new StringBuffer();
110         buffer.append("Authentication domain: '");
111         buffer.append(this.domain);
112         buffer.append("', authentication realm: '");
113         buffer.append(this.realm);
114         buffer.append("'");
115         return buffer.toString();
116     }
117     /***
118      * @see java.lang.Object#hashCode()
119      */
120     public int hashCode() {
121         StringBuffer buffer = new StringBuffer();
122         buffer.append(this.domain);
123         buffer.append(this.realm);
124         return buffer.toString().hashCode();
125     }
126 
127 }