Search in sources :

Example 1 with TechnicalException

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

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);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with TechnicalException

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 4 with TechnicalException

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);
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) ApiMetadataService(io.gravitee.management.service.ApiMetadataService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) ApiMetadataNotFoundException(io.gravitee.management.service.exceptions.ApiMetadataNotFoundException) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) io.gravitee.management.model(io.gravitee.management.model) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 5 with TechnicalException

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);
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) ApiMetadataService(io.gravitee.management.service.ApiMetadataService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) ApiMetadataNotFoundException(io.gravitee.management.service.exceptions.ApiMetadataNotFoundException) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) io.gravitee.management.model(io.gravitee.management.model) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Metadata(io.gravitee.repository.management.model.Metadata) ApiMetadataNotFoundException(io.gravitee.management.service.exceptions.ApiMetadataNotFoundException) 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