Search in sources :

Example 26 with TechnicalException

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);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) 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) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) MetadataEntity(io.gravitee.management.model.MetadataEntity) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 27 with TechnicalException

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

Example 28 with TechnicalException

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

Example 29 with TechnicalException

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

Example 30 with TechnicalException

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