@Override public void markAsFailed(HttpUrl failedUrl) { if (useFailedUrlCache) { Optional<Integer> indexForFailedUrl = indexFor(failedUrl); indexForFailedUrl.ifPresent(index -> failedUrls.put(baseUrls.get(index), UrlAvailability.FAILED) ); } }
@Override public void markAsFailed(HttpUrl failedUrl) { if (useFailedUrlCache) { Optional<Integer> indexForFailedUrl = indexFor(failedUrl); indexForFailedUrl.ifPresent(index -> failedUrls.put(baseUrls.get(index), UrlAvailability.FAILED) ); } }
@Override public Optional<HttpUrl> redirectToNext(HttpUrl existingUrl) { // if possible, determine the index of the passed in url (so we can be sure to return a url which is different) Optional<Integer> existingUrlIndex = indexFor(existingUrl); int potentialNextIndex = existingUrlIndex.orElse(currentUrl.get()); Optional<HttpUrl> nextUrl = getNext(potentialNextIndex); if (nextUrl.isPresent()) { return redirectTo(existingUrl, nextUrl.get()); } // No healthy URLs remain; re-balance across any specified nodes return redirectTo(existingUrl, baseUrls.get((existingUrlIndex.orElse(currentUrl.get()) + 1) % baseUrls.size())); }
@Override public Optional<HttpUrl> redirectToNext(HttpUrl existingUrl) { // if possible, determine the index of the passed in url (so we can be sure to return a url which is different) Optional<Integer> existingUrlIndex = indexFor(existingUrl); int potentialNextIndex = existingUrlIndex.orElse(currentUrl.get()); Optional<HttpUrl> nextUrl = getNext(potentialNextIndex); if (nextUrl.isPresent()) { return redirectTo(existingUrl, nextUrl.get()); } // No healthy URLs remain; re-balance across any specified nodes return redirectTo(existingUrl, baseUrls.get((existingUrlIndex.orElse(currentUrl.get()) + 1) % baseUrls.size())); }
private Optional<HttpUrl> redirectTo(HttpUrl current, HttpUrl redirectBaseUrl) { Optional<Integer> baseUrlIndex = indexFor(redirectBaseUrl); baseUrlIndex.ifPresent(currentUrl::set); return baseUrlIndex .map(baseUrls::get) .flatMap(baseUrl -> { if (!isPathPrefixFor(baseUrl, current)) { // The requested redirectBaseUrl has a path that is not compatible with // the path of the current URL return Optional.empty(); } else { return Optional.of(current.newBuilder() .scheme(baseUrl.scheme()) .host(baseUrl.host()) .port(baseUrl.port()) .encodedPath( baseUrl.encodedPath() // matching prefix + current.encodedPath().substring(baseUrl.encodedPath().length())) .build()); } }); }
private Optional<HttpUrl> redirectTo(HttpUrl current, HttpUrl redirectBaseUrl) { Optional<Integer> baseUrlIndex = indexFor(redirectBaseUrl); baseUrlIndex.ifPresent(currentUrl::set); return baseUrlIndex .map(baseUrls::get) .flatMap(baseUrl -> { if (!isPathPrefixFor(baseUrl, current)) { // The requested redirectBaseUrl has a path that is not compatible with // the path of the current URL return Optional.empty(); } else { return Optional.of(current.newBuilder() .scheme(baseUrl.scheme()) .host(baseUrl.host()) .port(baseUrl.port()) .encodedPath( baseUrl.encodedPath() // matching prefix + current.encodedPath().substring(baseUrl.encodedPath().length())) .build()); } }); }