Search in sources :

Example 1 with FetcherException

use of io.gravitee.fetcher.api.FetcherException 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)

Example 2 with FetcherException

use of io.gravitee.fetcher.api.FetcherException in project gravitee-management-rest-api by gravitee-io.

the class PageServiceImpl method getContentFromFetcher.

private String getContentFromFetcher(PageSource ps) throws FetcherException {
    if (ps.getConfiguration().isEmpty()) {
        return null;
    }
    try {
        FetcherPlugin fetcherPlugin = fetcherPluginManager.get(ps.getType());
        ClassLoader fetcherCL = fetcherPlugin.fetcher().getClassLoader();
        Class<? extends FetcherConfiguration> fetcherConfigurationClass = (Class<? extends FetcherConfiguration>) fetcherCL.loadClass(fetcherPlugin.configuration().getName());
        Class<? extends Fetcher> fetcherClass = (Class<? extends Fetcher>) fetcherCL.loadClass(fetcherPlugin.clazz());
        FetcherConfiguration fetcherConfigurationInstance = fetcherConfigurationFactory.create(fetcherConfigurationClass, ps.getConfiguration());
        Fetcher fetcher = fetcherClass.getConstructor(fetcherConfigurationClass).newInstance(fetcherConfigurationInstance);
        // Autowire fetcher
        applicationContext.getAutowireCapableBeanFactory().autowireBean(fetcher);
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(fetcher.fetch()))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        }
        return sb.toString();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new FetcherException(e.getMessage(), e);
    }
}
Also used : FetcherConfiguration(io.gravitee.fetcher.api.FetcherConfiguration) InputStreamReader(java.io.InputStreamReader) TemplateException(freemarker.template.TemplateException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) PageAlreadyExistsException(io.gravitee.management.service.exceptions.PageAlreadyExistsException) PageNotFoundException(io.gravitee.management.service.exceptions.PageNotFoundException) FetcherException(io.gravitee.fetcher.api.FetcherException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) FetcherException(io.gravitee.fetcher.api.FetcherException) BufferedReader(java.io.BufferedReader) Fetcher(io.gravitee.fetcher.api.Fetcher)

Example 3 with FetcherException

use of io.gravitee.fetcher.api.FetcherException in project gravitee-management-rest-api by gravitee-io.

the class PageServiceImpl method createPortalPage.

@Override
public PageEntity createPortalPage(NewPageEntity newPageEntity) {
    try {
        logger.debug("Create portal page {}", newPageEntity);
        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);
        // Set date fields
        page.setCreatedAt(new Date());
        page.setUpdatedAt(page.getCreatedAt());
        Page createdPage = pageRepository.create(page);
        // only one homepage is allowed
        onlyOneHomepage(page);
        createAuditLog(null, 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

FetcherException (io.gravitee.fetcher.api.FetcherException)3 PageAlreadyExistsException (io.gravitee.management.service.exceptions.PageAlreadyExistsException)3 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 TemplateException (freemarker.template.TemplateException)1 Fetcher (io.gravitee.fetcher.api.Fetcher)1 FetcherConfiguration (io.gravitee.fetcher.api.FetcherConfiguration)1 PageNotFoundException (io.gravitee.management.service.exceptions.PageNotFoundException)1 FetcherPlugin (io.gravitee.plugin.fetcher.FetcherPlugin)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 BeansException (org.springframework.beans.BeansException)1