use of io.gravitee.management.service.exceptions.ClientIdAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method update.
@Override
public ApplicationEntity update(String applicationId, UpdateApplicationEntity updateApplicationEntity) {
try {
LOGGER.debug("Update application {}", applicationId);
if (updateApplicationEntity.getGroups() != null && !updateApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(updateApplicationEntity.getGroups());
}
Optional<Application> optApplicationToUpdate = applicationRepository.findById(applicationId);
if (!optApplicationToUpdate.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
// If clientId is set, check for uniqueness
String clientId = updateApplicationEntity.getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
final Optional<Application> byClientId = applications.stream().filter(application -> clientId.equals(application.getClientId())).findAny();
if (byClientId.isPresent() && !byClientId.get().getId().equals(optApplicationToUpdate.get().getId())) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
Application application = convert(updateApplicationEntity);
application.setId(applicationId);
application.setStatus(ApplicationStatus.ACTIVE);
application.setCreatedAt(optApplicationToUpdate.get().getCreatedAt());
application.setUpdatedAt(new Date());
Application updatedApplication = applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(updatedApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, updatedApplication.getUpdatedAt(), optApplicationToUpdate.get(), updatedApplication);
// Set correct client_id for all subscriptions
SubscriptionQuery subQuery = new SubscriptionQuery();
subQuery.setApplication(applicationId);
subQuery.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
subscriptionService.search(subQuery).forEach(new Consumer<SubscriptionEntity>() {
@Override
public void accept(SubscriptionEntity subscriptionEntity) {
UpdateSubscriptionEntity updateSubscriptionEntity = new UpdateSubscriptionEntity();
updateSubscriptionEntity.setId(subscriptionEntity.getId());
updateSubscriptionEntity.setStartingAt(subscriptionEntity.getStartingAt());
updateSubscriptionEntity.setEndingAt(subscriptionEntity.getEndingAt());
subscriptionService.update(updateSubscriptionEntity, application.getClientId());
}
});
return convert(Collections.singleton(updatedApplication)).iterator().next();
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to update application %s", applicationId), ex);
}
}
use of io.gravitee.management.service.exceptions.ClientIdAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method create.
@Override
public ApplicationEntity create(NewApplicationEntity newApplicationEntity, String userId) {
try {
LOGGER.debug("Create {} for user {}", newApplicationEntity, userId);
// If clientId is set, check for uniqueness
String clientId = newApplicationEntity.getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
final boolean alreadyExistingApp = applications.stream().anyMatch(application -> clientId.equals(application.getClientId()));
if (alreadyExistingApp) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
if (newApplicationEntity.getGroups() != null && !newApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(newApplicationEntity.getGroups());
}
Application application = convert(newApplicationEntity);
application.setId(UUID.toString(UUID.random()));
application.setStatus(ApplicationStatus.ACTIVE);
// Add Default groups
Set<String> defaultGroups = groupService.findByEvent(GroupEvent.APPLICATION_CREATE).stream().map(GroupEntity::getId).collect(Collectors.toSet());
if (!defaultGroups.isEmpty() && application.getGroups() == null) {
application.setGroups(defaultGroups);
} else if (!defaultGroups.isEmpty()) {
application.getGroups().addAll(defaultGroups);
}
// Set date fields
application.setCreatedAt(new Date());
application.setUpdatedAt(application.getCreatedAt());
Application createdApplication = applicationRepository.create(application);
// Audit
auditService.createApplicationAuditLog(createdApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, isAuthenticated() ? getAuthenticatedUsername() : userId, createdApplication.getCreatedAt(), null, createdApplication);
// Add the primary owner of the newly created API
Membership membership = new Membership(userId, createdApplication.getId(), MembershipReferenceType.APPLICATION);
membership.setRoles(singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
membership.setCreatedAt(application.getCreatedAt());
membership.setUpdatedAt(application.getCreatedAt());
membershipRepository.create(membership);
// create the default mail notification
UserEntity userEntity = userService.findById(userId);
if (userEntity.getEmail() != null && !userEntity.getEmail().isEmpty()) {
GenericNotificationConfigEntity notificationConfigEntity = new GenericNotificationConfigEntity();
notificationConfigEntity.setName("Default Mail Notifications");
notificationConfigEntity.setReferenceType(HookScope.APPLICATION.name());
notificationConfigEntity.setReferenceId(createdApplication.getId());
notificationConfigEntity.setHooks(Arrays.stream(ApplicationHook.values()).map(Enum::name).collect(Collectors.toList()));
notificationConfigEntity.setNotifier(NotifierServiceImpl.DEFAULT_EMAIL_NOTIFIER_ID);
notificationConfigEntity.setConfig(userEntity.getEmail());
genericNotificationConfigService.create(notificationConfigEntity);
}
// TODO add membership log
return convert(createdApplication, userEntity);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create {} for user {}", newApplicationEntity, userId, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newApplicationEntity + " for user " + userId, ex);
}
}
Aggregations