make forbiddenApisTest work for client and client-sniffer
This commit is contained in:
parent
cf6e713d77
commit
f38ce72004
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<String, List<String>> 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<String, List<String>> 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
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue