Search in sources :

Example 11 with TechnicalException

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

the class ApplicationServiceImpl method findById.

@Override
public ApplicationEntity findById(String applicationId) {
    try {
        LOGGER.debug("Find application by ID: {}", applicationId);
        Optional<Application> application = applicationRepository.findById(applicationId);
        if (application.isPresent()) {
            Optional<Membership> primaryOwnerMembership = membershipRepository.findByReferenceAndRole(MembershipReferenceType.APPLICATION, applicationId, RoleScope.APPLICATION, SystemRole.PRIMARY_OWNER.name()).stream().findFirst();
            if (!primaryOwnerMembership.isPresent()) {
                LOGGER.error("The Application {} doesn't have any primary owner.", applicationId);
                throw new TechnicalException("The Application " + applicationId + " doesn't have any primary owner.");
            }
            return convert(application.get(), userService.findById(primaryOwnerMembership.get().getUserId()));
        }
        throw new ApplicationNotFoundException(applicationId);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find an application using its ID {}", applicationId, ex);
        throw new TechnicalManagementException("An error occurs while trying to find an application using its ID " + applicationId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApplicationNotFoundException(io.gravitee.management.service.exceptions.ApplicationNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 12 with TechnicalException

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

the class ApplicationServiceImpl method findAll.

@Override
public Set<ApplicationEntity> findAll() {
    try {
        LOGGER.debug("Find all applications");
        final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
        if (applications == null || applications.isEmpty()) {
            return emptySet();
        }
        return this.convert(applications);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find all applications", ex);
        throw new TechnicalManagementException("An error occurs while trying to find all applications", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 13 with TechnicalException

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

the class ApplicationServiceImpl method archive.

@Override
public void archive(String applicationId) {
    try {
        LOGGER.debug("Delete application {}", applicationId);
        Optional<Application> optApplication = applicationRepository.findById(applicationId);
        if (!optApplication.isPresent()) {
            throw new ApplicationNotFoundException(applicationId);
        }
        Application application = optApplication.get();
        Application previousApplication = new Application(application);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.findByApplicationAndPlan(applicationId, null);
        subscriptions.forEach(subscription -> {
            Set<ApiKeyEntity> apiKeys = apiKeyService.findBySubscription(subscription.getId());
            apiKeys.forEach(apiKey -> {
                try {
                    apiKeyService.delete(apiKey.getKey());
                } catch (TechnicalManagementException tme) {
                    LOGGER.error("An error occurs while deleting API Key {}", apiKey.getKey(), tme);
                }
            });
            try {
                subscriptionService.close(subscription.getId());
            } catch (SubscriptionNotClosableException snce) {
                // Subscription can not be closed because it is already closed or not yet accepted
                LOGGER.debug("The subscription can not be closed: {}", snce.getMessage());
            }
        });
        // Archive the application
        application.setUpdatedAt(new Date());
        application.setStatus(ApplicationStatus.ARCHIVED);
        applicationRepository.update(application);
        // Audit
        auditService.createApplicationAuditLog(application.getId(), Collections.emptyMap(), APPLICATION_ARCHIVED, application.getUpdatedAt(), previousApplication, application);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete application {}", applicationId, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to delete application %s", applicationId), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) SubscriptionNotClosableException(io.gravitee.management.service.exceptions.SubscriptionNotClosableException) ApplicationNotFoundException(io.gravitee.management.service.exceptions.ApplicationNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 14 with TechnicalException

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

the class AuditServiceImpl method create.

@Async
protected void create(Audit.AuditReferenceType referenceType, String referenceId, Map<Audit.AuditProperties, String> properties, Audit.AuditEvent event, String userId, Date createdAt, Object oldValue, Object newValue) {
    Audit audit = new Audit();
    audit.setId(UUID.toString(UUID.random()));
    audit.setUser(userId);
    audit.setCreatedAt(createdAt);
    if (properties != null) {
        Map<String, String> stringStringMap = new HashMap<>(properties.size());
        properties.forEach((auditProperties, s) -> stringStringMap.put(auditProperties.name(), s));
        audit.setProperties(stringStringMap);
    }
    audit.setReferenceType(referenceType);
    audit.setReferenceId(referenceId);
    audit.setEvent(event.name());
    ObjectNode oldNode = oldValue == null ? mapper.createObjectNode() : mapper.convertValue(oldValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));
    ObjectNode newNode = newValue == null ? mapper.createObjectNode() : mapper.convertValue(newValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));
    audit.setPatch(JsonDiff.asJson(oldNode, newNode).toString());
    try {
        auditRepository.create(audit);
    } catch (TechnicalException e) {
        LOGGER.error("Error occurs during the creation of an Audit Log {}.", e);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Async(org.springframework.scheduling.annotation.Async)

Example 15 with TechnicalException

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

the class AuditServiceImpl method getMetadata.

private Map<String, String> getMetadata(List<AuditEntity> content) {
    Map<String, String> metadata = new HashMap<>();
    for (AuditEntity auditEntity : content) {
        if (auditEntity.getProperties() != null) {
            for (Map.Entry<String, String> property : auditEntity.getProperties().entrySet()) {
                String metadataKey = new StringJoiner(":").add(property.getKey()).add(property.getValue()).add("name").toString();
                if (!metadata.containsKey(metadataKey)) {
                    String name = property.getValue();
                    try {
                        switch(Audit.AuditProperties.valueOf(property.getKey())) {
                            case PAGE:
                                Optional<io.gravitee.repository.management.model.Page> optPage = pageRepository.findById(property.getValue());
                                if (optPage.isPresent()) {
                                    name = optPage.get().getName();
                                }
                                break;
                            case PLAN:
                                Optional<Plan> optPlan = planRepository.findById(property.getValue());
                                if (optPlan.isPresent()) {
                                    name = optPlan.get().getName();
                                }
                                break;
                            case METADATA:
                                MetadataReferenceType refType = (Audit.AuditReferenceType.API.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.API : (Audit.AuditReferenceType.APPLICATION.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.APPLICATION : MetadataReferenceType.DEFAULT;
                                String refId = refType.equals(MetadataReferenceType.DEFAULT) ? getDefautReferenceId() : auditEntity.getReferenceId();
                                Optional<Metadata> optMetadata = metadataRepository.findById(property.getValue(), refId, refType);
                                if (optMetadata.isPresent()) {
                                    name = optMetadata.get().getName();
                                }
                                break;
                            case GROUP:
                                Optional<Group> optGroup = groupRepository.findById(property.getValue());
                                if (optGroup.isPresent()) {
                                    name = optGroup.get().getName();
                                }
                                break;
                            case USER:
                                Optional<User> optUser = userRepository.findById(property.getValue());
                                if (optUser.isPresent()) {
                                    if (optUser.get().getFirstname() != null && optUser.get().getLastname() != null) {
                                        name = optUser.get().getFirstname() + " " + optUser.get().getLastname();
                                    } else {
                                        name = optUser.get().getUsername();
                                    }
                                }
                            default:
                                break;
                        }
                    } catch (TechnicalException e) {
                        LOGGER.error("Error finding metadata {}", metadataKey);
                        name = property.getValue();
                    }
                    metadata.put(metadataKey, name);
                }
            }
        }
    }
    return metadata;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Page(io.gravitee.common.data.domain.Page) MetadataPage(io.gravitee.common.data.domain.MetadataPage) AuditEntity(io.gravitee.management.model.audit.AuditEntity)

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