From 78e234e57636423ff7a16df3faa37d509e286947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Wed, 21 Apr 2021 13:17:07 +0200 Subject: [PATCH] fix HTTP client host/port parsing --- .../java/org/xbib/elx/http/HttpAction.java | 2 + .../org/xbib/elx/http/HttpClientHelper.java | 31 ++++++++++++-- .../org/xbib/elx/http/test/DumpIDTest.java | 42 +++++++++++++++++++ .../xbib/elx/transport/test/DumpIDTest.java | 41 ++++++++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 elx-http/src/test/java/org/xbib/elx/http/test/DumpIDTest.java create mode 100644 elx-transport/src/test/java/org/xbib/elx/transport/test/DumpIDTest.java diff --git a/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java b/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java index 6dcb579..f3906dc 100644 --- a/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java +++ b/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java @@ -166,6 +166,8 @@ public abstract class HttpAction URL.http().host("localhost").port(9200).build()); httpAddress = HttpAddress.http1(u); this.url = u.toExternalForm(); } else { @@ -151,4 +151,27 @@ public class HttpClientHelper { logger.error(e.getMessage(), e); } } + + + private List findAddresses(Settings settings) { + final int defaultPort = settings.getAsInt("port", 9200); + List addresses = new ArrayList<>(); + for (String hostname : settings.getAsList("host")) { + String[] splitHost = hostname.split(":", 2); + if (splitHost.length == 2) { + try { + String host = splitHost[0]; + int port = Integer.parseInt(splitHost[1]); + addresses.add(URL.from("http://" + host + ":" + port)); + } catch (NumberFormatException e) { + logger.warn(e.getMessage(), e); + } + } + if (splitHost.length == 1) { + String host = splitHost[0]; + addresses.add(URL.from("http://" + host + ":" + defaultPort)); + } + } + return addresses; + } } diff --git a/elx-http/src/test/java/org/xbib/elx/http/test/DumpIDTest.java b/elx-http/src/test/java/org/xbib/elx/http/test/DumpIDTest.java new file mode 100644 index 0000000..edc2508 --- /dev/null +++ b/elx-http/src/test/java/org/xbib/elx/http/test/DumpIDTest.java @@ -0,0 +1,42 @@ +package org.xbib.elx.http.test; + +import org.elasticsearch.index.query.QueryBuilders; +import org.junit.jupiter.api.Test; +import org.xbib.elx.api.IndexDefinition; +import org.xbib.elx.common.ClientBuilder; +import org.xbib.elx.common.DefaultIndexDefinition; +import org.xbib.elx.http.HttpSearchClient; +import org.xbib.elx.http.HttpSearchClientProvider; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +class DumpIDTest { + + @Test + void testDump() throws Exception { + IndexDefinition indexDefinition = new DefaultIndexDefinition("zdb", "zdb"); + try (HttpSearchClient searchClient = ClientBuilder.builder() + .setSearchClientProvider(HttpSearchClientProvider.class) + .put("cluster.name", "es2") + .put("host", "atlas:9202") + .put("pool.enabled", false) + .build(); + BufferedWriter writer = Files.newBufferedWriter(Paths.get("zdb.txt"))) { + Stream stream = searchClient.getIds(qb -> qb + .setIndices(indexDefinition.getIndex()) + .setQuery(QueryBuilders.matchAllQuery())); + stream.forEach(id -> { + try { + writer.write(id); + writer.newLine(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + } +} diff --git a/elx-transport/src/test/java/org/xbib/elx/transport/test/DumpIDTest.java b/elx-transport/src/test/java/org/xbib/elx/transport/test/DumpIDTest.java new file mode 100644 index 0000000..b6a18fd --- /dev/null +++ b/elx-transport/src/test/java/org/xbib/elx/transport/test/DumpIDTest.java @@ -0,0 +1,41 @@ +package org.xbib.elx.transport.test; + +import org.elasticsearch.index.query.QueryBuilders; +import org.junit.jupiter.api.Test; +import org.xbib.elx.api.IndexDefinition; +import org.xbib.elx.common.ClientBuilder; +import org.xbib.elx.common.DefaultIndexDefinition; +import org.xbib.elx.transport.TransportSearchClient; +import org.xbib.elx.transport.TransportSearchClientProvider; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +class DumpIDTest { + + @Test + void testDump() throws Exception { + IndexDefinition indexDefinition = new DefaultIndexDefinition("zdb", "zdb"); + try (TransportSearchClient searchClient = ClientBuilder.builder() + .setSearchClientProvider(TransportSearchClientProvider.class) + .put("cluster.name", "es2") + .put("host", "atlas:9302") + .build(); + BufferedWriter writer = Files.newBufferedWriter(Paths.get("zdb.txt"))) { + Stream stream = searchClient.getIds(qb -> qb + .setIndices(indexDefinition.getIndex()) + .setQuery(QueryBuilders.matchAllQuery())); + stream.forEach(id -> { + try { + writer.write(id); + writer.newLine(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + } +}