use of io.gravitee.repository.exceptions.TechnicalException 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.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiKeyServiceImpl method generate.
@Override
public ApiKeyEntity generate(String subscription) {
try {
LOGGER.debug("Generate an API Key for subscription {}", subscription);
ApiKey apiKey = generateForSubscription(subscription);
apiKey = apiKeyRepository.create(apiKey);
// TODO: Send a notification to the application owner
// Audit
final PlanEntity plan = planService.findById(apiKey.getPlan());
auditService.createApiAuditLog(plan.getApis().iterator().next(), Collections.singletonMap(API_KEY, apiKey.getKey()), APIKEY_CREATED, apiKey.getCreatedAt(), null, apiKey);
return convert(apiKey);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to generate an API Key for {} - {}", subscription, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to generate an API Key for %s", subscription), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method delete.
@Override
public void delete(final String metadataId, final String apiId) {
LOGGER.debug("Delete metadata by id {} and api {}", metadataId, apiId);
// prevent deletion of a metadata not in the given api
final ApiMetadataEntity apiMetadata = findByIdAndApi(metadataId, apiId);
try {
metadataRepository.delete(metadataId, apiMetadata.getApiId(), MetadataReferenceType.API);
// Audit
auditService.createApiAuditLog(apiId, Collections.singletonMap(METADATA, metadataId), METADATA_DELETED, new Date(), apiMetadata, null);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete metadata {}", metadataId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete metadata " + metadataId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method update.
@Override
public ApiMetadataEntity update(final UpdateApiMetadataEntity metadataEntity) {
final ApiEntity apiEntity = apiService.findById(metadataEntity.getApiId());
// First we prevent the duplicate metadata name
final Optional<ApiMetadataEntity> optionalMetadata = findAllByApi(apiEntity.getId()).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
if (optionalMetadata.isPresent()) {
throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
}
metadataService.checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getDefaultValue());
try {
final List<MetadataEntity> defaultMedatata = metadataService.findAllDefault();
final Optional<MetadataEntity> optDefaultMetadata = defaultMedatata.stream().filter(metadata -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
final Optional<Metadata> apiMetadata = metadataRepository.findById(metadataEntity.getKey(), metadataEntity.getApiId(), MetadataReferenceType.API);
final Metadata savedMetadata;
final Metadata metadata = convertForAPI(metadataEntity);
final Date now = new Date();
if (apiMetadata.isPresent()) {
metadata.setUpdatedAt(now);
savedMetadata = metadataRepository.update(metadata);
// Audit
auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getUpdatedAt(), apiMetadata.get(), metadata);
} else {
metadata.setCreatedAt(now);
metadata.setUpdatedAt(now);
savedMetadata = metadataRepository.create(metadata);
// Audit
auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
}
final ApiMetadataEntity apiMetadataEntity = convert(savedMetadata, null);
optDefaultMetadata.ifPresent(defaultMetadata -> apiMetadataEntity.setDefaultValue(defaultMetadata.getValue()));
return apiMetadataEntity;
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to update metadata {} on API {}", metadataEntity.getName(), metadataEntity.getApiId(), ex);
throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName() + " on API " + metadataEntity.getApiId(), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method findByIdAndApi.
@Override
public ApiMetadataEntity findByIdAndApi(final String metadataId, final String apiId) {
LOGGER.debug("Find metadata by id {} and api {}", metadataId, apiId);
try {
final List<MetadataEntity> defaultMedatata = metadataService.findAllDefault();
final Optional<Metadata> optMetadata = metadataRepository.findById(metadataId, apiId, MetadataReferenceType.API);
if (optMetadata.isPresent()) {
final Metadata metadata = optMetadata.get();
final Optional<MetadataEntity> optDefaultMetadata = defaultMedatata.stream().filter(metadataEntity -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
final ApiMetadataEntity apiMetadataEntity = convert(metadata, apiId);
optDefaultMetadata.ifPresent(defMetadata -> apiMetadataEntity.setDefaultValue(defMetadata.getValue()));
return apiMetadataEntity;
}
throw new ApiMetadataNotFoundException(apiId, metadataId);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find metadata by id and API", ex);
throw new TechnicalManagementException("An error occurred while trying to find metadata by id and API", ex);
}
}
Aggregations