Search in sources :

Example 1 with EmailTarget

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

the class EmailChannelMessagingServiceTest method testCreateTarget.

@Test
public void testCreateTarget() {
    EmailChannelMessageModel emailChannelMessageModel = EmailChannelMessageModel.simple(EXPECTED_SUBJECT_LINE, EXPECTED_CONTENT, EXPECTED_PROVIDER_NAME, EXPECTED_PROVIDER_URL);
    try {
        EmailTarget target = emailChannelMessagingService.createTarget(emailChannelMessageModel, EXPECTED_EMAIL_ADDRESS_A, EXPECTED_EMAIL_ADDRESS_B);
        assertTrue(target.getEmailAddresses().containsAll(Set.of(EXPECTED_EMAIL_ADDRESS_A, EXPECTED_EMAIL_ADDRESS_B)));
        assertEquals(2, target.getEmailAddresses().size());
        assertEquals(EXPECTED_SUBJECT_LINE, target.getModel().get(EmailPropertyKeys.TEMPLATE_KEY_SUBJECT_LINE.getPropertyKey()));
        assertEquals(EXPECTED_CONTENT, target.getModel().get(EmailPropertyKeys.EMAIL_CONTENT.getPropertyKey()));
        assertEquals(EXPECTED_PROVIDER_NAME, target.getModel().get(EmailPropertyKeys.TEMPLATE_KEY_PROVIDER_NAME.getPropertyKey()));
        assertEquals(EXPECTED_PROVIDER_URL, target.getModel().get(EmailPropertyKeys.TEMPLATE_KEY_PROVIDER_URL.getPropertyKey()));
        assertTrue(target.getModel().containsKey(EmailPropertyKeys.EMAIL_CATEGORY.getPropertyKey()));
        assertTrue(target.getModel().containsKey(EmailPropertyKeys.TEMPLATE_KEY_PROVIDER_PROJECT_NAME.getPropertyKey()));
        assertTrue(target.getModel().containsKey(EmailPropertyKeys.TEMPLATE_KEY_START_DATE.getPropertyKey()));
        assertTrue(target.getModel().containsKey(EmailPropertyKeys.TEMPLATE_KEY_END_DATE.getPropertyKey()));
        assertTrue(target.getModel().containsKey(FreemarkerTemplatingService.KEY_ALERT_SERVER_URL));
    } catch (Exception e) {
        fail("Unexpected exception occurred when creating the EmailTarget", e);
    }
}
Also used : EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) Test(org.junit.jupiter.api.Test)

Example 2 with EmailTarget

use of com.synopsys.integration.alert.service.email.EmailTarget 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)

Example 3 with EmailTarget

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

the class EmailGlobalFieldModelTestAction method testConfig.

@Override
public MessageResult testConfig(String configId, FieldModel fieldModel, FieldUtility registeredFieldValues) throws AlertException {
    String addressString = fieldModel.getFieldValue(FieldModelTestAction.KEY_DESTINATION_NAME).orElse("");
    if (StringUtils.isBlank(addressString)) {
        throw new AlertException(String.format("Could not determine what email address to send this content to. %s was not provided or was blank. Please provide a valid email address to test the configuration.", FieldModelTestAction.KEY_DESTINATION_NAME));
    }
    try {
        InternetAddress emailAddress = new InternetAddress(addressString);
        emailAddress.validate();
    } catch (AddressException ex) {
        throw new AlertException(String.format("%s is not a valid email address. %s", addressString, ex.getMessage()));
    }
    EmailChannelMessageModel testMessage = EmailChannelMessageModel.simple(TEST_SUBJECT_LINE, TEST_MESSAGE_CONTENT, "", "");
    EmailTarget emailTarget = emailChannelMessagingService.createTarget(testMessage, addressString);
    SmtpConfig smtpConfig = SmtpConfig.builder().setJavamailProperties(javamailPropertiesFactory.createJavaMailProperties(registeredFieldValues)).setSmtpFrom(registeredFieldValues.getString(EmailPropertyKeys.JAVAMAIL_FROM_KEY.getPropertyKey()).orElse(null)).setSmtpHost(registeredFieldValues.getString(EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey()).orElse(null)).setSmtpPort(registeredFieldValues.getInteger(EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey()).orElse(-1)).setSmtpAuth(registeredFieldValues.getBooleanOrFalse(EmailPropertyKeys.JAVAMAIL_AUTH_KEY.getPropertyKey())).setSmtpUsername(registeredFieldValues.getString(EmailPropertyKeys.JAVAMAIL_USER_KEY.name()).orElse(null)).setSmtpPassword(registeredFieldValues.getString(EmailPropertyKeys.JAVAMAIL_PASSWORD_KEY.getPropertyKey()).orElse(null)).build();
    return emailChannelMessagingService.sendMessage(smtpConfig, emailTarget);
}
Also used : 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) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 4 with EmailTarget

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

the class EmailChannelMessagingServiceTest method testSendMessage.

@Test
public void testSendMessage() {
    EmailChannelMessageModel emailChannelMessageModel = EmailChannelMessageModel.simple(EXPECTED_SUBJECT_LINE, EXPECTED_CONTENT, EXPECTED_PROVIDER_NAME, EXPECTED_PROVIDER_URL);
    try {
        EmailTarget emailTarget = emailChannelMessagingService.createTarget(emailChannelMessageModel, EXPECTED_EMAIL_ADDRESS_A, EXPECTED_EMAIL_ADDRESS_B);
        MessageResult messageResult = emailChannelMessagingService.sendMessage(SmtpConfig.builder().build(), emailTarget);
        assertFalse(messageResult.hasErrors());
        assertEquals("Successfully sent 2 email(s)", messageResult.getStatusMessage());
    } catch (Exception e) {
        fail("Unexpected exception occurred when sending emails", e);
    }
}
Also used : EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) Test(org.junit.jupiter.api.Test)

Example 5 with EmailTarget

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

the class EmailChannelMessageSender method sendMessages.

@Override
public MessageResult sendMessages(EmailJobDetailsModel emailJobDetails, List<EmailChannelMessageModel> emailMessages) throws AlertException {
    // Validation
    ValidatedEmailAddresses validatedAdditionalEmailAddresses;
    UUID jobId = emailJobDetails.getJobId();
    if (null != jobId) {
        validatedAdditionalEmailAddresses = emailAddressValidator.validate(jobId, emailJobDetails.getAdditionalEmailAddresses());
    } else {
        validatedAdditionalEmailAddresses = new ValidatedEmailAddresses(new HashSet<>(emailJobDetails.getAdditionalEmailAddresses()), Set.of());
    }
    Set<String> invalidEmailAddresses = validatedAdditionalEmailAddresses.getInvalidEmailAddresses();
    if (!invalidEmailAddresses.isEmpty()) {
        emailJobDetails = new EmailJobDetailsModel(emailJobDetails.getJobId(), emailJobDetails.getSubjectLine().orElse(null), emailJobDetails.isProjectOwnerOnly(), emailJobDetails.isAdditionalEmailAddressesOnly(), emailJobDetails.getAttachmentFileType(), new ArrayList<>(validatedAdditionalEmailAddresses.getValidEmailAddresses()));
    }
    // Distribution
    EmailGlobalConfigModel emailServerConfiguration = emailGlobalConfigAccessor.getConfiguration().orElseThrow(() -> new AlertConfigurationException("ERROR: Missing Email global config."));
    SmtpConfig smtpConfig = SmtpConfig.builder().setJavamailProperties(javamailPropertiesFactory.createJavaMailProperties(emailServerConfiguration)).setSmtpFrom(emailServerConfiguration.getSmtpFrom().orElse(null)).setSmtpHost(emailServerConfiguration.getSmtpHost().orElse(null)).setSmtpPort(emailServerConfiguration.getSmtpPort().orElse(-1)).setSmtpAuth(emailServerConfiguration.getSmtpAuth().orElse(false)).setSmtpUsername(emailServerConfiguration.getSmtpUsername().orElse(null)).setSmtpPassword(emailServerConfiguration.getSmtpPassword().orElse(null)).build();
    int totalEmailsSent = 0;
    for (EmailChannelMessageModel message : emailMessages) {
        Set<String> projectHrefs = message.getSource().map(ProjectMessage::getProject).flatMap(LinkableItem::getUrl).map(Set::of).orElse(Set.of());
        Set<String> gatheredEmailAddresses = emailAddressGatherer.gatherEmailAddresses(emailJobDetails, projectHrefs);
        if (gatheredEmailAddresses.isEmpty()) {
            if (invalidEmailAddresses.isEmpty()) {
                throw new AlertException("Could not determine what email addresses to send this content to");
            } else {
                String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
                throw new AlertException(String.format("No valid email addresses to send this content to. The following email addresses were invalid: %s", invalidEmailAddressesString));
            }
        }
        EmailTarget emailTarget = emailChannelMessagingService.createTarget(message, gatheredEmailAddresses);
        Optional<ProjectMessage> optionalProjectMessage = message.getSource();
        if (optionalProjectMessage.isPresent()) {
            EmailAttachmentFormat attachmentFormat = EmailAttachmentFormat.getValueSafely(emailJobDetails.getAttachmentFileType());
            emailChannelMessagingService.sendMessageWithAttachedProjectMessage(smtpConfig, emailTarget, optionalProjectMessage.get(), attachmentFormat);
        } else {
            emailChannelMessagingService.sendMessage(smtpConfig, emailTarget);
        }
        totalEmailsSent += emailTarget.getEmailAddresses().size();
    }
    // Reporting
    if (!invalidEmailAddresses.isEmpty()) {
        String invalidEmailAddressesString = StringUtils.join(invalidEmailAddresses, ", ");
        String errorMessage = String.format("No emails were sent to the following recipients because they were invalid: %s", invalidEmailAddressesString);
        AlertFieldStatus errorStatus = new AlertFieldStatus(EmailDescriptor.KEY_EMAIL_ADDITIONAL_ADDRESSES, FieldStatusSeverity.ERROR, errorMessage);
        return new MessageResult(errorMessage, List.of(errorStatus));
    }
    return new MessageResult(String.format("Successfully sent %d email(s)", totalEmailsSent));
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) SmtpConfig(com.synopsys.integration.alert.service.email.SmtpConfig) LinkableItem(com.synopsys.integration.alert.common.message.model.LinkableItem) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) EmailAttachmentFormat(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFormat) ProjectMessage(com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage) EmailJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel) EmailTarget(com.synopsys.integration.alert.service.email.EmailTarget) UUID(java.util.UUID) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HashSet(java.util.HashSet) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Aggregations

EmailTarget (com.synopsys.integration.alert.service.email.EmailTarget)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)3 SmtpConfig (com.synopsys.integration.alert.service.email.SmtpConfig)3 EmailChannelMessageModel (com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessageModel)2 AddressException (javax.mail.internet.AddressException)2 InternetAddress (javax.mail.internet.InternetAddress)2 Test (org.junit.jupiter.api.Test)2 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 EmailAttachmentFormat (com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFormat)1 ValidatedEmailAddresses (com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses)1 AlertFieldStatus (com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)1 LinkableItem (com.synopsys.integration.alert.common.message.model.LinkableItem)1 EmailJobDetailsModel (com.synopsys.integration.alert.common.persistence.model.job.details.EmailJobDetailsModel)1 ProjectMessage (com.synopsys.integration.alert.processor.api.extract.model.project.ProjectMessage)1 SmtpConfigBuilder (com.synopsys.integration.alert.service.email.SmtpConfigBuilder)1 EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1