use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationAlertServiceImpl method deleteMemberFromApplication.
@Override
public void deleteMemberFromApplication(String applicationId, String email) {
if (StringUtils.isEmpty(email)) {
return;
}
// check existence of application
applicationService.findById(applicationId);
alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
if (trigger.getNotifications() == null) {
trigger.setNotifications(createNotification(trigger.getType()));
}
final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
if (notificationOpt.isPresent()) {
final Notification notification = notificationOpt.get();
try {
ObjectNode configuration = mapper.createObjectNode();
JsonNode emailNode = mapper.readTree(notification.getConfiguration());
final String to = Arrays.stream(emailNode.path("to").asText().split(",|;|\\s")).filter(mailTo -> !mailTo.equals(email)).collect(Collectors.joining(","));
if (StringUtils.isEmpty(to)) {
trigger.setNotifications(emptyList());
} else {
configuration.put("to", to);
configuration.put("from", emailNode.path("from").asText());
configuration.put("subject", emailNode.path("subject").asText());
configuration.put("body", emailNode.path("body").asText());
notification.setConfiguration(mapper.writeValueAsString(configuration));
}
alertService.update(convert(trigger));
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
}
}
});
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method findDefaultByKey.
@Override
public MetadataEntity findDefaultByKey(final String key) {
try {
LOGGER.debug("Find default metadata by key");
final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAULT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
if (optMetadata.isPresent()) {
return convert(optMetadata.get());
} else {
return null;
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find default metadata by key", ex);
throw new TechnicalManagementException("An error occurred while trying to find default metadata by key", ex);
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method create.
@Override
public MetadataEntity create(final NewMetadataEntity metadataEntity) {
// if no format defined, we just set String format
if (metadataEntity.getFormat() == null) {
metadataEntity.setFormat(MetadataFormat.STRING);
}
try {
// First we prevent the duplicate metadata name
final Optional<MetadataEntity> optionalMetadata = findAllDefault().stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
if (optionalMetadata.isPresent()) {
throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
}
checkMetadataValue(metadataEntity.getValue());
checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
final Metadata metadata = convert(metadataEntity);
final Date now = new Date();
metadata.setCreatedAt(now);
metadata.setUpdatedAt(now);
metadataRepository.create(metadata);
// Audit
auditService.createEnvironmentAuditLog(singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
return convert(metadata);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to create metadata {}", metadataEntity.getName(), ex);
throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName(), ex);
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method findSummaryByApi.
@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
if (!isEnabled()) {
throw new ApiRatingUnavailableException();
}
try {
final List<Rating> ratings = ratingRepository.findByReferenceIdAndReferenceType(api, RatingReferenceType.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);
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method create.
@Override
public RatingEntity create(final NewRatingEntity ratingEntity) {
if (!isEnabled()) {
throw new ApiRatingUnavailableException();
}
try {
final Optional<Rating> ratingOptional = ratingRepository.findByReferenceIdAndReferenceTypeAndUser(ratingEntity.getApi(), RatingReferenceType.API, getAuthenticatedUsername());
if (ratingOptional.isPresent()) {
throw new RatingAlreadyExistsException(ratingEntity.getApi(), getAuthenticatedUsername());
}
Rating rating = ratingRepository.create(convert(ratingEntity));
auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_CREATED, rating.getCreatedAt(), null, rating);
notifierService.trigger(ApiHook.NEW_RATING, rating.getReferenceId(), new NotificationParamsBuilder().api(apiService.findById(rating.getReferenceId())).build());
return convert(rating);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to create rating on api {}", ratingEntity.getApi(), ex);
throw new TechnicalManagementException("An error occurred while trying to create rating on api " + ratingEntity.getApi(), ex);
}
}
Aggregations