Search in sources :

Example 1 with IndexerUnreachableException

use of org.nzbhydra.indexers.exceptions.IndexerUnreachableException 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);
    }
}
Also used : HashMap(java.util.HashMap) IndexerUnreachableException(org.nzbhydra.indexers.exceptions.IndexerUnreachableException) StreamSource(javax.xml.transform.stream.StreamSource) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) IndexerProgramErrorException(org.nzbhydra.indexers.exceptions.IndexerProgramErrorException) IndexerUnreachableException(org.nzbhydra.indexers.exceptions.IndexerUnreachableException) SocketTimeoutException(java.net.SocketTimeoutException) IndexerAccessException(org.nzbhydra.indexers.exceptions.IndexerAccessException) IndexerProgramErrorException(org.nzbhydra.indexers.exceptions.IndexerProgramErrorException) SocketTimeoutException(java.net.SocketTimeoutException) StringReader(java.io.StringReader) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

StringReader (java.io.StringReader)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 StreamSource (javax.xml.transform.stream.StreamSource)1 IndexerAccessException (org.nzbhydra.indexers.exceptions.IndexerAccessException)1 IndexerProgramErrorException (org.nzbhydra.indexers.exceptions.IndexerProgramErrorException)1 IndexerUnreachableException (org.nzbhydra.indexers.exceptions.IndexerUnreachableException)1