Search in sources :

Example 11 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class EmailServiceImpl method sendSimpleMessage.

@Override
public void sendSimpleMessage(String to, String subject, String text) {
    try {
        MimeMessage mail = mailSender.createMimeMessage();
        mail.setFrom(appProperties.getSender().getFrom());
        MimeMessageHelper helper = new MimeMessageHelper(mail, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, true);
        mailSender.send(mail);
    } catch (Exception e) {
        LOG.error("Mail server connection failed ", e);
        throw new CustomParameterizedException(i18n.translate("email.serverError"));
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MessagingException(javax.mail.MessagingException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)

Example 12 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class EmailServiceImpl method sendMessageWithAttachment.

@Override
public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) {
    try {
        MimeMessage message = mailSender.createMimeMessage();
        message.setFrom(appProperties.getSender().getFrom());
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
        helper.addAttachment("File", file);
        mailSender.send(message);
    } catch (MessagingException e) {
        LOG.error("Error sending mail ", e);
        throw new CustomParameterizedException(i18n.translate("email.errorSendingMail"));
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Example 13 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method validatePasswordResetToken.

@Override
public void validatePasswordResetToken(String token, Long userId, String password, String repeatPassword) {
    checkArgument(userId > 0, i18n.translate("argument.nonNegative", userId));
    UserEntity userEntity = userRepository.findOne(userId);
    if (userEntity == null) {
        throw new UnknownResourceException(i18n.translate("user.notExist", userId));
    }
    PasswordResetTokenEntity passwordResetTokenEntity = passwordTokenRepository.findByToken(token);
    if (passwordResetTokenEntity == null || passwordResetTokenEntity.getUser().getId().longValue() != userId.longValue()) {
        throw new CustomParameterizedException(i18n.translate("email.invalidToken", token));
    }
    Calendar cal = Calendar.getInstance();
    if ((passwordResetTokenEntity.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
        throw new CustomParameterizedException(i18n.translate("email.expiredToken", token));
    }
    if (!password.equals(repeatPassword)) {
        throw new CustomParameterizedException("Password does not match the confirm password");
    }
    userEntity.setPass(encryptPassword(repeatPassword));
    userRepository.save(userEntity);
}
Also used : PasswordResetTokenEntity(py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) Calendar(java.util.Calendar) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)

Example 14 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method loadTemplate.

private String loadTemplate(String template) {
    URL url = Resources.getResource(template);
    String content = "";
    try {
        content = Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new CustomParameterizedException(i18n.translate("email.readById", template));
    }
    return content;
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) IOException(java.io.IOException) URL(java.net.URL)

Example 15 with CustomParameterizedException

use of py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException in project FP-PSP-SERVER by FundacionParaguaya.

the class UserServiceImpl method addUserWithRoleAndApplication.

@Override
public UserDTO addUserWithRoleAndApplication(UserRoleApplicationDTO userRoleApplicationDTO, UserDetailsDTO userDetails) {
    userRepository.findOneByUsername(userRoleApplicationDTO.getUsername()).ifPresent(user -> {
        throw new CustomParameterizedException("User already exists.", new ImmutableMultimap.Builder<String, String>().put("username", user.getUsername()).build().asMap());
    });
    UserEntity user = new UserEntity();
    user.setUsername(userRoleApplicationDTO.getUsername());
    user.setEmail(userRoleApplicationDTO.getEmail());
    user.setPass(new BCryptPasswordEncoder().encode(userRoleApplicationDTO.getPass()));
    user.setActive(true);
    UserEntity newUser = userRepository.save(user);
    if (userRoleApplicationDTO.getRole() != null) {
        createUserRole(newUser, userRoleApplicationDTO.getRole());
    }
    if (userRoleApplicationDTO.getOrganizationId() != null) {
        createUserOrganization(newUser, userRoleApplicationDTO);
    } else if (userRoleApplicationDTO.getApplicationId() != null) {
        createUserApplication(newUser, userRoleApplicationDTO);
    }
    return userMapper.entityToDto(newUser);
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Aggregations

CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)15 UserEntity (py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)4 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)3 IOException (java.io.IOException)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity)2 SnapshotIndicatorEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotIndicatorEntity)2 ValidationResults (py.org.fundacionparaguaya.pspserver.surveys.validation.ValidationResults)2 ImageDTO (py.org.fundacionparaguaya.pspserver.system.dtos.ImageDTO)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 File (java.io.File)1 URL (java.net.URL)1 Calendar (java.util.Calendar)1 NoSuchElementException (java.util.NoSuchElementException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1