Search in sources :

Example 16 with AlertException

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;
}
Also used : Field(java.lang.reflect.Field) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 17 with AlertException

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;
}
Also used : Field(java.lang.reflect.Field) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 18 with AlertException

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.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 19 with AlertException

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.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 20 with AlertException

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());
}
Also used : CommonDistributionConfigActions(com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) Test(org.junit.Test)

Aggregations

AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)24 Test (org.junit.Test)9 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)8 CommonDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity)8 CommonDistributionRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper)6 AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)5 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)5 CommonDistributionConfigRestModel (com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel)5 ArrayList (java.util.ArrayList)4 Logger (org.slf4j.Logger)4 NotificationManager (com.blackducksoftware.integration.hub.alert.NotificationManager)3 AuditEntryRepositoryWrapper (com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryRepositoryWrapper)3 AuditNotificationRepositoryWrapper (com.blackducksoftware.integration.hub.alert.audit.repository.AuditNotificationRepositoryWrapper)3 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)3 DistributionChannelConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.distribution.DistributionChannelConfigEntity)3 GlobalChannelConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalChannelConfigEntity)3 ProjectData (com.blackducksoftware.integration.hub.alert.digest.model.ProjectData)3 AbstractChannelEvent (com.blackducksoftware.integration.hub.alert.event.AbstractChannelEvent)3 Field (java.lang.reflect.Field)3 AuditEntryEntity (com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryEntity)2