Search in sources :

Example 11 with AlertException

use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.

the class GlobalProperties method createHubServerConfig.

public HubServerConfig createHubServerConfig(final IntLogger logger, final int hubTimeout, final String hubUsername, final String hubPassword) throws AlertException {
    final HubServerConfigBuilder hubServerConfigBuilder = createHubServerConfigBuilderWithoutAuthentication(logger, hubTimeout);
    hubServerConfigBuilder.setUsername(hubUsername);
    hubServerConfigBuilder.setPassword(hubPassword);
    try {
        return hubServerConfigBuilder.build();
    } catch (final IllegalStateException e) {
        throw new AlertException(e.getMessage(), e);
    }
}
Also used : HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 12 with AlertException

use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.

the class AuditEntryActions method createRestModel.

private AuditEntryRestModel createRestModel(final AuditEntryEntity auditEntryEntity) {
    final Long commonConfigId = auditEntryEntity.getCommonConfigId();
    final List<AuditNotificationRelation> relations = auditNotificationRepository.findByAuditEntryId(auditEntryEntity.getId());
    final List<Long> notificationIds = relations.stream().map(relation -> relation.getNotificationId()).collect(Collectors.toList());
    final List<NotificationModel> notifications = notificationManager.findByIds(notificationIds);
    final CommonDistributionConfigEntity commonConfigEntity = commonDistributionRepository.findOne(commonConfigId);
    final String id = objectTransformer.objectToString(auditEntryEntity.getId());
    final String timeCreated = objectTransformer.objectToString(auditEntryEntity.getTimeCreated());
    final String timeLastSent = objectTransformer.objectToString(auditEntryEntity.getTimeLastSent());
    String status = null;
    if (auditEntryEntity.getStatus() != null) {
        status = auditEntryEntity.getStatus().getDisplayName();
    }
    final String errorMessage = auditEntryEntity.getErrorMessage();
    final String errorStackTrace = auditEntryEntity.getErrorStackTrace();
    NotificationRestModel notificationRestModel = null;
    if (!notifications.isEmpty() && notifications.get(0) != null) {
        try {
            notificationRestModel = objectTransformer.databaseEntityToConfigRestModel(notifications.get(0).getNotificationEntity(), NotificationRestModel.class);
            final Set<String> notificationTypes = notifications.stream().map(notification -> notification.getNotificationType().name()).collect(Collectors.toSet());
            notificationRestModel.setNotificationTypes(notificationTypes);
            final Set<ComponentRestModel> components = notifications.stream().map(notification -> new ComponentRestModel(notification.getComponentName(), notification.getComponentVersion(), notification.getPolicyRuleName(), notification.getPolicyRuleUser())).collect(Collectors.toSet());
            notificationRestModel.setComponents(components);
        } catch (final AlertException e) {
            logger.error("Problem converting audit entry with id {}: {}", auditEntryEntity.getId(), e.getMessage());
        }
    }
    String distributionConfigName = null;
    String eventType = null;
    if (commonConfigEntity != null) {
        distributionConfigName = commonConfigEntity.getName();
        eventType = commonConfigEntity.getDistributionType();
    }
    return new AuditEntryRestModel(id, distributionConfigName, eventType, timeCreated, timeLastSent, status, errorMessage, errorStackTrace, notificationRestModel);
}
Also used : AuditEntryRepositoryWrapper(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryRepositoryWrapper) ChannelEventFactory(com.blackducksoftware.integration.hub.alert.channel.manager.ChannelEventFactory) AuditEntryEntity(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryEntity) DistributionChannelConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.distribution.DistributionChannelConfigEntity) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) ChannelTemplateManager(com.blackducksoftware.integration.hub.alert.channel.ChannelTemplateManager) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) NotificationManager(com.blackducksoftware.integration.hub.alert.NotificationManager) ArrayList(java.util.ArrayList) AuditNotificationRepositoryWrapper(com.blackducksoftware.integration.hub.alert.audit.repository.AuditNotificationRepositoryWrapper) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) NotificationModel(com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel) AuditNotificationRelation(com.blackducksoftware.integration.hub.alert.audit.repository.relation.AuditNotificationRelation) ComponentRestModel(com.blackducksoftware.integration.hub.alert.web.model.ComponentRestModel) Logger(org.slf4j.Logger) GlobalChannelConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalChannelConfigEntity) Transactional(javax.transaction.Transactional) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ProjectDataFactory(com.blackducksoftware.integration.hub.alert.digest.model.ProjectDataFactory) List(java.util.List) Component(org.springframework.stereotype.Component) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData) NotificationRestModel(com.blackducksoftware.integration.hub.alert.web.model.NotificationRestModel) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) AbstractChannelEvent(com.blackducksoftware.integration.hub.alert.event.AbstractChannelEvent) ComponentRestModel(com.blackducksoftware.integration.hub.alert.web.model.ComponentRestModel) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) NotificationModel(com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel) NotificationRestModel(com.blackducksoftware.integration.hub.alert.web.model.NotificationRestModel) AuditNotificationRelation(com.blackducksoftware.integration.hub.alert.audit.repository.relation.AuditNotificationRelation) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 13 with AlertException

use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.

the class DistributionChannel method sendAuditedMessage.

public void sendAuditedMessage(final E event, final C config) throws IntegrationException {
    try {
        sendMessage(event, config);
        setAuditEntrySuccess(event.getAuditEntryId());
    } catch (final Exception e) {
        setAuditEntryFailure(event.getAuditEntryId(), e.getMessage(), e);
        if (e instanceof IntegrationRestException) {
            logger.error(((IntegrationRestException) e).getHttpStatusCode() + ":" + ((IntegrationRestException) e).getHttpStatusMessage());
        }
        logger.error(e.getMessage(), e);
        throw new AlertException(e.getMessage());
    }
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

Example 14 with AlertException

use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.

the class AuditEntryActions method resendNotification.

public List<AuditEntryRestModel> resendNotification(final Long id) throws IntegrationException, IllegalArgumentException {
    AuditEntryEntity auditEntryEntity = null;
    auditEntryEntity = auditEntryRepository.findOne(id);
    if (auditEntryEntity == null) {
        throw new AlertException("No audit entry with the provided id exists.");
    }
    final List<AuditNotificationRelation> relations = auditNotificationRepository.findByAuditEntryId(auditEntryEntity.getId());
    final List<Long> notificationIds = relations.stream().map(relation -> relation.getNotificationId()).collect(Collectors.toList());
    final List<NotificationModel> notifications = notificationManager.findByIds(notificationIds);
    final Long commonConfigId = auditEntryEntity.getCommonConfigId();
    final CommonDistributionConfigEntity commonConfigEntity = commonDistributionRepository.findOne(commonConfigId);
    if (notifications == null || notifications.isEmpty()) {
        throw new IllegalArgumentException("The notification for this entry was purged. To edit the purge schedule, please see the Scheduling Configuration.");
    }
    if (commonConfigEntity == null) {
        throw new IllegalArgumentException("The job for this entry was deleted, can not re-send this entry.");
    }
    final Collection<ProjectData> projectDataList = projectDataFactory.createProjectDataCollection(notifications);
    for (final ProjectData projectData : projectDataList) {
        final AbstractChannelEvent event = channelEventFactory.createEvent(commonConfigId, commonConfigEntity.getDistributionType(), projectData);
        event.setAuditEntryId(auditEntryEntity.getId());
        channelTemplateManager.sendEvent(event);
    }
    return get();
}
Also used : AuditEntryRepositoryWrapper(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryRepositoryWrapper) ChannelEventFactory(com.blackducksoftware.integration.hub.alert.channel.manager.ChannelEventFactory) AuditEntryEntity(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryEntity) DistributionChannelConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.distribution.DistributionChannelConfigEntity) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) ChannelTemplateManager(com.blackducksoftware.integration.hub.alert.channel.ChannelTemplateManager) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) NotificationManager(com.blackducksoftware.integration.hub.alert.NotificationManager) ArrayList(java.util.ArrayList) AuditNotificationRepositoryWrapper(com.blackducksoftware.integration.hub.alert.audit.repository.AuditNotificationRepositoryWrapper) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) NotificationModel(com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel) AuditNotificationRelation(com.blackducksoftware.integration.hub.alert.audit.repository.relation.AuditNotificationRelation) ComponentRestModel(com.blackducksoftware.integration.hub.alert.web.model.ComponentRestModel) Logger(org.slf4j.Logger) GlobalChannelConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalChannelConfigEntity) Transactional(javax.transaction.Transactional) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ProjectDataFactory(com.blackducksoftware.integration.hub.alert.digest.model.ProjectDataFactory) List(java.util.List) Component(org.springframework.stereotype.Component) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData) NotificationRestModel(com.blackducksoftware.integration.hub.alert.web.model.NotificationRestModel) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) AbstractChannelEvent(com.blackducksoftware.integration.hub.alert.event.AbstractChannelEvent) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) NotificationModel(com.blackducksoftware.integration.hub.alert.hub.model.NotificationModel) AbstractChannelEvent(com.blackducksoftware.integration.hub.alert.event.AbstractChannelEvent) AuditNotificationRelation(com.blackducksoftware.integration.hub.alert.audit.repository.relation.AuditNotificationRelation) AuditEntryEntity(com.blackducksoftware.integration.hub.alert.audit.repository.AuditEntryEntity) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ProjectData(com.blackducksoftware.integration.hub.alert.digest.model.ProjectData)

Example 15 with AlertException

use of com.blackducksoftware.integration.hub.alert.exception.AlertException in project hub-alert by blackducksoftware.

the class ObjectTransformer method convert.

public <FROM extends Model, TO extends Model> TO convert(final FROM fromObject, final Class<TO> toClass) throws AlertException {
    if (fromObject != null && toClass != null) {
        final String fromClassName = fromObject.getClass().getSimpleName();
        final String toClassName = toClass.getSimpleName();
        try {
            final TO toObject = toClass.newInstance();
            final Map<String, Field> newFieldMap = createNewFieldMap(toObject);
            final List<Field> oldFieldList = createOldFieldList(fromObject);
            for (final Field oldField : oldFieldList) {
                try {
                    if (Modifier.isStatic(oldField.getModifiers())) {
                        continue;
                    }
                    oldField.setAccessible(true);
                    final String oldFieldName = oldField.getName();
                    final Field newField = newFieldMap.get(oldFieldName);
                    if (newField == null) {
                        throw new NoSuchFieldException("Could not find field '" + oldFieldName + "' in class " + toClassName);
                    }
                    newField.setAccessible(true);
                    if (conversionService.canConvert(oldField.getType(), newField.getType())) {
                        newField.set(toObject, conversionService.convert(oldField.get(fromObject), newField.getType()));
                    } else {
                        throw new AlertException(String.format("Could not transform object %s to %s because of field %s : The transformer does not support turning %s into %s", fromObject.getClass().getSimpleName(), toObject.getClass().getSimpleName(), oldField.getName(), oldField.getType().getSimpleName(), newField.getType().getSimpleName()));
                    }
                } catch (final NoSuchFieldException e) {
                    logger.trace(String.format("Could not find field %s from %s in %s", oldField.getName(), fromClassName, toClassName));
                    continue;
                }
            }
            return toObject;
        } catch (IllegalAccessException | InstantiationException | SecurityException e) {
            throw new AlertException(String.format("Could not transform object %s to %s: %s", fromClassName, toClassName, e.toString()), e);
        }
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException)

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