View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java,v 1.14 2004/04/18 23:51:35 jsdever Exp $
3    * $Revision: 326707 $
4    * $Date: 2005-10-19 16:51:46 -0400 (Wed, 19 Oct 2005) $
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;
31  
32  import org.apache.commons.httpclient.util.LangUtils;
33  
34  /***
35   * <p>Username and password {@link Credentials}.</p>
36   *
37   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
38   * @author Sean C. Sullivan
39   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
40   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
41   * 
42   * @version $Revision: 326707 $ $Date: 2005-10-19 16:51:46 -0400 (Wed, 19 Oct 2005) $
43   * 
44   */
45  public class UsernamePasswordCredentials implements Credentials {
46  
47      // ----------------------------------------------------------- Constructors
48  
49      /***
50       * Default constructor.
51       * 
52       * @deprecated Do not use. Null user name no longer allowed
53       */
54      public UsernamePasswordCredentials() {
55          super();
56      }
57  
58  
59      /***
60       * The constructor with the username and password combined string argument.
61       *
62       * @param usernamePassword the username:password formed string
63       * @see #toString
64       */
65      public UsernamePasswordCredentials(String usernamePassword) {
66          super();
67          if (usernamePassword == null) {
68              throw new IllegalArgumentException("Username:password string may not be null");            
69          }
70          int atColon = usernamePassword.indexOf(':');
71          if (atColon >= 0) {
72              this.userName = usernamePassword.substring(0, atColon);
73              this.password = usernamePassword.substring(atColon + 1);
74          } else {
75              this.userName = usernamePassword;
76          }
77      }
78  
79  
80      /***
81       * The constructor with the username and password arguments.
82       *
83       * @param userName the user name
84       * @param password the password
85       */
86      public UsernamePasswordCredentials(String userName, String password) {
87          super();
88          if (userName == null) {
89              throw new IllegalArgumentException("Username may not be null");            
90          }
91          this.userName = userName;
92          this.password = password;
93      }
94  
95      // ----------------------------------------------------- Instance Variables
96  
97      /***
98       * User name.
99       */
100     private String userName;
101 
102 
103     /***
104      * Password.
105      */
106     private String password;
107 
108 
109     // ------------------------------------------------------------- Properties
110 
111 
112     /***
113      * User name property setter. User name may not be null.
114      *
115      * @param userName
116      * @see #getUserName()
117      * 
118      * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
119      */
120     public void setUserName(String userName) {
121         if (userName == null) {
122             throw new IllegalArgumentException("Username may not be null");            
123         }
124         this.userName = userName;
125     }
126 
127 
128     /***
129      * User name property getter.
130      *
131      * @return the userName
132      * @see #setUserName(String)
133      */
134     public String getUserName() {
135         return userName;
136     }
137 
138 
139     /***
140      * Password property setter.
141      *
142      * @param password
143      * @see #getPassword()
144      * 
145      * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
146      */
147     public void setPassword(String password) {
148         this.password = password;
149     }
150 
151 
152     /***
153      * Password property getter.
154      *
155      * @return the password
156      * @see #setPassword(String)
157      */
158     public String getPassword() {
159         return password;
160     }
161 
162     
163     /***
164      * Get this object string.
165      *
166      * @return the username:password formed string
167      */
168     public String toString() {
169         StringBuffer result = new StringBuffer();
170         result.append(this.userName);
171         result.append(":");
172         result.append((this.password == null) ? "null" : this.password);
173         return result.toString();
174     }
175 
176     /***
177      * Does a hash of both user name and password.
178      *
179      * @return The hash code including user name and password.
180      */
181     public int hashCode() {
182         int hash = LangUtils.HASH_SEED;
183         hash = LangUtils.hashCode(hash, this.userName);
184         hash = LangUtils.hashCode(hash, this.password);
185         return hash;
186     }
187 
188     /***
189      * These credentials are assumed equal if the username and password are the
190      * same.
191      *
192      * @param o The other object to compare with.
193      *
194      * @return  <code>true</code> if the object is equivalent.
195      */
196     public boolean equals(Object o) {
197         if (o == null) return false;
198         if (this == o) return true;
199         // note - to allow for sub-classing, this checks that class is the same
200         // rather than do "instanceof".
201         if (this.getClass().equals(o.getClass())) {
202             UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
203 
204             if (LangUtils.equals(this.userName, that.userName)
205                     && LangUtils.equals(this.password, that.password) ) {
206                 return true;
207             }
208         }
209         return false;
210     }
211 
212 }
213