use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class MetadataServiceImpl method create.
@Override
public MetadataEntity create(final NewMetadataEntity metadataEntity) {
// if no format defined, we just set String format
if (metadataEntity.getFormat() == null) {
metadataEntity.setFormat(MetadataFormat.STRING);
}
try {
// First we prevent the duplicate metadata name
final Optional<MetadataEntity> optionalMetadata = findAllDefault().stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
if (optionalMetadata.isPresent()) {
throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
}
checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
final Metadata metadata = convert(metadataEntity);
final Date now = new Date();
metadata.setCreatedAt(now);
metadata.setUpdatedAt(now);
metadataRepository.create(metadata);
// Audit
auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
return convert(metadata);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to create metadata {}", metadataEntity.getName(), ex);
throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName(), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException 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, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
if (optMetadata.isPresent()) {
metadataRepository.delete(key, DEFAUT_REFERENCE_ID, MetadataReferenceType.DEFAULT);
// Audit
auditService.createPortalAuditLog(Collections.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(), Collections.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.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method delete.
@Override
public void delete(String pageId) {
try {
logger.debug("Delete Page : {}", pageId);
Optional<Page> optPage = pageRepository.findById(pageId);
if (!optPage.isPresent()) {
throw new PageNotFoundException(pageId);
}
pageRepository.delete(pageId);
createAuditLog(optPage.get().getApi(), PAGE_DELETED, new Date(), optPage.get(), null);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to delete Page {}", pageId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete Page " + pageId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method findById.
@Override
public PageEntity findById(String pageId, boolean transform) {
try {
logger.debug("Find page by ID: {}", pageId);
Optional<Page> page = pageRepository.findById(pageId);
if (page.isPresent()) {
PageEntity pageEntity = convert(page.get());
if (transform) {
transformUsingConfiguration(pageEntity);
if (page.get().getApi() != null) {
transformWithTemplate(pageEntity, page.get().getApi());
}
}
return pageEntity;
}
throw new PageNotFoundException(pageId);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to find a page using its ID {}", pageId, ex);
throw new TechnicalManagementException("An error occurs while trying to find a page using its ID " + pageId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method createApiPage.
@Override
public PageEntity createApiPage(String apiId, NewPageEntity newPageEntity) {
try {
logger.debug("Create page {} for API {}", newPageEntity, apiId);
String id = UUID.toString(UUID.random());
Optional<Page> checkPage = pageRepository.findById(id);
if (checkPage.isPresent()) {
throw new PageAlreadyExistsException(id);
}
Page page = convert(newPageEntity);
if (page.getSource() != null) {
String fetchedContent = this.getContentFromFetcher(page.getSource());
if (fetchedContent != null && !fetchedContent.isEmpty()) {
page.setContent(fetchedContent);
}
}
page.setId(id);
page.setApi(apiId);
// Set date fields
page.setCreatedAt(new Date());
page.setUpdatedAt(page.getCreatedAt());
Page createdPage = pageRepository.create(page);
// only one homepage is allowed
onlyOneHomepage(page);
createAuditLog(apiId, PAGE_CREATED, page.getCreatedAt(), null, page);
return convert(createdPage);
} catch (TechnicalException | FetcherException ex) {
logger.error("An error occurs while trying to create {}", newPageEntity, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newPageEntity, ex);
}
}
Aggregations