Private method does not throw checked exception

- Remove it from the private method signatures
- Remove exception handling no longer needed
- Add missing @Override
- No need to nest in an else clause (reduce clutter)
- Use ternary expression
- public methods in a package-private class can be package-private
This commit is contained in:
Gary Gregory 2024-08-26 11:29:09 -04:00
parent a1eed8084d
commit 25f080f307
34 changed files with 316 additions and 388 deletions

View File

@ -89,9 +89,8 @@ public abstract class AbstractSerializingCacheStorage<T, CAS> implements HttpCac
final HttpCacheStorageEntry entry = serializer.deserialize(storageObject);
if (key.equals(entry.getKey())) {
return entry.getContent();
} else {
return null;
}
return null;
}
@Override

View File

@ -534,12 +534,11 @@ class AsyncCachingExec extends CachingExecBase implements AsyncExecChainHandler
exchangeId, asyncExecCallback, backendResponse, entityDetails);
cachingConsumerRef.set(cachingDataConsumer);
return cachingDataConsumer;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return asyncExecCallback.handleResponse(backendResponse, entityDetails);
}
@Override
@ -1079,10 +1078,9 @@ class AsyncCachingExec extends CachingExecBase implements AsyncExecChainHandler
LOG.debug("{} serving stale response due to {} status and stale-if-error enabled", exchangeId, status);
}
return null;
} else {
committed.set(response);
return asyncExecCallback.handleResponse(response, entityDetails);
}
committed.set(response);
return asyncExecCallback.handleResponse(response, entityDetails);
}
@Override

View File

@ -144,9 +144,8 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
}));
return;
} else {
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
}
callback.completed(new CacheMatch(null, new CacheHit(rootKey, root)));
} else {
callback.completed(new CacheMatch(new CacheHit(rootKey, root), null));
}
@ -350,39 +349,38 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
}
});
} else {
final String variantCacheKey = variantKey + rootKey;
}
final String variantCacheKey = variantKey + rootKey;
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {
return storeInternal(variantCacheKey, entry, new CallbackContribution<Boolean>(callback) {
@Override
public void completed(final Boolean result) {
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
updateInternal(rootKey,
existing -> {
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variantMap.add(variantKey);
return cacheEntryFactory.createRoot(entry, variantMap);
},
new CallbackContribution<Boolean>(callback) {
@Override
public void completed(final Boolean result) {
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
}
});
@Override
public void completed(final Boolean result) {
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
});
}
updateInternal(rootKey,
existing -> {
final Set<String> variantMap = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variantMap.add(variantKey);
return cacheEntryFactory.createRoot(entry, variantMap);
},
new CallbackContribution<Boolean>(callback) {
@Override
public void completed(final Boolean result) {
callback.completed(new CacheHit(rootKey, variantCacheKey, entry));
}
});
}
});
}
@Override

View File

@ -164,9 +164,8 @@ class BasicHttpCache implements HttpCache {
}
}
return new CacheMatch(null, new CacheHit(rootKey, root));
} else {
return new CacheMatch(new CacheHit(rootKey, root), null);
}
return new CacheMatch(new CacheHit(rootKey, root), null);
}
@Override
@ -197,26 +196,25 @@ class BasicHttpCache implements HttpCache {
}
storeInternal(rootKey, entry);
return new CacheHit(rootKey, entry);
} else {
final String variantCacheKey = variantKey + rootKey;
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
storeInternal(variantCacheKey, entry);
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
updateInternal(rootKey, existing -> {
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variants.add(variantKey);
return cacheEntryFactory.createRoot(entry, variants);
});
return new CacheHit(rootKey, variantCacheKey, entry);
}
final String variantCacheKey = variantKey + rootKey;
if (LOG.isDebugEnabled()) {
LOG.debug("Store variant entry in cache: {}", variantCacheKey);
}
storeInternal(variantCacheKey, entry);
if (LOG.isDebugEnabled()) {
LOG.debug("Update root entry: {}", rootKey);
}
updateInternal(rootKey, existing -> {
final Set<String> variants = existing != null ? new HashSet<>(existing.getVariants()) : new HashSet<>();
variants.add(variantKey);
return cacheEntryFactory.createRoot(entry, variants);
});
return new CacheHit(rootKey, variantCacheKey, entry);
}
@Override

View File

@ -92,9 +92,8 @@ class CacheControlHeaderGenerator {
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(buf, params.get(i), false);
}
return BufferedHeader.create(buf);
} else {
return null;
}
return null;
}
public void generate(final RequestCacheControl cacheControl, final HttpMessage message) {

View File

@ -237,29 +237,28 @@ public class CacheKeyGenerator implements Resolver<URI, String> {
.map(e -> {
if (e.getValue() == null && e.getParameterCount() == 0) {
return e.getName().toLowerCase(Locale.ROOT);
} else {
final CharArrayBuffer buf = new CharArrayBuffer(1024);
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
e.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
false);
if (e.getParameterCount() > 0) {
for (final NameValuePair nvp : e.getParameters()) {
if (!TextUtils.isBlank(nvp.getName())) {
buf.append(';');
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
nvp.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
false);
}
}
final CharArrayBuffer buf = new CharArrayBuffer(1024);
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
e.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
false);
if (e.getParameterCount() > 0) {
for (final NameValuePair nvp : e.getParameters()) {
if (!TextUtils.isBlank(nvp.getName())) {
buf.append(';');
BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
buf,
new BasicNameValuePair(
nvp.getName().toLowerCase(Locale.ROOT),
!TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
false);
}
}
return buf.toString();
}
return buf.toString();
})
.sorted()
.distinct()
@ -311,9 +310,8 @@ public class CacheKeyGenerator implements Resolver<URI, String> {
if (entry.containsHeader(HttpHeaders.VARY)) {
final List<String> variantNames = variantNames(entry);
return generateVariantKey(request, variantNames);
} else {
return null;
}
return null;
}
/**
@ -334,9 +332,8 @@ public class CacheKeyGenerator implements Resolver<URI, String> {
final List<String> variantNames = variantNames(entry);
if (variantNames.isEmpty()) {
return rootKey;
} else {
return generateVariantKey(request, variantNames) + rootKey;
}
return generateVariantKey(request, variantNames) + rootKey;
}
}

View File

@ -55,9 +55,8 @@ public final class CacheSupport {
}
if (locationUri.isAbsolute()) {
return locationUri;
} else {
return URIUtils.resolve(requestUri, locationUri);
}
return URIUtils.resolve(requestUri, locationUri);
}
public static boolean isSameOrigin(final URI requestURI, final URI targetURI) {

View File

@ -131,9 +131,8 @@ class CacheValidityPolicy {
LOG.debug("No explicit expiration time present in the response. Using heuristic freshness lifetime calculation.");
}
return getHeuristicFreshnessLifetime(entry);
} else {
return TimeValue.ZERO_MILLISECONDS;
}
return TimeValue.ZERO_MILLISECONDS;
}
TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry) {

View File

@ -176,26 +176,24 @@ class CachedResponseSuitabilityChecker {
if (stale >= requestCacheControl.getMaxStale()) {
LOG.debug("Response from cache is not suitable due to the request max-stale requirement");
return CacheSuitability.REVALIDATION_REQUIRED;
} else {
LOG.debug("The cache entry is fresh enough");
return CacheSuitability.FRESH_ENOUGH;
}
LOG.debug("The cache entry is fresh enough");
return CacheSuitability.FRESH_ENOUGH;
}
if (fresh) {
LOG.debug("The cache entry is fresh");
return CacheSuitability.FRESH;
} else {
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
LOG.debug("The cache entry is stale but suitable while being revalidated");
return CacheSuitability.STALE_WHILE_REVALIDATED;
}
}
LOG.debug("The cache entry is stale");
return CacheSuitability.STALE;
}
if (responseCacheControl.getStaleWhileRevalidate() > 0) {
final long stale = currentAge.compareTo(freshnessLifetime) > 0 ? currentAge.toSeconds() - freshnessLifetime.toSeconds() : 0;
if (stale < responseCacheControl.getStaleWhileRevalidate()) {
LOG.debug("The cache entry is stale but suitable while being revalidated");
return CacheSuitability.STALE_WHILE_REVALIDATED;
}
}
LOG.debug("The cache entry is stale");
return CacheSuitability.STALE;
}
boolean requestMethodMatch(final HttpRequest request, final HttpCacheEntry entry) {
@ -209,9 +207,8 @@ class CachedResponseSuitabilityChecker {
final URI cacheURI = new URI(entry.getRequestURI());
if (requestURI.isAbsolute()) {
return Objects.equals(requestURI, cacheURI);
} else {
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
}
return Objects.equals(requestURI.getPath(), cacheURI.getPath()) && Objects.equals(requestURI.getQuery(), cacheURI.getQuery());
} catch (final URISyntaxException ex) {
return false;
}

View File

@ -182,14 +182,13 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
if (hit == null) {
return handleCacheMiss(requestCacheControl, root, target, request, scope, chain);
} else {
final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
context.setResponseCacheControl(responseCacheControl);
if (LOG.isDebugEnabled()) {
LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
}
return handleCacheHit(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
}
final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
context.setResponseCacheControl(responseCacheControl);
if (LOG.isDebugEnabled()) {
LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
}
return handleCacheHit(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
}
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse) {
@ -279,73 +278,71 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
context.setCacheResponseStatus(CacheResponseStatus.FAILURE);
return chain.proceed(request, scope);
}
} else {
if (requestCacheControl.isOnlyIfCached()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry not is not fresh and only-if-cached requested", exchangeId);
}
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
return convert(generateGatewayTimeout());
} else if (cacheSuitability == CacheSuitability.MISMATCH) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry does not match the request; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (request.getEntity() != null && !request.getEntity().isRepeatable()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} request is not repeatable; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (hit.entry.getStatus() == HttpStatus.SC_NOT_MODIFIED && !suitabilityChecker.isConditional(request)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} non-modified cache entry does not match the non-conditional request; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (cacheSuitability == CacheSuitability.REVALIDATION_REQUIRED) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidation required; revalidating cache entry", exchangeId);
}
return revalidateCacheEntryWithoutFallback(responseCacheControl, hit, target, request, scope, chain);
} else if (cacheSuitability == CacheSuitability.STALE_WHILE_REVALIDATED) {
if (cacheRevalidator != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} serving stale with asynchronous revalidation", exchangeId);
}
final String revalidationExchangeId = ExecSupport.getNextExchangeId();
context.setExchangeId(revalidationExchangeId);
final ExecChain.Scope fork = new ExecChain.Scope(
revalidationExchangeId,
scope.route,
scope.originalRequest,
scope.execRuntime.fork(null),
HttpCacheContext.create());
if (LOG.isDebugEnabled()) {
LOG.debug("{} starting asynchronous revalidation exchange {}", exchangeId, revalidationExchangeId);
}
cacheRevalidator.revalidateCacheEntry(
hit.getEntryKey(),
() -> revalidateCacheEntry(responseCacheControl, hit, target, request, fork, chain));
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
final SimpleHttpResponse cacheResponse = responseGenerator.generateResponse(request, hit.entry);
context.setCacheEntry(hit.entry);
return convert(cacheResponse);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry (asynchronous revalidation disabled)", exchangeId);
}
return revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
}
} else if (cacheSuitability == CacheSuitability.STALE) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry", exchangeId);
}
return revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry not usable; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
}
if (requestCacheControl.isOnlyIfCached()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry not is not fresh and only-if-cached requested", exchangeId);
}
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
return convert(generateGatewayTimeout());
} else if (cacheSuitability == CacheSuitability.MISMATCH) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry does not match the request; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (request.getEntity() != null && !request.getEntity().isRepeatable()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} request is not repeatable; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (hit.entry.getStatus() == HttpStatus.SC_NOT_MODIFIED && !suitabilityChecker.isConditional(request)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} non-modified cache entry does not match the non-conditional request; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
} else if (cacheSuitability == CacheSuitability.REVALIDATION_REQUIRED) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidation required; revalidating cache entry", exchangeId);
}
return revalidateCacheEntryWithoutFallback(responseCacheControl, hit, target, request, scope, chain);
} else if (cacheSuitability == CacheSuitability.STALE_WHILE_REVALIDATED) {
if (cacheRevalidator != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} serving stale with asynchronous revalidation", exchangeId);
}
final String revalidationExchangeId = ExecSupport.getNextExchangeId();
context.setExchangeId(revalidationExchangeId);
final ExecChain.Scope fork = new ExecChain.Scope(
revalidationExchangeId,
scope.route,
scope.originalRequest,
scope.execRuntime.fork(null),
HttpCacheContext.create());
if (LOG.isDebugEnabled()) {
LOG.debug("{} starting asynchronous revalidation exchange {}", exchangeId, revalidationExchangeId);
}
cacheRevalidator.revalidateCacheEntry(
hit.getEntryKey(),
() -> revalidateCacheEntry(responseCacheControl, hit, target, request, fork, chain));
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
final SimpleHttpResponse cacheResponse = responseGenerator.generateResponse(request, hit.entry);
context.setCacheEntry(hit.entry);
return convert(cacheResponse);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry (asynchronous revalidation disabled)", exchangeId);
}
return revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
} else if (cacheSuitability == CacheSuitability.STALE) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry", exchangeId);
}
return revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry not usable; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
}
}
@ -437,9 +434,8 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
final SimpleHttpResponse cacheResponse = responseGenerator.generateResponse(request, hit.entry);
context.setCacheEntry(hit.entry);
return convert(cacheResponse);
} else {
return convert(generateGatewayTimeout());
}
return convert(generateGatewayTimeout());
}
final int status = response.getCode();
if (staleIfErrorAppliesTo(status) &&
@ -479,12 +475,11 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
LOG.debug("{} caching backend response", exchangeId);
}
return cacheAndReturnResponse(target, request, scope, backendResponse, requestDate, responseDate);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return backendResponse;
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} backend response is not cacheable", exchangeId);
}
return backendResponse;
}
ClassicHttpResponse cacheAndReturnResponse(
@ -623,10 +618,9 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
if (backendResponse.getCode() != HttpStatus.SC_NOT_MODIFIED) {
return handleBackendResponse(target, request, scope, requestDate, responseDate, backendResponse);
} else {
// 304 response are not expected to have an enclosed content body, but still
backendResponse.close();
}
// 304 response are not expected to have an enclosed content body, but still
backendResponse.close();
final ETag resultEtag = ETag.get(backendResponse);
if (resultEtag == null) {

View File

@ -114,9 +114,8 @@ public class CachingExecBase {
final Instant now) throws ResourceIOException {
if (shouldSendNotModifiedResponse(request, entry, now)) {
return responseGenerator.generateNotModifiedResponse(entry);
} else {
return responseGenerator.generateResponse(request, entry);
}
return responseGenerator.generateResponse(request, entry);
}
SimpleHttpResponse generateGatewayTimeout() {

View File

@ -101,18 +101,16 @@ public class FileResourceFactory implements ResourceFactory {
public Resource generate(final String requestId, final byte[] content, final int off, final int len) throws ResourceIOException {
if (content != null) {
return generate(requestId, null, content, off, len);
} else {
return generate(requestId, null, null, 0, 0);
}
return generate(requestId, null, null, 0, 0);
}
@Override
public Resource generate(final String requestId, final byte[] content) throws ResourceIOException {
if (content != null) {
return generate(requestId, null, content, 0, content.length);
} else {
return generate(requestId, null, null, 0, 0);
}
return generate(requestId, null, null, 0, 0);
}
/**

View File

@ -62,11 +62,7 @@ public class HeapResource extends Resource {
@Override
public long length() {
final byte[] byteArray = this.arrayRef.get();
if (byteArray != null) {
return byteArray.length;
} else {
return -1;
}
return byteArray != null ? byteArray.length : -1;
}
@Override

View File

@ -54,9 +54,8 @@ final public class InternalCacheStorage {
evictionQueue.add(new HttpCacheStorageEntry(eldest.getKey(), eldest.getValue()));
}
return true;
} else {
return false;
}
return false;
}
};

View File

@ -231,12 +231,11 @@ class ResponseCachingPolicy {
protected boolean isExplicitlyNonCacheable(final ResponseCacheControl cacheControl) {
if (cacheControl == null) {
return false;
} else {
// The response is considered explicitly non-cacheable if it contains
// "no-store" or (if sharedCache is true) "private" directives.
// Note that "no-cache" is considered cacheable but requires validation before use.
return cacheControl.isNoStore() || (sharedCache && cacheControl.isCachePrivate());
}
// The response is considered explicitly non-cacheable if it contains
// "no-store" or (if sharedCache is true) "private" directives.
// Note that "no-cache" is considered cacheable but requires validation before use.
return cacheControl.isNoStore() || (sharedCache && cacheControl.isCachePrivate());
}
protected boolean isExplicitlyCacheable(final ResponseCacheControl cacheControl, final HttpResponse response) {

View File

@ -160,9 +160,8 @@ public class AuthenticatingAsyncDecorator implements AsyncServerExchangeHandler
final AsyncResponseProducer responseProducer = responseProducerRef.get();
if (responseProducer == null) {
return exchangeHandler.available();
} else {
return responseProducer.available();
}
return responseProducer.available();
}
@Override

View File

@ -129,11 +129,7 @@ public class RedirectingAsyncDecorator implements AsyncServerExchangeHandler {
@Override
public int available() {
if (!redirecting.get()) {
return exchangeHandler.available();
} else {
return 0;
}
return redirecting.get() ? 0 : exchangeHandler.available();
}
@Override

View File

@ -109,11 +109,7 @@ public class ServiceUnavailableAsyncDecorator implements AsyncServerExchangeHand
@Override
public int available() {
if (!serviceUnavailable.get()) {
return exchangeHandler.available();
} else {
return 0;
}
return serviceUnavailable.get() ? 0 : exchangeHandler.available();
}
@Override

View File

@ -69,9 +69,8 @@ abstract class AbstractAuthenticationHandler implements AuthenticationHandler<St
} catch (final IllegalArgumentException ex) {
throw new ProtocolException("Malformed " + getSchemeName() + " credentials");
}
} else {
throw new ProtocolException("Unexpected challenge type");
}
throw new ProtocolException("Unexpected challenge type");
}
}

View File

@ -57,14 +57,12 @@ public class BasicTestAuthenticator implements Authenticator {
final boolean result = authenticate(authority, requestUri, credentials);
if (result) {
return new AuthResult(true);
} else {
if (TextUtils.isBlank(credentials)) {
return new AuthResult(false);
} else {
final String error = credentials.endsWith("-expired") ? "token expired" : "invalid token";
return new AuthResult(false, new BasicNameValuePair("error", error));
}
}
if (TextUtils.isBlank(credentials)) {
return new AuthResult(false);
}
final String error = credentials.endsWith("-expired") ? "token expired" : "invalid token";
return new AuthResult(false, new BasicNameValuePair("error", error));
}
@Override

View File

@ -93,9 +93,8 @@ public class TestAsyncServer {
public InetSocketAddress start() throws Exception {
if (http1Config == null) {
return server.start(httpProcessor, exchangeHandlerDecorator, h2Config);
} else {
return server.start(httpProcessor, exchangeHandlerDecorator, http1Config);
}
return server.start(httpProcessor, exchangeHandlerDecorator, http1Config);
}
}

View File

@ -685,10 +685,9 @@ abstract class TestRedirects extends AbstractIntegrationTestBase {
final HttpContext context) throws HttpException, IOException {
if (count.incrementAndGet() == 1) {
throw new IOException("Boom");
} else {
response.setCode(200);
response.setEntity(new StringEntity("test"));
}
response.setCode(200);
response.setEntity(new StringEntity("test"));
}
}));

View File

@ -178,11 +178,10 @@ public class DefaultHttpRequestRetryStrategy implements HttpRequestRetryStrategy
}
if (this.nonRetriableIOExceptionClasses.contains(exception.getClass())) {
return false;
} else {
for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) {
if (rejectException.isInstance(exception)) {
return false;
}
}
for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) {
if (rejectException.isInstance(exception)) {
return false;
}
}
if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) {

View File

@ -57,15 +57,14 @@ public final class RequestSupport {
}
if (pathSegments.isEmpty()) {
return "/";
} else {
final StringBuilder buf = new StringBuilder();
buf.append('/');
for (final String pathSegment : pathSegments) {
PercentCodec.encode(buf, pathSegment, StandardCharsets.US_ASCII);
buf.append('/');
}
return buf.toString();
}
final StringBuilder buf = new StringBuilder();
buf.append('/');
for (final String pathSegment : pathSegments) {
PercentCodec.encode(buf, pathSegment, StandardCharsets.US_ASCII);
buf.append('/');
}
return buf.toString();
} catch (final URISyntaxException ex) {
return path;
}

View File

@ -253,68 +253,61 @@ public final class AsyncConnectExec implements AsyncExecChainHandler {
}));
break;
case HttpRouteDirector.TUNNEL_TARGET:
try {
final HttpHost proxy = route.getProxyHost();
final HttpHost target = route.getTargetHost();
if (LOG.isDebugEnabled()) {
LOG.debug("{} create tunnel", exchangeId);
}
createTunnel(state, proxy, target, scope, new AsyncExecCallback() {
@Override
public AsyncDataConsumer handleResponse(
final HttpResponse response,
final EntityDetails entityDetails) throws HttpException, IOException {
return asyncExecCallback.handleResponse(response, entityDetails);
}
@Override
public void handleInformationResponse(
final HttpResponse response) throws HttpException, IOException {
asyncExecCallback.handleInformationResponse(response);
}
@Override
public void completed() {
if (!execRuntime.isEndpointConnected()) {
// Remote endpoint disconnected. Need to start over
if (LOG.isDebugEnabled()) {
LOG.debug("{} proxy disconnected", exchangeId);
}
state.tracker.reset();
}
if (state.challenged) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} proxy authentication required", exchangeId);
}
proceedToNextHop(state, request, entityProducer, scope, chain, asyncExecCallback);
} else {
if (state.tunnelRefused) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} tunnel refused", exchangeId);
}
asyncExecCallback.completed();
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} tunnel to target created", exchangeId);
}
tracker.tunnelTarget(false);
proceedToNextHop(state, request, entityProducer, scope, chain, asyncExecCallback);
}
}
}
@Override
public void failed(final Exception cause) {
execRuntime.markConnectionNonReusable();
asyncExecCallback.failed(cause);
}
});
} catch (final HttpException | IOException ex) {
asyncExecCallback.failed(ex);
case HttpRouteDirector.TUNNEL_TARGET:
final HttpHost proxy = route.getProxyHost();
final HttpHost target = route.getTargetHost();
if (LOG.isDebugEnabled()) {
LOG.debug("{} create tunnel", exchangeId);
}
createTunnel(state, proxy, target, scope, new AsyncExecCallback() {
@Override
public AsyncDataConsumer handleResponse(final HttpResponse response, final EntityDetails entityDetails) throws HttpException, IOException {
return asyncExecCallback.handleResponse(response, entityDetails);
}
@Override
public void handleInformationResponse(final HttpResponse response) throws HttpException, IOException {
asyncExecCallback.handleInformationResponse(response);
}
@Override
public void completed() {
if (!execRuntime.isEndpointConnected()) {
// Remote endpoint disconnected. Need to start over
if (LOG.isDebugEnabled()) {
LOG.debug("{} proxy disconnected", exchangeId);
}
state.tracker.reset();
}
if (state.challenged) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} proxy authentication required", exchangeId);
}
proceedToNextHop(state, request, entityProducer, scope, chain, asyncExecCallback);
} else {
if (state.tunnelRefused) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} tunnel refused", exchangeId);
}
asyncExecCallback.completed();
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} tunnel to target created", exchangeId);
}
tracker.tunnelTarget(false);
proceedToNextHop(state, request, entityProducer, scope, chain, asyncExecCallback);
}
}
}
@Override
public void failed(final Exception cause) {
execRuntime.markConnectionNonReusable();
asyncExecCallback.failed(cause);
}
});
break;
case HttpRouteDirector.TUNNEL_PROXY:
@ -372,7 +365,7 @@ public final class AsyncConnectExec implements AsyncExecChainHandler {
final HttpHost proxy,
final HttpHost nextHop,
final AsyncExecChain.Scope scope,
final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
final AsyncExecCallback asyncExecCallback) {
final CancellableDependency operation = scope.cancellableDependency;
final HttpClientContext clientContext = scope.clientContext;

View File

@ -234,6 +234,7 @@ class InternalH2AsyncExecRuntime implements AsyncExecRuntime {
throw new UnsupportedOperationException();
}
@Override
public EndpointInfo getEndpointInfo() {
final Endpoint endpoint = sessionRef.get();
if (endpoint != null && endpoint.session.isOpen()) {

View File

@ -267,6 +267,7 @@ class InternalHttpAsyncExecRuntime implements AsyncExecRuntime {
});
}
@Override
public EndpointInfo getEndpointInfo() {
final AsyncConnectionEndpoint endpoint = endpointRef.get();
return endpoint != null ? endpoint.getInfo() : null;

View File

@ -262,7 +262,7 @@ class LoggingIOSession implements IOSession {
}
private void logData(final ByteBuffer data, final String prefix) throws IOException {
private void logData(final ByteBuffer data, final String prefix) {
final byte[] line = new byte[16];
final StringBuilder buf = new StringBuilder();
while (data.hasRemaining()) {

View File

@ -352,7 +352,7 @@ final class NTLMEngineImpl implements NTLMEngine {
byte[] ntlm2SessionResponseUserSessionKey;
byte[] lanManagerSessionKey;
public CipherGen(final Random random, final long currentTime,
CipherGen(final Random random, final long currentTime,
final String domain, final String user, final char[] password,
final byte[] challenge, final String target, final byte[] targetInformation,
final byte[] clientChallenge, final byte[] clientChallenge2,
@ -372,7 +372,7 @@ final class NTLMEngineImpl implements NTLMEngine {
this.timestamp = timestamp;
}
public CipherGen(final Random random, final long currentTime,
CipherGen(final Random random, final long currentTime,
final String domain,
final String user,
final char[] password,
@ -383,7 +383,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return client challenge */
public byte[] getClientChallenge() {
byte[] getClientChallenge() {
if (clientChallenge == null) {
clientChallenge = makeRandomChallenge(random);
}
@ -391,7 +391,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return second client challenge */
public byte[] getClientChallenge2() {
byte[] getClientChallenge2() {
if (clientChallenge2 == null) {
clientChallenge2 = makeRandomChallenge(random);
}
@ -399,7 +399,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return random secondary key */
public byte[] getSecondaryKey() {
byte[] getSecondaryKey() {
if (secondaryKey == null) {
secondaryKey = makeSecondaryKey(random);
}
@ -407,7 +407,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return the LMHash */
public byte[] getLMHash()
byte[] getLMHash()
throws NTLMEngineException {
if (lmHash == null) {
lmHash = lmHash(password);
@ -416,17 +416,16 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return the LMResponse */
public byte[] getLMResponse()
throws NTLMEngineException {
byte[] getLMResponse()
throws NTLMEngineException {
if (lmResponse == null) {
lmResponse = lmResponse(getLMHash(),challenge);
lmResponse = lmResponse(getLMHash(), challenge);
}
return lmResponse;
}
/** Calculate and return the NTLMHash */
public byte[] getNTLMHash()
throws NTLMEngineException {
byte[] getNTLMHash() {
if (ntlmHash == null) {
ntlmHash = ntlmHash(password);
}
@ -434,17 +433,16 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate and return the NTLMResponse */
public byte[] getNTLMResponse()
throws NTLMEngineException {
byte[] getNTLMResponse()
throws NTLMEngineException {
if (ntlmResponse == null) {
ntlmResponse = lmResponse(getNTLMHash(),challenge);
ntlmResponse = lmResponse(getNTLMHash(), challenge);
}
return ntlmResponse;
}
/** Calculate the LMv2 hash */
public byte[] getLMv2Hash()
throws NTLMEngineException {
byte[] getLMv2Hash() {
if (lmv2Hash == null) {
lmv2Hash = lmv2Hash(domain, user, getNTLMHash());
}
@ -452,8 +450,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate the NTLMv2 hash */
public byte[] getNTLMv2Hash()
throws NTLMEngineException {
byte[] getNTLMv2Hash() {
if (ntlmv2Hash == null) {
ntlmv2Hash = ntlmv2Hash(domain, user, getNTLMHash());
}
@ -461,7 +458,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate a timestamp */
public byte[] getTimestamp() {
byte[] getTimestamp() {
if (timestamp == null) {
long time = this.currentTime;
time += 11644473600000L; // milliseconds from January 1, 1601 -> epoch.
@ -477,7 +474,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate the NTLMv2Blob */
public byte[] getNTLMv2Blob() {
byte[] getNTLMv2Blob() {
if (ntlmv2Blob == null) {
ntlmv2Blob = createBlob(getClientChallenge2(), targetInformation, getTimestamp());
}
@ -485,34 +482,32 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Calculate the NTLMv2Response */
public byte[] getNTLMv2Response()
throws NTLMEngineException {
byte[] getNTLMv2Response() {
if (ntlmv2Response == null) {
ntlmv2Response = lmv2Response(getNTLMv2Hash(),challenge,getNTLMv2Blob());
ntlmv2Response = lmv2Response(getNTLMv2Hash(), challenge, getNTLMv2Blob());
}
return ntlmv2Response;
}
/** Calculate the LMv2Response */
public byte[] getLMv2Response()
throws NTLMEngineException {
byte[] getLMv2Response() {
if (lmv2Response == null) {
lmv2Response = lmv2Response(getLMv2Hash(),challenge,getClientChallenge());
lmv2Response = lmv2Response(getLMv2Hash(), challenge, getClientChallenge());
}
return lmv2Response;
}
/** Get NTLM2SessionResponse */
public byte[] getNTLM2SessionResponse()
throws NTLMEngineException {
byte[] getNTLM2SessionResponse()
throws NTLMEngineException {
if (ntlm2SessionResponse == null) {
ntlm2SessionResponse = ntlm2SessionResponse(getNTLMHash(),challenge,getClientChallenge());
ntlm2SessionResponse = ntlm2SessionResponse(getNTLMHash(), challenge, getClientChallenge());
}
return ntlm2SessionResponse;
}
/** Calculate and return LM2 session response */
public byte[] getLM2SessionResponse() {
byte[] getLM2SessionResponse() {
if (lm2SessionResponse == null) {
final byte[] clntChallenge = getClientChallenge();
lm2SessionResponse = new byte[24];
@ -523,7 +518,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Get LMUserSessionKey */
public byte[] getLMUserSessionKey()
byte[] getLMUserSessionKey()
throws NTLMEngineException {
if (lmUserSessionKey == null) {
lmUserSessionKey = new byte[16];
@ -534,8 +529,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Get NTLMUserSessionKey */
public byte[] getNTLMUserSessionKey()
throws NTLMEngineException {
byte[] getNTLMUserSessionKey() {
if (ntlmUserSessionKey == null) {
final MD4 md4 = new MD4();
md4.update(getNTLMHash());
@ -545,8 +539,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** GetNTLMv2UserSessionKey */
public byte[] getNTLMv2UserSessionKey()
throws NTLMEngineException {
byte[] getNTLMv2UserSessionKey() {
if (ntlmv2UserSessionKey == null) {
final byte[] ntlmv2hash = getNTLMv2Hash();
final byte[] truncatedResponse = new byte[16];
@ -557,8 +550,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Get NTLM2SessionResponseUserSessionKey */
public byte[] getNTLM2SessionResponseUserSessionKey()
throws NTLMEngineException {
byte[] getNTLM2SessionResponseUserSessionKey() {
if (ntlm2SessionResponseUserSessionKey == null) {
final byte[] ntlm2SessionResponseNonce = getLM2SessionResponse();
final byte[] sessionNonce = new byte[challenge.length + ntlm2SessionResponseNonce.length];
@ -570,7 +562,7 @@ final class NTLMEngineImpl implements NTLMEngine {
}
/** Get LAN Manager session key */
public byte[] getLanManagerSessionKey()
byte[] getLanManagerSessionKey()
throws NTLMEngineException {
if (lanManagerSessionKey == null) {
try {
@ -689,7 +681,7 @@ final class NTLMEngineImpl implements NTLMEngine {
* @return The NTLM Hash of the given password, used in the calculation of
* the NTLM Response and the NTLMv2 and LMv2 Hashes.
*/
private static byte[] ntlmHash(final char[] password) throws NTLMEngineException {
private static byte[] ntlmHash(final char[] password) {
final byte[] unicodePassword = new ByteArrayBuilder()
.charset(UNICODE_LITTLE_UNMARKED).append(password).toByteArray();
final MD4 md4 = new MD4();
@ -703,8 +695,7 @@ final class NTLMEngineImpl implements NTLMEngine {
* @return The LMv2 Hash, used in the calculation of the NTLMv2 and LMv2
* Responses.
*/
private static byte[] lmv2Hash(final String domain, final String user, final byte[] ntlmHash)
throws NTLMEngineException {
private static byte[] lmv2Hash(final String domain, final String user, final byte[] ntlmHash) {
final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash);
// Upper case username, upper case domain!
hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED));
@ -720,8 +711,7 @@ final class NTLMEngineImpl implements NTLMEngine {
* @return The NTLMv2 Hash, used in the calculation of the NTLMv2 and LMv2
* Responses.
*/
private static byte[] ntlmv2Hash(final String domain, final String user, final byte[] ntlmHash)
throws NTLMEngineException {
private static byte[] ntlmv2Hash(final String domain, final String user, final byte[] ntlmHash) {
final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash);
// Upper case username, mixed case target!!
hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED));
@ -835,13 +825,13 @@ final class NTLMEngineImpl implements NTLMEngine {
rc4 = initCipher();
}
public byte[] getSigningKey()
byte[] getSigningKey()
{
return signingKey;
}
public byte[] getSealingKey()
byte[] getSealingKey()
{
return sealingKey;
}
@ -929,7 +919,7 @@ final class NTLMEngineImpl implements NTLMEngine {
return MessageDigest.isEqual( signature, computedSignature );
}
public byte[] signAndEncryptMessage( final byte[] cleartextMessage ) throws NTLMEngineException
byte[] signAndEncryptMessage( final byte[] cleartextMessage ) throws NTLMEngineException
{
final byte[] encryptedMessage = encrypt( cleartextMessage );
final byte[] signature = computeSignature( cleartextMessage );
@ -940,7 +930,7 @@ final class NTLMEngineImpl implements NTLMEngine {
return outMessage;
}
public byte[] decryptAndVerifySignedMessage( final byte[] inMessage ) throws NTLMEngineException
byte[] decryptAndVerifySignedMessage( final byte[] inMessage ) throws NTLMEngineException
{
final byte[] signature = new byte[16];
System.arraycopy( inMessage, 0, signature, 0, signature.length );
@ -1060,7 +1050,7 @@ final class NTLMEngineImpl implements NTLMEngine {
* @param flags is the flags.
* @return the character set.
*/
private static Charset getCharset(final int flags) throws NTLMEngineException
private static Charset getCharset(final int flags)
{
if ((flags & FLAG_REQUEST_UNICODE_ENCODING) == 0) {
return DEFAULT_CHARSET;
@ -1217,11 +1207,11 @@ final class NTLMEngineImpl implements NTLMEngine {
*
* @return The response as above.
*/
public String getResponse() {
String getResponse() {
return new String(Base64.encodeBase64(getBytes()), StandardCharsets.US_ASCII);
}
public byte[] getBytes() {
byte[] getBytes() {
if (messageContents == null) {
buildMessage();
}
@ -1616,11 +1606,11 @@ final class NTLMEngineImpl implements NTLMEngine {
userBytes = user.getBytes(charset);
}
public byte[] getEncryptedRandomSessionKey() {
byte[] getEncryptedRandomSessionKey() {
return sessionKey;
}
public byte[] getExportedSessionKey() {
byte[] getExportedSessionKey() {
return exportedSessionKey;
}

View File

@ -66,14 +66,13 @@ public class AsyncClientConnectionConfig {
.setValidateAfterInactivity(TimeValue.ofMinutes(1))
.setTimeToLive(TimeValue.ofHours(1))
.build();
} else {
return ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMinutes(1))
.setSocketTimeout(Timeout.ofMinutes(1))
.setValidateAfterInactivity(TimeValue.ofSeconds(15))
.setTimeToLive(TimeValue.ofMinutes(15))
.build();
}
return ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMinutes(1))
.setSocketTimeout(Timeout.ofMinutes(1))
.setValidateAfterInactivity(TimeValue.ofSeconds(15))
.setTimeToLive(TimeValue.ofMinutes(15))
.build();
})
.setTlsConfigResolver(host -> {
// Use different settings for specific hosts
@ -82,9 +81,8 @@ public class AsyncClientConnectionConfig {
.setSupportedProtocols(TLS.V_1_3)
.setHandshakeTimeout(Timeout.ofSeconds(10))
.build();
} else {
return TlsConfig.DEFAULT;
}
return TlsConfig.DEFAULT;
})
.build();
try (final CloseableHttpAsyncClient client = HttpAsyncClients.custom()

View File

@ -160,9 +160,8 @@ public class ClientConfiguration {
public InetAddress[] resolve(final String host) throws UnknownHostException {
if (host.equalsIgnoreCase("myhost")) {
return new InetAddress[] { InetAddress.getByAddress(new byte[] {127, 0, 0, 1}) };
} else {
return super.resolve(host);
}
return super.resolve(host);
}
};

View File

@ -60,14 +60,13 @@ public class ClientConnectionConfig {
.setValidateAfterInactivity(TimeValue.ofMinutes(1))
.setTimeToLive(TimeValue.ofHours(1))
.build();
} else {
return ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMinutes(1))
.setSocketTimeout(Timeout.ofMinutes(1))
.setValidateAfterInactivity(TimeValue.ofSeconds(15))
.setTimeToLive(TimeValue.ofMinutes(15))
.build();
}
return ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMinutes(1))
.setSocketTimeout(Timeout.ofMinutes(1))
.setValidateAfterInactivity(TimeValue.ofSeconds(15))
.setTimeToLive(TimeValue.ofMinutes(15))
.build();
})
.setTlsConfigResolver(host -> {
// Use different settings for specific hosts
@ -76,9 +75,8 @@ public class ClientConnectionConfig {
.setSupportedProtocols(TLS.V_1_3)
.setHandshakeTimeout(Timeout.ofSeconds(10))
.build();
} else {
return TlsConfig.DEFAULT;
}
return TlsConfig.DEFAULT;
})
.build();
try (CloseableHttpClient httpclient = HttpClients.custom()

View File

@ -79,9 +79,8 @@ public class ClientInterceptors {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
response.setEntity(new StringEntity("bad luck", ContentType.TEXT_PLAIN));
return response;
} else {
return chain.proceed(request, scope);
}
return chain.proceed(request, scope);
})
.build()) {

View File

@ -88,11 +88,7 @@ public final class MockConnPoolControl implements ConnPoolControl<HttpRoute> {
@Override
public int getMaxPerRoute(final HttpRoute route) {
final Integer max = this.maxPerHostMap.get(route);
if (max != null) {
return max;
} else {
return this.defaultMax;
}
return max != null ? max : this.defaultMax;
}
@Override