Search in sources :

Example 71 with TechnicalException

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

the class RoleServiceImpl method findById.

@Override
public RoleEntity findById(final RoleScope scope, final String name) {
    try {
        LOGGER.debug("Find Role by id");
        Optional<Role> role = roleRepository.findById(scope, name);
        if (!role.isPresent()) {
            throw new RoleNotFoundException(scope, name);
        }
        return convert(role.get());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find a role : {} {}", scope, name, ex);
        throw new TechnicalManagementException("An error occurs while trying to find a role : " + scope + " " + name, ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 72 with TechnicalException

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

the class RoleServiceImpl method findDefaultRoleByScopes.

@Override
public List<RoleEntity> findDefaultRoleByScopes(RoleScope... scopes) {
    try {
        LOGGER.debug("Find default Roles by scope");
        List<RoleEntity> roles = new ArrayList<>();
        for (RoleScope scope : scopes) {
            roles.addAll(roleRepository.findByScope(scope).stream().filter(Role::isDefaultRole).map(this::convert).collect(Collectors.toList()));
        }
        return roles;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find default roles by scope", ex);
        throw new TechnicalManagementException("An error occurs while trying to find default roles by scope", ex);
    }
}
Also used : UpdateRoleEntity(io.gravitee.management.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.management.model.NewRoleEntity) RoleEntity(io.gravitee.management.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleScope(io.gravitee.repository.management.model.RoleScope) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 73 with TechnicalException

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

the class RoleServiceImpl method createOrUpdateSystemRoles.

@Override
public void createOrUpdateSystemRoles() {
    try {
        // MANAGEMENT - ADMIN
        createOrUpdateSystemRole(SystemRole.ADMIN, RoleScope.MANAGEMENT, io.gravitee.management.model.permissions.RoleScope.MANAGEMENT, ManagementPermission.values());
        // PORTAL - ADMIN
        createOrUpdateSystemRole(SystemRole.ADMIN, RoleScope.PORTAL, io.gravitee.management.model.permissions.RoleScope.PORTAL, PortalPermission.values());
        // API - PRIMARY_OWNER
        createOrUpdateSystemRole(SystemRole.PRIMARY_OWNER, RoleScope.API, io.gravitee.management.model.permissions.RoleScope.API, ApiPermission.values());
        // APPLICATION - PRIMARY_OWNER
        createOrUpdateSystemRole(SystemRole.PRIMARY_OWNER, RoleScope.APPLICATION, io.gravitee.management.model.permissions.RoleScope.APPLICATION, ApplicationPermission.values());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create admin roles", ex);
        throw new TechnicalManagementException("An error occurs while trying to create admin roles ", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 74 with TechnicalException

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

the class ApiKeyServiceImpl method update.

@Override
public ApiKeyEntity update(ApiKeyEntity apiKeyEntity) {
    try {
        LOGGER.debug("Update API Key {}", apiKeyEntity.getKey());
        Optional<ApiKey> optKey = apiKeyRepository.findById(apiKeyEntity.getKey());
        if (!optKey.isPresent()) {
            throw new ApiKeyNotFoundException();
        }
        ApiKey key = optKey.get();
        setExpiration(apiKeyEntity.getExpireAt(), key);
        return convert(key);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while updating an API Key {}", apiKeyEntity.getKey(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while updating an API Key %s", apiKeyEntity.getKey()), ex);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApiKeyNotFoundException(io.gravitee.management.service.exceptions.ApiKeyNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 75 with TechnicalException

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

the class ApiKeyServiceImpl method renew.

@Override
public ApiKeyEntity renew(String subscription) {
    try {
        LOGGER.debug("Renew API Key for subscription {}", subscription);
        ApiKey newApiKey = generateForSubscription(subscription);
        newApiKey = apiKeyRepository.create(newApiKey);
        Instant expirationInst = newApiKey.getCreatedAt().toInstant().plus(Duration.ofHours(2));
        Date expirationDate = Date.from(expirationInst);
        // Previously generated keys should be set as revoked
        // Get previously generated keys to set their expiration date
        Set<ApiKey> oldKeys = apiKeyRepository.findBySubscription(subscription);
        for (ApiKey oldKey : oldKeys) {
            if (!oldKey.equals(newApiKey)) {
                setExpiration(expirationDate, oldKey);
            }
        }
        // TODO: Send a notification to the application owner
        // Audit
        final PlanEntity plan = planService.findById(newApiKey.getPlan());
        auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(API_KEY, newApiKey.getKey()), APIKEY_RENEWED, newApiKey.getCreatedAt(), null, newApiKey);
        return convert(newApiKey);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to renew an API Key for {}", subscription, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to renew an API Key for %s", subscription), ex);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Instant(java.time.Instant) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

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