1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
94
95 /***
96 * User name.
97 */
98 private String userName;
99
100
101 /***
102 * Password.
103 */
104 private String password;
105
106
107
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