Search in sources :

Example 56 with TechnicalManagementException

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

the class MetadataServiceImpl method checkMetadataFormat.

@Override
public void checkMetadataFormat(final MetadataFormat format, final String value, final MetadataReferenceType referenceType, final Object entity) {
    try {
        String decodedValue = value;
        if (entity != null && !isBlank(value) && value.startsWith("${")) {
            decodedValue = this.notificationTemplateService.resolveInlineTemplateWithParam(value, new StringReader(value), singletonMap(referenceType.name().toLowerCase(), entity));
        }
        if (isBlank(decodedValue)) {
            return;
        }
        switch(format) {
            case BOOLEAN:
                Boolean.valueOf(decodedValue);
                break;
            case URL:
                new URL(decodedValue);
                break;
            case MAIL:
                final InternetAddress email = new InternetAddress(decodedValue);
                email.validate();
                break;
            case DATE:
                final SimpleDateFormat sdf = new SimpleDateFormat("YYYY-mm-dd");
                sdf.setLenient(false);
                sdf.parse(decodedValue);
                break;
            case NUMERIC:
                Double.valueOf(decodedValue);
                break;
        }
    } catch (final Exception e) {
        LOGGER.error("Error occurred while trying to validate format '{}' of value '{}'", format, value, e);
        throw new TechnicalManagementException("Error occurred while trying to validate format " + format + " of value " + value, e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) StringReader(java.io.StringReader) SimpleDateFormat(java.text.SimpleDateFormat) URL(java.net.URL) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 57 with TechnicalManagementException

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

the class MetadataServiceImpl method update.

@Override
public MetadataEntity update(final UpdateMetadataEntity metadataEntity) {
    try {
        // First we prevent the duplicate metadata name
        final Optional<Metadata> optionalMetadata = metadataRepository.findByReferenceType(MetadataReferenceType.DEFAULT).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataValue(metadataEntity.getValue());
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setUpdatedAt(now);
        metadataRepository.update(metadata);
        // Audit
        auditService.createEnvironmentAuditLog(singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) AuditEvent(io.gravitee.repository.management.model.Metadata.AuditEvent) UpdateMetadataEntity(io.gravitee.rest.api.model.UpdateMetadataEntity) MetadataService(io.gravitee.rest.api.service.MetadataService) Collections.singletonMap(java.util.Collections.singletonMap) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) Logger(org.slf4j.Logger) NewMetadataEntity(io.gravitee.rest.api.model.NewMetadataEntity) AuditService(io.gravitee.rest.api.service.AuditService) MetadataFormat(io.gravitee.rest.api.model.MetadataFormat) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Metadata(io.gravitee.repository.management.model.Metadata) StringReader(java.io.StringReader) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Optional(java.util.Optional) MetadataEntity(io.gravitee.rest.api.model.MetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.rest.api.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 58 with TechnicalManagementException

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

the class MetadataServiceImpl method delete.

@Override
public void delete(final String key) {
    try {
        final Optional<Metadata> optMetadata = metadataRepository.findById(key, DEFAULT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
        if (optMetadata.isPresent()) {
            metadataRepository.delete(key, DEFAULT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
            // Audit
            auditService.createEnvironmentAuditLog(singletonMap(METADATA, key), METADATA_DELETED, new Date(), optMetadata.get(), null);
            // delete all overridden API metadata
            final List<Metadata> apiMetadata = metadataRepository.findByKeyAndReferenceType(key, MetadataReferenceType.API);
            for (final Metadata metadata : apiMetadata) {
                metadataRepository.delete(key, metadata.getReferenceId(), metadata.getReferenceType());
                // Audit
                auditService.createApiAuditLog(metadata.getReferenceId(), singletonMap(METADATA, key), METADATA_DELETED, new Date(), metadata, null);
            }
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete metadata {}", key, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete metadata " + key, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 59 with TechnicalManagementException

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

the class TenantServiceImpl method update.

@Override
public List<TenantEntity> update(final List<UpdateTenantEntity> tenantEntities, String referenceId, TenantReferenceType referenceType) {
    final List<TenantEntity> savedTenants = new ArrayList<>(tenantEntities.size());
    tenantEntities.forEach(tenantEntity -> {
        try {
            Tenant tenant = convert(tenantEntity);
            Optional<Tenant> tenantOptional = tenantRepository.findByIdAndReference(tenant.getId(), referenceId, repoTenantReferenceType(referenceType));
            if (tenantOptional.isPresent()) {
                Tenant existingTenant = tenantOptional.get();
                tenant.setReferenceId(existingTenant.getReferenceId());
                tenant.setReferenceType(existingTenant.getReferenceType());
                savedTenants.add(convert(tenantRepository.update(tenant)));
                auditService.createEnvironmentAuditLog(Collections.singletonMap(TENANT, tenant.getId()), TENANT_UPDATED, new Date(), tenantOptional.get(), tenant);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update tenant {}", tenantEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update tenant " + tenantEntity.getName(), ex);
        }
    });
    return savedTenants;
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTenantEntity(io.gravitee.rest.api.model.NewTenantEntity) TenantEntity(io.gravitee.rest.api.model.TenantEntity) UpdateTenantEntity(io.gravitee.rest.api.model.UpdateTenantEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 60 with TechnicalManagementException

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

the class TenantServiceImpl method delete.

@Override
public void delete(final String tenantId, String referenceId, TenantReferenceType referenceType) {
    try {
        Optional<Tenant> tenantOptional = tenantRepository.findByIdAndReference(tenantId, referenceId, repoTenantReferenceType(referenceType));
        if (tenantOptional.isPresent()) {
            tenantRepository.delete(tenantId);
            auditService.createEnvironmentAuditLog(Collections.singletonMap(TENANT, tenantId), TENANT_DELETED, new Date(), null, tenantOptional.get());
            tenantRepository.delete(tenantId);
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete tenant {}", tenantId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete tenant " + tenantId, ex);
    }
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6