Search in sources :

Example 41 with IntegrationException

use of com.synopsys.integration.exception.IntegrationException in project hub-alert by blackducksoftware.

the class ProviderDataAccessorTest method getEmailAddressesForNonExistentProjectHrefTest.

@Test
void getEmailAddressesForNonExistentProjectHrefTest() throws Exception {
    Mockito.when(blackDuckApiClient.getResponse(Mockito.any(HttpUrl.class), Mockito.eq(ProjectView.class))).thenThrow(new IntegrationException("Could not find the project."));
    Long providerConfigId = providerConfiguration.getConfigurationId();
    BlackDuckProviderDataAccessor providerDataAccessor = new BlackDuckProviderDataAccessor(configurationModelConfigurationAccessor, blackDuckPropertiesFactory);
    Set<String> foundEmailAddresses = providerDataAccessor.getEmailAddressesForProjectHref(providerConfigId, "expecting no results");
    assertEquals(0, foundEmailAddresses.size());
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) HttpUrl(com.synopsys.integration.rest.HttpUrl) Test(org.junit.jupiter.api.Test)

Example 42 with IntegrationException

use of com.synopsys.integration.exception.IntegrationException in project hub-alert by blackducksoftware.

the class BlackDuckApiTokenValidator method hasPermittedRole.

private boolean hasPermittedRole(BlackDuckServicesFactory blackDuckServicesFactory) {
    BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
    ApiDiscovery apiDiscovery = blackDuckServicesFactory.getApiDiscovery();
    UserView currentUser;
    try {
        currentUser = blackDuckApiClient.getResponse(apiDiscovery.metaCurrentUserLink());
    } catch (IntegrationException integrationException) {
        logger.error("Failed to GET the currently authenticated Black Duck user", integrationException);
        return false;
    }
    try {
        List<RoleAssignmentView> allRolesForCurrentUser = blackDuckApiClient.getAllResponses(currentUser.metaRolesLink());
        return allRolesForCurrentUser.stream().anyMatch(this::isPermittedRole);
    } catch (IntegrationException integrationException) {
        logger.error("Failed to GET the currently authenticated Black Duck user's roles", integrationException);
    }
    return false;
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) RoleAssignmentView(com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView)

Example 43 with IntegrationException

use of com.synopsys.integration.exception.IntegrationException in project hub-alert by blackducksoftware.

the class JiraIssueCreator method createIssueAndExtractDetails.

@Override
protected final ExistingIssueDetails<String> createIssueAndExtractDetails(IssueCreationModel alertIssueCreationModel) throws AlertException {
    MessageReplacementValues replacementValues = alertIssueCreationModel.getSource().map(this::createCustomFieldReplacementValues).orElse(new MessageReplacementValues.Builder(alertIssueCreationModel.getProvider().getLabel(), MessageReplacementValues.DEFAULT_NOTIFICATION_REPLACEMENT_VALUE).build());
    T creationRequest = createIssueCreationRequest(alertIssueCreationModel, replacementValues);
    try {
        IssueCreationResponseModel issueCreationResponseModel = createIssue(creationRequest);
        IssueResponseModel createdIssue = fetchIssue(issueCreationResponseModel.getKey());
        IssueFieldsComponent createdIssueFields = createdIssue.getFields();
        String issueUILink = JiraCallbackUtils.createUILink(createdIssue);
        IssueCategory issueCategory = alertIssueCreationModel.getSource().map(issueCategoryRetriever::retrieveIssueCategoryFromProjectIssueModel).orElse(IssueCategory.BOM);
        return new ExistingIssueDetails<>(createdIssue.getId(), createdIssue.getKey(), createdIssueFields.getSummary(), issueUILink, IssueStatus.RESOLVABLE, issueCategory);
    } catch (IntegrationRestException restException) {
        throw jiraErrorMessageUtility.improveRestException(restException, issueCreatorDescriptorKey, extractReporter(creationRequest));
    } catch (JiraPreconditionNotMetException jiraException) {
        String message = StringUtils.join(FAILED_TO_CREATE_ISSUE_MESSAGE, jiraException.getMessage(), " ");
        throw new AlertException(message, jiraException);
    } catch (IntegrationException intException) {
        throw new AlertException(FAILED_TO_CREATE_ISSUE_MESSAGE, intException);
    }
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) IssueCreationResponseModel(com.synopsys.integration.jira.common.model.response.IssueCreationResponseModel) IssueCategory(com.synopsys.integration.alert.api.channel.issue.search.enumeration.IssueCategory) IntegrationException(com.synopsys.integration.exception.IntegrationException) IssueResponseModel(com.synopsys.integration.jira.common.model.response.IssueResponseModel) MessageReplacementValues(com.synopsys.integration.alert.api.channel.jira.distribution.custom.MessageReplacementValues) JiraPreconditionNotMetException(com.synopsys.integration.jira.common.exception.JiraPreconditionNotMetException) ExistingIssueDetails(com.synopsys.integration.alert.api.channel.issue.search.ExistingIssueDetails) IssueFieldsComponent(com.synopsys.integration.jira.common.model.components.IssueFieldsComponent) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 44 with IntegrationException

use of com.synopsys.integration.exception.IntegrationException in project hub-alert by blackducksoftware.

the class AuthenticationFieldModelTestAction method performSAMLTest.

private void performSAMLTest(FieldUtility registeredFieldValues) throws IntegrationException {
    List<AlertFieldStatus> errors = new ArrayList<>();
    Optional<String> registeredEntityId = registeredFieldValues.getString(AuthenticationDescriptor.KEY_SAML_ENTITY_ID);
    if (registeredEntityId.isEmpty()) {
        errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_SAML_ENTITY_ID, "Entity ID missing."));
    }
    logger.info("Testing SAML Metadata URL...");
    try {
        Optional<ExtendedMetadataDelegate> provider = samlManager.createHttpProvider(registeredFieldValues.getStringOrEmpty(AuthenticationDescriptor.KEY_SAML_METADATA_URL));
        if (provider.isPresent()) {
            ExtendedMetadataDelegate extendedMetadataDelegate = provider.get();
            extendedMetadataDelegate.initialize();
        }
    } catch (Exception ex) {
        logger.error("Testing SAML Metadata URL error: ", ex);
        errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_SAML_METADATA_URL, ex.getMessage()));
    }
    logger.info("Testing SAML Metadata File...");
    try {
        Optional<ExtendedMetadataDelegate> provider = samlManager.createFileProvider();
        if (provider.isPresent()) {
            ExtendedMetadataDelegate extendedMetadataDelegate = provider.get();
            extendedMetadataDelegate.initialize();
        }
    } catch (Exception ex) {
        logger.error("Testing SAML Metadata File error: ", ex);
        errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_SAML_METADATA_FILE, ex.getMessage()));
    }
    samlManager.initializeConfiguration();
    if (!errors.isEmpty()) {
        throw new AlertFieldException(errors);
    }
}
Also used : ArrayList(java.util.ArrayList) ExtendedMetadataDelegate(org.springframework.security.saml.metadata.ExtendedMetadataDelegate) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) IntegrationException(com.synopsys.integration.exception.IntegrationException)

Example 45 with IntegrationException

use of com.synopsys.integration.exception.IntegrationException in project hub-alert by blackducksoftware.

the class EmailMessagingService method sendEmailMessage.

public void sendEmailMessage(Properties javamailProperties, String smtpFrom, String smtpHost, int smtpPort, boolean smtpAuth, String smtpUsername, String smtpPassword, EmailTarget emailTarget) throws AlertException {
    try {
        String templateName = StringUtils.trimToEmpty(emailTarget.getTemplateName());
        Set<String> emailAddresses = emailTarget.getEmailAddresses().stream().map(String::trim).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
        if (emailAddresses.isEmpty() || StringUtils.isBlank(templateName)) {
            // Nothing to send
            logger.debug("No non-blank email addresses were provided");
            return;
        }
        Map<String, Object> model = emailTarget.getModel();
        Session session = Session.getInstance(javamailProperties);
        TemplateLoader templateLoader = freemarkerTemplatingService.createClassTemplateLoader("/templates/email");
        Configuration templateDirectory = freemarkerTemplatingService.createFreemarkerConfig(templateLoader);
        Template emailTemplate = templateDirectory.getTemplate(templateName);
        String html = freemarkerTemplatingService.resolveTemplate(model, emailTemplate);
        MimeMultipartBuilder mimeMultipartBuilder = new MimeMultipartBuilder();
        mimeMultipartBuilder.addHtmlContent(html);
        mimeMultipartBuilder.addTextContent(Jsoup.parse(html).text());
        mimeMultipartBuilder.addEmbeddedImages(emailTarget.getContentIdsToFilePaths());
        List<String> attachmentFilePaths = emailTarget.getAttachmentFilePaths();
        if (!attachmentFilePaths.isEmpty()) {
            mimeMultipartBuilder.addAttachments(attachmentFilePaths);
        }
        MimeMultipart mimeMultipart = mimeMultipartBuilder.build();
        String subjectLine = (String) model.get(EmailPropertyKeys.TEMPLATE_KEY_SUBJECT_LINE.getPropertyKey());
        if (StringUtils.isBlank(subjectLine)) {
            subjectLine = "Default Subject Line - please define one";
        }
        Template subjectLineTemplate = new Template(EMAIL_SUBJECT_LINE_TEMPLATE, subjectLine, templateDirectory);
        String resolvedSubjectLine = freemarkerTemplatingService.resolveTemplate(model, subjectLineTemplate);
        List<Message> messages = createMessages(emailAddresses, resolvedSubjectLine, session, mimeMultipart, smtpFrom);
        sendMessages(smtpAuth, smtpHost, smtpPort, smtpUsername, smtpPassword, session, messages);
    } catch (MessagingException | IOException | IntegrationException ex) {
        String errorMessage = "Could not send the email. " + ex.getMessage();
        throw new AlertException(errorMessage, ex);
    }
}
Also used : Configuration(freemarker.template.Configuration) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IntegrationException(com.synopsys.integration.exception.IntegrationException) MessagingException(javax.mail.MessagingException) TemplateLoader(freemarker.cache.TemplateLoader) IOException(java.io.IOException) Template(freemarker.template.Template) MimeMultipart(javax.mail.internet.MimeMultipart) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Session(javax.mail.Session)

Aggregations

IntegrationException (com.synopsys.integration.exception.IntegrationException)53 HttpUrl (com.synopsys.integration.rest.HttpUrl)19 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)14 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)13 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)12 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)11 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)10 UserView (com.synopsys.integration.blackduck.api.generated.view.UserView)9 ArrayList (java.util.ArrayList)9 ProviderProject (com.synopsys.integration.alert.common.persistence.model.ProviderProject)8 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)8 Set (java.util.Set)8 Test (org.junit.jupiter.api.Test)8 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)7 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)7 List (java.util.List)7 Optional (java.util.Optional)7 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)6 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)6 AlertPagedModel (com.synopsys.integration.alert.common.rest.model.AlertPagedModel)6