diff --git a/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/net/client/HttpClient.java b/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/net/client/HttpClient.java index a26e34275e5..643931c4d70 100644 --- a/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/net/client/HttpClient.java +++ b/sql/cli/src/main/java/org/elasticsearch/xpack/sql/cli/net/client/HttpClient.java @@ -7,10 +7,12 @@ package org.elasticsearch.xpack.sql.cli.net.client; import org.elasticsearch.xpack.sql.cli.CliConfiguration; import org.elasticsearch.xpack.sql.net.client.ClientException; -import org.elasticsearch.xpack.sql.net.client.DataOutputConsumer; -import org.elasticsearch.xpack.sql.net.client.jre.JreHttpUrlConnection; +import org.elasticsearch.xpack.sql.net.client.JreHttpUrlConnection; import org.elasticsearch.xpack.sql.net.client.util.Bytes; +import org.elasticsearch.xpack.sql.net.client.util.CheckedConsumer; +import java.io.DataOutput; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.security.AccessController; @@ -42,11 +44,11 @@ class HttpClient { } } - Bytes put(DataOutputConsumer os) { + Bytes put(CheckedConsumer os) { return put("", os); } - Bytes put(String path, DataOutputConsumer os) { + Bytes put(String path, CheckedConsumer os) { try { return AccessController.doPrivileged((PrivilegedAction) () -> { return JreHttpUrlConnection.http(url(path), cfg, con -> { diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/client/HttpClient.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/client/HttpClient.java index 79d53e70a64..a33cacf849a 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/client/HttpClient.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/client/HttpClient.java @@ -9,9 +9,11 @@ import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration; import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcException; import org.elasticsearch.xpack.sql.jdbc.util.BytesArray; import org.elasticsearch.xpack.sql.net.client.ClientException; -import org.elasticsearch.xpack.sql.net.client.DataOutputConsumer; -import org.elasticsearch.xpack.sql.net.client.jre.JreHttpUrlConnection; +import org.elasticsearch.xpack.sql.net.client.JreHttpUrlConnection; +import org.elasticsearch.xpack.sql.net.client.util.CheckedConsumer; +import java.io.DataOutput; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.security.AccessController; @@ -56,11 +58,11 @@ class HttpClient { } } - BytesArray put(DataOutputConsumer os) throws SQLException { + BytesArray put(CheckedConsumer os) throws SQLException { return put("_jdbc?error_trace=true", os); // NOCOMMIT Do something with the error trace. Useful for filing bugs and debugging. } - BytesArray put(String path, DataOutputConsumer os) throws SQLException { // NOCOMMIT remove path? + BytesArray put(String path, CheckedConsumer os) throws SQLException { // NOCOMMIT remove path? try { return AccessController.doPrivileged((PrivilegedAction) () -> { return JreHttpUrlConnection.http(url(path), cfg, con -> { diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java index 30e477898bf..8402b011efa 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java @@ -10,6 +10,7 @@ import org.elasticsearch.xpack.sql.net.client.util.StringUtils; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; public class BytesArray { @@ -140,7 +141,7 @@ public class BytesArray { if (string == null) { return; } - add(string.getBytes(StringUtils.UTF_8)); + add(string.getBytes(StandardCharsets.UTF_8)); } private void checkSize(int newcount) { diff --git a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/DataOutputConsumer.java b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/DataOutputConsumer.java deleted file mode 100644 index 1a4f1e37efa..00000000000 --- a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/DataOutputConsumer.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.net.client; - -import java.io.DataOutput; -import java.io.IOException; -import java.util.function.Consumer; - -public interface DataOutputConsumer extends Consumer { - - default void accept(DataOutput out) { - try { - acceptThrows(out); - } catch (IOException ex) { - throw new ClientException(ex, "Cannot write request"); - } - } - - void acceptThrows(DataOutput out) throws IOException; -} \ No newline at end of file diff --git a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/jre/JreHttpUrlConnection.java b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/JreHttpUrlConnection.java similarity index 92% rename from sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/jre/JreHttpUrlConnection.java rename to sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/JreHttpUrlConnection.java index ccc2b076b6f..34e1cd3df7d 100644 --- a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/jre/JreHttpUrlConnection.java +++ b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/JreHttpUrlConnection.java @@ -3,15 +3,14 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.xpack.sql.net.client.jre; +package org.elasticsearch.xpack.sql.net.client; -import org.elasticsearch.xpack.sql.net.client.ClientException; -import org.elasticsearch.xpack.sql.net.client.ConnectionConfiguration; -import org.elasticsearch.xpack.sql.net.client.DataOutputConsumer; import org.elasticsearch.xpack.sql.net.client.util.Bytes; +import org.elasticsearch.xpack.sql.net.client.util.CheckedConsumer; import org.elasticsearch.xpack.sql.net.client.util.IOUtils; import java.io.Closeable; +import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; @@ -57,7 +56,7 @@ public class JreHttpUrlConnection implements Closeable { } } - public Bytes put(DataOutputConsumer doc) throws ClientException { // NOCOMMIT why is this called put when it is a post? + public Bytes put(CheckedConsumer doc) throws ClientException { // NOCOMMIT why is this called put when it is a post? try { con.setRequestMethod("POST"); con.setDoOutput(true); diff --git a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/CheckedConsumer.java b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/CheckedConsumer.java new file mode 100644 index 00000000000..d00e4a6952b --- /dev/null +++ b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/CheckedConsumer.java @@ -0,0 +1,17 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.sql.net.client.util; + +import java.util.function.Consumer; + +/** + * A {@link Consumer}-like interface which allows throwing checked exceptions. + * Elasticsearch has one of these but we don't depend on Elasticsearch. + */ +@FunctionalInterface +public interface CheckedConsumer { + void accept(T t) throws E; +} diff --git a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/StringUtils.java b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/StringUtils.java index c3fbd521a40..565251c6cc4 100644 --- a/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/StringUtils.java +++ b/sql/net-client/src/main/java/org/elasticsearch/xpack/sql/net/client/util/StringUtils.java @@ -5,7 +5,7 @@ */ package org.elasticsearch.xpack.sql.net.client.util; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -15,8 +15,6 @@ import java.util.List; import java.util.StringTokenizer; public abstract class StringUtils { - - public static final Charset UTF_8 = Charset.forName("UTF-8"); public static final String EMPTY = ""; public static final String SLASH = "/"; public static final String PATH_TOP = ".."; @@ -175,11 +173,11 @@ public abstract class StringUtils { } public static String asUTFString(byte[] content, int offset, int length) { - return (content == null || length == 0 ? EMPTY : new String(content, offset, length, UTF_8)); + return (content == null || length == 0 ? EMPTY : new String(content, offset, length, StandardCharsets.UTF_8)); } public static byte[] toUTF(String string) { - return string.getBytes(UTF_8); + return string.getBytes(StandardCharsets.UTF_8); } // Based on "Algorithms on Strings, Trees and Sequences by Dan Gusfield". diff --git a/sql/net-client/src/test/java/org/elasticsearch/xpack/sql/net/client/util/StringUtilsTests.java b/sql/net-client/src/test/java/org/elasticsearch/xpack/sql/net/client/util/StringUtilsTests.java new file mode 100644 index 00000000000..da7c39c1c4c --- /dev/null +++ b/sql/net-client/src/test/java/org/elasticsearch/xpack/sql/net/client/util/StringUtilsTests.java @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.sql.net.client.util; + +import org.elasticsearch.test.ESTestCase; + +import static org.elasticsearch.xpack.sql.net.client.util.StringUtils.nullAsEmpty; + +public class StringUtilsTests extends ESTestCase { + public void testNullAsEmpty() { + assertEquals("", nullAsEmpty(null)); + assertEquals("", nullAsEmpty("")); + String rando = randomRealisticUnicodeOfCodepointLength(5); + assertEquals(rando, nullAsEmpty(rando)); + } +}