Search in sources :

Example 91 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceImpl method deleteMemberFromApplication.

@Override
public void deleteMemberFromApplication(String applicationId, String email) {
    if (StringUtils.isEmpty(email)) {
        return;
    }
    // check existence of application
    applicationService.findById(applicationId);
    alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
        if (trigger.getNotifications() == null) {
            trigger.setNotifications(createNotification(trigger.getType()));
        }
        final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
        if (notificationOpt.isPresent()) {
            final Notification notification = notificationOpt.get();
            try {
                ObjectNode configuration = mapper.createObjectNode();
                JsonNode emailNode = mapper.readTree(notification.getConfiguration());
                final String to = Arrays.stream(emailNode.path("to").asText().split(",|;|\\s")).filter(mailTo -> !mailTo.equals(email)).collect(Collectors.joining(","));
                if (StringUtils.isEmpty(to)) {
                    trigger.setNotifications(emptyList());
                } else {
                    configuration.put("to", to);
                    configuration.put("from", emailNode.path("from").asText());
                    configuration.put("subject", emailNode.path("subject").asText());
                    configuration.put("body", emailNode.path("body").asText());
                    notification.setConfiguration(mapper.writeValueAsString(configuration));
                }
                alertService.update(convert(trigger));
            } catch (JsonProcessingException e) {
                LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
                throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
            }
        }
    });
}
Also used : AlertHook(io.gravitee.rest.api.service.notification.AlertHook) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HookScope(io.gravitee.rest.api.service.notification.HookScope) AlertTriggerRepository(io.gravitee.repository.management.api.AlertTriggerRepository) Collections.singletonList(java.util.Collections.singletonList) StringCondition(io.gravitee.alert.api.condition.StringCondition) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) AlertStatusEntity(io.gravitee.rest.api.model.alert.AlertStatusEntity) Collections.emptyList(java.util.Collections.emptyList) MembershipService(io.gravitee.rest.api.service.MembershipService) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertService(io.gravitee.rest.api.service.AlertService) CollectionUtils(org.springframework.util.CollectionUtils) ApplicationService(io.gravitee.rest.api.service.ApplicationService) Optional(java.util.Optional) Filter(io.gravitee.alert.api.condition.Filter) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Trigger(io.gravitee.alert.api.trigger.Trigger) Async(org.springframework.scheduling.annotation.Async) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ApplicationAlertService(io.gravitee.rest.api.service.ApplicationAlertService) ArrayList(java.util.ArrayList) ApplicationAlertMembershipEvent(io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) UserService(io.gravitee.rest.api.service.UserService) Notification(io.gravitee.notifier.api.Notification) AlertReferenceType(io.gravitee.rest.api.model.alert.AlertReferenceType) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Event(io.gravitee.common.event.Event) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Dampening(io.gravitee.alert.api.trigger.Dampening) Consumer(java.util.function.Consumer) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) Component(org.springframework.stereotype.Component) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) StringUtils(org.springframework.util.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Notification(io.gravitee.notifier.api.Notification) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 92 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class MetadataServiceImpl method findDefaultByKey.

@Override
public MetadataEntity findDefaultByKey(final String key) {
    try {
        LOGGER.debug("Find default metadata by key");
        final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAULT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
        if (optMetadata.isPresent()) {
            return convert(optMetadata.get());
        } else {
            return null;
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find default metadata by key", ex);
        throw new TechnicalManagementException("An error occurred while trying to find default metadata by key", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Metadata(io.gravitee.repository.management.model.Metadata) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 93 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class MetadataServiceImpl method create.

@Override
public MetadataEntity create(final NewMetadataEntity metadataEntity) {
    // if no format defined, we just set String format
    if (metadataEntity.getFormat() == null) {
        metadataEntity.setFormat(MetadataFormat.STRING);
    }
    try {
        // First we prevent the duplicate metadata name
        final Optional<MetadataEntity> optionalMetadata = findAllDefault().stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataValue(metadataEntity.getValue());
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setCreatedAt(now);
        metadata.setUpdatedAt(now);
        metadataRepository.create(metadata);
        // Audit
        auditService.createEnvironmentAuditLog(singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) AuditEvent(io.gravitee.repository.management.model.Metadata.AuditEvent) UpdateMetadataEntity(io.gravitee.rest.api.model.UpdateMetadataEntity) MetadataService(io.gravitee.rest.api.service.MetadataService) Collections.singletonMap(java.util.Collections.singletonMap) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) Logger(org.slf4j.Logger) NewMetadataEntity(io.gravitee.rest.api.model.NewMetadataEntity) AuditService(io.gravitee.rest.api.service.AuditService) MetadataFormat(io.gravitee.rest.api.model.MetadataFormat) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Metadata(io.gravitee.repository.management.model.Metadata) StringReader(java.io.StringReader) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Optional(java.util.Optional) MetadataEntity(io.gravitee.rest.api.model.MetadataEntity) UpdateMetadataEntity(io.gravitee.rest.api.model.UpdateMetadataEntity) NewMetadataEntity(io.gravitee.rest.api.model.NewMetadataEntity) MetadataEntity(io.gravitee.rest.api.model.MetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 94 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findSummaryByApi.

@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByReferenceIdAndReferenceType(api, RatingReferenceType.API);
        final RatingSummaryEntity ratingSummary = new RatingSummaryEntity();
        ratingSummary.setApi(api);
        ratingSummary.setNumberOfRatings(ratings.size());
        final OptionalDouble optionalAvg = ratings.stream().mapToInt(Rating::getRate).average();
        if (optionalAvg.isPresent()) {
            ratingSummary.setAverageRate(optionalAvg.getAsDouble());
        }
        ratingSummary.setNumberOfRatingsByRate(ratings.stream().collect(groupingBy(Rating::getRate, counting())));
        return ratingSummary;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find summary rating for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find summary rating for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) OptionalDouble(java.util.OptionalDouble) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 95 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method create.

@Override
public RatingEntity create(final NewRatingEntity ratingEntity) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Optional<Rating> ratingOptional = ratingRepository.findByReferenceIdAndReferenceTypeAndUser(ratingEntity.getApi(), RatingReferenceType.API, getAuthenticatedUsername());
        if (ratingOptional.isPresent()) {
            throw new RatingAlreadyExistsException(ratingEntity.getApi(), getAuthenticatedUsername());
        }
        Rating rating = ratingRepository.create(convert(ratingEntity));
        auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_CREATED, rating.getCreatedAt(), null, rating);
        notifierService.trigger(ApiHook.NEW_RATING, rating.getReferenceId(), new NotificationParamsBuilder().api(apiService.findById(rating.getReferenceId())).build());
        return convert(rating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create rating on api {}", ratingEntity.getApi(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create rating on api " + ratingEntity.getApi(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) RatingAlreadyExistsException(io.gravitee.rest.api.service.exceptions.RatingAlreadyExistsException) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6