From f38ce72004b9041b9f3b54af5b011b43b48275e3 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 9 Jun 2016 23:44:34 +0200 Subject: [PATCH] make forbiddenApisTest work for client and client-sniffer --- client-sniffer/build.gradle | 10 +--- .../client/sniff/HostsSnifferTests.java | 45 +++++++++----- client/build.gradle | 5 -- .../client/RestClientIntegTests.java | 58 +++++++++++-------- .../client/SuppressForbidden.java | 34 ----------- 5 files changed, 66 insertions(+), 86 deletions(-) delete mode 100644 client/src/test/java/org/elasticsearch/client/SuppressForbidden.java diff --git a/client-sniffer/build.gradle b/client-sniffer/build.gradle index 7c6527abb4c..3f1b8c37765 100644 --- a/client-sniffer/build.gradle +++ b/client-sniffer/build.gradle @@ -51,14 +51,10 @@ forbiddenApisMain { signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] } -//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes -//excludes don't seem to work though and we don't want to have our own @SuppressForbidden -forbiddenApisTest.enabled=false - -//forbiddenApisTest { +forbiddenApisTest { //client does not depend on core, so only jdk signatures should be checked - //signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] -//} + signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] +} //JarHell is part of es core, which we don't want to pull in jarHell.enabled=false diff --git a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java index d714e89eeb3..4ff11a24ea5 100644 --- a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java +++ b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java @@ -31,6 +31,7 @@ import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpGet; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util.SuppressForbidden; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; @@ -54,6 +55,7 @@ import java.util.Set; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; +@SuppressForbidden(reason = "uses sun HttpServer") public class HostsSnifferTests extends LuceneTestCase { private int sniffRequestTimeout; @@ -109,26 +111,37 @@ public class HostsSnifferTests extends LuceneTestCase { } } - private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeout) throws IOException { + private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeoutMillis) throws IOException { HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); - httpServer.createContext("/_nodes/http", new HttpHandler() { - @Override - public void handle(HttpExchange httpExchange) throws IOException { - if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) { - if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeout + "ms")) { - String nodesInfoBody = sniffResponse.nodesInfoBody; - httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length()); - try (OutputStream out = httpExchange.getResponseBody()) { - out.write(nodesInfoBody.getBytes(Consts.UTF_8)); - return; - } + httpServer.createContext("/_nodes/http", new ResponseHandler(sniffTimeoutMillis, sniffResponse)); + return httpServer; + } + + @SuppressForbidden(reason = "uses sun HttpServer") + private static class ResponseHandler implements HttpHandler { + private final int sniffTimeoutMillis; + private final SniffResponse sniffResponse; + + ResponseHandler(int sniffTimeoutMillis, SniffResponse sniffResponse) { + this.sniffTimeoutMillis = sniffTimeoutMillis; + this.sniffResponse = sniffResponse; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) { + if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeoutMillis + "ms")) { + String nodesInfoBody = sniffResponse.nodesInfoBody; + httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length()); + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(nodesInfoBody.getBytes(Consts.UTF_8)); + return; } } - httpExchange.sendResponseHeaders(404, 0); - httpExchange.close(); } - }); - return httpServer; + httpExchange.sendResponseHeaders(404, 0); + httpExchange.close(); + } } private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException { diff --git a/client/build.gradle b/client/build.gradle index c947f94ff26..6cdc0d818ac 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -49,14 +49,9 @@ forbiddenApisMain { signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] } -//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes -//excludes don't seem to work though and we don't want to have our own @SuppressForbidden -forbiddenApisTest.enabled=false - forbiddenApisTest { //client does not depend on core, so only jdk signatures should be checked signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] - suppressAnnotations = ['**.SuppressForbidden'] } //JarHell is part of es core, which we don't want to pull in diff --git a/client/src/test/java/org/elasticsearch/client/RestClientIntegTests.java b/client/src/test/java/org/elasticsearch/client/RestClientIntegTests.java index 1505ccd55d2..36a7e45b728 100644 --- a/client/src/test/java/org/elasticsearch/client/RestClientIntegTests.java +++ b/client/src/test/java/org/elasticsearch/client/RestClientIntegTests.java @@ -32,6 +32,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util.SuppressForbidden; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -83,32 +84,41 @@ public class RestClientIntegTests extends LuceneTestCase { } private static void createStatusCodeContext(HttpServer httpServer, final int statusCode) { - httpServer.createContext("/" + statusCode, new HttpHandler() { - @Override - public void handle(HttpExchange httpExchange) throws IOException { - StringBuilder body = new StringBuilder(); - try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) { - char[] buffer = new char[256]; - int read; - while ((read = reader.read(buffer)) != -1) { - body.append(buffer, 0, read); - } + httpServer.createContext("/" + statusCode, new ResponseHandler(statusCode)); + } + + @SuppressForbidden(reason = "uses sun HttpServer") + private static class ResponseHandler implements HttpHandler { + private final int statusCode; + + ResponseHandler(int statusCode) { + this.statusCode = statusCode; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + StringBuilder body = new StringBuilder(); + try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) { + char[] buffer = new char[256]; + int read; + while ((read = reader.read(buffer)) != -1) { + body.append(buffer, 0, read); } - Headers requestHeaders = httpExchange.getRequestHeaders(); - Headers responseHeaders = httpExchange.getResponseHeaders(); - for (Map.Entry> header : requestHeaders.entrySet()) { - responseHeaders.put(header.getKey(), header.getValue()); - } - httpExchange.getRequestBody().close(); - httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length()); - if (body.length() > 0) { - try (OutputStream out = httpExchange.getResponseBody()) { - out.write(body.toString().getBytes(Consts.UTF_8)); - } - } - httpExchange.close(); } - }); + Headers requestHeaders = httpExchange.getRequestHeaders(); + Headers responseHeaders = httpExchange.getResponseHeaders(); + for (Map.Entry> header : requestHeaders.entrySet()) { + responseHeaders.put(header.getKey(), header.getValue()); + } + httpExchange.getRequestBody().close(); + httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length()); + if (body.length() > 0) { + try (OutputStream out = httpExchange.getResponseBody()) { + out.write(body.toString().getBytes(Consts.UTF_8)); + } + } + httpExchange.close(); + } } @AfterClass diff --git a/client/src/test/java/org/elasticsearch/client/SuppressForbidden.java b/client/src/test/java/org/elasticsearch/client/SuppressForbidden.java deleted file mode 100644 index 309cc666bc4..00000000000 --- a/client/src/test/java/org/elasticsearch/client/SuppressForbidden.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.client; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field. - */ -@Retention(RetentionPolicy.CLASS) -@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE }) -public @interface SuppressForbidden { - String reason(); -} \ No newline at end of file