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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   */
    30
    31  package org.apache.commons.httpclient;
    32
    33  import org.apache.commons.httpclient.util.LangUtils;
    34
    35  /**
    36   * <p>Username and password {@link Credentials}.</p>
    37   *
    38   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    39   * @author Sean C. Sullivan
    40   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
    41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    42   *
    43   * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    44   *
    45   */
    46  public class UsernamePasswordCredentials implements Credentials {
    47
    48      // ----------------------------------------------------------- Constructors
    49
    50      /**
    51       * Default constructor.
    52       *
    53       * @deprecated Do not use. Null user name no longer allowed
    54       */
    55      public UsernamePasswordCredentials() {
    56          super();
    57      }
    58
    59
    60      /**
    61       * The constructor with the username and password combined string argument.
    62       *
    63       * @param usernamePassword the username:password formed string
    64       * @see #toString
    65       */
    66      public UsernamePasswordCredentials(String usernamePassword) {
    67          super();
    68          if (usernamePassword == null) {
    69              throw new IllegalArgumentException("Username:password string may not be null");
    70          }
    71          int atColon = usernamePassword.indexOf(':');
    72          if (atColon >= 0) {
    73              this.userName = usernamePassword.substring(0, atColon);
    74              this.password = usernamePassword.substring(atColon + 1);
    75          } else {
    76              this.userName = usernamePassword;
    77          }
    78      }
    79
    80
    81      /**
    82       * The constructor with the username and password arguments.
    83       *
    84       * @param userName the user name
    85       * @param password the password
    86       */
    87      public UsernamePasswordCredentials(String userName, String password) {
    88          super();
    89          if (userName == null) {
    90              throw new IllegalArgumentException("Username may not be null");
    91          }
    92          this.userName = userName;
    93          this.password = password;
    94      }
    95
    96      // ----------------------------------------------------- Instance Variables
    97
    98      /**
    99       * User name.
    100      */
    101     private String userName;
    102
    103
    104     /**
    105      * Password.
    106      */
    107     private String password;
    108
    109
    110     // ------------------------------------------------------------- Properties
    111
    112
    113     /**
    114      * User name property setter. User name may not be null.
    115      *
    116      * @param userName
    117      * @see #getUserName()
    118      *
    119      * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
    120      */
    121     public void setUserName(String userName) {
    122         if (userName == null) {
    123             throw new IllegalArgumentException("Username may not be null");
    124         }
    125         this.userName = userName;
    126     }
    127
    128
    129     /**
    130      * User name property getter.
    131      *
    132      * @return the userName
    133      * @see #setUserName(String)
    134      */
    135     public String getUserName() {
    136         return userName;
    137     }
    138
    139
    140     /**
    141      * Password property setter.
    142      *
    143      * @param password
    144      * @see #getPassword()
    145      *
    146      * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
    147      */
    148     public void setPassword(String password) {
    149         this.password = password;
    150     }
    151
    152
    153     /**
    154      * Password property getter.
    155      *
    156      * @return the password
    157      * @see #setPassword(String)
    158      */
    159     public String getPassword() {
    160         return password;
    161     }
    162
    163
    164     /**
    165      * Get this object string.
    166      *
    167      * @return the username:password formed string
    168      */
    169     public String toString() {
    170         StringBuffer result = new StringBuffer();
    171         result.append(this.userName);
    172         result.append(":");
    173         result.append((this.password == null) ? "null" : this.password);
    174         return result.toString();
    175     }
    176
    177     /**
    178      * Does a hash of both user name and password.
    179      *
    180      * @return The hash code including user name and password.
    181      */
    182     public int hashCode() {
    183         int hash = LangUtils.HASH_SEED;
    184         hash = LangUtils.hashCode(hash, this.userName);
    185         hash = LangUtils.hashCode(hash, this.password);
    186         return hash;
    187     }
    188
    189     /**
    190      * These credentials are assumed equal if the username and password are the
    191      * same.
    192      *
    193      * @param o The other object to compare with.
    194      *
    195      * @return  <code>true</code> if the object is equivalent.
    196      */
    197     public boolean equals(Object o) {
    198         if (o == null) return false;
    199         if (this == o) return true;
    200         // note - to allow for sub-classing, this checks that class is the same
    201         // rather than do "instanceof".
    202         if (this.getClass().equals(o.getClass())) {
    203             UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
    204
    205             if (LangUtils.equals(this.userName, that.userName)
    206                     && LangUtils.equals(this.password, that.password) ) {
    207                 return true;
    208             }
    209         }
    210         return false;
    211     }
    212
    213 }
    214