Search in sources :

Example 66 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method publish.

@Override
public PlanEntity publish(String planId) {
    try {
        logger.debug("Publish plan {}", planId);
        Optional<Plan> optPlan = planRepository.findById(planId);
        if (!optPlan.isPresent()) {
            throw new PlanNotFoundException(planId);
        }
        Plan plan = optPlan.get();
        Plan previousPlan = new Plan(plan);
        if (plan.getStatus() == Plan.Status.CLOSED) {
            throw new PlanAlreadyClosedException(planId);
        } else if (plan.getStatus() == Plan.Status.PUBLISHED) {
            throw new PlanAlreadyPublishedException(planId);
        }
        Set<Plan> plans = planRepository.findByApi(plan.getApis().iterator().next());
        if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // Look to other plans if there is already a keyless-published plan
            long count = plans.stream().filter(plan1 -> plan1.getStatus() == Plan.Status.PUBLISHED).filter(plan1 -> plan1.getSecurity() == Plan.PlanSecurityType.KEY_LESS).count();
            if (count > 0) {
                throw new KeylessPlanAlreadyPublishedException(planId);
            }
        } else if (plan.getSecurity() == Plan.PlanSecurityType.OAUTH2 || plan.getSecurity() == Plan.PlanSecurityType.JWT) {
            // Look to other plans if there is already an OAuth2 or JWT plan
            long count = plans.stream().filter(plan1 -> plan1.getStatus() == Plan.Status.PUBLISHED).filter(plan1 -> plan1.getSecurity() == Plan.PlanSecurityType.OAUTH2 || plan1.getSecurity() == Plan.PlanSecurityType.JWT).count();
            if (count > 0) {
                throw new ClientIdBasedPlanAlreadyPublishedException(planId);
            }
        }
        // Update plan status
        plan.setStatus(Plan.Status.PUBLISHED);
        // Update plan order
        List<Plan> orderedPublishedPlans = plans.stream().filter(plan1 -> Plan.Status.PUBLISHED.equals(plan1.getStatus())).sorted(Comparator.comparingInt(Plan::getOrder)).collect(Collectors.toList());
        plan.setOrder(orderedPublishedPlans.isEmpty() ? 1 : (orderedPublishedPlans.get(orderedPublishedPlans.size() - 1).getOrder() + 1));
        plan.setPublishedAt(new Date());
        plan.setUpdatedAt(plan.getPublishedAt());
        // Save plan
        plan = planRepository.update(plan);
        // Audit
        auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(PLAN, plan.getId()), PLAN_PUBLISHED, plan.getUpdatedAt(), previousPlan, plan);
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to publish plan: {}", planId, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to publish plan: %s", planId), ex);
    }
}
Also used : PlanQuery(io.gravitee.management.model.plan.PlanQuery) java.util(java.util) Logger(org.slf4j.Logger) SubscriptionService(io.gravitee.management.service.SubscriptionService) Collections.emptySet(java.util.Collections.emptySet) PlanService(io.gravitee.management.service.PlanService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AuditService(io.gravitee.management.service.AuditService) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) PlanRepository(io.gravitee.repository.management.api.PlanRepository) io.gravitee.management.service.exceptions(io.gravitee.management.service.exceptions) Plan(io.gravitee.repository.management.model.Plan) Component(org.springframework.stereotype.Component) UUID(io.gravitee.common.utils.UUID) io.gravitee.management.model(io.gravitee.management.model) PLAN(io.gravitee.repository.management.model.Audit.AuditProperties.PLAN) AuditEvent(io.gravitee.repository.management.model.Plan.AuditEvent) Path(io.gravitee.definition.model.Path) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan)

Example 67 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class PortalNotificationConfigServiceImpl method save.

@Override
public PortalNotificationConfigEntity save(PortalNotificationConfigEntity notificationEntity) {
    try {
        if (notificationEntity.getHooks() == null || notificationEntity.getHooks().isEmpty()) {
            portalNotificationConfigRepository.delete(convert(notificationEntity));
            return getDefaultEmpty(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
        } else {
            Optional<PortalNotificationConfig> optionalConfig = portalNotificationConfigRepository.findById(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
            PortalNotificationConfig notificationConfig = convert(notificationEntity);
            if (optionalConfig.isPresent()) {
                notificationConfig.setCreatedAt(optionalConfig.get().getCreatedAt());
                notificationConfig.setUpdatedAt(new Date());
                return convert(portalNotificationConfigRepository.update(notificationConfig));
            } else {
                notificationConfig.setCreatedAt(new Date());
                notificationConfig.setUpdatedAt(notificationConfig.getCreatedAt());
                return convert(portalNotificationConfigRepository.create(notificationConfig));
            }
        }
    } catch (TechnicalException te) {
        LOGGER.error("An error occurs while trying to save the notification settings {}", notificationEntity, te);
        throw new TechnicalManagementException("An error occurs while trying to save the notification settings " + notificationEntity, te);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 68 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findByApi.

@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Page<Rating> pageRating = ratingRepository.findByApiPageable(api, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
        final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
        return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) Page(io.gravitee.common.data.domain.Page) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 69 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method deleteAnswer.

@Override
public void deleteAnswer(final String ratingId, final String answerId) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        Rating rating = findById(ratingId);
        ratingAnswerRepository.delete(answerId);
        auditService.createApiAuditLog(rating.getApi(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_DELETED, new Date(), rating, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete rating answer {}", answerId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete rating answer " + answerId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 70 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method convert.

private RatingEntity convert(final Rating rating) {
    final RatingEntity ratingEntity = new RatingEntity();
    final UserEntity user = userService.findById(rating.getUser());
    ratingEntity.setUser(user.getId());
    if (user.getFirstname() != null && user.getLastname() != null) {
        ratingEntity.setUserDisplayName(user.getFirstname() + ' ' + user.getLastname());
    } else {
        ratingEntity.setUserDisplayName(user.getUsername());
    }
    ratingEntity.setId(rating.getId());
    ratingEntity.setApi(rating.getApi());
    ratingEntity.setTitle(rating.getTitle());
    ratingEntity.setComment(rating.getComment());
    ratingEntity.setRate(rating.getRate());
    ratingEntity.setCreatedAt(rating.getCreatedAt());
    ratingEntity.setUpdatedAt(rating.getUpdatedAt());
    try {
        final List<RatingAnswer> ratingAnswers = ratingAnswerRepository.findByRating(rating.getId());
        if (ratingAnswers != null) {
            ratingEntity.setAnswers(ratingAnswers.stream().map(ratingAnswer -> {
                final RatingAnswerEntity ratingAnswerEntity = new RatingAnswerEntity();
                ratingAnswerEntity.setId(ratingAnswer.getId());
                final UserEntity userAnswer = userService.findById(ratingAnswer.getUser());
                ratingAnswerEntity.setUser(userAnswer.getId());
                if (userAnswer.getFirstname() != null && userAnswer.getLastname() != null) {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getFirstname() + ' ' + userAnswer.getLastname());
                } else {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getUsername());
                }
                ratingAnswerEntity.setComment(ratingAnswer.getComment());
                ratingAnswerEntity.setCreatedAt(ratingAnswer.getCreatedAt());
                return ratingAnswerEntity;
            }).sorted(comparing(RatingAnswerEntity::getCreatedAt, reverseOrder())).collect(toList()));
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find rating answers by rating id {}", rating.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to find rating answers by rating id " + rating.getId(), ex);
    }
    return ratingEntity;
}
Also used : Rating(io.gravitee.repository.management.model.Rating) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) RatingAlreadyExistsException(io.gravitee.management.service.exceptions.RatingAlreadyExistsException) Date(java.util.Date) Page(io.gravitee.common.data.domain.Page) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) OptionalDouble(java.util.OptionalDouble) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) Value(org.springframework.beans.factory.annotation.Value) UUID(io.gravitee.common.utils.UUID) io.gravitee.management.model(io.gravitee.management.model) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) RatingAnswerRepository(io.gravitee.repository.management.api.RatingAnswerRepository) Comparator.comparing(java.util.Comparator.comparing) Logger(org.slf4j.Logger) RatingService(io.gravitee.management.service.RatingService) RatingRepository(io.gravitee.repository.management.api.RatingRepository) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Pageable(io.gravitee.repository.management.api.search.Pageable) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) UserService(io.gravitee.management.service.UserService) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Comparator.reverseOrder(java.util.Comparator.reverseOrder) RatingNotFoundException(io.gravitee.management.service.exceptions.RatingNotFoundException) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)102 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)80 Logger (org.slf4j.Logger)22 LoggerFactory (org.slf4j.LoggerFactory)22 Autowired (org.springframework.beans.factory.annotation.Autowired)22 Component (org.springframework.stereotype.Component)20 java.util (java.util)18 Collectors (java.util.stream.Collectors)18 io.gravitee.management.model (io.gravitee.management.model)16 AuditService (io.gravitee.management.service.AuditService)12 UUID (io.gravitee.common.utils.UUID)11 Date (java.util.Date)11 IdGenerator (io.gravitee.common.utils.IdGenerator)9 IOException (java.io.IOException)9 io.gravitee.management.service (io.gravitee.management.service)8 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7