Search in sources :

Example 36 with HttpUrl

use of com.synopsys.integration.rest.HttpUrl in project hub-alert by blackducksoftware.

the class BlackDuckProviderDataAccessor method getProjectByHref.

@Override
public Optional<ProviderProject> getProjectByHref(Long providerConfigId, String projectHref) {
    try {
        Optional<ConfigurationModel> providerConfigOptional = configurationModelConfigurationAccessor.getConfigurationById(providerConfigId);
        if (providerConfigOptional.isPresent()) {
            BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(providerConfigOptional.get());
            BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
            ProjectView foundProject = blackDuckApiClient.getResponse(new HttpUrl(projectHref), ProjectView.class);
            return convertBlackDuckProjects(List.of(foundProject), blackDuckApiClient).stream().findFirst();
        }
    } catch (IntegrationException e) {
        logger.errorAndDebug(createProjectNotFoundString(providerConfigId, e.getMessage()), e);
    }
    return Optional.empty();
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 37 with HttpUrl

use of com.synopsys.integration.rest.HttpUrl in project hub-alert by blackducksoftware.

the class BlackDuckGlobalFieldModelTestAction method testConfig.

@Override
public MessageResult testConfig(String configId, FieldModel fieldModel, FieldUtility registeredFieldValues) throws IntegrationException {
    Slf4jIntLogger intLogger = new Slf4jIntLogger(logger);
    String apiToken = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY);
    String url = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_URL);
    String timeout = registeredFieldValues.getStringOrEmpty(BlackDuckDescriptor.KEY_BLACKDUCK_TIMEOUT);
    Long parsedConfigurationId = ProviderProperties.UNKNOWN_CONFIG_ID;
    if (StringUtils.isNotBlank(configId)) {
        try {
            parsedConfigurationId = Long.valueOf(configId);
        } catch (NumberFormatException ex) {
            throw new AlertException("Configuration id not valid.");
        }
    }
    BlackDuckProperties blackDuckProperties = blackDuckPropertiesFactory.createProperties(parsedConfigurationId, registeredFieldValues);
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = blackDuckProperties.createServerConfigBuilderWithoutAuthentication(intLogger, NumberUtils.toInt(timeout, 300));
    blackDuckServerConfigBuilder.setApiToken(apiToken);
    blackDuckServerConfigBuilder.setUrl(url);
    validateBlackDuckConfiguration(blackDuckServerConfigBuilder);
    BlackDuckServerConfig blackDuckServerConfig = blackDuckServerConfigBuilder.build();
    ConnectionResult connectionResult = blackDuckServerConfig.attemptConnection(intLogger);
    if (connectionResult.isFailure()) {
        String failureMessage = connectionResult.getFailureMessage().orElse("");
        Exception errorException = connectionResult.getException().orElse(null);
        if (RestConstants.UNAUTHORIZED_401 == connectionResult.getHttpStatusCode()) {
            throw AlertFieldException.singleFieldError(String.format("Invalid credential(s) for: %s. %s", url, failureMessage), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, "This API Key isn't valid, try a different one.");
        } else if (connectionResult.getHttpStatusCode() > 0) {
            // TODO why are we throwing a non-alert exception?
            HttpUrl connectionUrl = new HttpUrl(url);
            throw new IntegrationRestException(HttpMethod.GET, connectionUrl, connectionResult.getHttpStatusCode(), String.format("Could not connect to: %s", url), failureMessage, errorException);
        }
        throw new AlertException(String.format("Could not connect to: %s. %s", url, failureMessage), errorException);
    }
    BlackDuckApiTokenValidator blackDuckAPITokenValidator = new BlackDuckApiTokenValidator(blackDuckProperties);
    if (!blackDuckAPITokenValidator.isApiTokenValid()) {
        throw AlertFieldException.singleFieldError(BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, "User permission failed. Cannot read notifications from Black Duck.");
    }
    return new MessageResult("Successfully connected to BlackDuck server.");
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) IntegrationException(com.synopsys.integration.exception.IntegrationException) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) HttpUrl(com.synopsys.integration.rest.HttpUrl) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) ConnectionResult(com.synopsys.integration.rest.client.ConnectionResult) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) BlackDuckApiTokenValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)

Example 38 with HttpUrl

use of com.synopsys.integration.rest.HttpUrl in project hub-alert by blackducksoftware.

the class ComponentUnknownVersionExtractor method createBomComponentDetails.

@Override
protected List<BomComponentDetails> createBomComponentDetails(ComponentUnknownVersionWithStatusNotificationContent notificationContent, BlackDuckServicesFactory blackDuckServicesFactory) throws IntegrationException {
    BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
    BlackDuckMessageBomComponentDetailsCreator bomComponentDetailsCreator = detailsCreatorFactory.createBomComponentDetailsCreator(blackDuckServicesFactory);
    List<ComponentConcern> componentConcerns = createComponentConcerns(notificationContent);
    BomComponentDetails bomComponentDetails;
    try {
        ProjectVersionComponentVersionView bomComponent = blackDuckApiClient.getResponse(new HttpUrl(notificationContent.getBomComponent()), ProjectVersionComponentVersionView.class);
        bomComponentDetails = bomComponentDetailsCreator.createBomComponentUnknownVersionDetails(bomComponent, componentConcerns, ComponentUpgradeGuidance.none(), List.of());
    } catch (IntegrationRestException e) {
        bomComponent404Handler.logIf404OrThrow(e, notificationContent.getComponentName(), null);
        bomComponentDetails = bomComponentDetailsCreator.createMissingBomComponentDetailsForUnknownVersion(notificationContent.getComponentName(), notificationContent.getBomComponent(), BlackDuckMessageBomComponentDetailsCreator.COMPONENT_VERSION_UNKNOWN, componentConcerns, ComponentUpgradeGuidance.none(), List.of());
    }
    return List.of(bomComponentDetails);
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckMessageBomComponentDetailsCreator(com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckMessageBomComponentDetailsCreator) ComponentConcern(com.synopsys.integration.alert.processor.api.extract.model.project.ComponentConcern) HttpUrl(com.synopsys.integration.rest.HttpUrl) BomComponentDetails(com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)

Example 39 with HttpUrl

use of com.synopsys.integration.rest.HttpUrl in project hub-alert by blackducksoftware.

the class BomEditNotificationDetailExtractor method retrieveProjectAndVersion.

private Optional<ProjectVersionWrapper> retrieveProjectAndVersion(Long blackDuckConfigId, String projectVersionUrl) {
    try {
        BlackDuckServicesFactory blackDuckServicesFactory = servicesFactoryCache.retrieveBlackDuckServicesFactory(blackDuckConfigId);
        BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
        ProjectVersionView projectVersion = blackDuckApiClient.getResponse(new HttpUrl(projectVersionUrl), ProjectVersionView.class);
        ProjectView projectView = blackDuckApiClient.getResponse(projectVersion.metaProjectLink());
        return Optional.of(new ProjectVersionWrapper(projectView, projectVersion));
    } catch (IntegrationException e) {
        logger.error("Failed to connect to BlackDuck. Config ID: {}", blackDuckConfigId, e);
    }
    return Optional.empty();
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 40 with HttpUrl

use of com.synopsys.integration.rest.HttpUrl in project hub-alert by blackducksoftware.

the class BomEditNotificationMessageExtractor method createBomComponentDetails.

@Override
protected List<BomComponentDetails> createBomComponentDetails(BomEditWithProjectNameNotificationContent notificationContent, BlackDuckServicesFactory blackDuckServicesFactory) throws IntegrationException {
    BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
    BlackDuckMessageBomComponentDetailsCreator bomComponentDetailsCreator = detailsCreatorFactory.createBomComponentDetailsCreator(blackDuckServicesFactory);
    BomComponentDetails bomComponentDetails;
    try {
        ProjectVersionComponentVersionView bomComponent = blackDuckApiClient.getResponse(new HttpUrl(notificationContent.getBomComponent()), ProjectVersionComponentVersionView.class);
        bomComponentDetails = bomComponentDetailsCreator.createBomComponentDetails(bomComponent, List.of(), ComponentUpgradeGuidance.none(), List.of());
    } catch (IntegrationRestException e) {
        bomComponent404Handler.logIf404OrThrow(e, notificationContent.getComponentName(), notificationContent.getComponentVersionName());
        bomComponentDetails = bomComponentDetailsCreator.createMissingBomComponentDetails(notificationContent.getComponentName(), notificationContent.getBomComponent(), notificationContent.getComponentVersionName(), notificationContent.getBomComponent(), List.of(), ComponentUpgradeGuidance.none(), List.of());
    }
    return List.of(bomComponentDetails);
}
Also used : IntegrationRestException(com.synopsys.integration.rest.exception.IntegrationRestException) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckMessageBomComponentDetailsCreator(com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckMessageBomComponentDetailsCreator) HttpUrl(com.synopsys.integration.rest.HttpUrl) BomComponentDetails(com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails) ProjectVersionComponentVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)

Aggregations

HttpUrl (com.synopsys.integration.rest.HttpUrl)65 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)35 ProjectVersionComponentVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionComponentVersionView)29 Test (org.junit.jupiter.api.Test)28 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)23 ResourceMetadata (com.synopsys.integration.blackduck.api.core.ResourceMetadata)21 BomComponentDetails (com.synopsys.integration.alert.processor.api.extract.model.project.BomComponentDetails)18 IntegrationException (com.synopsys.integration.exception.IntegrationException)15 IntegrationRestException (com.synopsys.integration.rest.exception.IntegrationRestException)15 ComponentUpgradeGuidance (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentUpgradeGuidance)13 ResourceLink (com.synopsys.integration.blackduck.api.core.ResourceLink)9 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)9 UrlSingleResponse (com.synopsys.integration.blackduck.api.core.response.UrlSingleResponse)7 ProjectVersionComponentVersionLicensesView (com.synopsys.integration.blackduck.api.generated.component.ProjectVersionComponentVersionLicensesView)7 ComponentPolicy (com.synopsys.integration.alert.processor.api.extract.model.project.ComponentPolicy)6 ComponentVersionUpgradeGuidanceView (com.synopsys.integration.blackduck.api.generated.response.ComponentVersionUpgradeGuidanceView)6 ComponentPolicyRulesView (com.synopsys.integration.blackduck.api.generated.view.ComponentPolicyRulesView)6 PolicyRuleView (com.synopsys.integration.blackduck.api.generated.view.PolicyRuleView)6 BlackDuckMessageBomComponentDetailsCreator (com.synopsys.integration.alert.provider.blackduck.processor.message.service.BlackDuckMessageBomComponentDetailsCreator)5 ComponentVersionView (com.synopsys.integration.blackduck.api.generated.view.ComponentVersionView)5