View Javadoc

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/NTCredentials.java,v 1.10 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  /** {@link Credentials} for use with the NTLM authentication scheme which requires additional
    36   * information.
    37   *
    38   * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
    39   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
    40   *
    41   * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    42   *
    43   * @since 2.0
    44   */
    45  public class NTCredentials extends UsernamePasswordCredentials {
    46
    47      // ----------------------------------------------------- Instance Variables
    48
    49      /** The Domain to authenticate with.  */
    50      private String domain;
    51
    52      /** The host the authentication request is originating from.  */
    53      private String host;
    54
    55
    56      // ----------------------------------------------------------- Constructors
    57
    58      /**
    59       * Default constructor.
    60       *
    61       * @deprecated Do not use. Null user name, domain & host no longer allowed
    62       */
    63      public NTCredentials() {
    64          super();
    65      }
    66
    67      /**
    68       * Constructor.
    69       * @param userName The user name.  This should not include the domain to authenticate with.
    70       * For example: "user" is correct whereas "DOMAIN\\user" is not.
    71       * @param password The password.
    72       * @param host The host the authentication request is originating from.  Essentially, the
    73       * computer name for this machine.
    74       * @param domain The domain to authenticate within.
    75       */
    76      public NTCredentials(String userName, String password, String host,
    77              String domain) {
    78          super(userName, password);
    79          if (domain == null) {
    80              throw new IllegalArgumentException("Domain may not be null");
    81          }
    82          this.domain = domain;
    83          if (host == null) {
    84              throw new IllegalArgumentException("Host may not be null");
    85          }
    86          this.host = host;
    87      }
    88      // ------------------------------------------------------- Instance Methods
    89
    90
    91      /**
    92       * Sets the domain to authenticate with. The domain may not be null.
    93       *
    94       * @param domain the NT domain to authenticate in.
    95       *
    96       * @see #getDomain()
    97       *
    98       * @deprecated Do not use. The NTCredentials objects should be immutable
    99       */
    100     public void setDomain(String domain) {
    101         if (domain == null) {
    102             throw new IllegalArgumentException("Domain may not be null");
    103         }
    104         this.domain = domain;
    105     }
    106
    107     /**
    108      * Retrieves the name to authenticate with.
    109      *
    110      * @return String the domain these credentials are intended to authenticate with.
    111      *
    112      * @see #setDomain(String)
    113      *
    114      */
    115     public String getDomain() {
    116         return domain;
    117     }
    118
    119     /**
    120      * Sets the host name of the computer originating the request. The host name may
    121      * not be null.
    122      *
    123      * @param host the Host the user is logged into.
    124      *
    125      * @deprecated Do not use. The NTCredentials objects should be immutable
    126      */
    127     public void setHost(String host) {
    128         if (host == null) {
    129             throw new IllegalArgumentException("Host may not be null");
    130         }
    131         this.host = host;
    132     }
    133
    134     /**
    135      * Retrieves the host name of the computer originating the request.
    136      *
    137      * @return String the host the user is logged into.
    138      */
    139     public String getHost() {
    140         return this.host;
    141     }
    142
    143     /**
    144      * Return a string representation of this object.
    145      * @return A string represenation of this object.
    146      */
    147     public String toString() {
    148         final StringBuffer sbResult = new StringBuffer(super.toString());
    149
    150         sbResult.append("@");
    151         sbResult.append(this.host);
    152         sbResult.append(".");
    153         sbResult.append(this.domain);
    154
    155         return sbResult.toString();
    156     }
    157
    158     /**
    159      * Computes a hash code based on all the case-sensitive parts of the credentials object.
    160      *
    161      * @return  The hash code for the credentials.
    162      */
    163     public int hashCode() {
    164         int hash = super.hashCode();
    165         hash = LangUtils.hashCode(hash, this.host);
    166         hash = LangUtils.hashCode(hash, this.domain);
    167         return hash;
    168     }
    169
    170     /**
    171      * Performs a case-sensitive check to see if the components of the credentials
    172      * are the same.
    173      *
    174      * @param o  The object to match.
    175      *
    176      * @return <code>true</code> if all of the credentials match.
    177      */
    178     public boolean equals(Object o) {
    179         if (o == null) return false;
    180         if (this == o) return true;
    181         if (super.equals(o) ) {
    182             if (o instanceof NTCredentials) {
    183                 NTCredentials that = (NTCredentials) o;
    184
    185                 return LangUtils.equals(this.domain, that.domain)
    186                     && LangUtils.equals(this.host, that.host);
    187             }
    188         }
    189
    190         return false;
    191     }
    192 }