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: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 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  /***
33   * <p>Username and password {@link Credentials}.</p>
34   *
35   * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
36   * @author Sean C. Sullivan
37   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
38   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
39   * 
40   * @version $Revision: 155418 $ $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
41   * 
42   */
43  public class UsernamePasswordCredentials implements Credentials {
44  
45      // ----------------------------------------------------------- Constructors
46  
47      /***
48       * Default constructor.
49       * 
50       * @deprecated Do not use. Null user name no longer allowed
51       */
52      public UsernamePasswordCredentials() {
53          super();
54      }
55  
56  
57      /***
58       * The constructor with the username and password combined string argument.
59       *
60       * @param usernamePassword the username:password formed string
61       * @see #toString
62       */
63      public UsernamePasswordCredentials(String usernamePassword) {
64          super();
65          if (usernamePassword == null) {
66              throw new IllegalArgumentException("Username:password string may not be null");            
67          }
68          int atColon = usernamePassword.indexOf(':');
69          if (atColon >= 0) {
70              this.userName = usernamePassword.substring(0, atColon);
71              this.password = usernamePassword.substring(atColon + 1);
72          } else {
73              this.userName = usernamePassword;
74          }
75      }
76  
77  
78      /***
79       * The constructor with the username and password arguments.
80       *
81       * @param userName the user name
82       * @param password the password
83       */
84      public UsernamePasswordCredentials(String userName, String password) {
85          super();
86          if (userName == null) {
87              throw new IllegalArgumentException("Username may not be null");            
88          }
89          this.userName = userName;
90          this.password = password;
91      }
92  
93      // ----------------------------------------------------- Instance Variables
94  
95      /***
96       * User name.
97       */
98      private String userName;
99  
100 
101     /***
102      * Password.
103      */
104     private String password;
105 
106 
107     // ------------------------------------------------------------- Properties
108 
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("Username may not be null");            
121         }
122         this.userName = userName;
123     }
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     /***
138      * Password property setter.
139      *
140      * @param password
141      * @see #getPassword()
142      * 
143      * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
144      */
145     public void setPassword(String password) {
146         this.password = password;
147     }
148 
149 
150     /***
151      * Password property getter.
152      *
153      * @return the password
154      * @see #setPassword(String)
155      */
156     public String getPassword() {
157         return password;
158     }
159 
160     
161     /***
162      * Get this object string.
163      *
164      * @return the username:password formed string
165      */
166     public String toString() {
167         StringBuffer result = new StringBuffer();
168         result.append(this.userName);
169         result.append(":");
170         result.append((this.password == null) ? "null" : this.password);
171         return result.toString();
172     }
173 
174 }
175