use of org.nzbhydra.indexers.exceptions.IndexerProgramErrorException in project nzbhydra2 by theotherp.
the class IndexerWebAccess method get.
public <T> T get(URI uri, IndexerConfig indexerConfig, Class responseType) throws IndexerAccessException {
int timeout = indexerConfig.getTimeout().orElse(configProvider.getBaseConfig().getSearching().getTimeout());
String userAgent = indexerConfig.getUserAgent().orElse(configProvider.getBaseConfig().getSearching().getUserAgent().orElse("NZBHydra2"));
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", userAgent);
if (indexerConfig.getUsername().isPresent() && indexerConfig.getPassword().isPresent()) {
headers.put("Authorization", "Basic " + BaseEncoding.base64().encode((indexerConfig.getUsername().get() + ":" + indexerConfig.getPassword().get()).getBytes()));
}
Future<T> future;
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
future = executorService.submit(() -> {
String response = webAccess.callUrl(uri.toString(), headers, timeout);
if (responseType == String.class) {
return (T) response;
}
return (T) unmarshaller.unmarshal(new StreamSource(new StringReader(response)));
});
} catch (RejectedExecutionException e) {
logger.error("Unexpected execution exception while executing call for indexer " + indexerConfig.getName() + ". This will hopefully be fixed soon", e);
throw new IndexerProgramErrorException("Unexpected error in hydra code. Sorry...");
} finally {
executorService.shutdown();
}
try {
// Give it one second more than the actual timeout
return future.get(timeout + 1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
if (e.getCause() instanceof SocketTimeoutException) {
throw new IndexerUnreachableException("Connection with indexer timed out with a time out of " + timeout + " seconds: " + e.getCause().getMessage());
}
throw new IndexerUnreachableException("Error while communicating with indexer " + indexerConfig.getName() + ". Server returned: " + e.getMessage(), e.getCause());
} catch (TimeoutException e) {
throw new IndexerAccessException("Indexer did not complete request within " + timeout + " seconds");
} catch (Exception e) {
throw new RuntimeException("Unexpected error while accessing indexer", e);
}
}
Aggregations