Search in sources :

Example 1 with PLAN

use of io.gravitee.repository.management.model.Audit.AuditProperties.PLAN in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method close.

@Override
public PlanEntity close(String planId, String userId) {
    try {
        logger.debug("Close 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);
        }
        // Update plan status
        plan.setStatus(Plan.Status.CLOSED);
        plan.setClosedAt(new Date());
        plan.setUpdatedAt(plan.getClosedAt());
        // Close active subscriptions and reject pending
        if (plan.getSecurity() != Plan.PlanSecurityType.KEY_LESS) {
            subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.ACCEPTED).forEach(subscription -> subscriptionService.close(subscription.getId()));
            final String planName = plan.getName();
            subscriptionService.findByPlan(planId).stream().filter(subscriptionEntity -> subscriptionEntity.getStatus() == SubscriptionStatus.PENDING).forEach(subscription -> {
                ProcessSubscriptionEntity processSubscriptionEntity = new ProcessSubscriptionEntity();
                processSubscriptionEntity.setId(subscription.getId());
                processSubscriptionEntity.setAccepted(false);
                processSubscriptionEntity.setReason("Plan " + planName + " has been closed.");
                subscriptionService.process(processSubscriptionEntity, userId);
            });
        }
        // Save plan
        plan = planRepository.update(plan);
        // Audit
        auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CLOSED, plan.getUpdatedAt(), previousPlan, plan);
        // reorder plan
        reorderedAndSavePlansAfterRemove(optPlan.get());
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to delete plan: {}", planId, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to delete 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 2 with PLAN

use of io.gravitee.repository.management.model.Audit.AuditProperties.PLAN 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UUID (io.gravitee.common.utils.UUID)2 Path (io.gravitee.definition.model.Path)2 io.gravitee.management.model (io.gravitee.management.model)2 PlanQuery (io.gravitee.management.model.plan.PlanQuery)2 AuditService (io.gravitee.management.service.AuditService)2 PlanService (io.gravitee.management.service.PlanService)2 SubscriptionService (io.gravitee.management.service.SubscriptionService)2 io.gravitee.management.service.exceptions (io.gravitee.management.service.exceptions)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 PlanRepository (io.gravitee.repository.management.api.PlanRepository)2 PLAN (io.gravitee.repository.management.model.Audit.AuditProperties.PLAN)2 Plan (io.gravitee.repository.management.model.Plan)2 AuditEvent (io.gravitee.repository.management.model.Plan.AuditEvent)2 IOException (java.io.IOException)2 java.util (java.util)2 Collections.emptySet (java.util.Collections.emptySet)2 Collectors (java.util.stream.Collectors)2