Search in sources :

Example 1 with BlackDuckServerConfigBuilder

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder in project hub-alert by blackducksoftware.

the class BlackDuckProperties method createBlackDuckServerConfig.

public BlackDuckServerConfig createBlackDuckServerConfig(IntLogger logger, int blackDuckTimeout, String blackDuckApiToken) throws AlertException {
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = createServerConfigBuilderWithoutAuthentication(logger, blackDuckTimeout);
    blackDuckServerConfigBuilder.setApiToken(blackDuckApiToken);
    try {
        return blackDuckServerConfigBuilder.build();
    } catch (IllegalStateException e) {
        throw new AlertException(e.getMessage(), e);
    }
}
Also used : BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 2 with BlackDuckServerConfigBuilder

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder in project hub-alert by blackducksoftware.

the class BlackDuckProviderService method setupBlackDuckServicesFactory.

private BlackDuckServicesFactory setupBlackDuckServicesFactory() {
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = new BlackDuckServerConfigBuilder();
    blackDuckServerConfigBuilder.setUrl(blackDuckProviderUrl).setApiToken(blackDuckApiToken).setTimeoutInSeconds(blackDuckTimeout).setTrustCert(true);
    BlackDuckServerConfig blackDuckServerConfig = blackDuckServerConfigBuilder.build();
    return blackDuckServerConfig.createBlackDuckServicesFactory(intLogger);
}
Also used : BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)

Example 3 with BlackDuckServerConfigBuilder

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder in project hub-alert by blackducksoftware.

the class BlackDuckProperties method createServerConfigBuilderWithoutAuthentication.

public BlackDuckServerConfigBuilder createServerConfigBuilderWithoutAuthentication(IntLogger logger, int blackDuckTimeout) {
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = new BlackDuckServerConfigBuilder();
    String blackDuckUrl = getBlackDuckUrl().orElse("");
    blackDuckServerConfigBuilder.setProperties(createBlackDuckProperties(blackDuckUrl).entrySet());
    blackDuckServerConfigBuilder.setLogger(logger);
    blackDuckServerConfigBuilder.setTimeoutInSeconds(blackDuckTimeout);
    blackDuckServerConfigBuilder.setUrl(blackDuckUrl);
    return blackDuckServerConfigBuilder;
}
Also used : BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)

Example 4 with BlackDuckServerConfigBuilder

use of com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder 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)

Aggregations

BlackDuckServerConfigBuilder (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)4 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 BlackDuckServerConfig (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig)2 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)1 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)1 BlackDuckProperties (com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties)1 BlackDuckApiTokenValidator (com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)1 HttpUrl (com.synopsys.integration.rest.HttpUrl)1 ConnectionResult (com.synopsys.integration.rest.client.ConnectionResult)1 IntegrationRestException (com.synopsys.integration.rest.exception.IntegrationRestException)1