use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.
the class AzureBoardsGlobalApiAction method beforeSaveAction.
@Override
public FieldModel beforeSaveAction(FieldModel fieldModel) throws AlertException {
FieldModel updatedFieldModel = super.beforeSaveAction(fieldModel);
Optional<DescriptorKey> descriptorKey = descriptorMap.getDescriptorKey(fieldModel.getDescriptorName());
if (!descriptorKey.isPresent()) {
return updatedFieldModel;
}
ConfigContextEnum context = ConfigContextEnum.valueOf(fieldModel.getContext());
List<ConfigurationModel> existingConfig = configurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(descriptorKey.get(), context);
Optional<ConfigurationModel> configurationModel = existingConfig.stream().findFirst();
return configurationModel.map(config -> updateTokenFields(updatedFieldModel, config)).orElse(updatedFieldModel);
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertException 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.");
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.
the class BlackDuckIssueTrackerCallbackEventHandler method createBlackDuckServicesFactory.
private Optional<BlackDuckServicesFactory> createBlackDuckServicesFactory(BlackDuckProperties blackDuckProperties) {
IntLogger intLogger = new Slf4jIntLogger(logger);
try {
BlackDuckHttpClient blackDuckHttpClient = blackDuckProperties.createBlackDuckHttpClient(intLogger);
BlackDuckServicesFactory blackDuckServicesFactory = blackDuckProperties.createBlackDuckServicesFactory(blackDuckHttpClient, intLogger);
return Optional.of(blackDuckServicesFactory);
} catch (AlertException e) {
logger.error("Failed to create a BlackDuck http client", e);
return Optional.empty();
}
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.
the class JobConfigActions method createDistributionJobRequestModel.
private DistributionJobRequestModel createDistributionJobRequestModel(Collection<ConfigurationFieldModel> configFieldModels, List<JobProviderProjectFieldModel> jobProjects, OffsetDateTime createdAt, @Nullable OffsetDateTime lastUpdated) throws AlertException {
List<BlackDuckProjectDetailsModel> projectFilterDetails = jobProjects.stream().map(jobProject -> new BlackDuckProjectDetailsModel(jobProject.getName(), jobProject.getHref())).collect(Collectors.toList());
Map<String, ConfigurationFieldModel> configuredFieldsMap = DataStructureUtils.mapToValues(configFieldModels, ConfigurationFieldModel::getFieldKey);
DistributionJobModel fromResource = distributionJobModelExtractor.convertToJobModel(null, configuredFieldsMap, createdAt, lastUpdated, projectFilterDetails);
return new DistributionJobRequestModel(fromResource.isEnabled(), fromResource.getName(), fromResource.getDistributionFrequency(), fromResource.getProcessingType(), fromResource.getChannelDescriptorName(), fromResource.getChannelGlobalConfigId(), fromResource.getBlackDuckGlobalConfigId(), fromResource.isFilterByProject(), fromResource.getProjectNamePattern().orElse(null), fromResource.getProjectVersionNamePattern().orElse(null), fromResource.getNotificationTypes(), projectFilterDetails, fromResource.getPolicyFilterPolicyNames(), fromResource.getVulnerabilityFilterSeverityNames(), fromResource.getDistributionJobDetails());
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.
the class ConfigActions method updateWithoutChecks.
@Override
protected ActionResponse<FieldModel> updateWithoutChecks(Long id, FieldModel resource) {
Optional<ConfigurationModel> optionalPreviousConfig = configurationModelConfigurationAccessor.getConfigurationById(id);
FieldModel previousFieldModel = optionalPreviousConfig.isPresent() ? modelConverter.convertToFieldModel(optionalPreviousConfig.get()) : null;
try {
FieldModel updatedFieldModel = fieldModelProcessor.performBeforeUpdateAction(resource);
Collection<ConfigurationFieldModel> updatedFields = fieldModelProcessor.fillFieldModelWithExistingData(id, updatedFieldModel);
ConfigurationModel configurationModel = configurationModelConfigurationAccessor.updateConfiguration(id, updatedFields);
globalConfigurationModelToConcreteConversionService.updateDefaultConcreteModel(updatedFieldModel.getDescriptorName(), configurationModel);
FieldModel dbSavedModel = modelConverter.convertToFieldModel(configurationModel);
FieldModel afterUpdateAction = fieldModelProcessor.performAfterUpdateAction(previousFieldModel, dbSavedModel);
FieldModel responseModel = dbSavedModel.fill(afterUpdateAction);
return new ActionResponse<>(HttpStatus.OK, responseModel);
} catch (AlertException ex) {
logger.error("Error creating configuration", ex);
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("Error creating config: %s", ex.getMessage()));
}
}
Aggregations