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