001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import org.apache.commons.httpclient.util.LangUtils;
034:
035: /**
036: * <p>Username and password {@link Credentials}.</p>
037: *
038: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
039: * @author Sean C. Sullivan
040: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
041: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
042: *
043: * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
044: *
045: */
046: public class UsernamePasswordCredentials implements Credentials {
047:
048: // ----------------------------------------------------------- Constructors
049:
050: /**
051: * Default constructor.
052: *
053: * @deprecated Do not use. Null user name no longer allowed
054: */
055: public UsernamePasswordCredentials() {
056: super ();
057: }
058:
059: /**
060: * The constructor with the username and password combined string argument.
061: *
062: * @param usernamePassword the username:password formed string
063: * @see #toString
064: */
065: public UsernamePasswordCredentials(String usernamePassword) {
066: super ();
067: if (usernamePassword == null) {
068: throw new IllegalArgumentException(
069: "Username:password string may not be null");
070: }
071: int atColon = usernamePassword.indexOf(':');
072: if (atColon >= 0) {
073: this .userName = usernamePassword.substring(0, atColon);
074: this .password = usernamePassword.substring(atColon + 1);
075: } else {
076: this .userName = usernamePassword;
077: }
078: }
079:
080: /**
081: * The constructor with the username and password arguments.
082: *
083: * @param userName the user name
084: * @param password the password
085: */
086: public UsernamePasswordCredentials(String userName, String password) {
087: super ();
088: if (userName == null) {
089: throw new IllegalArgumentException(
090: "Username may not be null");
091: }
092: this .userName = userName;
093: this .password = password;
094: }
095:
096: // ----------------------------------------------------- Instance Variables
097:
098: /**
099: * User name.
100: */
101: private String userName;
102:
103: /**
104: * Password.
105: */
106: private String password;
107:
108: // ------------------------------------------------------------- Properties
109:
110: /**
111: * User name property setter. User name may not be null.
112: *
113: * @param userName
114: * @see #getUserName()
115: *
116: * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
117: */
118: public void setUserName(String userName) {
119: if (userName == null) {
120: throw new IllegalArgumentException(
121: "Username may not be null");
122: }
123: this .userName = userName;
124: }
125:
126: /**
127: * User name property getter.
128: *
129: * @return the userName
130: * @see #setUserName(String)
131: */
132: public String getUserName() {
133: return userName;
134: }
135:
136: /**
137: * Password property setter.
138: *
139: * @param password
140: * @see #getPassword()
141: *
142: * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
143: */
144: public void setPassword(String password) {
145: this .password = password;
146: }
147:
148: /**
149: * Password property getter.
150: *
151: * @return the password
152: * @see #setPassword(String)
153: */
154: public String getPassword() {
155: return password;
156: }
157:
158: /**
159: * Get this object string.
160: *
161: * @return the username:password formed string
162: */
163: public String toString() {
164: StringBuffer result = new StringBuffer();
165: result.append(this .userName);
166: result.append(":");
167: result.append((this .password == null) ? "null" : this .password);
168: return result.toString();
169: }
170:
171: /**
172: * Does a hash of both user name and password.
173: *
174: * @return The hash code including user name and password.
175: */
176: public int hashCode() {
177: int hash = LangUtils.HASH_SEED;
178: hash = LangUtils.hashCode(hash, this .userName);
179: hash = LangUtils.hashCode(hash, this .password);
180: return hash;
181: }
182:
183: /**
184: * These credentials are assumed equal if the username and password are the
185: * same.
186: *
187: * @param o The other object to compare with.
188: *
189: * @return <code>true</code> if the object is equivalent.
190: */
191: public boolean equals(Object o) {
192: if (o == null)
193: return false;
194: if (this == o)
195: return true;
196: // note - to allow for sub-classing, this checks that class is the same
197: // rather than do "instanceof".
198: if (this .getClass().equals(o.getClass())) {
199: UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
200:
201: if (LangUtils.equals(this .userName, that.userName)
202: && LangUtils.equals(this .password, that.password)) {
203: return true;
204: }
205: }
206: return false;
207: }
208:
209: }
|