Search in sources :

Example 16 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class EmailMessagingService method sendMessages.

public void sendMessages(boolean auth, String host, int port, String username, String password, Session session, List<Message> messages) throws AlertException {
    Set<String> errorMessages = new HashSet<>();
    try (Transport transport = getAndConnectTransport(auth, host, port, username, password, session)) {
        for (Message message : messages) {
            Optional<String> errors = sendMessage(transport, message);
            errors.ifPresent(errorMessages::add);
        }
    } catch (MessagingException e) {
        String errorMessage = "Could not setup the email transport: " + e.getMessage();
        logger.error(errorMessage);
        throw new AlertException(errorMessage, e);
    }
    logger.trace("Transport session closed.");
    if (!errorMessages.isEmpty()) {
        String joinedErrorMessages = StringUtils.join(errorMessages, System.lineSeparator());
        logger.error(joinedErrorMessages);
        throw new AlertException(joinedErrorMessages);
    }
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HashSet(java.util.HashSet)

Example 17 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class AlertStartupInitializer method initializeConfiguration.

private void initializeConfiguration(Collection<DescriptorKey> descriptorKeys) {
    for (DescriptorKey descriptorKey : descriptorKeys) {
        logger.info(LINE_DIVIDER);
        logger.info("Descriptor: {}", descriptorKey.getUniversalKey());
        logger.info(LINE_DIVIDER);
        logger.info("  Starting Descriptor Initialization...");
        try {
            List<DefinedFieldModel> fieldsForDescriptor = descriptorAccessor.getFieldsForDescriptor(descriptorKey, ConfigContextEnum.GLOBAL).stream().sorted(Comparator.comparing(DefinedFieldModel::getKey)).collect(Collectors.toList());
            List<ConfigurationModel> foundConfigurationModels = fieldConfigurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(descriptorKey, ConfigContextEnum.GLOBAL);
            Map<String, ConfigurationFieldModel> existingConfiguredFields = new HashMap<>();
            foundConfigurationModels.forEach(config -> existingConfiguredFields.putAll(config.getCopyOfKeyToFieldMap()));
            Set<ConfigurationFieldModel> configurationModels = createFieldModelsFromDefinedFields(descriptorKey, fieldsForDescriptor, existingConfiguredFields);
            logConfiguration(configurationModels);
            updateConfigurationFields(descriptorKey, foundConfigurationModels, configurationModels);
        } catch (IllegalArgumentException | SecurityException | AlertException ex) {
            logger.error("error initializing descriptor", ex);
        } finally {
            logger.info("  Finished Descriptor Initialization...");
            logger.info(LINE_DIVIDER);
        }
    }
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) HashMap(java.util.HashMap) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 18 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class ConfigurationOverridesStartupComponent method initialize.

@Override
protected void initialize() {
    try {
        FieldModel fieldModel = getFieldModel();
        checkAndDisableLdapAuthentication(fieldModel);
        checkAndDisableSamlAuthentication(fieldModel);
        checkAndResetDefaultAdminPassword();
        if (StringUtils.isBlank(fieldModel.getId())) {
            configUtility.save(fieldModel);
        } else {
            configUtility.update(Long.valueOf(fieldModel.getId()), fieldModel);
        }
    } catch (AlertException | NumberFormatException ex) {
        logger.error("Error performing configuration overrides.", ex);
    }
}
Also used : FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 19 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class EmailMessagingService method createMessages.

private List<Message> createMessages(Collection<String> emailAddresses, String subjectLine, Session session, MimeMultipart mimeMultipart, String fromString) throws AlertException, MessagingException {
    List<InternetAddress> addresses = new ArrayList<>();
    Set<String> invalidAddresses = new HashSet<>();
    for (String emailAddress : emailAddresses) {
        try {
            InternetAddress toAddress = new InternetAddress(emailAddress);
            toAddress.validate();
            addresses.add(toAddress);
        } catch (AddressException e) {
            invalidAddresses.add(emailAddress);
            logger.warn("Could not create the address from {}: {}", emailAddress, e.getMessage());
        }
    }
    if (addresses.isEmpty()) {
        String noValidAddressesErrorMessage = "There were no valid email addresses supplied.";
        if (!invalidAddresses.isEmpty()) {
            String invalidAddressesString = StringUtils.join(invalidAddresses, ", ");
            noValidAddressesErrorMessage = String.format("%s Invalid addresses: %s", noValidAddressesErrorMessage, invalidAddressesString);
        }
        throw new AlertException(noValidAddressesErrorMessage);
    }
    InternetAddress fromAddress;
    if (StringUtils.isBlank(fromString)) {
        logger.warn("No 'from' address specified");
        throw new AlertException(String.format("Required field '%s' was blank", EmailPropertyKeys.JAVAMAIL_FROM_KEY.getPropertyKey()));
    } else {
        fromAddress = new InternetAddress(fromString);
        try {
            fromAddress.validate();
        } catch (AddressException e) {
            logger.warn("Invalid 'from' address specified: {}", fromString);
            throw new AlertException(String.format("'%s' is not a valid email address: %s", fromString, e.getMessage()));
        }
    }
    List<Message> messages = new ArrayList<>(addresses.size());
    for (InternetAddress address : addresses) {
        Message message = new MimeMessage(session);
        message.setContent(mimeMultipart);
        message.setFrom(fromAddress);
        message.setRecipients(Message.RecipientType.TO, new Address[] { address });
        message.setSubject(subjectLine);
        messages.add(message);
    }
    return messages;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HashSet(java.util.HashSet)

Example 20 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class AzureCustomFieldManager method findOrCreateAlertCustomProjectField.

private ProjectWorkItemFieldModel findOrCreateAlertCustomProjectField(String projectName, String fieldName, String fieldReferenceName, String fieldDescription) throws AlertException {
    ProjectWorkItemFieldModel fieldRequestModel = ProjectWorkItemFieldModel.workItemStringField(fieldName, fieldReferenceName, fieldDescription);
    Optional<ProjectWorkItemFieldModel> customField = getAlertCustomProjectField(projectName, fieldRequestModel.getReferenceName());
    if (customField.isPresent()) {
        return customField.get();
    }
    // custom field not found so create it
    try {
        return projectService.createProjectField(organizationName, projectName, fieldRequestModel);
    } catch (IOException e) {
        throw new AlertException(String.format("There was a problem creating the request to create the Alert Custom Field (%s) in the Azure project: %s", fieldReferenceName, projectName), e);
    } catch (HttpServiceException e) {
        throw new AlertException(String.format("There was a problem creating the Alert Custom Field (%s) in the Azure project: %s", fieldReferenceName, projectName), e);
    }
}
Also used : HttpServiceException(com.synopsys.integration.azure.boards.common.http.HttpServiceException) ProjectWorkItemFieldModel(com.synopsys.integration.azure.boards.common.service.project.ProjectWorkItemFieldModel) IOException(java.io.IOException) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Aggregations

AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)101 Test (org.junit.jupiter.api.Test)35 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)22 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)18 List (java.util.List)18 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)17 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)16 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)16 Optional (java.util.Optional)13 IssueTrackerModelHolder (com.synopsys.integration.alert.api.channel.issue.model.IssueTrackerModelHolder)12 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)12 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)11 HashMap (java.util.HashMap)11 IntegrationException (com.synopsys.integration.exception.IntegrationException)10 ArrayList (java.util.ArrayList)9 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)8 DistributionJobDetailsModel (com.synopsys.integration.alert.common.persistence.model.job.details.DistributionJobDetailsModel)8 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)8 UUID (java.util.UUID)8 IssueOperation (com.synopsys.integration.alert.common.channel.issuetracker.enumeration.IssueOperation)7