`gradle check -xforbiddenPatterns` passes!

Hurray! Now we just have to keep it that way!

Original commit: elastic/x-pack-elasticsearch@98b8ede44e
This commit is contained in:
Nik Everett 2017-07-06 16:24:19 -04:00
parent fc0bce0a3e
commit edcc87e30e
8 changed files with 57 additions and 42 deletions

View File

@ -7,10 +7,12 @@ package org.elasticsearch.xpack.sql.cli.net.client;
import org.elasticsearch.xpack.sql.cli.CliConfiguration; import org.elasticsearch.xpack.sql.cli.CliConfiguration;
import org.elasticsearch.xpack.sql.net.client.ClientException; import org.elasticsearch.xpack.sql.net.client.ClientException;
import org.elasticsearch.xpack.sql.net.client.DataOutputConsumer; import org.elasticsearch.xpack.sql.net.client.JreHttpUrlConnection;
import org.elasticsearch.xpack.sql.net.client.jre.JreHttpUrlConnection;
import org.elasticsearch.xpack.sql.net.client.util.Bytes; 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.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.security.AccessController; import java.security.AccessController;
@ -42,11 +44,11 @@ class HttpClient {
} }
} }
Bytes put(DataOutputConsumer os) { Bytes put(CheckedConsumer<DataOutput, IOException> os) {
return put("", os); return put("", os);
} }
Bytes put(String path, DataOutputConsumer os) { Bytes put(String path, CheckedConsumer<DataOutput, IOException> os) {
try { try {
return AccessController.doPrivileged((PrivilegedAction<Bytes>) () -> { return AccessController.doPrivileged((PrivilegedAction<Bytes>) () -> {
return JreHttpUrlConnection.http(url(path), cfg, con -> { return JreHttpUrlConnection.http(url(path), cfg, con -> {

View File

@ -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.jdbc.JdbcException;
import org.elasticsearch.xpack.sql.jdbc.util.BytesArray; import org.elasticsearch.xpack.sql.jdbc.util.BytesArray;
import org.elasticsearch.xpack.sql.net.client.ClientException; import org.elasticsearch.xpack.sql.net.client.ClientException;
import org.elasticsearch.xpack.sql.net.client.DataOutputConsumer; import org.elasticsearch.xpack.sql.net.client.JreHttpUrlConnection;
import org.elasticsearch.xpack.sql.net.client.jre.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.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.security.AccessController; import java.security.AccessController;
@ -56,11 +58,11 @@ class HttpClient {
} }
} }
BytesArray put(DataOutputConsumer os) throws SQLException { BytesArray put(CheckedConsumer<DataOutput, IOException> os) throws SQLException {
return put("_jdbc?error_trace=true", os); // NOCOMMIT Do something with the error trace. Useful for filing bugs and debugging. 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<DataOutput, IOException> os) throws SQLException { // NOCOMMIT remove path?
try { try {
return AccessController.doPrivileged((PrivilegedAction<BytesArray>) () -> { return AccessController.doPrivileged((PrivilegedAction<BytesArray>) () -> {
return JreHttpUrlConnection.http(url(path), cfg, con -> { return JreHttpUrlConnection.http(url(path), cfg, con -> {

View File

@ -10,6 +10,7 @@ import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class BytesArray { public class BytesArray {
@ -140,7 +141,7 @@ public class BytesArray {
if (string == null) { if (string == null) {
return; return;
} }
add(string.getBytes(StringUtils.UTF_8)); add(string.getBytes(StandardCharsets.UTF_8));
} }
private void checkSize(int newcount) { private void checkSize(int newcount) {

View File

@ -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<DataOutput> {
default void accept(DataOutput out) {
try {
acceptThrows(out);
} catch (IOException ex) {
throw new ClientException(ex, "Cannot write request");
}
}
void acceptThrows(DataOutput out) throws IOException;
}

View File

@ -3,15 +3,14 @@
* or more contributor license agreements. Licensed under the Elastic License; * or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with 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.Bytes;
import org.elasticsearch.xpack.sql.net.client.util.CheckedConsumer;
import org.elasticsearch.xpack.sql.net.client.util.IOUtils; import org.elasticsearch.xpack.sql.net.client.util.IOUtils;
import java.io.Closeable; import java.io.Closeable;
import java.io.DataOutput;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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<DataOutput, IOException> doc) throws ClientException { // NOCOMMIT why is this called put when it is a post?
try { try {
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setDoOutput(true); con.setDoOutput(true);

View File

@ -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<T, E extends Exception> {
void accept(T t) throws E;
}

View File

@ -5,7 +5,7 @@
*/ */
package org.elasticsearch.xpack.sql.net.client.util; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -15,8 +15,6 @@ import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
public abstract class StringUtils { public abstract class StringUtils {
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final String EMPTY = ""; public static final String EMPTY = "";
public static final String SLASH = "/"; public static final String SLASH = "/";
public static final String PATH_TOP = ".."; public static final String PATH_TOP = "..";
@ -175,11 +173,11 @@ public abstract class StringUtils {
} }
public static String asUTFString(byte[] content, int offset, int length) { 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) { 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". // Based on "Algorithms on Strings, Trees and Sequences by Dan Gusfield".

View File

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