Search in sources :

Example 76 with TechnicalManagementException

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

the class NotificationTemplateServiceImpl method create.

@Override
public NotificationTemplateEntity create(NotificationTemplateEntity newNotificationTemplate) {
    try {
        LOGGER.debug("Create notificationTemplate {}", newNotificationTemplate);
        newNotificationTemplate.setId(UuidString.generateRandom());
        if (newNotificationTemplate.getCreatedAt() == null) {
            newNotificationTemplate.setCreatedAt(new Date());
        }
        NotificationTemplate createdNotificationTemplate = notificationTemplateRepository.create(convert(newNotificationTemplate));
        this.createAuditLog(NotificationTemplate.AuditEvent.NOTIFICATION_TEMPLATE_CREATED, newNotificationTemplate.getCreatedAt(), null, createdNotificationTemplate);
        final NotificationTemplateEntity createdNotificationTemplateEntity = convert(createdNotificationTemplate);
        // Update template in loader cache
        updateFreemarkerCache(createdNotificationTemplateEntity);
        return createdNotificationTemplateEntity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create or update notificationTemplate {}", newNotificationTemplate, ex);
        throw new TechnicalManagementException("An error occurs while trying to create or update " + newNotificationTemplate, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) NotificationTemplate(io.gravitee.repository.management.model.NotificationTemplate) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 77 with TechnicalManagementException

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

the class TagServiceImpl method findByIdAndReference.

@Override
public TagEntity findByIdAndReference(String tagId, String referenceId, TagReferenceType referenceType) {
    try {
        LOGGER.debug("Find tag by ID: {}", tagId);
        Optional<Tag> optTag = tagRepository.findByIdAndReference(tagId, referenceId, repoTagReferenceType(referenceType));
        if (!optTag.isPresent()) {
            throw new TagNotFoundException(tagId);
        }
        return convert(optTag.get());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find tag by ID", ex);
        throw new TechnicalManagementException("An error occurs while trying to find tag by ID", ex);
    }
}
Also used : TagNotFoundException(io.gravitee.rest.api.service.exceptions.TagNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 78 with TechnicalManagementException

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

the class TagServiceImpl method update.

@Override
public List<TagEntity> update(final List<UpdateTagEntity> tagEntities, String referenceId, TagReferenceType referenceType) {
    final List<TagEntity> savedTags = new ArrayList<>(tagEntities.size());
    tagEntities.forEach(tagEntity -> {
        try {
            Tag tag = convert(tagEntity);
            Optional<Tag> tagOptional = tagRepository.findByIdAndReference(tag.getId(), referenceId, repoTagReferenceType(referenceType));
            if (tagOptional.isPresent()) {
                Tag existingTag = tagOptional.get();
                tag.setReferenceId(existingTag.getReferenceId());
                tag.setReferenceType(existingTag.getReferenceType());
                savedTags.add(convert(tagRepository.update(tag)));
                auditService.createOrganizationAuditLog(Collections.singletonMap(TAG, tag.getId()), TAG_UPDATED, new Date(), tagOptional.get(), tag);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update tag {}", tagEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update tag " + tagEntity.getName(), ex);
        }
    });
    return savedTags;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) TagEntity(io.gravitee.rest.api.model.TagEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 79 with TechnicalManagementException

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

the class V3UpgraderService method moveIdpPermission.

public void moveIdpPermission() {
    try {
        final Set<Role> allRole = this.roleRepository.findByScopeAndReferenceIdAndReferenceType(RoleScope.ENVIRONMENT, "DEFAULT", RoleReferenceType.ORGANIZATION);
        logger.info("{} environment roles found", allRole.size());
        for (Role envRole : allRole) {
            int idpPerm = -1;
            int idpPermIndex = -1;
            final int[] envPermissions = envRole.getPermissions();
            for (int index = 0; index < envPermissions.length; index++) {
                if (envPermissions[index] > 3399 && envPermissions[index] < 3500) {
                    idpPerm = envPermissions[index];
                    idpPermIndex = index;
                }
            }
            if (idpPerm != -1 && idpPermIndex != -1) {
                logger.info("Permission: {} found on role {}", idpPerm, envRole.getName());
                Role orgRole;
                final Optional<Role> existingOrgRoleWithSameNameRoleCursor = this.roleRepository.findByScopeAndNameAndReferenceIdAndReferenceType(RoleScope.ORGANIZATION, envRole.getName(), envRole.getReferenceId(), envRole.getReferenceType());
                if (existingOrgRoleWithSameNameRoleCursor.isPresent()) {
                    logger.info("An org role exist with the same name");
                    orgRole = existingOrgRoleWithSameNameRoleCursor.get();
                    orgRole.setPermissions(ArrayUtils.add(orgRole.getPermissions(), 1300 + idpPerm % 100));
                    logger.info("permissions updated");
                    this.roleRepository.update(orgRole);
                    logger.info("Update of org role done");
                } else {
                    logger.info("No org role exist with the same name");
                    orgRole = new Role();
                    orgRole.setId(UuidString.generateRandom());
                    orgRole.setName(envRole.getName());
                    orgRole.setDefaultRole(envRole.isDefaultRole());
                    orgRole.setReferenceId(GraviteeContext.getDefaultOrganization());
                    orgRole.setReferenceType(RoleReferenceType.ORGANIZATION);
                    orgRole.setScope(RoleScope.ORGANIZATION);
                    orgRole.setCreatedAt(new Date());
                    orgRole.setDescription(envRole.getDescription());
                    orgRole.setSystem(envRole.isSystem());
                    orgRole.setPermissions(new int[] { 1300 + idpPerm % 100 });
                    this.roleRepository.create(orgRole);
                    logger.info("Creation of org role done");
                }
                envRole.setPermissions(ArrayUtils.remove(envPermissions, idpPermIndex));
                this.roleRepository.update(envRole);
                logger.info("Remove permission from env role done");
                // Update memberships
                final Set<Membership> envMembershipsWithRole = this.membershipRepository.findByReferenceAndRoleId(MembershipReferenceType.ENVIRONMENT, "DEFAULT", envRole.getId());
                for (Membership membership : envMembershipsWithRole) {
                    Set<Membership> orgMembershipsWithRole = this.membershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceIdAndRoleId(membership.getMemberId(), membership.getMemberType(), MembershipReferenceType.ORGANIZATION, "DEFAULT", orgRole.getId());
                    if (orgMembershipsWithRole.isEmpty()) {
                        Membership newOrganizationMembership = membership;
                        newOrganizationMembership.setId(UuidString.generateRandom());
                        newOrganizationMembership.setReferenceId("DEFAULT");
                        newOrganizationMembership.setReferenceType(MembershipReferenceType.ORGANIZATION);
                        newOrganizationMembership.setRoleId(orgRole.getId());
                        this.membershipRepository.create(newOrganizationMembership);
                        logger.info("New membership {} on organization created for user {}", newOrganizationMembership.getId(), membership.getMemberId());
                    }
                }
            }
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to retrieve all identity providers", ex);
        throw new TechnicalManagementException("An error occurs while trying to retrieve identity providers", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 80 with TechnicalManagementException

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

the class V3UpgraderService method convertIDPRoleMapping.

public void convertIDPRoleMapping() {
    try {
        Set<IdentityProvider> allIdps = identityProviderRepository.findAll();
        logger.debug("{} Idp found", allIdps.size());
        for (IdentityProvider idp : allIdps) {
            boolean idpHasBeenModified = false;
            Map<String, String[]> roleMappings = idp.getRoleMappings();
            if (roleMappings != null) {
                logger.debug("Idp '{}' has {} roleMappings", idp.getId(), roleMappings.size());
                for (Entry<String, String[]> entry : roleMappings.entrySet()) {
                    String[] roles = entry.getValue();
                    if (roles != null) {
                        logger.debug("Idp '{}' - RoleMapping with condition '{}' has {} roles", idp.getId(), entry.getKey(), roles.length);
                        List<String> newRoles = new ArrayList<>(roles.length);
                        boolean entryHasBeenModified = false;
                        for (String role : roles) {
                            String[] splittedRole = role.split(":");
                            if ("1".equals(splittedRole[0])) {
                                Optional<Role> existingOrgaRoleWithSameName = roleRepository.findByScopeAndNameAndReferenceIdAndReferenceType(RoleScope.ORGANIZATION, splittedRole[1], "DEFAULT", RoleReferenceType.ORGANIZATION);
                                if (existingOrgaRoleWithSameName.isPresent()) {
                                    newRoles.add("ORGANIZATION:" + splittedRole[1]);
                                }
                                newRoles.add("ENVIRONMENT:" + splittedRole[1]);
                                entryHasBeenModified = true;
                            } else if ("2".equals(splittedRole[0])) {
                                newRoles.add("ENVIRONMENT:" + splittedRole[1]);
                                entryHasBeenModified = true;
                            }
                        }
                        if (entryHasBeenModified) {
                            entry.setValue(newRoles.toArray(new String[newRoles.size()]));
                            idpHasBeenModified = true;
                        }
                    } else {
                        logger.debug("Idp '{}' - RoleMapping with condition '{}' has no roles", idp.getId(), entry.getKey());
                    }
                }
            }
            if (idpHasBeenModified) {
                identityProviderRepository.update(idp);
                logger.info("Idp '{}' has been updated", idp.getId());
            } else {
                logger.info("Idp '{}' has not been updated", idp.getId());
            }
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to retrieve all identity providers", ex);
        throw new TechnicalManagementException("An error occurs while trying to retrieve identity providers", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6