Search in sources :

Example 6 with TechnicalManagementException

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

the class DictionaryServiceImpl method stop.

@Override
public DictionaryEntity stop(String id) {
    try {
        LOGGER.debug("Stop dictionary {}", id);
        Optional<Dictionary> optDictionary = dictionaryRepository.findById(id);
        if (!optDictionary.isPresent()) {
            throw new DictionaryNotFoundException(id);
        }
        // add deployment date
        Dictionary dictionary = optDictionary.get();
        dictionary.setUpdatedAt(new Date());
        dictionary.setState(LifecycleState.STOPPED);
        dictionary = dictionaryRepository.update(dictionary);
        Map<String, String> properties = new HashMap<>();
        properties.put(Event.EventProperties.DICTIONARY_ID.getValue(), id);
        // And create event
        eventService.create(EventType.STOP_DICTIONARY, null, properties);
        // Audit
        createAuditLog(Dictionary.AuditEvent.DICTIONARY_UPDATED, dictionary.getCreatedAt(), optDictionary.get(), dictionary);
        return convert(dictionary);
    } catch (Exception ex) {
        LOGGER.error("An error occurs while trying to undeploy dictionary {}", id, ex);
        throw new TechnicalManagementException("An error occurs while trying to undeploy " + id, ex);
    }
}
Also used : Dictionary(io.gravitee.repository.management.model.Dictionary) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IOException(java.io.IOException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 7 with TechnicalManagementException

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

the class IdentityProviderServiceImpl method create.

@Override
public IdentityProviderEntity create(NewIdentityProviderEntity newIdentityProviderEntity) {
    try {
        LOGGER.debug("Create identity provider {}", newIdentityProviderEntity);
        Optional<IdentityProvider> optIdentityProvider = identityProviderRepository.findById(IdGenerator.generate(newIdentityProviderEntity.getName()));
        if (optIdentityProvider.isPresent()) {
            throw new IdentityProviderAlreadyExistsException(newIdentityProviderEntity.getName());
        }
        IdentityProvider identityProvider = convert(newIdentityProviderEntity);
        identityProvider.setOrganizationId(GraviteeContext.getCurrentOrganization());
        // If provider is a social type, we must ensure required parameters
        if (identityProvider.getType() == IdentityProviderType.GOOGLE || identityProvider.getType() == IdentityProviderType.GITHUB) {
            checkSocialProvider(identityProvider);
        }
        // Set date fields
        identityProvider.setCreatedAt(new Date());
        identityProvider.setUpdatedAt(identityProvider.getCreatedAt());
        IdentityProvider createdIdentityProvider = identityProviderRepository.create(identityProvider);
        identityProviderActivationService.activateIdpOnTargets(createdIdentityProvider.getId(), new IdentityProviderActivationService.ActivationTarget(GraviteeContext.getCurrentOrganization(), IdentityProviderActivationReferenceType.ORGANIZATION));
        auditService.createOrganizationAuditLog(singletonMap(IDENTITY_PROVIDER, createdIdentityProvider.getId()), IdentityProvider.AuditEvent.IDENTITY_PROVIDER_CREATED, createdIdentityProvider.getUpdatedAt(), null, createdIdentityProvider);
        return convert(createdIdentityProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create identity provider {}", newIdentityProviderEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying to create " + newIdentityProviderEntity, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IdentityProviderActivationService(io.gravitee.rest.api.service.configuration.identity.IdentityProviderActivationService) IdentityProvider(io.gravitee.repository.management.model.IdentityProvider) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 8 with TechnicalManagementException

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

the class IdentityProviderServiceImpl method update.

@Override
public IdentityProviderEntity update(String id, UpdateIdentityProviderEntity updateIdentityProvider) {
    try {
        LOGGER.debug("Update identity provider {}", updateIdentityProvider);
        Optional<IdentityProvider> optIdentityProvider = identityProviderRepository.findById(id);
        if (!optIdentityProvider.isPresent()) {
            throw new IdentityProviderNotFoundException(updateIdentityProvider.getName());
        }
        // TODO: Find a way to validate mapping expression
        IdentityProvider identityProvider = convert(updateIdentityProvider);
        final IdentityProvider idpToUpdate = optIdentityProvider.get();
        identityProvider.setId(id);
        identityProvider.setType(idpToUpdate.getType());
        identityProvider.setCreatedAt(idpToUpdate.getCreatedAt());
        identityProvider.setUpdatedAt(new Date());
        identityProvider.setOrganizationId(optIdentityProvider.get().getOrganizationId());
        IdentityProvider updatedIdentityProvider = identityProviderRepository.update(identityProvider);
        // Audit
        auditService.createOrganizationAuditLog(singletonMap(IDENTITY_PROVIDER, id), IdentityProvider.AuditEvent.IDENTITY_PROVIDER_UPDATED, identityProvider.getUpdatedAt(), idpToUpdate, updatedIdentityProvider);
        return convert(updatedIdentityProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update identity provider {}", updateIdentityProvider, ex);
        throw new TechnicalManagementException("An error occurs while trying to update " + updateIdentityProvider, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IdentityProvider(io.gravitee.repository.management.model.IdentityProvider) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 9 with TechnicalManagementException

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

the class TaskServiceImpl method convert.

private TaskEntity convert(UserEntity user) {
    TaskEntity taskEntity = new TaskEntity();
    try {
        taskEntity.setType(TaskType.USER_REGISTRATION_APPROVAL);
        taskEntity.setCreatedAt(user.getCreatedAt());
        taskEntity.setData(user);
    } catch (Exception e) {
        LOGGER.error("Error converting user {} to a Task", user.getId());
        throw new TechnicalManagementException("Error converting user " + user.getId() + " to a Task", e);
    }
    return taskEntity;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 10 with TechnicalManagementException

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

the class TaskServiceImpl method findAll.

@Override
public List<TaskEntity> findAll(String userId) {
    if (userId == null) {
        throw new UnauthorizedAccessException();
    }
    try {
        // because Tasks only consists on subscriptions, we can optimize the search by only look for apis where
        // the user has a SUBSCRIPTION_UPDATE permission
        // search for PENDING subscriptions
        Set<String> apiIds = getApisForAPermission(userId, SUBSCRIPTION.getName());
        final List<TaskEntity> tasks;
        if (apiIds.isEmpty()) {
            tasks = new ArrayList<>();
        } else {
            SubscriptionQuery query = new SubscriptionQuery();
            query.setStatuses(singleton(PENDING));
            query.setApis(apiIds);
            tasks = subscriptionService.search(query).stream().map(this::convert).collect(toList());
        }
        // search for PENDING user registration
        final Page<UserEntity> pendingUsers = userService.search(new UserCriteria.Builder().statuses(UserStatus.PENDING).build(), new PageableImpl(1, NUMBER_OF_PENDING_USERS_TO_SEARCH));
        if (pendingUsers.getContent() != null && !pendingUsers.getContent().isEmpty()) {
            tasks.addAll(pendingUsers.getContent().stream().map(this::convert).collect(toList()));
        }
        // search for IN_REVIEW apis
        apiIds = getApisForAPermission(userId, REVIEWS.getName());
        if (!apiIds.isEmpty()) {
            apiIds.forEach(apiId -> {
                final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
                if (workflows != null && !workflows.isEmpty()) {
                    final Workflow currentWorkflow = workflows.get(0);
                    if (WorkflowState.IN_REVIEW.name().equals(currentWorkflow.getState())) {
                        tasks.add(convert(currentWorkflow));
                    }
                }
            });
        }
        // search for REQUEST_FOR_CHANGES apis
        apiIds = getApisForAPermission(userId, DEFINITION.getName());
        if (!apiIds.isEmpty()) {
            apiIds.forEach(apiId -> {
                final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
                if (workflows != null && !workflows.isEmpty()) {
                    final Workflow currentWorkflow = workflows.get(0);
                    if (WorkflowState.REQUEST_FOR_CHANGES.name().equals(currentWorkflow.getState())) {
                        tasks.add(convert(currentWorkflow));
                    }
                }
            });
        }
        // search for TO_BE_VALIDATED promotions
        tasks.addAll(promotionTasksService.getPromotionTasks(GraviteeContext.getCurrentOrganization()));
        return tasks;
    } catch (TechnicalException e) {
        LOGGER.error("Error retrieving user tasks {}", e.getMessage());
        throw new TechnicalManagementException("Error retreiving user tasks", e);
    }
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Workflow(io.gravitee.repository.management.model.Workflow) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) 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