fix HTTP client host/port parsing

This commit is contained in:
Jörg Prante 2021-04-21 13:17:07 +02:00
parent 870cc09767
commit 78e234e576
4 changed files with 112 additions and 4 deletions

View file

@ -166,6 +166,8 @@ public abstract class HttpAction<R extends ActionRequest, T extends ActionRespon
} catch (Throwable e) { } catch (Throwable e) {
// catch all kinds of errors in the entity parsing process // catch all kinds of errors in the entity parsing process
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
logger.error("status = " + httpActionContext.getHttpResponse().getStatus().getCode());
logger.error("body = " + httpActionContext.getHttpResponse().getBodyAsString(StandardCharsets.UTF_8));
return null; return null;
} }
} }

View file

@ -68,10 +68,10 @@ public class HttpClientHelper {
if (settings.hasValue("url")) { if (settings.hasValue("url")) {
this.url = settings.get("url"); this.url = settings.get("url");
httpAddress = HttpAddress.http1(this.url); httpAddress = HttpAddress.http1(this.url);
} else if (settings.hasValue("host") && settings.hasValue("post")) { } else if (settings.hasValue("host")) {
URL u = URL.http() // use only first host
.host(settings.get("host")).port(settings.getAsInt("port", 9200)) URL u = findAddresses(settings).stream().findFirst()
.build(); .orElseGet(() -> URL.http().host("localhost").port(9200).build());
httpAddress = HttpAddress.http1(u); httpAddress = HttpAddress.http1(u);
this.url = u.toExternalForm(); this.url = u.toExternalForm();
} else { } else {
@ -151,4 +151,27 @@ public class HttpClientHelper {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
} }
private List<URL> findAddresses(Settings settings) {
final int defaultPort = settings.getAsInt("port", 9200);
List<URL> 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;
}
} }

View file

@ -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<String> 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();
}
});
}
}
}

View file

@ -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<String> 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();
}
});
}
}
}