Search in sources :

Example 36 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class TagServiceImpl method create.

@Override
public List<TagEntity> create(final List<NewTagEntity> tagEntities) {
    // First we prevent the duplicate tag name
    final List<String> tagNames = tagEntities.stream().map(NewTagEntity::getName).collect(Collectors.toList());
    final Optional<TagEntity> optionalTag = findAll().stream().filter(tag -> tagNames.contains(tag.getName())).findAny();
    if (optionalTag.isPresent()) {
        throw new DuplicateTagNameException(optionalTag.get().getName());
    }
    final List<TagEntity> savedTags = new ArrayList<>(tagEntities.size());
    tagEntities.forEach(tagEntity -> {
        try {
            Tag tag = convert(tagEntity);
            savedTags.add(convert(tagRepository.create(tag)));
            auditService.createPortalAuditLog(Collections.singletonMap(TAG, tag.getId()), TAG_CREATED, new Date(), null, tag);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create tag {}", tagEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create tag " + tagEntity.getName(), ex);
        }
    });
    return savedTags;
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) NewTagEntity(io.gravitee.management.model.NewTagEntity) ApiService(io.gravitee.management.service.ApiService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) TagEntity(io.gravitee.management.model.TagEntity) Collectors(java.util.stream.Collectors) AuditEvent(io.gravitee.repository.management.model.Tag.AuditEvent) TagRepository(io.gravitee.repository.management.api.TagRepository) IdGenerator(io.gravitee.common.utils.IdGenerator) Tag(io.gravitee.repository.management.model.Tag) Component(org.springframework.stereotype.Component) TagService(io.gravitee.management.service.TagService) DuplicateTagNameException(io.gravitee.management.service.exceptions.DuplicateTagNameException) UpdateTagEntity(io.gravitee.management.model.UpdateTagEntity) TAG(io.gravitee.repository.management.model.Audit.AuditProperties.TAG) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTagEntity(io.gravitee.management.model.NewTagEntity) TagEntity(io.gravitee.management.model.TagEntity) UpdateTagEntity(io.gravitee.management.model.UpdateTagEntity) DuplicateTagNameException(io.gravitee.management.service.exceptions.DuplicateTagNameException) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 37 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method create.

@Override
public List<TenantEntity> create(final List<NewTenantEntity> tenantEntities) {
    // First we prevent the duplicate tenant name
    final List<String> tenantNames = tenantEntities.stream().map(NewTenantEntity::getName).collect(Collectors.toList());
    final Optional<TenantEntity> optionalTenant = findAll().stream().filter(tenant -> tenantNames.contains(tenant.getName())).findAny();
    if (optionalTenant.isPresent()) {
        throw new DuplicateTenantNameException(optionalTenant.get().getName());
    }
    final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
    tenantEntities.forEach(tenantEntity -> {
        try {
            Tenant tenant = convert(tenantEntity);
            savedTenants.add(convert(tenantRepository.create(tenant)));
            auditService.createPortalAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_CREATED, new Date(), null, tenant);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create tenant {}", tenantEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create tenant " + tenantEntity.getName(), ex);
        }
    });
    return savedTenants;
}
Also used : TenantEntity(io.gravitee.management.model.TenantEntity) TENANT_UPDATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_UPDATED) java.util(java.util) TenantNotFoundException(io.gravitee.management.service.exceptions.TenantNotFoundException) Logger(org.slf4j.Logger) NewTenantEntity(io.gravitee.management.model.NewTenantEntity) TENANT_CREATED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_CREATED) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TENANT(io.gravitee.repository.management.model.Audit.AuditProperties.TENANT) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) TENANT_DELETED(io.gravitee.repository.management.model.Tenant.AuditEvent.TENANT_DELETED) UpdateTenantEntity(io.gravitee.management.model.UpdateTenantEntity) Collectors(java.util.stream.Collectors) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) TenantRepository(io.gravitee.repository.management.api.TenantRepository) Tenant(io.gravitee.repository.management.model.Tenant) TenantService(io.gravitee.management.service.TenantService) DuplicateTenantNameException(io.gravitee.management.service.exceptions.DuplicateTenantNameException) Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TenantEntity(io.gravitee.management.model.TenantEntity) NewTenantEntity(io.gravitee.management.model.NewTenantEntity) UpdateTenantEntity(io.gravitee.management.model.UpdateTenantEntity) DuplicateTenantNameException(io.gravitee.management.service.exceptions.DuplicateTenantNameException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 38 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method findAll.

@Override
public Set<UserEntity> findAll(boolean loadRoles) {
    try {
        LOGGER.debug("Find all users");
        Set<User> users = userRepository.findAll();
        return users.stream().map(u -> convert(u, loadRoles)).collect(Collectors.toSet());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find all users", ex);
        throw new TechnicalManagementException("An error occurs while trying to find all users", ex);
    }
}
Also used : RoleScope(io.gravitee.repository.management.model.RoleScope) java.util(java.util) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) NotificationParamsBuilder(io.gravitee.management.service.notification.NotificationParamsBuilder) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) MembershipDefaultReferenceId(io.gravitee.repository.management.model.MembershipDefaultReferenceId) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EmailNotificationBuilder(io.gravitee.management.service.builder.EmailNotificationBuilder) StringUtils(org.apache.commons.lang3.StringUtils) Value(org.springframework.beans.factory.annotation.Value) DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER(io.gravitee.management.service.common.JWTHelper.DefaultValues.DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER) JWTVerifier(com.auth0.jwt.JWTVerifier) UUID(io.gravitee.common.utils.UUID) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) io.gravitee.management.model(io.gravitee.management.model) UserRepository(io.gravitee.repository.management.api.UserRepository) JWTSigner(com.auth0.jwt.JWTSigner) DEFAULT_JWT_ISSUER(io.gravitee.management.service.common.JWTHelper.DefaultValues.DEFAULT_JWT_ISSUER) UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) Claims(io.gravitee.management.service.common.JWTHelper.Claims) MembershipReferenceType(io.gravitee.repository.management.model.MembershipReferenceType) UsernameAlreadyExistsException(io.gravitee.management.service.exceptions.UsernameAlreadyExistsException) Logger(org.slf4j.Logger) DefaultRoleNotFoundException(io.gravitee.management.service.exceptions.DefaultRoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PortalHook(io.gravitee.management.service.notification.PortalHook) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) Component(org.springframework.stereotype.Component) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) USER(io.gravitee.repository.management.model.Audit.AuditProperties.USER) io.gravitee.management.service(io.gravitee.management.service) User(io.gravitee.repository.management.model.User) io.gravitee.common.utils(io.gravitee.common.utils) DatatypeConverter(javax.xml.bind.DatatypeConverter) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 39 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method findByUsername.

@Override
public UserEntity findByUsername(String username, boolean loadRoles) {
    try {
        LOGGER.debug("Find user by name: {}", username);
        Optional<User> optionalUser = userRepository.findByUsername(username);
        if (optionalUser.isPresent()) {
            return convert(optionalUser.get(), loadRoles);
        }
        // should never happen
        throw new UserNotFoundException(username);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find user using its username {}", username, ex);
        throw new TechnicalManagementException("An error occurs while trying to find user using its username " + username, ex);
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 40 with TechnicalManagementException

use of io.gravitee.management.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method update.

@Override
public UserEntity update(UpdateUserEntity updateUserEntity) {
    try {
        LOGGER.debug("Updating {}", updateUserEntity);
        Optional<User> checkUser = userRepository.findByUsername(updateUserEntity.getUsername());
        if (!checkUser.isPresent()) {
            throw new UserNotFoundException(updateUserEntity.getUsername());
        }
        User user = checkUser.get();
        User previousUser = new User(user);
        // Set date fields
        user.setUpdatedAt(new Date());
        // Set variant fields
        user.setPicture(updateUserEntity.getPicture());
        user.setFirstname(updateUserEntity.getFirstname());
        user.setLastname(updateUserEntity.getLastname());
        User updatedUser = userRepository.update(user);
        auditService.createPortalAuditLog(Collections.singletonMap(USER, user.getUsername()), User.AuditEvent.USER_UPDATED, user.getUpdatedAt(), previousUser, user);
        return convert(updatedUser, true);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update {}", updateUserEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying update " + updateUserEntity, ex);
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)90 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)83 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 Autowired (org.springframework.beans.factory.annotation.Autowired)17 Component (org.springframework.stereotype.Component)17 Collectors (java.util.stream.Collectors)13 java.util (java.util)11 Date (java.util.Date)11 AuditService (io.gravitee.management.service.AuditService)10 IdGenerator (io.gravitee.common.utils.IdGenerator)9 io.gravitee.management.model (io.gravitee.management.model)9 User (io.gravitee.repository.management.model.User)9 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)7 ApiService (io.gravitee.management.service.ApiService)6 MetadataService (io.gravitee.management.service.MetadataService)6