PageRenderTime 168ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/addon-cloud-foundry/src/main/java/org/springframework/roo/addon/cloud/foundry/CloudCredentials.java

http://github.com/SpringSource/spring-roo
Java | 163 lines | 112 code | 18 blank | 33 comment | 23 complexity | 7bbd213a5e547f97d8738440aa205546 MD5 | raw file
  1. package org.springframework.roo.addon.cloud.foundry;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.apache.commons.codec.binary.Base64;
  7. import org.apache.commons.lang3.StringUtils;
  8. /**
  9. * The credentials for logging into a cloud.
  10. */
  11. public class CloudCredentials {
  12. private static final String EMAIL_KEY = "email";
  13. private static final String PASSWORD_KEY = "password";
  14. private static final String URL_KEY = "url";
  15. public static CloudCredentials decode(final String encoded) {
  16. if (StringUtils.isBlank(encoded)) {
  17. throw new IllegalStateException(
  18. "Stored login invalid; cannot continue");
  19. }
  20. final Map<String, String> map = new HashMap<String, String>();
  21. final String[] encodedFields = encoded.split(",");
  22. for (final String encodedField : encodedFields) {
  23. final String[] valuePair = encodedField.split(":");
  24. if (valuePair.length == 2) {
  25. final String decoded = new String(
  26. Base64.decodeBase64(valuePair[1]));
  27. map.put(valuePair[0], decoded);
  28. }
  29. }
  30. return new CloudCredentials(map);
  31. }
  32. private final String email;
  33. private final String password;
  34. private final String url;
  35. /**
  36. * Constructor that reads the relevant entries of the given map
  37. *
  38. * @param properties the map from which to read (required)
  39. */
  40. public CloudCredentials(final Map<String, String> properties) {
  41. this(properties.get(EMAIL_KEY), properties.get(PASSWORD_KEY),
  42. properties.get(URL_KEY));
  43. }
  44. /**
  45. * Constructor that accepts distinct values
  46. *
  47. * @param email the email address with which to log in (can be blank)
  48. * @param password the password for that email address (can be blank)
  49. * @param url the URL to log into (can be blank)
  50. */
  51. public CloudCredentials(final String email, final String password,
  52. final String url) {
  53. this.email = email;
  54. this.password = password;
  55. this.url = url;
  56. }
  57. public String encode() {
  58. if (!isValid()) {
  59. throw new IllegalStateException(
  60. "Credentials invalid; cannot continue");
  61. }
  62. final StringBuilder builder = new StringBuilder();
  63. builder.append(EMAIL_KEY).append(":")
  64. .append(Base64.encodeBase64String(getEmail().getBytes()))
  65. .append(",");
  66. builder.append(PASSWORD_KEY).append(":")
  67. .append(Base64.encodeBase64String(getPassword().getBytes()))
  68. .append(",");
  69. builder.append(URL_KEY).append(":")
  70. .append(Base64.encodeBase64String(getUrl().getBytes()));
  71. return builder.toString();
  72. }
  73. @Override
  74. public boolean equals(final Object o) {
  75. if (this == o) {
  76. return true;
  77. }
  78. if (o == null || getClass() != o.getClass()) {
  79. return false;
  80. }
  81. final CloudCredentials clouldCredentials = (CloudCredentials) o;
  82. if (email != null ? !email.equals(clouldCredentials.email)
  83. : clouldCredentials.email != null) {
  84. return false;
  85. }
  86. if (url != null ? !url.equals(clouldCredentials.url)
  87. : clouldCredentials.url != null) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. public String getEmail() {
  93. return email;
  94. }
  95. public String getPassword() {
  96. return password;
  97. }
  98. public String getUrl() {
  99. return url;
  100. }
  101. /**
  102. * Returns the URL for these credentials
  103. *
  104. * @return <code>null</code> if none is set
  105. */
  106. public URL getUrlObject() {
  107. if (StringUtils.isNotBlank(url)) {
  108. try {
  109. return new URL(url);
  110. }
  111. catch (final MalformedURLException e) {
  112. throw new IllegalStateException(e);
  113. }
  114. }
  115. return null;
  116. }
  117. @Override
  118. public int hashCode() {
  119. int result = email != null ? email.hashCode() : 0;
  120. result = 31 * result + (url != null ? url.hashCode() : 0);
  121. return result;
  122. }
  123. /**
  124. * Indicates whether the given account details match these credentials
  125. *
  126. * @param url the URL to check (can be <code>null</code>)
  127. * @param email the email to check (can be <code>null</code>)
  128. * @return see above
  129. */
  130. public boolean isSameAccount(final String url, final String email) {
  131. return StringUtils.equals(url, this.url)
  132. && StringUtils.equals(email, this.email);
  133. }
  134. /**
  135. * Indicates whether these credentials are complete, i.e. contain enough
  136. * information to attempt a login
  137. *
  138. * @return see above
  139. */
  140. public boolean isValid() {
  141. return StringUtils.isNotBlank(email)
  142. && StringUtils.isNotBlank(password)
  143. && StringUtils.isNotBlank(url);
  144. }
  145. }