use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.
the class ConfigActions method updateNewConfigWithSavedConfig.
@SuppressWarnings(value = "rawtypes")
public <T> T updateNewConfigWithSavedConfig(final T newConfig, final D savedConfig) throws AlertException {
try {
final Class newConfigClass = newConfig.getClass();
final Set<Field> sensitiveFields = SensitiveFieldFinder.findSensitiveFields(newConfigClass);
for (final Field field : sensitiveFields) {
field.setAccessible(true);
final Object value = field.get(newConfig);
if (value != null) {
final String newValue = (String) value;
if (StringUtils.isBlank(newValue) && savedConfig != null) {
final Class savedConfigClass = savedConfig.getClass();
Field savedField = null;
try {
savedField = savedConfigClass.getDeclaredField(field.getName());
} catch (final NoSuchFieldException e) {
continue;
}
savedField.setAccessible(true);
final String savedValue = (String) savedField.get(savedConfig);
field.set(newConfig, savedValue);
}
}
}
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new AlertException(e.getMessage(), e);
}
return newConfig;
}
use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.
the class ConfigActions method maskRestModel.
public R maskRestModel(final R restModel) throws AlertException {
try {
final Class<? extends ConfigRestModel> restModelClass = restModel.getClass();
final Set<Field> sensitiveFields = SensitiveFieldFinder.findSensitiveFields(restModelClass);
for (final Field sensitiveField : sensitiveFields) {
boolean isFieldSet = false;
sensitiveField.setAccessible(true);
final Object sensitiveFieldValue = sensitiveField.get(restModel);
if (sensitiveFieldValue != null) {
final String sensitiveFieldString = (String) sensitiveFieldValue;
if (StringUtils.isNotBlank(sensitiveFieldString)) {
isFieldSet = true;
}
}
sensitiveField.set(restModel, null);
final Field fieldIsSet = restModelClass.getDeclaredField(sensitiveField.getName() + "IsSet");
fieldIsSet.setAccessible(true);
final boolean sensitiveIsSetFieldValue = (boolean) fieldIsSet.get(restModel);
if (!sensitiveIsSetFieldValue) {
fieldIsSet.setBoolean(restModel, isFieldSet);
}
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new AlertException(e.getMessage(), e);
}
return restModel;
}
use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.
the class CommonConfigHandler method postConfig.
public ResponseEntity<String> postConfig(final R restModel) {
if (restModel == null) {
return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
}
if (!configActions.doesConfigExist(restModel.getId())) {
try {
configActions.validateConfig(restModel);
configActions.configurationChangeTriggers(restModel);
try {
final D updatedEntity = configActions.saveConfig(restModel);
return createResponse(HttpStatus.CREATED, updatedEntity.getId(), "Created");
} catch (final AlertException e) {
logger.error(e.getMessage(), e);
return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
}
} catch (final AlertFieldException e) {
final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
responseBuilder.putErrors(e.getFieldErrors());
return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
}
}
return createResponse(HttpStatus.CONFLICT, restModel.getId(), "Provided id must not be in use. To update an existing configuration, use PUT.");
}
use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.
the class CommonConfigHandler method putConfig.
public ResponseEntity<String> putConfig(final R restModel) {
if (restModel == null) {
return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
}
if (configActions.doesConfigExist(restModel.getId())) {
try {
configActions.validateConfig(restModel);
configActions.configurationChangeTriggers(restModel);
try {
final D updatedEntity = configActions.saveNewConfigUpdateFromSavedConfig(restModel);
return createResponse(HttpStatus.ACCEPTED, updatedEntity.getId(), "Updated");
} catch (final AlertException e) {
logger.error(e.getMessage(), e);
return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
}
} catch (final AlertFieldException e) {
final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
responseBuilder.putErrors(e.getFieldErrors());
return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
}
}
return createResponse(HttpStatus.BAD_REQUEST, restModel.getId(), "No configuration with the specified id.");
}
use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.
the class CommonConfigHandlerTest method postWithInternalServerErrorTest.
@Test
public void postWithInternalServerErrorTest() throws IntegrationException {
final CommonDistributionConfigActions configActions = Mockito.mock(CommonDistributionConfigActions.class);
final CommonConfigHandler<CommonDistributionConfigEntity, CommonDistributionConfigRestModel, CommonDistributionRepositoryWrapper> handler = new CommonConfigHandler<>(CommonDistributionConfigEntity.class, CommonDistributionConfigRestModel.class, configActions, objectTransformer);
Mockito.when(configActions.doesConfigExist(Mockito.anyString())).thenReturn(false);
Mockito.doNothing().when(configActions).configurationChangeTriggers(Mockito.any());
Mockito.when(configActions.saveConfig(Mockito.any())).thenThrow(new AlertException());
final CommonDistributionConfigRestModel restModel = mockCommonDistributionRestModel.createRestModel();
final ResponseEntity<String> response = handler.postConfig(restModel);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
Aggregations