Search in sources :

Example 31 with TechnicalException

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

the class ParameterServiceImpl method createMultipleValue.

@Override
public void createMultipleValue(final String key, final String value) {
    try {
        final Optional<Parameter> optionalParameter = parameterRepository.findById(key);
        final Parameter parameter = new Parameter();
        parameter.setKey(key);
        if (optionalParameter.isPresent()) {
            final String existingValue = optionalParameter.get().getValue();
            if (existingValue.isEmpty()) {
                parameter.setValue(value);
            } else {
                parameter.setValue(existingValue + SEPARATOR + value);
            }
            update(parameter.getKey(), parameter.getValue());
        } else {
            parameter.setValue(value);
            create(parameter.getKey(), parameter.getValue());
        }
    } catch (final TechnicalException ex) {
        final String message = "An error occurs while trying to update parameter for key/value: " + key + '/' + value;
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Parameter(io.gravitee.repository.management.model.Parameter) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 32 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException 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 33 with TechnicalException

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

the class PlanServiceImpl method create.

@Override
public PlanEntity create(NewPlanEntity newPlan) {
    try {
        logger.debug("Create a new plan {} for API {}", newPlan.getName(), newPlan.getApi());
        Plan plan = new Plan();
        plan.setId(UUID.toString(UUID.random()));
        plan.setApis(Collections.singleton(newPlan.getApi()));
        plan.setName(newPlan.getName());
        plan.setDescription(newPlan.getDescription());
        plan.setCreatedAt(new Date());
        plan.setUpdatedAt(plan.getCreatedAt());
        plan.setType(Plan.PlanType.valueOf(newPlan.getType().name()));
        plan.setSecurity(Plan.PlanSecurityType.valueOf(newPlan.getSecurity().name()));
        plan.setSecurityDefinition(newPlan.getSecurityDefinition());
        plan.setStatus(Plan.Status.valueOf(newPlan.getStatus().name()));
        plan.setExcludedGroups(newPlan.getExcludedGroups());
        if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // There is no need for a validation when authentication is KEY_LESS, force to AUTO
            plan.setValidation(Plan.PlanValidationType.AUTO);
        } else {
            plan.setValidation(Plan.PlanValidationType.valueOf(newPlan.getValidation().name()));
        }
        plan.setCharacteristics(newPlan.getCharacteristics());
        String planPolicies = objectMapper.writeValueAsString(newPlan.getPaths());
        plan.setDefinition(planPolicies);
        plan = planRepository.create(plan);
        auditService.createApiAuditLog(newPlan.getApi(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CREATED, plan.getCreatedAt(), null, plan);
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to create a plan {} for API {}", newPlan.getName(), newPlan.getApi(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), ex);
    } catch (JsonProcessingException jse) {
        logger.error("Unexpected error while generating plan definition", jse);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), jse);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 34 with TechnicalException

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

the class PortalNotificationServiceImpl method create.

private void create(List<NewPortalNotificationEntity> notificationEntities) {
    final Date now = new Date();
    List<PortalNotification> notifications = notificationEntities.stream().map(this::convert).collect(Collectors.toList());
    notifications.forEach(n -> {
        n.setId(UUID.toString(UUID.random()));
        n.setCreatedAt(now);
    });
    try {
        portalNotificationRepository.create(notifications);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {}", notifications, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + notifications, ex);
    }
}
Also used : PortalNotification(io.gravitee.repository.management.model.PortalNotification) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 35 with TechnicalException

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

the class RatingServiceImpl method findSummaryByApi.

@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByApi(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.management.service.exceptions.ApiRatingUnavailableException) OptionalDouble(java.util.OptionalDouble) 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