use of io.gravitee.fetcher.api.Fetcher 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);
}
}
Aggregations