PageRenderTime 69ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source/calculator/aerius-connect-service/src/main/java/nl/overheid/aerius/connectservice/resource/UserResource.java

https://gitlab.com/AERIUS/AERIUS
Java | 114 lines | 75 code | 15 blank | 24 comment | 5 complexity | 297ad69ab96f03fb508f63f6b9cd62f3 MD5 | raw file
  1. /*
  2. * Copyright the State of the Netherlands
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see http://www.gnu.org/licenses/.
  16. */
  17. package nl.overheid.aerius.connectservice.resource;
  18. import java.io.IOException;
  19. import java.util.Date;
  20. import java.util.Locale;
  21. import java.util.regex.Pattern;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.http.ResponseEntity;
  24. import org.springframework.stereotype.Service;
  25. import nl.overheid.aerius.connectservice.api.UserApiDelegate;
  26. import nl.overheid.aerius.connectservice.model.GenerateApiKey;
  27. import nl.overheid.aerius.connectservice.service.ConstantService;
  28. import nl.overheid.aerius.connectservice.service.LocaleService;
  29. import nl.overheid.aerius.connectservice.service.ResponseService;
  30. import nl.overheid.aerius.connectservice.service.UserService;
  31. import nl.overheid.aerius.enums.MessagesEnum;
  32. import nl.overheid.aerius.mail.MailMessageData;
  33. import nl.overheid.aerius.mail.MailTo;
  34. import nl.overheid.aerius.mail.MessageTaskClient;
  35. import nl.overheid.aerius.mail.ReplacementToken;
  36. import nl.overheid.aerius.shared.Constants;
  37. import nl.overheid.aerius.shared.constants.SharedConstantsEnum;
  38. import nl.overheid.aerius.shared.exception.AeriusException;
  39. import nl.overheid.aerius.shared.exception.AeriusExceptionReason;
  40. import nl.overheid.aerius.taskmanager.client.TaskManagerClient;
  41. @Service
  42. public class UserResource implements UserApiDelegate {
  43. private static final Pattern EMAIL_PATTERN = Pattern.compile(Constants.VALID_EMAIL_ADDRESS_REGEX, Pattern.CASE_INSENSITIVE);
  44. private final ResponseService responseService;
  45. private final UserService userService;
  46. private final TaskManagerClient taskManagerClient;
  47. private final LocaleService localeService;
  48. private final ConstantService constantService;
  49. @Autowired
  50. UserResource(final ResponseService responseService, final UserService userService, final TaskManagerClient taskManagerClient,
  51. final LocaleService localeService, final ConstantService constantService) {
  52. this.responseService = responseService;
  53. this.userService = userService;
  54. this.taskManagerClient = taskManagerClient;
  55. this.localeService = localeService;
  56. this.constantService = constantService;
  57. }
  58. @Override
  59. public ResponseEntity<Void> generateApiKey(final GenerateApiKey generateApiKey) {
  60. try {
  61. generateApiKey(generateApiKey.getEmail());
  62. return responseService.toOkResponse();
  63. } catch (final AeriusException | IOException e) {
  64. throw responseService.toResponseStatusException(e);
  65. }
  66. }
  67. private void generateApiKey(final String email) throws AeriusException, IOException {
  68. validateEmail(email);
  69. final String apiKey = userService.generateAPIKey(email);
  70. sendMail(email, apiKey);
  71. }
  72. /**
  73. * Validates the email address.
  74. * @param emailAddress email address
  75. * @throws AeriusException throws exception in case of validation errors
  76. */
  77. private void validateEmail(final String emailAddress) throws AeriusException {
  78. if (emailAddress == null || emailAddress.length() == 0 || !EMAIL_PATTERN.matcher(emailAddress.trim()).matches()) {
  79. throw new AeriusException(AeriusExceptionReason.CONNECT_NO_VALID_EMAIL_SUPPLIED, emailAddress);
  80. }
  81. }
  82. private void sendMail(final String email, final String apiKey) throws IOException {
  83. final Locale locale = localeService.getLocale();
  84. final MailMessageData messageData = new MailMessageData(
  85. MessagesEnum.CONNECT_APIKEY_CONFIRM_SUBJECT, MessagesEnum.CONNECT_APIKEY_CONFIRM_BODY,
  86. locale, new MailTo(email));
  87. // Yeah, we could set this before generation, but I really don't care about the probable 1 second precision in this case.
  88. final Date creationDate = new Date();
  89. messageData.setReplacement(ReplacementToken.CONNECT_APIKEY, apiKey);
  90. // I would recommend refactoring CALC_CREATION_* stuff to CREATION_* stuff as this is, well, less okay. Something to do on the Master branch.
  91. messageData.setReplacement(ReplacementToken.CALC_CREATION_DATE, MessageTaskClient.getDefaultDateFormatted(creationDate, locale));
  92. messageData.setReplacement(ReplacementToken.CALC_CREATION_TIME, MessageTaskClient.getDefaultTimeFormatted(creationDate, locale));
  93. // Default urls for usage in e-mail
  94. messageData.setReplacement(ReplacementToken.MANUAL_URL, constantService.getValue(SharedConstantsEnum.MANUAL_URL));
  95. messageData.setReplacement(ReplacementToken.QUICK_START_URL, constantService.getValue(SharedConstantsEnum.QUICK_START_URL));
  96. messageData.setReplacement(ReplacementToken.BIJ12_HELPDESK_URL, constantService.getValue(SharedConstantsEnum.BIJ12_HELPDESK_URL));
  97. MessageTaskClient.startMessageTask(taskManagerClient, messageData);
  98. }
  99. }