<commit_before>package com.sequenceiq.cloudbreak.service.user;


import com.sequenceiq.cloudbreak.controller.InternalServerException;
import com.sequenceiq.cloudbreak.domain.User;
import com.sequenceiq.cloudbreak.domain.UserStatus;
import com.sequenceiq.cloudbreak.repository.UserRepository;
import freemarker.template.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.DigestUtils;

import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

@Service
public class SimpleUserService implements UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleUserService.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private Configuration freemarkerConfiguration;

    @Value("${host.addr}")
    private String hostAddress;

    @Value("${mail.sender.from}")
    private String msgFrom;


    @Override
    public Long registerUser(User user) {
        String confToken = generateRegistrationId(user);
        user.setConfToken(confToken);
        User savedUser = userRepository.save(user);
        LOGGER.info("User {} successfully saved", user);
        sendConfirmationEmail(user);
        return savedUser.getId();
    }

    @Override
    public void confirmRegistration(String confToken) {
        User user = userRepository.findUserByConfToken(confToken);
        if (null != user) {
            user.setConfToken(null);
            user.setStatus(UserStatus.ACTIVE);
            userRepository.save(user);
        } else {
            LOGGER.warn("There's no user registration pending for confToken: {}", confToken);
        }
    }

    private String generateRegistrationId(User user) {
        LOGGER.debug("Generating registration id ...");
        String textToDigest = user.getEmail() + user.getPassword();
        return DigestUtils.md5DigestAsHex(textToDigest.getBytes());
    }

    private String getEmailBody(User user) {
        String text = null;
        try {
            String template = "templates/confirmation-email.ftl";
            Map<String, Object> model = new HashMap<>();
            model.put("user", user);
            model.put("confirm", hostAddress + "/users/confirm/" + user.getConfToken());
            text = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(template, "UTF-8"), model);
        } catch (Exception e) {
            LOGGER.error("Confirmation email assembling failed. Exception: {}", e);
            throw new InternalServerException("Failed to assemble confirmation email message", e);
        }
        return text;
    }

    @Async
    public void sendConfirmationEmail(final User user) {
        LOGGER.info("Sending confirmation email ...");
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setFrom(msgFrom);
                message.setTo(user.getEmail());
                message.setSubject("Cloudbreak - confirm registration");
                message.setText(getEmailBody(user), true);
            }
        };
        ((JavaMailSender) mailSender).send(preparator);
        LOGGER.info("Confirmation email sent");
    }

}<commit_msg>Separated the async mail sending<commit_after>package com.sequenceiq.cloudbreak.service.user;


import com.sequenceiq.cloudbreak.controller.InternalServerException;
import com.sequenceiq.cloudbreak.domain.User;
import com.sequenceiq.cloudbreak.domain.UserStatus;
import com.sequenceiq.cloudbreak.repository.UserRepository;
import freemarker.template.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.DigestUtils;

import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

@Service
public class SimpleUserService implements UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleUserService.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private Configuration freemarkerConfiguration;

    @Value("${host.addr}")
    private String hostAddress;

    @Value("${mail.sender.from}")
    private String msgFrom;


    @Override
    public Long registerUser(User user) {
        String confToken = generateRegistrationId(user);
        user.setConfToken(confToken);
        User savedUser = userRepository.save(user);
        LOGGER.info("User {} successfully saved", user);
        MimeMessagePreparator msgPreparator = prepareMessage(user);
        sendConfirmationEmail(msgPreparator);
        return savedUser.getId();
    }

    @Override
    public void confirmRegistration(String confToken) {
        User user = userRepository.findUserByConfToken(confToken);
        if (null != user) {
            user.setConfToken(null);
            user.setStatus(UserStatus.ACTIVE);
            userRepository.save(user);
        } else {
            LOGGER.warn("There's no user registration pending for confToken: {}", confToken);
        }
    }

    private String generateRegistrationId(User user) {
        LOGGER.debug("Generating registration id ...");
        String textToDigest = user.getEmail() + user.getPassword();
        return DigestUtils.md5DigestAsHex(textToDigest.getBytes());
    }

    private String getEmailBody(User user) {
        String text = null;
        try {
            String template = "templates/confirmation-email.ftl";
            Map<String, Object> model = new HashMap<>();
            model.put("user", user);
            model.put("confirm", hostAddress + "/users/confirm/" + user.getConfToken());
            text = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(template, "UTF-8"), model);
        } catch (Exception e) {
            LOGGER.error("Confirmation email assembling failed. Exception: {}", e);
            throw new InternalServerException("Failed to assemble confirmation email message", e);
        }
        return text;
    }

    private MimeMessagePreparator prepareMessage(final User user) {
        return new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setFrom(msgFrom);
                message.setTo(user.getEmail());
                message.setSubject("Cloudbreak - confirm registration");
                message.setText(getEmailBody(user), true);
            }
        };
    }

    @Async
    public void sendConfirmationEmail(final MimeMessagePreparator preparator) {
        LOGGER.info("Sending confirmation email ...");
        ((JavaMailSender) mailSender).send(preparator);
        LOGGER.info("Confirmation email sent");
    }

}
{'fraction_non_alphanumeric': 0.09675421209117939, 'fraction_numerical': 0.0009910802775024777, 'mean_word_length': 3.375609756097561, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4156416', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2089, 'n_words': 431}