Search in sources :

Example 71 with TechnicalManagementException

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

the class GroupServiceImpl method findAll.

@Override
public List<GroupEntity> findAll() {
    try {
        logger.debug("Find all groups");
        Set<Group> all = groupRepository.findAll();
        logger.debug("Find all groups - DONE");
        return all.stream().map(this::map).collect(Collectors.toList());
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to find all groups", ex);
        throw new TechnicalManagementException("An error occurs while trying to find all groups", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 72 with TechnicalManagementException

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

the class GroupServiceImpl method update.

@Override
public GroupEntity update(String groupId, UpdateGroupEntity group) {
    try {
        logger.debug("update {}", group);
        GroupEntity updatedGroupEntity = this.findById(groupId);
        Group previousGroup = this.map(updatedGroupEntity);
        updatedGroupEntity.setName(group.getName());
        updatedGroupEntity.setUpdatedAt(new Date());
        updatedGroupEntity.setEventRules(group.getEventRules());
        Group updatedGroup = this.map(updatedGroupEntity);
        GroupEntity grp = this.map(groupRepository.update(updatedGroup));
        logger.debug("update {} - DONE", grp);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(GROUP, groupId), GROUP_UPDATED, updatedGroupEntity.getUpdatedAt(), previousGroup, updatedGroup);
        return grp;
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to update a group", ex);
        throw new TechnicalManagementException("An error occurs while trying to update a group", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 73 with TechnicalManagementException

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

the class GroupServiceImpl method delete.

@Override
public void delete(String groupId) {
    try {
        logger.debug("delete {}", groupId);
        Optional<Group> group = groupRepository.findById(groupId);
        if (!group.isPresent()) {
            throw new GroupNotFoundException(groupId);
        }
        // remove all members
        membershipRepository.findByReferenceAndRole(MembershipReferenceType.GROUP, groupId, null, null).forEach(member -> {
            try {
                membershipRepository.delete(member);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        // remove all applications or apis
        Date updatedDate = new Date();
        apiRepository.findByGroups(Collections.singletonList(groupId)).forEach(api -> {
            api.getGroups().remove(groupId);
            api.setUpdatedAt(updatedDate);
            try {
                apiRepository.update(api);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        applicationRepository.findByGroups(Collections.singletonList(groupId)).forEach(application -> {
            application.getGroups().remove(groupId);
            application.setUpdatedAt(updatedDate);
            try {
                applicationRepository.update(application);
            } catch (TechnicalException ex) {
                logger.error("An error occurs while trying to delete a group", ex);
                throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
            }
        });
        // remove group
        groupRepository.delete(groupId);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(GROUP, groupId), GROUP_DELETED, new Date(), group.get(), null);
        logger.debug("delete {} - DONE", groupId);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to delete a group", ex);
        throw new TechnicalManagementException("An error occurs while trying to delete a group", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) GroupNotFoundException(io.gravitee.management.service.exceptions.GroupNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 74 with TechnicalManagementException

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

the class GroupServiceImpl method isUserAuthorizedToAccessPortalData.

@Override
public boolean isUserAuthorizedToAccessPortalData(List<String> excludedGroups, String username) {
    // in anonymous mode, only pages without restrictions are authorized
    if (username == null) {
        return (excludedGroups == null || excludedGroups.isEmpty());
    }
    if (excludedGroups != null && !excludedGroups.isEmpty()) {
        // for public apis, default authorized groups are all groups,
        Set<String> authorizedGroups;
        try {
            authorizedGroups = groupRepository.findAll().stream().map(Group::getId).collect(Collectors.toSet());
        } catch (TechnicalException ex) {
            logger.error("An error occurs while trying to find all groups", ex);
            throw new TechnicalManagementException("An error occurs while trying to find all groups", ex);
        }
        authorizedGroups.removeAll(excludedGroups);
        for (String authorizedGroupId : authorizedGroups) {
            if (membershipService.getMember(MembershipReferenceType.GROUP, authorizedGroupId, username, RoleScope.API) != null) {
                return true;
            }
        }
        return false;
    }
    return true;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 75 with TechnicalManagementException

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

the class LogsServiceImpl method findByApplication.

@Override
public SearchLogResponse findByApplication(String application, LogQuery query) {
    try {
        TabularResponse response = logRepository.query(QueryBuilders.tabular().page(query.getPage()).size(query.getSize()).query(query.getQuery()).timeRange(DateRangeBuilder.between(query.getFrom(), query.getTo()), IntervalBuilder.interval(query.getInterval())).root("application", application).build());
        SearchLogResponse<ApplicationRequestItem> logResponse = new SearchLogResponse<>(response.getSize());
        // Transform repository logs
        logResponse.setLogs(response.getLogs().stream().map(this::toApplicationRequestItem).collect(Collectors.toList()));
        // Add metadata (only if they are results)
        if (response.getSize() > 0) {
            Map<String, Map<String, String>> metadata = new HashMap<>();
            logResponse.getLogs().forEach(logItem -> {
                String api = logItem.getApi();
                String plan = logItem.getPlan();
                if (api != null) {
                    metadata.computeIfAbsent(api, getAPIMetadata(api));
                }
                if (plan != null) {
                    metadata.computeIfAbsent(plan, getPlanMetadata(plan));
                }
            });
            logResponse.setMetadata(metadata);
        }
        return logResponse;
    } catch (AnalyticsException ae) {
        logger.error("Unable to retrieve logs: ", ae);
        throw new TechnicalManagementException("Unable to retrieve logs", ae);
    }
}
Also used : TabularResponse(io.gravitee.repository.analytics.query.tabular.TabularResponse) HashMap(java.util.HashMap) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) HashMap(java.util.HashMap) Map(java.util.Map) 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