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);
}
}
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);
}
}
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);
}
}
Aggregations