PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ris-yips-spring-ws/src/main/java/com/roycat/yips/spring/ws/services/DefaultUserService.java

https://bitbucket.org/roycat/ris-yips-spring-ws
Java | 145 lines | 108 code | 28 blank | 9 comment | 20 complexity | b2c2b5419efd533f5ce8e18c86f7d958 MD5 | raw file
  1. package com.roycat.yips.spring.ws.services;
  2. import java.sql.SQLException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import com.roycat.yips.spring.ws.api.UserDao;
  10. import com.roycat.yips.spring.ws.api.UserService;
  11. import com.roycat.yips.spring.ws.exceptions.DuplicateEmailException;
  12. import com.roycat.yips.spring.ws.exceptions.DuplicateUsernameException;
  13. import com.roycat.yips.spring.ws.exceptions.InvalidPasswordException;
  14. import com.roycat.yips.spring.ws.exceptions.MalformedEmailException;
  15. import com.roycat.yips.spring.ws.models.User;
  16. /**
  17. * This class implements all services related to User objects.
  18. * @author Steve Bolin - Apr 2018
  19. */
  20. @Component
  21. public class DefaultUserService implements UserService {
  22. private static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
  23. Pattern.CASE_INSENSITIVE);
  24. private static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^[a-zA-Z0-9]+$");
  25. private static final String PW_MASK = "***";
  26. private UserDao userDao;
  27. @Autowired
  28. public DefaultUserService(final UserDao userDao) {
  29. this.userDao = userDao;
  30. }
  31. @Override
  32. public List<String> retrieveUsernames() throws SQLException {
  33. List<User> users = this.userDao.retrieveUsers();
  34. List<String> result = new ArrayList<>(users.size());
  35. for (User user : users) {
  36. result.add(user.getUsername());
  37. }
  38. return result;
  39. }
  40. @Override
  41. public List<User> retrieveUsers() throws SQLException {
  42. List<User> result = this.userDao.retrieveUsers();
  43. for (User user : result) {
  44. this.maskPassword(user);
  45. }
  46. return result;
  47. }
  48. @Override
  49. public List<User> searchUsersByUsername(final String username) throws SQLException {
  50. List<User> result = this.userDao.searchUsersByUsername(username);
  51. for (User user : result) {
  52. this.maskPassword(user);
  53. }
  54. return result;
  55. }
  56. @Override
  57. public User retrieveUserById(final Long id) throws SQLException {
  58. User result = this.userDao.retrieveUserById(id);
  59. maskPassword(result);
  60. return result;
  61. }
  62. @Override
  63. public User retrieveUserByUsername(String username) throws SQLException {
  64. return this.userDao.retrieveUserByUsername(username);
  65. }
  66. @Override
  67. public List<User> searchUsersByEmail(final String email) throws SQLException {
  68. List<User> result = this.userDao.searchUsersByEmail(email);
  69. for (User user : result) {
  70. this.maskPassword(user);
  71. }
  72. return result;
  73. }
  74. @Override
  75. public void registerUser(final User user) throws SQLException, DuplicateEmailException, MalformedEmailException, InvalidPasswordException, DuplicateUsernameException {
  76. // usernames are reqd to be unique.
  77. if (!this.searchUsersByUsername(user.getUsername()).isEmpty()) {
  78. throw new DuplicateUsernameException("A user with this username already exists.");
  79. }
  80. // Check email string
  81. if (!this.isValidEmail(user.getEmail())) {
  82. throw new MalformedEmailException("The email address is not a properly formed email address.");
  83. }
  84. if (!this.isValidPassword(user.getPassword())) {
  85. throw new InvalidPasswordException("The password is not a valid password configuration.");
  86. }
  87. // emails are reqd to be unique.
  88. if (!this.searchUsersByEmail(user.getEmail()).isEmpty()) {
  89. throw new DuplicateEmailException("A user with this email already exists.");
  90. }
  91. // Don't accept supplied ids for new users. The id is created by the db in this demo.
  92. if (user.getId() != null) {
  93. user.setId(null);
  94. }
  95. // If it survives the checks, then create the user.
  96. this.userDao.createUser(user);
  97. }
  98. private boolean isValidEmail(String email) {
  99. if (email == null || email.length() == 0) {
  100. return false;
  101. }
  102. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
  103. return matcher.find();
  104. }
  105. private boolean isValidPassword(String password) {
  106. if (password == null || password.length() == 0) {
  107. return false;
  108. }
  109. Matcher matcher = VALID_PASSWORD_REGEX.matcher(password);
  110. return matcher.find();
  111. }
  112. private void maskPassword(User user) {
  113. if (null != user) {
  114. user.setPassword(PW_MASK);
  115. }
  116. }
  117. }