Search in sources :

Example 21 with TechnicalException

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

the class GroupServiceImpl method findByName.

@Override
public List<GroupEntity> findByName(String name) {
    try {
        logger.debug("findByUsername : {}", name);
        if (name == null) {
            return Collections.emptyList();
        }
        List<GroupEntity> groupEntities = groupRepository.findAll().stream().filter(group -> group.getName().equals(name)).map(this::map).collect(Collectors.toList());
        logger.debug("findByUsername : {} - DONE", name);
        return groupEntities;
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to find groups by name", ex);
        throw new TechnicalManagementException("An error occurs while trying to find groups by name", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 22 with TechnicalException

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

the class GroupServiceImpl method create.

@Override
public GroupEntity create(NewGroupEntity group) {
    try {
        logger.debug("create {}", group);
        if (!this.findByName(group.getName()).isEmpty()) {
            throw new GroupNameAlreadyExistsException(group.getName());
        }
        Group newGroup = this.map(group);
        newGroup.setId(UUID.toString(UUID.random()));
        newGroup.setCreatedAt(new Date());
        newGroup.setUpdatedAt(newGroup.getCreatedAt());
        GroupEntity grp = this.map(groupRepository.create(newGroup));
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(GROUP, newGroup.getId()), GROUP_CREATED, newGroup.getCreatedAt(), null, newGroup);
        logger.debug("create {} - DONE", grp);
        return grp;
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to create a group", ex);
        throw new TechnicalManagementException("An error occurs while trying to create a group", ex);
    }
}
Also used : GroupNameAlreadyExistsException(io.gravitee.management.service.exceptions.GroupNameAlreadyExistsException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 23 with TechnicalException

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

the class MembershipServiceImpl method addOrUpdateMember.

@Override
public MemberEntity addOrUpdateMember(MembershipReference reference, MembershipUser user, MembershipRole role) {
    try {
        LOGGER.debug("Add a new member for {} {}", reference.getType(), reference.getId());
        RoleEntity roleEntity = roleService.findById(role.getScope(), role.getName());
        if (API.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.API.equals(roleEntity.getScope())) {
            throw new NotAuthorizedMembershipException(role.getName());
        } else if (MembershipReferenceType.APPLICATION.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.APPLICATION.equals(roleEntity.getScope())) {
            throw new NotAuthorizedMembershipException(role.getName());
        } else if (MembershipReferenceType.GROUP.equals(reference.getType()) && !io.gravitee.management.model.permissions.RoleScope.APPLICATION.equals(roleEntity.getScope()) && !io.gravitee.management.model.permissions.RoleScope.API.equals(roleEntity.getScope())) {
            throw new NotAuthorizedMembershipException(role.getName());
        }
        UserEntity userEntity;
        if (user.getId() != null) {
            userEntity = userService.findById(user.getId());
        } else {
            // We have a user reference, meaning that the user is coming from an external system
            // User does not exist so we are looking into defined providers
            Optional<io.gravitee.management.model.providers.User> providerUser = identityService.findByReference(user.getReference());
            if (providerUser.isPresent()) {
                try {
                    userEntity = userService.findByUsername(providerUser.get().getUsername(), false);
                } catch (UserNotFoundException unfe) {
                    User identityUser = providerUser.get();
                    // The user is not yet registered in repository
                    // Information will be updated after the first connection of the user
                    NewExternalUserEntity newUser = new NewExternalUserEntity();
                    newUser.setUsername(identityUser.getUsername());
                    newUser.setFirstname(identityUser.getFirstname());
                    newUser.setLastname(identityUser.getLastname());
                    newUser.setSource(identityUser.getSource());
                    newUser.setEmail(identityUser.getEmail());
                    newUser.setSourceId(identityUser.getSourceId());
                    userEntity = userService.create(newUser, true);
                }
            } else {
                throw new UserNotFoundException(user.getReference());
            }
        }
        Optional<Membership> optionalMembership = membershipRepository.findById(userEntity.getId(), reference.getType(), reference.getId());
        Date updateDate = new Date();
        Membership returnedMembership;
        if (optionalMembership.isPresent()) {
            Membership updatedMembership = optionalMembership.get();
            Membership previousMembership = new Membership(updatedMembership);
            updatedMembership.getRoles().put(role.getScope().getId(), role.getName());
            updatedMembership.setUpdatedAt(updateDate);
            returnedMembership = membershipRepository.update(optionalMembership.get());
            createAuditLog(MEMBERSHIP_UPDATED, updatedMembership.getUpdatedAt(), previousMembership, updatedMembership);
        } else {
            Membership membership = new Membership(userEntity.getId(), reference.getId(), reference.getType());
            membership.setRoles(Collections.singletonMap(role.getScope().getId(), role.getName()));
            membership.setCreatedAt(updateDate);
            membership.setUpdatedAt(updateDate);
            returnedMembership = membershipRepository.create(membership);
            createAuditLog(MEMBERSHIP_CREATED, membership.getCreatedAt(), null, membership);
            if (userEntity.getEmail() != null && !userEntity.getEmail().isEmpty()) {
                EmailNotification emailNotification = buildEmailNotification(userEntity, reference.getType(), reference.getId());
                if (emailNotification != null) {
                    emailService.sendAsyncEmailNotification(emailNotification);
                }
            }
        }
        return convert(returnedMembership, role.getScope());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to add member for {} {}", reference.getType(), reference.getId(), ex);
        throw new TechnicalManagementException("An error occurs while trying to add member for " + reference.getType() + " " + reference.getId(), ex);
    }
}
Also used : User(io.gravitee.management.model.providers.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Membership(io.gravitee.repository.management.model.Membership) io.gravitee.management.model(io.gravitee.management.model)

Example 24 with TechnicalException

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

the class MembershipServiceImpl method deleteMember.

@Override
public void deleteMember(MembershipReferenceType referenceType, String referenceId, String userId) {
    try {
        LOGGER.debug("Delete member {} for {} {}", userId, referenceType, referenceId);
        if (!MembershipReferenceType.GROUP.equals(referenceType)) {
            RoleScope roleScope = getScopeByMembershipReferenceType(referenceType);
            RoleEntity roleEntity = this.getRole(referenceType, referenceId, userId, roleScope);
            if (roleEntity != null && PRIMARY_OWNER.name().equals(roleEntity.getName())) {
                throw new SinglePrimaryOwnerException(referenceType.equals(API) ? RoleScope.API : RoleScope.APPLICATION);
            }
        }
        Membership membership = new Membership(userId, referenceId, referenceType);
        membershipRepository.delete(membership);
        createAuditLog(MEMBERSHIP_DELETED, new Date(), membership, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete member {} for {} {}", userId, referenceType, referenceId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete member " + userId + " for " + referenceType + " " + referenceId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleScope(io.gravitee.repository.management.model.RoleScope) Membership(io.gravitee.repository.management.model.Membership)

Example 25 with TechnicalException

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

the class MetadataServiceImpl method update.

@Override
public MetadataEntity update(final UpdateMetadataEntity metadataEntity) {
    try {
        // First we prevent the duplicate metadata name
        final Optional<Metadata> optionalMetadata = metadataRepository.findByReferenceType(MetadataReferenceType.DEFAULT).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setUpdatedAt(now);
        metadataRepository.update(metadata);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) 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