From f3420c6a809b40ea53a7eaa16a7c0a98913b4cd4 Mon Sep 17 00:00:00 2001 From: Steven Kradel Date: Wed, 6 Nov 2019 18:15:34 -0500 Subject: [PATCH 1/5] allow external provision of http client in ApacheHttpRequester via alternate ctor --- .../algolia/search/ApacheHttpRequester.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java index 7e53ac3b7..df1a66c89 100644 --- a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java +++ b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java @@ -8,6 +8,7 @@ import java.net.ConnectException; import java.net.SocketTimeoutException; import java.util.Map; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeoutException; import java.util.function.Consumer; @@ -36,6 +37,7 @@ final class ApacheHttpRequester implements HttpRequester { private final CloseableHttpAsyncClient asyncHttpClient; private final RequestConfig requestConfig; private final ConfigBase config; + private final boolean isSelfManagedClient; ApacheHttpRequester(@Nonnull ConfigBase config) { @@ -52,9 +54,26 @@ final class ApacheHttpRequester implements HttpRequester { ? HttpAsyncClients.createSystem() : HttpAsyncClients.createDefault(); + isSelfManagedClient = true; + asyncHttpClient.start(); } + ApacheHttpRequester(@Nonnull ConfigBase config, @Nonnull CloseableHttpAsyncClient externalClient) { + + this.config = config; + + requestConfig = + RequestConfig.custom() + .setConnectTimeout(config.getConnectTimeOut()) + .setContentCompressionEnabled(true) + .build(); + + asyncHttpClient = Objects.requireNonNull(externalClient); + + isSelfManagedClient = false; + } + /** * Sends the http request asynchronously to the API If the request is time out it creates a new * response object with timeout set to true Otherwise it throws a run time exception @@ -84,7 +103,9 @@ public CompletableFuture performRequestAsync(HttpRequest request) /** Closes the http client. */ public void close() throws IOException { - asyncHttpClient.close(); + if (isSelfManagedClient) { + asyncHttpClient.close(); + } } /** From e454afadaae73fec869529eb53676001fbd4d099 Mon Sep 17 00:00:00 2001 From: Steven Kradel Date: Wed, 6 Nov 2019 18:26:22 -0500 Subject: [PATCH 2/5] incr version to 3.6.1 and add remark to ChangeLog --- ChangeLog.md | 6 ++++++ pom.xml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index d2871f4df..3bf444105 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,11 @@ # ChangeLog +## 3.6.1 + +### Feat + +- feat: add alternate constructor to ApacheHttpRequester to permit external management of the client + ## [3.6.0](https://github.com/algolia/algoliasearch-client-java-2/compare/3.5.0...3.6.0) (2019-11-06) ### Feat diff --git a/pom.xml b/pom.xml index 42be329c6..5782b20bb 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.algolia algoliasearch - 3.6.0 + 3.6.1 pom From 673169221c32c45a7a1dddea528ee9605f3f1643 Mon Sep 17 00:00:00 2001 From: Steven Kradel Date: Fri, 8 Nov 2019 10:44:34 -0500 Subject: [PATCH 3/5] port HttpRequester utility methods to abstract superclass --- .../executionHistory/executionHistory.bin | Bin 0 -> 18732 bytes .../executionHistory/executionHistory.lock | Bin 0 -> 17 bytes .gradle/5.6.3/fileChanges/last-build.bin | Bin 0 -> 1 bytes .gradle/5.6.3/fileHashes/fileHashes.lock | Bin 0 -> 17 bytes .gradle/5.6.3/gc.properties | 0 .../buildOutputCleanup.lock | Bin 0 -> 17 bytes .gradle/buildOutputCleanup/cache.properties | 2 + .gradle/vcs-1/gc.properties | 0 .../search/AbstractApacheHttpRequester.java | 207 ++++++++++++++++++ .../algolia/search/ApacheHttpRequester.java | 200 ++--------------- .../search/models/indexing/Explain.java | 4 +- 11 files changed, 230 insertions(+), 183 deletions(-) create mode 100644 .gradle/5.6.3/executionHistory/executionHistory.bin create mode 100644 .gradle/5.6.3/executionHistory/executionHistory.lock create mode 100644 .gradle/5.6.3/fileChanges/last-build.bin create mode 100644 .gradle/5.6.3/fileHashes/fileHashes.lock create mode 100644 .gradle/5.6.3/gc.properties create mode 100644 .gradle/buildOutputCleanup/buildOutputCleanup.lock create mode 100644 .gradle/buildOutputCleanup/cache.properties create mode 100644 .gradle/vcs-1/gc.properties create mode 100644 algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java diff --git a/.gradle/5.6.3/executionHistory/executionHistory.bin b/.gradle/5.6.3/executionHistory/executionHistory.bin new file mode 100644 index 0000000000000000000000000000000000000000..d46a19305fad847bd8d11f380e4f31236019b64c GIT binary patch literal 18732 zcmeI%ze@u#6u|MEqX>2py1BRrdI+McSSoZD6~v*W&GnjVFS#FS`=>Yvj)J3;vrGR7 z$AUNtPHviVE&U4wk#FE7k0gZmK5Lpn)N9|1Ay#8+m69QV00IagfB*srAbdcIFVNDttcOGT7a2NhwyBT$DAzh?ut=aufd;9aTkw2Zh QJtx<}uf6O`4}84;0KeIGasU7T literal 0 HcmV?d00001 diff --git a/.gradle/5.6.3/executionHistory/executionHistory.lock b/.gradle/5.6.3/executionHistory/executionHistory.lock new file mode 100644 index 0000000000000000000000000000000000000000..0ff3f97a95fa4b197a70a04c498024f1909dd346 GIT binary patch literal 17 UcmZRcQWSIe^`GLK3=qH!06`lCUjP6A literal 0 HcmV?d00001 diff --git a/.gradle/5.6.3/fileChanges/last-build.bin b/.gradle/5.6.3/fileChanges/last-build.bin new file mode 100644 index 0000000000000000000000000000000000000000..f76dd238ade08917e6712764a16a22005a50573d GIT binary patch literal 1 IcmZPo000310RR91 literal 0 HcmV?d00001 diff --git a/.gradle/5.6.3/fileHashes/fileHashes.lock b/.gradle/5.6.3/fileHashes/fileHashes.lock new file mode 100644 index 0000000000000000000000000000000000000000..5f0efdf1b2f918d27c5414ee4f04fd07357d7027 GIT binary patch literal 17 TcmZSP*#BXUjoV>n1}FdkE{p@8 literal 0 HcmV?d00001 diff --git a/.gradle/5.6.3/gc.properties b/.gradle/5.6.3/gc.properties new file mode 100644 index 000000000..e69de29bb diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 0000000000000000000000000000000000000000..9f942ea978bff08ceae252b77fa38787049e759a GIT binary patch literal 17 UcmZRccgnRxw{PYQ1_)pT05mZKCIA2c literal 0 HcmV?d00001 diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 000000000..2d29d0c40 --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Fri Nov 08 10:42:41 EST 2019 +gradle.version=5.6.3 diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties new file mode 100644 index 000000000..e69de29bb diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java new file mode 100644 index 000000000..607b913b8 --- /dev/null +++ b/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java @@ -0,0 +1,207 @@ +package com.algolia.search; + +import com.algolia.search.exceptions.AlgoliaRuntimeException; +import com.algolia.search.models.HttpRequest; +import com.algolia.search.models.HttpResponse; +import com.algolia.search.util.HttpStatusCodeUtils; +import java.io.IOException; +import java.net.ConnectException; +import java.net.SocketTimeoutException; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; +import javax.annotation.Nonnull; +import org.apache.http.*; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.DeflateDecompressingEntity; +import org.apache.http.client.entity.GzipDecompressingEntity; +import org.apache.http.client.methods.*; +import org.apache.http.concurrent.FutureCallback; +import org.apache.http.conn.ConnectTimeoutException; +import org.apache.http.conn.ConnectionPoolTimeoutException; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.InputStreamEntity; +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; +import org.apache.http.util.EntityUtils; + +/** + * The Algolia http requester is a wrapper on top of the HttpAsyncClient of Apache. It's an + * implementation of {@link HttpRequester} It takes an {@link HttpRequest} as input. It returns an + * {@link HttpResponse}. + */ +public abstract class AbstractApacheHttpRequester implements HttpRequester { + + protected abstract CloseableHttpAsyncClient getAsyncHttpClient(); + + protected abstract boolean getIsSelfManagedClient(); + + protected abstract ConfigBase getConfig(); + + protected abstract RequestConfig getRequestConfig(); + + /** + * Sends the http request asynchronously to the API If the request is time out it creates a new + * response object with timeout set to true Otherwise it throws a run time exception + * + * @param request the request to send + * @throws AlgoliaRuntimeException When an error occurred while sending the request + */ + public CompletableFuture performRequestAsync(HttpRequest request) { + HttpRequestBase requestToSend = buildRequest(request); + return toCompletableFuture(fc -> getAsyncHttpClient().execute(requestToSend, fc)) + .thenApplyAsync(this::buildResponse, getConfig().getExecutor()) + .exceptionally( + t -> { + if (t.getCause() instanceof ConnectTimeoutException + || t.getCause() instanceof SocketTimeoutException + || t.getCause() instanceof ConnectException + || t.getCause() instanceof TimeoutException + || t.getCause() instanceof ConnectionPoolTimeoutException + || t.getCause() instanceof NoHttpResponseException) { + return new HttpResponse(true); + } else if (t.getCause() instanceof HttpException) { + return new HttpResponse().setNetworkError(true); + } + throw new AlgoliaRuntimeException(t); + }); + } + + /** Closes the http client. */ + public void close() throws IOException { + if (getIsSelfManagedClient()) { + getAsyncHttpClient().close(); + } + } + + /** + * Builds an Algolia response from the server response + * + * @param response The server response + */ + protected HttpResponse buildResponse(org.apache.http.HttpResponse response) { + try { + if (HttpStatusCodeUtils.isSuccess(response.getStatusLine().getStatusCode())) { + + HttpEntity entity = handleCompressedEntity(response.getEntity()); + + return new HttpResponse(response.getStatusLine().getStatusCode(), entity.getContent()); + } + return new HttpResponse( + response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity())); + } catch (IOException e) { + throw new AlgoliaRuntimeException(e); + } + } + + /** + * Builds an Apache HttpRequest from an Algolia Request object + * + * @param algoliaRequest The Algolia request object + */ + protected HttpRequestBase buildRequest(HttpRequest algoliaRequest) { + + switch (algoliaRequest.getMethod().toString()) { + case HttpGet.METHOD_NAME: + HttpGet get = new HttpGet(algoliaRequest.getUri().toString()); + get.setConfig(buildRequestConfig(algoliaRequest)); + return addHeaders(get, algoliaRequest.getHeaders()); + + case HttpDelete.METHOD_NAME: + HttpDelete delete = new HttpDelete(algoliaRequest.getUri().toString()); + delete.setConfig(buildRequestConfig(algoliaRequest)); + return addHeaders(delete, algoliaRequest.getHeaders()); + + case HttpPost.METHOD_NAME: + HttpPost post = new HttpPost(algoliaRequest.getUri().toString()); + if (algoliaRequest.getBody() != null) post.setEntity(addEntity(algoliaRequest)); + post.setConfig(buildRequestConfig(algoliaRequest)); + return addHeaders(post, algoliaRequest.getHeaders()); + + case HttpPut.METHOD_NAME: + HttpPut put = new HttpPut(algoliaRequest.getUri().toString()); + if (algoliaRequest.getBody() != null) put.setEntity(addEntity(algoliaRequest)); + put.setConfig(buildRequestConfig(algoliaRequest)); + return addHeaders(put, algoliaRequest.getHeaders()); + + case HttpPatch.METHOD_NAME: + HttpPatch patch = new HttpPatch(algoliaRequest.getUri().toString()); + if (algoliaRequest.getBody() != null) patch.setEntity(addEntity(algoliaRequest)); + patch.setConfig(buildRequestConfig(algoliaRequest)); + return addHeaders(patch, algoliaRequest.getHeaders()); + + default: + throw new UnsupportedOperationException( + "HTTP method not supported: " + algoliaRequest.getMethod().toString()); + } + } + + protected RequestConfig buildRequestConfig(HttpRequest algoliaRequest) { + return RequestConfig.copy(getRequestConfig()) + .setSocketTimeout(algoliaRequest.getTimeout()) + .build(); + } + + protected HttpRequestBase addHeaders(HttpRequestBase request, Map headers) { + headers.forEach(request::addHeader); + return request; + } + + protected HttpEntity addEntity(@Nonnull HttpRequest request) { + try { + InputStreamEntity entity = + new InputStreamEntity( + request.getBody(), request.getBody().available(), ContentType.APPLICATION_JSON); + + if (request.canCompress()) { + entity.setContentEncoding(Defaults.CONTENT_ENCODING_GZIP); + } + + return entity; + } catch (IOException e) { + throw new AlgoliaRuntimeException("Error while getting body's content length.", e); + } + } + + protected HttpEntity handleCompressedEntity(HttpEntity entity) { + + Header contentEncoding = entity.getContentEncoding(); + + if (contentEncoding != null) + for (HeaderElement e : contentEncoding.getElements()) { + if (Defaults.CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) { + return new GzipDecompressingEntity(entity); + } + + if (Defaults.CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) { + return new DeflateDecompressingEntity(entity); + } + } + + return entity; + } + + protected static CompletableFuture toCompletableFuture( + Consumer> c) { + CompletableFuture promise = new CompletableFuture<>(); + + c.accept( + new FutureCallback() { + @Override + public void completed(org.apache.http.HttpResponse t) { + promise.complete(t); + } + + @Override + public void failed(Exception e) { + promise.completeExceptionally(e); + } + + @Override + public void cancelled() { + promise.cancel(true); + } + }); + return promise; + } +} diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java index df1a66c89..9761cf4a0 100644 --- a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java +++ b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java @@ -1,38 +1,19 @@ package com.algolia.search; -import com.algolia.search.exceptions.AlgoliaRuntimeException; import com.algolia.search.models.HttpRequest; import com.algolia.search.models.HttpResponse; -import com.algolia.search.util.HttpStatusCodeUtils; -import java.io.IOException; -import java.net.ConnectException; -import java.net.SocketTimeoutException; -import java.util.Map; import java.util.Objects; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeoutException; -import java.util.function.Consumer; import javax.annotation.Nonnull; -import org.apache.http.*; import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.DeflateDecompressingEntity; -import org.apache.http.client.entity.GzipDecompressingEntity; -import org.apache.http.client.methods.*; -import org.apache.http.concurrent.FutureCallback; -import org.apache.http.conn.ConnectTimeoutException; -import org.apache.http.conn.ConnectionPoolTimeoutException; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.InputStreamEntity; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; -import org.apache.http.util.EntityUtils; /** * The Algolia http requester is a wrapper on top of the HttpAsyncClient of Apache. It's an * implementation of {@link HttpRequester} It takes an {@link HttpRequest} as input. It returns an * {@link HttpResponse}. */ -final class ApacheHttpRequester implements HttpRequester { +final class ApacheHttpRequester extends AbstractApacheHttpRequester { private final CloseableHttpAsyncClient asyncHttpClient; private final RequestConfig requestConfig; @@ -59,182 +40,39 @@ final class ApacheHttpRequester implements HttpRequester { asyncHttpClient.start(); } - ApacheHttpRequester(@Nonnull ConfigBase config, @Nonnull CloseableHttpAsyncClient externalClient) { + ApacheHttpRequester( + @Nonnull ConfigBase config, @Nonnull CloseableHttpAsyncClient externalClient) { this.config = config; requestConfig = - RequestConfig.custom() - .setConnectTimeout(config.getConnectTimeOut()) - .setContentCompressionEnabled(true) - .build(); + RequestConfig.custom() + .setConnectTimeout(config.getConnectTimeOut()) + .setContentCompressionEnabled(true) + .build(); asyncHttpClient = Objects.requireNonNull(externalClient); isSelfManagedClient = false; } - /** - * Sends the http request asynchronously to the API If the request is time out it creates a new - * response object with timeout set to true Otherwise it throws a run time exception - * - * @param request the request to send - * @throws AlgoliaRuntimeException When an error occurred while sending the request - */ - public CompletableFuture performRequestAsync(HttpRequest request) { - HttpRequestBase requestToSend = buildRequest(request); - return toCompletableFuture(fc -> asyncHttpClient.execute(requestToSend, fc)) - .thenApplyAsync(this::buildResponse, config.getExecutor()) - .exceptionally( - t -> { - if (t.getCause() instanceof ConnectTimeoutException - || t.getCause() instanceof SocketTimeoutException - || t.getCause() instanceof ConnectException - || t.getCause() instanceof TimeoutException - || t.getCause() instanceof ConnectionPoolTimeoutException - || t.getCause() instanceof NoHttpResponseException) { - return new HttpResponse(true); - } else if (t.getCause() instanceof HttpException) { - return new HttpResponse().setNetworkError(true); - } - throw new AlgoliaRuntimeException(t); - }); - } - - /** Closes the http client. */ - public void close() throws IOException { - if (isSelfManagedClient) { - asyncHttpClient.close(); - } - } - - /** - * Builds an Algolia response from the server response - * - * @param response The server response - */ - private HttpResponse buildResponse(org.apache.http.HttpResponse response) { - try { - if (HttpStatusCodeUtils.isSuccess(response.getStatusLine().getStatusCode())) { - - HttpEntity entity = handleCompressedEntity(response.getEntity()); - - return new HttpResponse(response.getStatusLine().getStatusCode(), entity.getContent()); - } - return new HttpResponse( - response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity())); - } catch (IOException e) { - throw new AlgoliaRuntimeException(e); - } - } - - /** - * Builds an Apache HttpRequest from an Algolia Request object - * - * @param algoliaRequest The Algolia request object - */ - private HttpRequestBase buildRequest(HttpRequest algoliaRequest) { - - switch (algoliaRequest.getMethod().toString()) { - case HttpGet.METHOD_NAME: - HttpGet get = new HttpGet(algoliaRequest.getUri().toString()); - get.setConfig(buildRequestConfig(algoliaRequest)); - return addHeaders(get, algoliaRequest.getHeaders()); - - case HttpDelete.METHOD_NAME: - HttpDelete delete = new HttpDelete(algoliaRequest.getUri().toString()); - delete.setConfig(buildRequestConfig(algoliaRequest)); - return addHeaders(delete, algoliaRequest.getHeaders()); - - case HttpPost.METHOD_NAME: - HttpPost post = new HttpPost(algoliaRequest.getUri().toString()); - if (algoliaRequest.getBody() != null) post.setEntity(addEntity(algoliaRequest)); - post.setConfig(buildRequestConfig(algoliaRequest)); - return addHeaders(post, algoliaRequest.getHeaders()); - - case HttpPut.METHOD_NAME: - HttpPut put = new HttpPut(algoliaRequest.getUri().toString()); - if (algoliaRequest.getBody() != null) put.setEntity(addEntity(algoliaRequest)); - put.setConfig(buildRequestConfig(algoliaRequest)); - return addHeaders(put, algoliaRequest.getHeaders()); - - case HttpPatch.METHOD_NAME: - HttpPatch patch = new HttpPatch(algoliaRequest.getUri().toString()); - if (algoliaRequest.getBody() != null) patch.setEntity(addEntity(algoliaRequest)); - patch.setConfig(buildRequestConfig(algoliaRequest)); - return addHeaders(patch, algoliaRequest.getHeaders()); - - default: - throw new UnsupportedOperationException( - "HTTP method not supported: " + algoliaRequest.getMethod().toString()); - } + @Override + protected CloseableHttpAsyncClient getAsyncHttpClient() { + return asyncHttpClient; } - private RequestConfig buildRequestConfig(HttpRequest algoliaRequest) { - return RequestConfig.copy(requestConfig).setSocketTimeout(algoliaRequest.getTimeout()).build(); + @Override + protected boolean getIsSelfManagedClient() { + return isSelfManagedClient; } - private HttpRequestBase addHeaders( - org.apache.http.client.methods.HttpRequestBase request, Map headers) { - headers.forEach(request::addHeader); - return request; - } - - private HttpEntity addEntity(@Nonnull HttpRequest request) { - try { - InputStreamEntity entity = - new InputStreamEntity( - request.getBody(), request.getBody().available(), ContentType.APPLICATION_JSON); - - if (request.canCompress()) { - entity.setContentEncoding(Defaults.CONTENT_ENCODING_GZIP); - } - - return entity; - } catch (IOException e) { - throw new AlgoliaRuntimeException("Error while getting body's content length.", e); - } - } - - private static HttpEntity handleCompressedEntity(org.apache.http.HttpEntity entity) { - - Header contentEncoding = entity.getContentEncoding(); - - if (contentEncoding != null) - for (HeaderElement e : contentEncoding.getElements()) { - if (Defaults.CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) { - return new GzipDecompressingEntity(entity); - } - - if (Defaults.CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) { - return new DeflateDecompressingEntity(entity); - } - } - - return entity; + @Override + protected ConfigBase getConfig() { + return config; } - private static CompletableFuture toCompletableFuture( - Consumer> c) { - CompletableFuture promise = new CompletableFuture<>(); - - c.accept( - new FutureCallback() { - @Override - public void completed(org.apache.http.HttpResponse t) { - promise.complete(t); - } - - @Override - public void failed(Exception e) { - promise.completeExceptionally(e); - } - - @Override - public void cancelled() { - promise.cancel(true); - } - }); - return promise; + @Override + protected RequestConfig getRequestConfig() { + return requestConfig; } } diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/Explain.java b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/Explain.java index 2b1df6284..8269a3435 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/Explain.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/Explain.java @@ -1,7 +1,6 @@ package com.algolia.search.models.indexing; import com.fasterxml.jackson.annotation.JsonProperty; - import java.util.Map; public class Explain { @@ -26,5 +25,6 @@ public Explain setParams(Map params) { @JsonProperty("match") private QueryMatch queryMatch; - private Map params; + + private Map params; } From 764e1f2a15d2961fe3c570ebcaa5b1732b53a857 Mon Sep 17 00:00:00 2001 From: Steven Kradel Date: Fri, 8 Nov 2019 10:46:55 -0500 Subject: [PATCH 4/5] remove accidental gradle files --- .../5.6.3/executionHistory/executionHistory.bin | Bin 18732 -> 0 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 0 bytes .gradle/5.6.3/fileChanges/last-build.bin | Bin 1 -> 0 bytes .gradle/5.6.3/fileHashes/fileHashes.lock | Bin 17 -> 0 bytes .gradle/5.6.3/gc.properties | 0 .../buildOutputCleanup/buildOutputCleanup.lock | Bin 17 -> 0 bytes .gradle/buildOutputCleanup/cache.properties | 2 -- .gradle/vcs-1/gc.properties | 0 8 files changed, 2 deletions(-) delete mode 100644 .gradle/5.6.3/executionHistory/executionHistory.bin delete mode 100644 .gradle/5.6.3/executionHistory/executionHistory.lock delete mode 100644 .gradle/5.6.3/fileChanges/last-build.bin delete mode 100644 .gradle/5.6.3/fileHashes/fileHashes.lock delete mode 100644 .gradle/5.6.3/gc.properties delete mode 100644 .gradle/buildOutputCleanup/buildOutputCleanup.lock delete mode 100644 .gradle/buildOutputCleanup/cache.properties delete mode 100644 .gradle/vcs-1/gc.properties diff --git a/.gradle/5.6.3/executionHistory/executionHistory.bin b/.gradle/5.6.3/executionHistory/executionHistory.bin deleted file mode 100644 index d46a19305fad847bd8d11f380e4f31236019b64c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 18732 zcmeI%ze@u#6u|MEqX>2py1BRrdI+McSSoZD6~v*W&GnjVFS#FS`=>Yvj)J3;vrGR7 z$AUNtPHviVE&U4wk#FE7k0gZmK5Lpn)N9|1Ay#8+m69QV00IagfB*srAbdcIFVNDttcOGT7a2NhwyBT$DAzh?ut=aufd;9aTkw2Zh QJtx<}uf6O`4}84;0KeIGasU7T diff --git a/.gradle/5.6.3/executionHistory/executionHistory.lock b/.gradle/5.6.3/executionHistory/executionHistory.lock deleted file mode 100644 index 0ff3f97a95fa4b197a70a04c498024f1909dd346..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17 UcmZRcQWSIe^`GLK3=qH!06`lCUjP6A diff --git a/.gradle/5.6.3/fileChanges/last-build.bin b/.gradle/5.6.3/fileChanges/last-build.bin deleted file mode 100644 index f76dd238ade08917e6712764a16a22005a50573d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1 IcmZPo000310RR91 diff --git a/.gradle/5.6.3/fileHashes/fileHashes.lock b/.gradle/5.6.3/fileHashes/fileHashes.lock deleted file mode 100644 index 5f0efdf1b2f918d27c5414ee4f04fd07357d7027..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17 TcmZSP*#BXUjoV>n1}FdkE{p@8 diff --git a/.gradle/5.6.3/gc.properties b/.gradle/5.6.3/gc.properties deleted file mode 100644 index e69de29bb..000000000 diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock deleted file mode 100644 index 9f942ea978bff08ceae252b77fa38787049e759a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17 UcmZRccgnRxw{PYQ1_)pT05mZKCIA2c diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties deleted file mode 100644 index 2d29d0c40..000000000 --- a/.gradle/buildOutputCleanup/cache.properties +++ /dev/null @@ -1,2 +0,0 @@ -#Fri Nov 08 10:42:41 EST 2019 -gradle.version=5.6.3 diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties deleted file mode 100644 index e69de29bb..000000000 From 9ebb8b73feeaa823a608c50ef40c2bcf9d4eeb5f Mon Sep 17 00:00:00 2001 From: Steven Kradel Date: Mon, 11 Nov 2019 16:20:58 -0500 Subject: [PATCH 5/5] remove (new) alternate ApacheHttpRequester ctor, implement via new class ExternalApacheHttpRequester; shift close() to abstract superclass --- .../search/AbstractApacheHttpRequester.java | 8 +-- .../algolia/search/ApacheHttpRequester.java | 27 ++------- .../search/ExternalApacheHttpRequester.java | 55 +++++++++++++++++++ 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 algoliasearch-apache/src/main/java/com/algolia/search/ExternalApacheHttpRequester.java diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java index 607b913b8..cef59761f 100644 --- a/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java +++ b/algoliasearch-apache/src/main/java/com/algolia/search/AbstractApacheHttpRequester.java @@ -34,8 +34,6 @@ public abstract class AbstractApacheHttpRequester implements HttpRequester { protected abstract CloseableHttpAsyncClient getAsyncHttpClient(); - protected abstract boolean getIsSelfManagedClient(); - protected abstract ConfigBase getConfig(); protected abstract RequestConfig getRequestConfig(); @@ -68,11 +66,7 @@ public CompletableFuture performRequestAsync(HttpRequest request) } /** Closes the http client. */ - public void close() throws IOException { - if (getIsSelfManagedClient()) { - getAsyncHttpClient().close(); - } - } + public abstract void close() throws IOException; /** * Builds an Algolia response from the server response diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java index 9761cf4a0..bcdc36042 100644 --- a/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java +++ b/algoliasearch-apache/src/main/java/com/algolia/search/ApacheHttpRequester.java @@ -2,7 +2,7 @@ import com.algolia.search.models.HttpRequest; import com.algolia.search.models.HttpResponse; -import java.util.Objects; +import java.io.IOException; import javax.annotation.Nonnull; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; @@ -18,7 +18,6 @@ final class ApacheHttpRequester extends AbstractApacheHttpRequester { private final CloseableHttpAsyncClient asyncHttpClient; private final RequestConfig requestConfig; private final ConfigBase config; - private final boolean isSelfManagedClient; ApacheHttpRequester(@Nonnull ConfigBase config) { @@ -35,25 +34,12 @@ final class ApacheHttpRequester extends AbstractApacheHttpRequester { ? HttpAsyncClients.createSystem() : HttpAsyncClients.createDefault(); - isSelfManagedClient = true; - asyncHttpClient.start(); } - ApacheHttpRequester( - @Nonnull ConfigBase config, @Nonnull CloseableHttpAsyncClient externalClient) { - - this.config = config; - - requestConfig = - RequestConfig.custom() - .setConnectTimeout(config.getConnectTimeOut()) - .setContentCompressionEnabled(true) - .build(); - - asyncHttpClient = Objects.requireNonNull(externalClient); - - isSelfManagedClient = false; + @Override + public void close() throws IOException { + getAsyncHttpClient().close(); } @Override @@ -61,11 +47,6 @@ protected CloseableHttpAsyncClient getAsyncHttpClient() { return asyncHttpClient; } - @Override - protected boolean getIsSelfManagedClient() { - return isSelfManagedClient; - } - @Override protected ConfigBase getConfig() { return config; diff --git a/algoliasearch-apache/src/main/java/com/algolia/search/ExternalApacheHttpRequester.java b/algoliasearch-apache/src/main/java/com/algolia/search/ExternalApacheHttpRequester.java new file mode 100644 index 000000000..0dfe07c44 --- /dev/null +++ b/algoliasearch-apache/src/main/java/com/algolia/search/ExternalApacheHttpRequester.java @@ -0,0 +1,55 @@ +package com.algolia.search; + +import com.algolia.search.models.HttpRequest; +import com.algolia.search.models.HttpResponse; +import java.io.IOException; +import java.util.Objects; +import javax.annotation.Nonnull; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; + +/** + * The Algolia http requester is a wrapper on top of the HttpAsyncClient of Apache. It's an + * implementation of {@link HttpRequester} It takes an {@link HttpRequest} as input. It returns an + * {@link HttpResponse}. + */ +public class ExternalApacheHttpRequester extends AbstractApacheHttpRequester { + + private final CloseableHttpAsyncClient asyncHttpClient; + private final RequestConfig requestConfig; + private final ConfigBase config; + + public ExternalApacheHttpRequester( + @Nonnull ConfigBase config, @Nonnull CloseableHttpAsyncClient client) { + + this.config = config; + + requestConfig = + RequestConfig.custom() + .setConnectTimeout(config.getConnectTimeOut()) + .setContentCompressionEnabled(true) + .build(); + + asyncHttpClient = Objects.requireNonNull(client); + } + + @Override + public void close() throws IOException { + // noop + } + + @Override + protected CloseableHttpAsyncClient getAsyncHttpClient() { + return asyncHttpClient; + } + + @Override + protected ConfigBase getConfig() { + return config; + } + + @Override + protected RequestConfig getRequestConfig() { + return requestConfig; + } +}