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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations