Search in sources :

Example 1 with GenericNotificationConfigEntity

use of io.gravitee.management.model.notification.GenericNotificationConfigEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiServiceImpl method create0.

private ApiEntity create0(UpdateApiEntity api, String userId) throws ApiAlreadyExistsException {
    try {
        LOGGER.debug("Create {} for user {}", api, userId);
        String id = UUID.toString(UUID.random());
        Optional<Api> checkApi = apiRepository.findById(id);
        if (checkApi.isPresent()) {
            throw new ApiAlreadyExistsException(id);
        }
        // Format context-path and check if context path is unique
        checkContextPath(api.getProxy().getContextPath());
        Api repoApi = convert(id, api);
        if (repoApi != null) {
            repoApi.setId(id);
            // Set date fields
            repoApi.setCreatedAt(new Date());
            repoApi.setUpdatedAt(repoApi.getCreatedAt());
            // Be sure that lifecycle is set to STOPPED by default and visibility is private
            repoApi.setLifecycleState(LifecycleState.STOPPED);
            repoApi.setVisibility(Visibility.PRIVATE);
            // Add Default groups
            Set<String> defaultGroups = groupService.findByEvent(GroupEvent.API_CREATE).stream().map(GroupEntity::getId).collect(Collectors.toSet());
            if (!defaultGroups.isEmpty() && repoApi.getGroups() == null) {
                repoApi.setGroups(defaultGroups);
            } else if (!defaultGroups.isEmpty()) {
                repoApi.getGroups().addAll(defaultGroups);
            }
            Api createdApi = apiRepository.create(repoApi);
            // Audit
            auditService.createApiAuditLog(createdApi.getId(), Collections.emptyMap(), API_CREATED, createdApi.getCreatedAt(), null, createdApi);
            // Add the primary owner of the newly created API
            UserEntity primaryOwner = userService.findById(userId);
            Membership membership = new Membership(primaryOwner.getId(), createdApi.getId(), MembershipReferenceType.API);
            membership.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
            membership.setCreatedAt(repoApi.getCreatedAt());
            membership.setUpdatedAt(repoApi.getCreatedAt());
            membershipRepository.create(membership);
            // create the default mail notification
            if (primaryOwner.getEmail() != null && !primaryOwner.getEmail().isEmpty()) {
                GenericNotificationConfigEntity notificationConfigEntity = new GenericNotificationConfigEntity();
                notificationConfigEntity.setName("Default Mail Notifications");
                notificationConfigEntity.setReferenceType(HookScope.API.name());
                notificationConfigEntity.setReferenceId(createdApi.getId());
                notificationConfigEntity.setHooks(Arrays.stream(ApiHook.values()).map(Enum::name).collect(Collectors.toList()));
                notificationConfigEntity.setNotifier(NotifierServiceImpl.DEFAULT_EMAIL_NOTIFIER_ID);
                notificationConfigEntity.setConfig(primaryOwner.getEmail());
                genericNotificationConfigService.create(notificationConfigEntity);
            }
            // TODO add membership log
            return convert(createdApi, primaryOwner, true);
        } else {
            LOGGER.error("Unable to create API {} because of previous error.");
            throw new TechnicalManagementException("Unable to create API " + id);
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create {} for user {}", api, userId, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + api + " for user " + userId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GenericNotificationConfigEntity(io.gravitee.management.model.notification.GenericNotificationConfigEntity)

Example 2 with GenericNotificationConfigEntity

use of io.gravitee.management.model.notification.GenericNotificationConfigEntity in project gravitee-management-rest-api by gravitee-io.

the class GenericNotificationConfigServiceImpl method convert.

private GenericNotificationConfigEntity convert(GenericNotificationConfig genericNotificationConfig) {
    GenericNotificationConfigEntity entity = new GenericNotificationConfigEntity();
    entity.setConfigType(NotificationConfigType.GENERIC);
    entity.setId(genericNotificationConfig.getId());
    entity.setName(genericNotificationConfig.getName());
    entity.setReferenceType(genericNotificationConfig.getReferenceType().name());
    entity.setReferenceId(genericNotificationConfig.getReferenceId());
    entity.setNotifier(genericNotificationConfig.getNotifier());
    entity.setConfig(genericNotificationConfig.getConfig());
    entity.setHooks(genericNotificationConfig.getHooks());
    return entity;
}
Also used : GenericNotificationConfigEntity(io.gravitee.management.model.notification.GenericNotificationConfigEntity)

Example 3 with GenericNotificationConfigEntity

use of io.gravitee.management.model.notification.GenericNotificationConfigEntity 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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GenericNotificationConfigEntity(io.gravitee.management.model.notification.GenericNotificationConfigEntity) ClientIdAlreadyExistsException(io.gravitee.management.service.exceptions.ClientIdAlreadyExistsException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

GenericNotificationConfigEntity (io.gravitee.management.model.notification.GenericNotificationConfigEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 ClientIdAlreadyExistsException (io.gravitee.management.service.exceptions.ClientIdAlreadyExistsException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1