From d9a78ce076fc0fb379940b567dabf54d85f9cf5b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 12 Sep 2024 14:46:19 +0200 Subject: [PATCH] Javadoc and reduce boilerplate - Use final - package-private methods can throw more precise exception - Add missing Javadoc comments - Use lambdas - Better StringBuilder allocation - Make private final class a static class - Make private instance variable final in test - Single lambda parameter does not need parentheses - Package-private class constructor can be package-private --- .../HttpByteArrayCacheEntrySerializer.java | 4 +- .../http/impl/cache/ViaCacheGenerator.java | 2 +- .../client5/testing/async/TestHttp1Async.java | 16 +- .../hc/client5/testing/fluent/TestFluent.java | 2 +- .../sync/AbstractIntegrationTestBase.java | 2 +- .../testing/sync/TestContentCodings.java | 168 +++++++----------- .../TestFutureRequestExecutionService.java | 4 +- .../client5/testing/sync/TestRedirects.java | 31 ++-- .../methods/SimpleAsyncEntityConsumer.java | 2 +- .../async/methods/SimpleResponseBuilder.java | 39 +++- .../async/methods/SimpleResponseConsumer.java | 5 + .../client5/http/classic/BackoffManager.java | 4 + .../hc/client5/http/classic/ExecRuntime.java | 16 +- .../classic/methods/ClassicHttpRequests.java | 116 +++++++++++- .../http/classic/methods/HttpDelete.java | 12 +- .../client5/http/classic/methods/HttpGet.java | 7 +- .../http/classic/methods/HttpHead.java | 7 +- .../http/classic/methods/HttpOptions.java | 13 +- .../http/classic/methods/HttpPatch.java | 7 +- .../http/classic/methods/HttpPost.java | 7 +- .../client5/http/classic/methods/HttpPut.java | 8 +- .../http/classic/methods/HttpTrace.java | 7 +- .../classic/methods/HttpUriRequestBase.java | 14 ++ .../hc/client5/http/config/Configurable.java | 4 +- .../client5/http/entity/mime/MimeField.java | 39 +++- .../DefaultHttpClientConnectionOperator.java | 9 +- .../http/ssl/DefaultHostnameVerifier.java | 23 ++- 27 files changed, 380 insertions(+), 188 deletions(-) diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java index 7c0dc7d5c4..be0785e602 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java @@ -181,7 +181,7 @@ public byte[] serialize(final HttpCacheStorageEntry storageEntry) throws Resourc final RequestLine requestLine = new RequestLine(cacheEntry.getRequestMethod(), cacheEntry.getRequestURI(), HttpVersion.HTTP_1_1); lineFormatter.formatRequestLine(line, requestLine); outputBuffer.writeLine(line, out); - for (Iterator
it = cacheEntry.requestHeaderIterator(); it.hasNext(); ) { + for (final Iterator
it = cacheEntry.requestHeaderIterator(); it.hasNext(); ) { line.clear(); lineFormatter.formatHeader(line, it.next()); outputBuffer.writeLine(line, out); @@ -193,7 +193,7 @@ public byte[] serialize(final HttpCacheStorageEntry storageEntry) throws Resourc final StatusLine statusLine = new StatusLine(HttpVersion.HTTP_1_1, cacheEntry.getStatus(), ""); lineFormatter.formatStatusLine(line, statusLine); outputBuffer.writeLine(line, out); - for (Iterator
it = cacheEntry.headerIterator(); it.hasNext(); ) { + for (final Iterator
it = cacheEntry.headerIterator(); it.hasNext(); ) { line.clear(); lineFormatter.formatHeader(line, it.next()); outputBuffer.writeLine(line, out); diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ViaCacheGenerator.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ViaCacheGenerator.java index 22260b86d2..3f333b1564 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ViaCacheGenerator.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ViaCacheGenerator.java @@ -53,7 +53,7 @@ String generateViaHeader(final VersionInfo vi, final ProtocolVersion pv) { } String lookup(final ProtocolVersion pv) { - return internalCache.computeIfAbsent(pv, (v) -> { + return internalCache.computeIfAbsent(pv, v -> { final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.hc.client5", getClass().getClassLoader()); return generateViaHeader(vi, v); }); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java index 15f93b597a..ac914deb84 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java @@ -157,13 +157,7 @@ void testRequestCancellation() throws Exception { .setPath("/random/1000") .build(), null); - executorService.schedule(new Runnable() { - - @Override - public void run() { - future.cancel(true); - } - }, i % 5, TimeUnit.MILLISECONDS); + executorService.schedule(() -> future.cancel(true), i % 5, TimeUnit.MILLISECONDS); try { future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); @@ -181,13 +175,7 @@ public void run() { .setPath("/random/1000") .build(), null); - executorService.schedule(new Runnable() { - - @Override - public void run() { - future.cancel(true); - } - }, rnd.nextInt(200), TimeUnit.MILLISECONDS); + executorService.schedule(() -> future.cancel(true), rnd.nextInt(200), TimeUnit.MILLISECONDS); try { future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java index 047178743e..2c0f366f27 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java @@ -56,7 +56,7 @@ class TestFluent { public static final Timeout TIMEOUT = Timeout.ofMinutes(1); @RegisterExtension - private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, ClientProtocolLevel.STANDARD, TIMEOUT); + private final TestClientResources testResources = new TestClientResources(URIScheme.HTTP, ClientProtocolLevel.STANDARD, TIMEOUT); @BeforeEach void setUp() { diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/AbstractIntegrationTestBase.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/AbstractIntegrationTestBase.java index bf31dbf4c9..daa5836ffe 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/AbstractIntegrationTestBase.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/AbstractIntegrationTestBase.java @@ -46,7 +46,7 @@ abstract class AbstractIntegrationTestBase { public static final Timeout TIMEOUT = Timeout.ofMinutes(1); @RegisterExtension - private TestClientResources testResources; + private final TestClientResources testResources; protected AbstractIntegrationTestBase(final URIScheme scheme, final ClientProtocolLevel clientProtocolLevel) { this.testResources = new TestClientResources(scheme, clientProtocolLevel, TIMEOUT); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestContentCodings.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestContentCodings.java index 5103c97565..0ec2f70c83 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestContentCodings.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestContentCodings.java @@ -29,7 +29,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -47,8 +46,6 @@ import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel; import org.apache.hc.client5.testing.extension.sync.TestClient; -import org.apache.hc.core5.http.ClassicHttpRequest; -import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.HeaderElement; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpStatus; @@ -58,7 +55,6 @@ import org.apache.hc.core5.http.io.entity.InputStreamEntity; import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.MessageSupport; -import org.apache.hc.core5.http.protocol.HttpContext; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -83,19 +79,7 @@ protected TestContentCodings(final URIScheme scheme) { @Test void testResponseWithNoContent() throws Exception { configureServer(bootstrap -> bootstrap - .register("*", new HttpRequestHandler() { - - /** - * {@inheritDoc} - */ - @Override - public void handle( - final ClassicHttpRequest request, - final ClassicHttpResponse response, - final HttpContext context) { - response.setCode(HttpStatus.SC_NO_CONTENT); - } - })); + .register("*", (request, response, context) -> response.setCode(HttpStatus.SC_NO_CONTENT))); final HttpHost target = startServer(); @@ -326,43 +310,33 @@ void deflateResponsesWorkWithBasicResponseHandler() throws Exception { */ private HttpRequestHandler createDeflateEncodingRequestHandler( final String entityText, final boolean rfc1951) { - return new HttpRequestHandler() { - - /** - * {@inheritDoc} - */ - @Override - public void handle( - final ClassicHttpRequest request, - final ClassicHttpResponse response, - final HttpContext context) { - response.setEntity(new StringEntity(entityText)); - response.addHeader("Content-Type", "text/plain"); - final Iterator it = MessageSupport.iterate(request, "Accept-Encoding"); - while (it.hasNext()) { - final HeaderElement element = it.next(); - if ("deflate".equalsIgnoreCase(element.getName())) { - response.addHeader("Content-Encoding", "deflate"); - - /* Gack. DeflaterInputStream is Java 6. */ - // response.setEntity(new InputStreamEntity(new DeflaterInputStream(new - // ByteArrayInputStream( - // entityText.getBytes("utf-8"))), -1)); - final byte[] uncompressed = entityText.getBytes(StandardCharsets.UTF_8); - final Deflater compressor = new Deflater(Deflater.DEFAULT_COMPRESSION, rfc1951); - compressor.setInput(uncompressed); - compressor.finish(); - final byte[] output = new byte[100]; - final int compressedLength = compressor.deflate(output); - final byte[] compressed = new byte[compressedLength]; - System.arraycopy(output, 0, compressed, 0, compressedLength); - response.setEntity(new InputStreamEntity( - new ByteArrayInputStream(compressed), compressedLength, null)); - return; - } - } + return (request, response, context) -> { + response.setEntity(new StringEntity(entityText)); + response.addHeader("Content-Type", "text/plain"); + final Iterator it = MessageSupport.iterate(request, "Accept-Encoding"); + while (it.hasNext()) { + final HeaderElement element = it.next(); + if ("deflate".equalsIgnoreCase(element.getName())) { + response.addHeader("Content-Encoding", "deflate"); + + /* Gack. DeflaterInputStream is Java 6. */ + // response.setEntity(new InputStreamEntity(new DeflaterInputStream(new + // ByteArrayInputStream( + // entityText.getBytes("utf-8"))), -1)); + final byte[] uncompressed = entityText.getBytes(StandardCharsets.UTF_8); + final Deflater compressor = new Deflater(Deflater.DEFAULT_COMPRESSION, rfc1951); + compressor.setInput(uncompressed); + compressor.finish(); + final byte[] output = new byte[100]; + final int compressedLength = compressor.deflate(output); + final byte[] compressed = new byte[compressedLength]; + System.arraycopy(output, 0, compressed, 0, compressedLength); + response.setEntity(new InputStreamEntity( + new ByteArrayInputStream(compressed), compressedLength, null)); + return; + } } - }; + }; } /** @@ -374,57 +348,47 @@ public void handle( * @return a non-null {@link HttpRequestHandler} */ private HttpRequestHandler createGzipEncodingRequestHandler(final String entityText) { - return new HttpRequestHandler() { - - /** - * {@inheritDoc} + return (request, response, context) -> { + response.setEntity(new StringEntity(entityText)); + response.addHeader("Content-Type", "text/plain"); + response.addHeader("Content-Type", "text/plain"); + final Iterator it = MessageSupport.iterate(request, "Accept-Encoding"); + while (it.hasNext()) { + final HeaderElement element = it.next(); + if ("gzip".equalsIgnoreCase(element.getName())) { + response.addHeader("Content-Encoding", "gzip"); + + /* + * We have to do a bit more work with gzip versus deflate, since + * Gzip doesn't appear to have an equivalent to DeflaterInputStream in + * the JDK. + * + * UPDATE: DeflaterInputStream is Java 6 anyway, so we have to do a bit + * of work there too! */ - @Override - public void handle( - final ClassicHttpRequest request, - final ClassicHttpResponse response, - final HttpContext context) throws IOException { - response.setEntity(new StringEntity(entityText)); - response.addHeader("Content-Type", "text/plain"); - response.addHeader("Content-Type", "text/plain"); - final Iterator it = MessageSupport.iterate(request, "Accept-Encoding"); - while (it.hasNext()) { - final HeaderElement element = it.next(); - if ("gzip".equalsIgnoreCase(element.getName())) { - response.addHeader("Content-Encoding", "gzip"); - - /* - * We have to do a bit more work with gzip versus deflate, since - * Gzip doesn't appear to have an equivalent to DeflaterInputStream in - * the JDK. - * - * UPDATE: DeflaterInputStream is Java 6 anyway, so we have to do a bit - * of work there too! - */ - final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - final OutputStream out = new GZIPOutputStream(bytes); - - final ByteArrayInputStream uncompressed = new ByteArrayInputStream( - entityText.getBytes(StandardCharsets.UTF_8)); - - final byte[] buf = new byte[60]; - - int n; - while ((n = uncompressed.read(buf)) != -1) { - out.write(buf, 0, n); - } - - out.close(); - - final byte[] arr = bytes.toByteArray(); - response.setEntity(new InputStreamEntity(new ByteArrayInputStream(arr), - arr.length, null)); - - return; - } - } + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + final OutputStream out = new GZIPOutputStream(bytes); + + final ByteArrayInputStream uncompressed = new ByteArrayInputStream( + entityText.getBytes(StandardCharsets.UTF_8)); + + final byte[] buf = new byte[60]; + + int n; + while ((n = uncompressed.read(buf)) != -1) { + out.write(buf, 0, n); + } + + out.close(); + + final byte[] arr = bytes.toByteArray(); + response.setEntity(new InputStreamEntity(new ByteArrayInputStream(arr), + arr.length, null)); + + return; + } } - }; + }; } /** diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java index 1c40f580f2..089c31f6f4 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java @@ -164,7 +164,7 @@ void shouldExecuteMultipleCallsAndCallback() throws Exception { } } - private final class CountingCallback implements FutureCallback { + private static final class CountingCallback implements FutureCallback { private final CountDownLatch latch; @@ -190,7 +190,7 @@ public void cancelled() { } - private final class OkidokiHandler implements HttpClientResponseHandler { + private static final class OkidokiHandler implements HttpClientResponseHandler { @Override public Boolean handleResponse( final ClassicHttpResponse response) { diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java index 1a46d99726..7d27efb00c 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java @@ -59,7 +59,6 @@ import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel; import org.apache.hc.client5.testing.extension.sync.TestClient; import org.apache.hc.client5.testing.redirect.Redirect; -import org.apache.hc.core5.function.Decorator; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.ConnectionClosedException; @@ -72,7 +71,6 @@ import org.apache.hc.core5.http.ProtocolException; import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.http.io.HttpRequestHandler; -import org.apache.hc.core5.http.io.HttpServerRequestHandler; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.BasicHeader; @@ -607,26 +605,19 @@ void testDefaultHeadersRedirect() throws Exception { void testCompressionHeaderRedirect() throws Exception { final Queue values = new ConcurrentLinkedQueue<>(); configureServer(bootstrap -> bootstrap - .setExchangeHandlerDecorator(new Decorator() { + .setExchangeHandlerDecorator(requestHandler -> new RedirectingDecorator( + requestHandler, + new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MOVED_TEMPORARILY)) { @Override - public HttpServerRequestHandler decorate(final HttpServerRequestHandler requestHandler) { - return new RedirectingDecorator( - requestHandler, - new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MOVED_TEMPORARILY)) { - - @Override - public void handle(final ClassicHttpRequest request, - final ResponseTrigger responseTrigger, - final HttpContext context) throws HttpException, IOException { - final Header header = request.getHeader(HttpHeaders.ACCEPT_ENCODING); - if (header != null) { - values.add(header.getValue()); - } - super.handle(request, responseTrigger, context); - } - - }; + public void handle(final ClassicHttpRequest request, + final ResponseTrigger responseTrigger, + final HttpContext context) throws HttpException, IOException { + final Header header = request.getHeader(HttpHeaders.ACCEPT_ENCODING); + if (header != null) { + values.add(header.getValue()); + } + super.handle(request, responseTrigger, context); } }) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleAsyncEntityConsumer.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleAsyncEntityConsumer.java index 00a94a81fd..b8a8ef6137 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleAsyncEntityConsumer.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/SimpleAsyncEntityConsumer.java @@ -39,7 +39,7 @@ final class SimpleAsyncEntityConsumer extends AbstractBinAsyncEntityConsumer getAllowedMethods(final HttpResponse response) { Args.notNull(response, "HTTP response"); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPatch.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPatch.java index c1ee0fc2a1..47947b448f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPatch.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPatch.java @@ -38,10 +38,13 @@ public class HttpPatch extends HttpUriRequestBase { private static final long serialVersionUID = 1L; + /** + * The method name {@value}. + */ public final static String METHOD_NAME = "PATCH"; /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is null. @@ -51,7 +54,7 @@ public HttpPatch(final URI uri) { } /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is invalid. diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPost.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPost.java index 41c14531fa..17b37f1f57 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPost.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPost.java @@ -38,10 +38,13 @@ public class HttpPost extends HttpUriRequestBase { private static final long serialVersionUID = 1L; + /** + * The method name {@value}. + */ public final static String METHOD_NAME = "POST"; /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is null. @@ -51,7 +54,7 @@ public HttpPost(final URI uri) { } /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is invalid. diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPut.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPut.java index 68e70b3ba1..2de79864f2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPut.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpPut.java @@ -37,10 +37,14 @@ public class HttpPut extends HttpUriRequestBase { private static final long serialVersionUID = 1L; + + /** + * The method name {@value}. + */ public final static String METHOD_NAME = "PUT"; /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is null. @@ -50,7 +54,7 @@ public HttpPut(final URI uri) { } /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is invalid. diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpTrace.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpTrace.java index a937ba21a2..c40dcc538b 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpTrace.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpTrace.java @@ -40,10 +40,13 @@ public class HttpTrace extends HttpUriRequestBase { private static final long serialVersionUID = 1L; + /** + * The method name {@value}. + */ public final static String METHOD_NAME = "TRACE"; /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is null. @@ -53,7 +56,7 @@ public HttpTrace(final URI uri) { } /** - * Creates a new instance initialized with the given URI. + * Constructs a new instance initialized with the given URI. * * @param uri a non-null request URI. * @throws IllegalArgumentException if the uri is invalid. diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java index d6ea8b6f8d..e410230a0b 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/HttpUriRequestBase.java @@ -34,6 +34,9 @@ import org.apache.hc.core5.concurrent.CancellableDependency; import org.apache.hc.core5.http.message.BasicClassicHttpRequest; +/** + * Base class for HTTP method subclasses. + */ public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpUriRequest, CancellableDependency { private static final long serialVersionUID = 1L; @@ -41,6 +44,12 @@ public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpU private final AtomicMarkableReference cancellableRef; private RequestConfig requestConfig; + /** + * Constructs a new instance with the given method and request URI. + * + * @param method request method. + * @param requestUri request URI. + */ public HttpUriRequestBase(final String method, final URI requestUri) { super(method, requestUri); this.cancellableRef = new AtomicMarkableReference<>(null, false); @@ -104,6 +113,11 @@ public boolean isAborted() { return isCancelled(); } + /** + * Sets the request configuration. + * + * @param requestConfig the request configuration. + */ public void setConfig(final RequestConfig requestConfig) { this.requestConfig = requestConfig; } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/config/Configurable.java b/httpclient5/src/main/java/org/apache/hc/client5/http/config/Configurable.java index 6614a194e3..f70006d58e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/config/Configurable.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/config/Configurable.java @@ -35,7 +35,9 @@ public interface Configurable { /** - * Returns actual request configuration. + * Gets the actual request configuration. + * + * @return the actual request configuration. */ RequestConfig getConfig(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MimeField.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MimeField.java index 4fbbee4bc9..6ad6359b25 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MimeField.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MimeField.java @@ -44,6 +44,12 @@ public class MimeField { private final String value; private final List parameters; + /** + * Constructs a new instance. + * + * @param name the field name. + * @param value the field value. + */ public MimeField(final String name, final String value) { super(); this.name = name; @@ -52,6 +58,11 @@ public MimeField(final String name, final String value) { } /** + * Constructs a new instance. + * + * @param name the field name. + * @param value the field value. + * @param parameters the field parameters. * @since 4.6 */ public MimeField(final String name, final String value, final List parameters) { @@ -61,26 +72,43 @@ public MimeField(final String name, final String value, final List(parameters)) : Collections.emptyList(); } + /** + * Constructs a new instance by copying another's properties. + * + * @param from the source field. + */ public MimeField(final MimeField from) { this(from.name, from.value, from.parameters); } + /** + * Gets the field name. + * + * @return the field name. + */ public String getName() { return this.name; } /** + * Gets the field value. + * + * @return the field value. * @since 4.6 */ public String getValue() { return this.value; } + /** + * Converts this instance to a body String. + * + * @return this instance as a body String. + */ public String getBody() { final StringBuilder sb = new StringBuilder(); sb.append(this.value); - for (int i = 0; i < this.parameters.size(); i++) { - final NameValuePair parameter = this.parameters.get(i); + parameters.forEach(parameter -> { sb.append("; "); sb.append(parameter.getName()); sb.append("=\""); @@ -93,10 +121,15 @@ public String getBody() { sb.append(ch); } sb.append("\""); - } + }); return sb.toString(); } + /** + * Gets the field parameters. + * + * @return the field parameters. + */ public List getParameters() { return this.parameters; } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java index fa9fd3a7aa..5d6b080c3a 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java @@ -78,14 +78,7 @@ public class DefaultHttpClientConnectionOperator implements HttpClientConnection private static final Logger LOG = LoggerFactory.getLogger(DefaultHttpClientConnectionOperator.class); - static final DetachedSocketFactory PLAIN_SOCKET_FACTORY = new DetachedSocketFactory() { - - @Override - public Socket create(final Proxy socksProxy) throws IOException { - return socksProxy == null ? new Socket() : new Socket(socksProxy); - } - - }; + static final DetachedSocketFactory PLAIN_SOCKET_FACTORY = socksProxy -> socksProxy == null ? new Socket() : new Socket(socksProxy); private final DetachedSocketFactory detachedSocketFactory; private final Lookup tlsSocketStrategyLookup; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java index 4808c44bdb..3d013865f8 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java @@ -78,10 +78,18 @@ enum HostNameType { private final PublicSuffixMatcher publicSuffixMatcher; + /** + * Constructs new instance with a PublicSuffixMatcher. + * + * @param publicSuffixMatcher a PublicSuffixMatcher. + */ public DefaultHostnameVerifier(final PublicSuffixMatcher publicSuffixMatcher) { this.publicSuffixMatcher = publicSuffixMatcher; } + /** + * Constructs new instance without a PublicSuffixMatcher. + */ public DefaultHostnameVerifier() { this(null); } @@ -123,9 +131,8 @@ public void verify(final String host, final X509Certificate cert) throws SSLExce } } - static void matchIPAddress(final String host, final List subjectAlts) throws SSLException { - for (int i = 0; i < subjectAlts.size(); i++) { - final SubjectName subjectAlt = subjectAlts.get(i); + static void matchIPAddress(final String host, final List subjectAlts) throws SSLPeerUnverifiedException { + for (final SubjectName subjectAlt : subjectAlts) { if (subjectAlt.getType() == SubjectName.IP) { if (host.equals(subjectAlt.getValue())) { return; @@ -136,10 +143,9 @@ static void matchIPAddress(final String host, final List subjectAlt "of the subject alternative names: " + subjectAlts); } - static void matchIPv6Address(final String host, final List subjectAlts) throws SSLException { + static void matchIPv6Address(final String host, final List subjectAlts) throws SSLPeerUnverifiedException { final String normalisedHost = normaliseAddress(host); - for (int i = 0; i < subjectAlts.size(); i++) { - final SubjectName subjectAlt = subjectAlts.get(i); + for (final SubjectName subjectAlt : subjectAlts) { if (subjectAlt.getType() == SubjectName.IP) { final String normalizedSubjectAlt = normaliseAddress(subjectAlt.getValue()); if (normalisedHost.equals(normalizedSubjectAlt)) { @@ -152,10 +158,9 @@ static void matchIPv6Address(final String host, final List subjectA } static void matchDNSName(final String host, final List subjectAlts, - final PublicSuffixMatcher publicSuffixMatcher) throws SSLException { + final PublicSuffixMatcher publicSuffixMatcher) throws SSLPeerUnverifiedException { final String normalizedHost = DnsUtils.normalize(host); - for (int i = 0; i < subjectAlts.size(); i++) { - final SubjectName subjectAlt = subjectAlts.get(i); + for (final SubjectName subjectAlt : subjectAlts) { if (subjectAlt.getType() == SubjectName.DNS) { final String normalizedSubjectAlt = DnsUtils.normalize(subjectAlt.getValue()); if (matchIdentityStrict(normalizedHost, normalizedSubjectAlt, publicSuffixMatcher)) {