Search in sources :

Example 1 with SmtpConfigBuilder

use of com.synopsys.integration.alert.service.email.SmtpConfigBuilder in project hub-alert by blackducksoftware.

the class EmailGlobalTestAction method testConfigModelContent.

public ConfigurationTestResult testConfigModelContent(String testAddress, EmailGlobalConfigModel emailGlobalConfigModel) {
    if (StringUtils.isBlank(testAddress)) {
        return ConfigurationTestResult.failure("Could not determine what email address to send this content to. testAddress was not provided or was blank. Please provide a valid email address to test the configuration.");
    }
    try {
        InternetAddress emailAddress = new InternetAddress(testAddress);
        emailAddress.validate();
    } catch (AddressException ex) {
        return ConfigurationTestResult.failure(String.format("%s is not a valid email address. %s", testAddress, ex.getMessage()));
    }
    EmailChannelMessageModel testMessage = EmailChannelMessageModel.simple(TEST_SUBJECT_LINE, TEST_MESSAGE_CONTENT, "", "");
    SmtpConfigBuilder smtpConfigBuilder = SmtpConfig.builder();
    smtpConfigBuilder.setJavamailProperties(javamailPropertiesFactory.createJavaMailProperties(emailGlobalConfigModel));
    emailGlobalConfigModel.getSmtpFrom().ifPresent(smtpConfigBuilder::setSmtpFrom);
    emailGlobalConfigModel.getSmtpHost().ifPresent(smtpConfigBuilder::setSmtpHost);
    emailGlobalConfigModel.getSmtpPort().ifPresent(smtpConfigBuilder::setSmtpPort);
    emailGlobalConfigModel.getSmtpAuth().ifPresent(smtpConfigBuilder::setSmtpAuth);
    emailGlobalConfigModel.getSmtpUsername().ifPresent(smtpConfigBuilder::setSmtpUsername);
    if (BooleanUtils.toBoolean(emailGlobalConfigModel.getIsSmtpPasswordSet()) && emailGlobalConfigModel.getSmtpPassword().isEmpty()) {
        // TODO: This assumes if the password is saved but not provided we only test using the default configuration password.
        // If the UI supports multiple configurations in the future we should determine which configuration to get the password from.
        configurationAccessor.getConfiguration().flatMap(EmailGlobalConfigModel::getSmtpPassword).ifPresent(emailGlobalConfigModel::setSmtpPassword);
    }
    emailGlobalConfigModel.getSmtpPassword().ifPresent(smtpConfigBuilder::setSmtpPassword);
    SmtpConfig smtpConfig = smtpConfigBuilder.build();
    try {
        EmailTarget emailTarget = emailChannelMessagingService.createTarget(testMessage, testAddress);
        MessageResult messageResult = emailChannelMessagingService.sendMessage(smtpConfig, emailTarget);
        return ConfigurationTestResult.success(messageResult.getStatusMessage());
    } catch (AlertException ex) {
        return ConfigurationTestResult.failure(ex.getMessage());
    }
}
Also used : SmtpConfigBuilder(com.synopsys.integration.alert.service.email.SmtpConfigBuilder) SmtpConfig(com.synopsys.integration.alert.service.email.SmtpConfig) InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) EmailChannelMessageModel(com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessageModel) EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Aggregations

AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)1 EmailChannelMessageModel (com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessageModel)1 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)1 EmailTarget (com.synopsys.integration.alert.service.email.EmailTarget)1 SmtpConfig (com.synopsys.integration.alert.service.email.SmtpConfig)1 SmtpConfigBuilder (com.synopsys.integration.alert.service.email.SmtpConfigBuilder)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1