Search in sources :

Example 6 with Plan

use of io.gravitee.repository.management.model.Plan in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method update.

@Override
public PlanEntity update(UpdatePlanEntity updatePlan) {
    try {
        logger.debug("Update plan {}", updatePlan.getName());
        Optional<Plan> optPlan = planRepository.findById(updatePlan.getId());
        if (!optPlan.isPresent()) {
            throw new PlanNotFoundException(updatePlan.getId());
        }
        Plan oldPlan = optPlan.get();
        Plan newPlan = new Plan();
        // copy immutable values
        newPlan.setId(oldPlan.getId());
        newPlan.setSecurity(oldPlan.getSecurity());
        newPlan.setType(oldPlan.getType());
        newPlan.setStatus(oldPlan.getStatus());
        newPlan.setOrder(oldPlan.getOrder());
        newPlan.setApis(oldPlan.getApis());
        newPlan.setCreatedAt(oldPlan.getCreatedAt());
        newPlan.setPublishedAt(oldPlan.getPublishedAt());
        newPlan.setClosedAt(oldPlan.getClosedAt());
        // update datas
        newPlan.setName(updatePlan.getName());
        newPlan.setDescription(updatePlan.getDescription());
        newPlan.setUpdatedAt(new Date());
        newPlan.setSecurityDefinition(updatePlan.getSecurityDefinition());
        String planPolicies = objectMapper.writeValueAsString(updatePlan.getPaths());
        newPlan.setDefinition(planPolicies);
        newPlan.setExcludedGroups(updatePlan.getExcludedGroups());
        if (newPlan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // There is no need for a validation when authentication is KEY_LESS, force to AUTO
            newPlan.setValidation(Plan.PlanValidationType.AUTO);
        } else {
            newPlan.setValidation(Plan.PlanValidationType.valueOf(updatePlan.getValidation().name()));
        }
        newPlan.setCharacteristics(updatePlan.getCharacteristics());
        // if order change, reorder all pages
        if (newPlan.getOrder() != updatePlan.getOrder()) {
            newPlan.setOrder(updatePlan.getOrder());
            reorderAndSavePlans(newPlan);
            return null;
        } else {
            newPlan = planRepository.update(newPlan);
            auditService.createApiAuditLog(newPlan.getApis().iterator().next(), Collections.singletonMap(PLAN, newPlan.getId()), PLAN_UPDATED, newPlan.getUpdatedAt(), oldPlan, newPlan);
            return convert(newPlan);
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to update plan {}", updatePlan.getName(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to update plan %s", updatePlan.getName()), ex);
    } catch (JsonProcessingException jse) {
        logger.error("Unexpected error while generating plan definition", jse);
        throw new TechnicalManagementException(String.format("An error occurs while trying to update a plan %s", updatePlan.getName()), jse);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with Plan

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

Plan (io.gravitee.repository.management.model.Plan)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 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 PlanRepository (io.gravitee.repository.management.api.PlanRepository)2 PLAN (io.gravitee.repository.management.model.Audit.AuditProperties.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