From 8fa068b6325125768b8243b128a9ea6c8c6ba52f Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 28 Sep 2018 18:37:42 -0400 Subject: [PATCH 1/3] Checkstyle: Catch wide snippets We use wrap code in `// tag` and `//end` to include it in our docs. Our current docs style wraps code snippets in a box that is only wide enough for 76 characters and adds a horizontal scroll bar for wider snippets which makes the snippet much harder to read. This adds a checkstyle check that looks for java code that is included in the docs and is wider than that 76 characters so all snippets fit into the box. It solves many of the failures that this catches but suppresses many more. I will clean those up in a follow up change. --- buildSrc/src/main/resources/checkstyle.xml | 14 ++ .../resources/checkstyle_suppressions.xml | 24 +++ .../RestClientDocumentation.java | 155 +++++++++++------- .../documentation/SnifferDocumentation.java | 52 +++--- .../low-level/configuration.asciidoc | 1 + .../qa/sql/jdbc/JdbcIntegrationTestCase.java | 3 +- .../qa/sql/jdbc/SimpleExampleTestCase.java | 14 +- 7 files changed, 171 insertions(+), 92 deletions(-) diff --git a/buildSrc/src/main/resources/checkstyle.xml b/buildSrc/src/main/resources/checkstyle.xml index 660c3661a4651..c6873b2c1277c 100644 --- a/buildSrc/src/main/resources/checkstyle.xml +++ b/buildSrc/src/main/resources/checkstyle.xml @@ -12,11 +12,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java b/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java index ce2e0907560cd..90801715b7e20 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java +++ b/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java @@ -45,6 +45,7 @@ import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; +import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; import javax.net.ssl.SSLContext; import java.io.IOException; @@ -93,8 +94,8 @@ public void testUsage() throws IOException, InterruptedException { //tag::rest-client-init RestClient restClient = RestClient.builder( - new HttpHost("localhost", 9200, "http"), - new HttpHost("localhost", 9201, "http")).build(); + new HttpHost("localhost", 9200, "http"), + new HttpHost("localhost", 9201, "http")).build(); //end::rest-client-init //tag::rest-client-close @@ -103,26 +104,30 @@ public void testUsage() throws IOException, InterruptedException { { //tag::rest-client-init-default-headers - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("header", "value")}; builder.setDefaultHeaders(defaultHeaders); // <1> //end::rest-client-init-default-headers } { //tag::rest-client-init-max-retry-timeout - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); builder.setMaxRetryTimeoutMillis(10000); // <1> //end::rest-client-init-max-retry-timeout } { //tag::rest-client-init-node-selector - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); builder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS); // <1> //end::rest-client-init-node-selector } { //tag::rest-client-init-allocation-aware-selector - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); builder.setNodeSelector(new NodeSelector() { // <1> @Override public void select(Iterable nodes) { @@ -155,7 +160,8 @@ public void select(Iterable nodes) { } { //tag::rest-client-init-failure-listener - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); builder.setFailureListener(new RestClient.FailureListener() { @Override public void onFailure(Node node) { @@ -166,24 +172,30 @@ public void onFailure(Node node) { } { //tag::rest-client-init-request-config-callback - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); - builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { - @Override - public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { - return requestConfigBuilder.setSocketTimeout(10000); // <1> - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); + builder.setRequestConfigCallback( + new RestClientBuilder.RequestConfigCallback() { + @Override + public RequestConfig.Builder customizeRequestConfig( + RequestConfig.Builder requestConfigBuilder) { + return requestConfigBuilder.setSocketTimeout(10000); // <1> + } + }); //end::rest-client-init-request-config-callback } { //tag::rest-client-init-client-config-callback - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); - builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - return httpClientBuilder.setProxy(new HttpHost("proxy", 9000, "http")); // <1> - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "http")); + builder.setHttpClientConfigCallback(new HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient( + HttpAsyncClientBuilder httpClientBuilder) { + return httpClientBuilder.setProxy( + new HttpHost("proxy", 9000, "http")); // <1> + } + }); //end::rest-client-init-client-config-callback } @@ -281,58 +293,74 @@ public void onFailure(Exception exception) { public void testCommonConfiguration() throws Exception { { //tag::rest-client-config-timeouts - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)) - .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200)) + .setRequestConfigCallback( + new RestClientBuilder.RequestConfigCallback() { @Override - public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { - return requestConfigBuilder.setConnectTimeout(5000) - .setSocketTimeout(60000); + public RequestConfig.Builder customizeRequestConfig( + RequestConfig.Builder requestConfigBuilder) { + return requestConfigBuilder + .setConnectTimeout(5000) + .setSocketTimeout(60000); } }) - .setMaxRetryTimeoutMillis(60000); + .setMaxRetryTimeoutMillis(60000); //end::rest-client-config-timeouts } { //tag::rest-client-config-threads - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)) - .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - return httpClientBuilder.setDefaultIOReactorConfig( - IOReactorConfig.custom().setIoThreadCount(1).build()); - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200)) + .setHttpClientConfigCallback(new HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient( + HttpAsyncClientBuilder httpClientBuilder) { + return httpClientBuilder.setDefaultIOReactorConfig( + IOReactorConfig.custom() + .setIoThreadCount(1) + .build()); + } + }); //end::rest-client-config-threads } { //tag::rest-client-config-basic-auth - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + final CredentialsProvider credentialsProvider = + new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, - new UsernamePasswordCredentials("user", "password")); + new UsernamePasswordCredentials("user", "password")); - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)) - .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200)) + .setHttpClientConfigCallback(new HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient( + HttpAsyncClientBuilder httpClientBuilder) { + return httpClientBuilder + .setDefaultCredentialsProvider(credentialsProvider); + } + }); //end::rest-client-config-basic-auth } { //tag::rest-client-config-disable-preemptive-auth - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + final CredentialsProvider credentialsProvider = + new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, - new UsernamePasswordCredentials("user", "password")); + new UsernamePasswordCredentials("user", "password")); - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)) - .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - httpClientBuilder.disableAuthCaching(); // <1> - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200)) + .setHttpClientConfigCallback(new HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient( + HttpAsyncClientBuilder httpClientBuilder) { + httpClientBuilder.disableAuthCaching(); // <1> + return httpClientBuilder + .setDefaultCredentialsProvider(credentialsProvider); + } + }); //end::rest-client-config-disable-preemptive-auth } { @@ -343,15 +371,18 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli try (InputStream is = Files.newInputStream(keyStorePath)) { truststore.load(is, keyStorePass.toCharArray()); } - SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null); + SSLContextBuilder sslBuilder = SSLContexts.custom() + .loadTrustMaterial(truststore, null); final SSLContext sslContext = sslBuilder.build(); - RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https")) - .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - return httpClientBuilder.setSSLContext(sslContext); - } - }); + RestClientBuilder builder = RestClient.builder( + new HttpHost("localhost", 9200, "https")) + .setHttpClientConfigCallback(new HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient( + HttpAsyncClientBuilder httpClientBuilder) { + return httpClientBuilder.setSSLContext(sslContext); + } + }); //end::rest-client-config-encrypted-communication } } diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/documentation/SnifferDocumentation.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/documentation/SnifferDocumentation.java index 5f305024dba20..70d7373dfc9eb 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/documentation/SnifferDocumentation.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/documentation/SnifferDocumentation.java @@ -56,8 +56,8 @@ public void testUsage() throws IOException { { //tag::sniffer-init RestClient restClient = RestClient.builder( - new HttpHost("localhost", 9200, "http")) - .build(); + new HttpHost("localhost", 9200, "http")) + .build(); Sniffer sniffer = Sniffer.builder(restClient).build(); //end::sniffer-init @@ -69,21 +69,23 @@ public void testUsage() throws IOException { { //tag::sniffer-interval RestClient restClient = RestClient.builder( - new HttpHost("localhost", 9200, "http")) - .build(); + new HttpHost("localhost", 9200, "http")) + .build(); Sniffer sniffer = Sniffer.builder(restClient) - .setSniffIntervalMillis(60000).build(); + .setSniffIntervalMillis(60000).build(); //end::sniffer-interval } { //tag::sniff-on-failure - SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener(); - RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)) - .setFailureListener(sniffOnFailureListener) // <1> - .build(); + SniffOnFailureListener sniffOnFailureListener = + new SniffOnFailureListener(); + RestClient restClient = RestClient.builder( + new HttpHost("localhost", 9200)) + .setFailureListener(sniffOnFailureListener) // <1> + .build(); Sniffer sniffer = Sniffer.builder(restClient) - .setSniffAfterFailureDelayMillis(30000) // <2> - .build(); + .setSniffAfterFailureDelayMillis(30000) // <2> + .build(); sniffOnFailureListener.setSniffer(sniffer); // <3> //end::sniff-on-failure } @@ -103,29 +105,29 @@ public void testUsage() throws IOException { { //tag::sniff-request-timeout RestClient restClient = RestClient.builder( - new HttpHost("localhost", 9200, "http")) - .build(); + new HttpHost("localhost", 9200, "http")) + .build(); NodesSniffer nodesSniffer = new ElasticsearchNodesSniffer( - restClient, - TimeUnit.SECONDS.toMillis(5), - ElasticsearchNodesSniffer.Scheme.HTTP); + restClient, + TimeUnit.SECONDS.toMillis(5), + ElasticsearchNodesSniffer.Scheme.HTTP); Sniffer sniffer = Sniffer.builder(restClient) - .setNodesSniffer(nodesSniffer).build(); + .setNodesSniffer(nodesSniffer).build(); //end::sniff-request-timeout } { //tag::custom-nodes-sniffer RestClient restClient = RestClient.builder( - new HttpHost("localhost", 9200, "http")) - .build(); + new HttpHost("localhost", 9200, "http")) + .build(); NodesSniffer nodesSniffer = new NodesSniffer() { - @Override - public List sniff() throws IOException { - return null; // <1> - } - }; + @Override + public List sniff() throws IOException { + return null; // <1> + } + }; Sniffer sniffer = Sniffer.builder(restClient) - .setNodesSniffer(nodesSniffer).build(); + .setNodesSniffer(nodesSniffer).build(); //end::custom-nodes-sniffer } } diff --git a/docs/java-rest/low-level/configuration.asciidoc b/docs/java-rest/low-level/configuration.asciidoc index aa4e843778a8f..b7da2b5ebccff 100644 --- a/docs/java-rest/low-level/configuration.asciidoc +++ b/docs/java-rest/low-level/configuration.asciidoc @@ -1,3 +1,4 @@ +[[java-rest-low-config]] == Common configuration As explained in <>, the `RestClientBuilder` diff --git a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java index 301e15c8efbd5..c6594d7205112 100644 --- a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java +++ b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/JdbcIntegrationTestCase.java @@ -61,7 +61,8 @@ protected Connection useDriverManager() throws SQLException { // tag::connect-dm String address = "jdbc:es://" + elasticsearchAddress; // <1> Properties connectionProperties = connectionProperties(); // <2> - Connection connection = DriverManager.getConnection(address, connectionProperties); + Connection connection = + DriverManager.getConnection(address, connectionProperties); // end::connect-dm assertNotNull("The timezone should be specified", connectionProperties.getProperty(JdbcConfiguration.TIME_ZONE)); return connection; diff --git a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java index f5d559d9bf0b3..cf3e2e6aea4e7 100644 --- a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java +++ b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java @@ -10,6 +10,8 @@ import java.sql.SQLException; import java.sql.Statement; +import static org.hamcrest.Matchers.containsString; + public class SimpleExampleTestCase extends JdbcIntegrationTestCase { public void testSimpleExample() throws Exception { index("library", builder -> { @@ -20,13 +22,17 @@ public void testSimpleExample() throws Exception { // tag::simple_example try (Statement statement = connection.createStatement(); ResultSet results = statement.executeQuery( - "SELECT name, page_count FROM library ORDER BY page_count DESC LIMIT 1")) { + " SELECT name, page_count" + + " FROM library" + + "ORDER BY page_count DESC" + + " LIMIT 1")) { assertTrue(results.next()); assertEquals("Don Quixote", results.getString(1)); assertEquals(1072, results.getInt(2)); - SQLException e = expectThrows(SQLException.class, () -> results.getInt(1)); - assertTrue(e.getMessage(), - e.getMessage().contains("Unable to convert value [Don Quixote] of type [VARCHAR] to an Integer")); + SQLException e = expectThrows(SQLException.class, () -> + results.getInt(1)); + assertThat(e.getMessage(), containsString("Unable to convert " + + "value [Don Quixote] of type [VARCHAR] to an Integer")); assertFalse(results.next()); } // end::simple_example From eebd65854e9d798a55885486fb9681524afec3f7 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 1 Oct 2018 09:52:07 -0400 Subject: [PATCH 2/3] Fixup --- buildSrc/src/main/resources/checkstyle_suppressions.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index 4ffb568eb1229..888bd4d1ad0c6 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -42,7 +42,11 @@ - + +