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