1. /*
  2. * $Header: /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: 1.14 $
  4. * $Date: 2004/04/18 23:51:35 $
  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. package org.apache.commons.httpclient;
  30. /**
  31. * <p>Username and password {@link Credentials}.</p>
  32. *
  33. * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  34. * @author Sean C. Sullivan
  35. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  36. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  37. *
  38. * @version $Revision: 1.14 $ $Date: 2004/04/18 23:51:35 $
  39. *
  40. */
  41. public class UsernamePasswordCredentials implements Credentials {
  42. // ----------------------------------------------------------- Constructors
  43. /**
  44. * Default constructor.
  45. *
  46. * @deprecated Do not use. Null user name no longer allowed
  47. */
  48. public UsernamePasswordCredentials() {
  49. super();
  50. }
  51. /**
  52. * The constructor with the username and password combined string argument.
  53. *
  54. * @param usernamePassword the username:password formed string
  55. * @see #toString
  56. */
  57. public UsernamePasswordCredentials(String usernamePassword) {
  58. super();
  59. if (usernamePassword == null) {
  60. throw new IllegalArgumentException("Username:password string may not be null");
  61. }
  62. int atColon = usernamePassword.indexOf(':');
  63. if (atColon >= 0) {
  64. this.userName = usernamePassword.substring(0, atColon);
  65. this.password = usernamePassword.substring(atColon + 1);
  66. } else {
  67. this.userName = usernamePassword;
  68. }
  69. }
  70. /**
  71. * The constructor with the username and password arguments.
  72. *
  73. * @param userName the user name
  74. * @param password the password
  75. */
  76. public UsernamePasswordCredentials(String userName, String password) {
  77. super();
  78. if (userName == null) {
  79. throw new IllegalArgumentException("Username may not be null");
  80. }
  81. this.userName = userName;
  82. this.password = password;
  83. }
  84. // ----------------------------------------------------- Instance Variables
  85. /**
  86. * User name.
  87. */
  88. private String userName;
  89. /**
  90. * Password.
  91. */
  92. private String password;
  93. // ------------------------------------------------------------- Properties
  94. /**
  95. * User name property setter. User name may not be null.
  96. *
  97. * @param userName
  98. * @see #getUserName()
  99. *
  100. * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
  101. */
  102. public void setUserName(String userName) {
  103. if (userName == null) {
  104. throw new IllegalArgumentException("Username may not be null");
  105. }
  106. this.userName = userName;
  107. }
  108. /**
  109. * User name property getter.
  110. *
  111. * @return the userName
  112. * @see #setUserName(String)
  113. */
  114. public String getUserName() {
  115. return userName;
  116. }
  117. /**
  118. * Password property setter.
  119. *
  120. * @param password
  121. * @see #getPassword()
  122. *
  123. * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable
  124. */
  125. public void setPassword(String password) {
  126. this.password = password;
  127. }
  128. /**
  129. * Password property getter.
  130. *
  131. * @return the password
  132. * @see #setPassword(String)
  133. */
  134. public String getPassword() {
  135. return password;
  136. }
  137. /**
  138. * Get this object string.
  139. *
  140. * @return the username:password formed string
  141. */
  142. public String toString() {
  143. StringBuffer result = new StringBuffer();
  144. result.append(this.userName);
  145. result.append(":");
  146. result.append((this.password == null) ? "null" : this.password);
  147. return result.toString();
  148. }
  149. }