Search in sources :

Example 1 with ApiKeyNotFoundException

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

the class ApiKeyServiceImpl method revoke.

@Override
public void revoke(String apiKey, boolean notify) {
    try {
        LOGGER.debug("Revoke API Key {}", apiKey);
        Optional<ApiKey> optKey = apiKeyRepository.findById(apiKey);
        if (!optKey.isPresent()) {
            throw new ApiKeyNotFoundException();
        }
        ApiKey key = optKey.get();
        if (!key.isRevoked()) {
            ApiKey previousApiKey = new ApiKey(key);
            key.setRevoked(true);
            key.setUpdatedAt(new Date());
            key.setRevokedAt(key.getUpdatedAt());
            apiKeyRepository.update(key);
            final PlanEntity plan = planService.findById(key.getPlan());
            // Audit
            auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(API_KEY, key.getKey()), APIKEY_REVOKED, key.getUpdatedAt(), previousApiKey, key);
            // notify
            if (notify) {
                final ApplicationEntity application = applicationService.findById(key.getApplication());
                final ApiModelEntity api = apiService.findByIdForTemplates(plan.getApis().iterator().next());
                final PrimaryOwnerEntity owner = application.getPrimaryOwner();
                final Map<String, Object> params = new NotificationParamsBuilder().application(application).plan(plan).api(api).owner(owner).apikey(key).build();
                notifierService.trigger(ApiHook.APIKEY_REVOKED, api.getId(), params);
            }
        } else {
            LOGGER.info("API Key {} already revoked. Skipping...", apiKey);
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to revoke a key {}", apiKey, ex);
        throw new TechnicalManagementException("An error occurs while trying to revoke a key " + apiKey, ex);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApiKeyNotFoundException(io.gravitee.management.service.exceptions.ApiKeyNotFoundException) NotificationParamsBuilder(io.gravitee.management.service.notification.NotificationParamsBuilder) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with ApiKeyNotFoundException

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

the class ApiKeyServiceImpl method findByKey.

@Override
public ApiKeyEntity findByKey(String apiKey) {
    try {
        LOGGER.debug("Find an API Key by key: {}", apiKey);
        Optional<ApiKey> optApiKey = apiKeyRepository.findById(apiKey);
        if (optApiKey.isPresent()) {
            return convert(optApiKey.get());
        }
        throw new ApiKeyNotFoundException();
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find an API Key by key {}", apiKey, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to find an API Key by key: %s", apiKey), 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 3 with ApiKeyNotFoundException

use of io.gravitee.management.service.exceptions.ApiKeyNotFoundException 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)

Aggregations

ApiKeyNotFoundException (io.gravitee.management.service.exceptions.ApiKeyNotFoundException)3 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 ApiKey (io.gravitee.repository.management.model.ApiKey)3 NotificationParamsBuilder (io.gravitee.management.service.notification.NotificationParamsBuilder)1