Search in sources :

Example 16 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ElasticsearchHostsSniffer method readHost.

private static HttpHost readHost(String nodeId, JsonParser parser, Scheme scheme) throws IOException {
    HttpHost httpHost = null;
    String fieldName = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            if ("http".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) {
                        URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
                        httpHost = new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), boundAddressAsURI.getScheme());
                    } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                        parser.skipChildren();
                    }
                }
            } else {
                parser.skipChildren();
            }
        }
    }
    //http section is not present if http is not enabled on the node, ignore such nodes
    if (httpHost == null) {
        logger.debug("skipping node [" + nodeId + "] with http disabled");
        return null;
    }
    return httpHost;
}
Also used : HttpHost(org.apache.http.HttpHost) URI(java.net.URI)

Example 17 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ElasticsearchHostsSniffer method readHosts.

private List<HttpHost> readHosts(HttpEntity entity) throws IOException {
    try (InputStream inputStream = entity.getContent()) {
        JsonParser parser = jsonFactory.createParser(inputStream);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("expected data to start with an object");
        }
        List<HttpHost> hosts = new ArrayList<>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                if ("nodes".equals(parser.getCurrentName())) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        JsonToken token = parser.nextToken();
                        assert token == JsonToken.START_OBJECT;
                        String nodeId = parser.getCurrentName();
                        HttpHost sniffedHost = readHost(nodeId, parser, this.scheme);
                        if (sniffedHost != null) {
                            logger.trace("adding node [" + nodeId + "]");
                            hosts.add(sniffedHost);
                        }
                    }
                } else {
                    parser.skipChildren();
                }
            }
        }
        return hosts;
    }
}
Also used : InputStream(java.io.InputStream) HttpHost(org.apache.http.HttpHost) ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 18 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ElasticsearchHostsSnifferTests method testConstructorValidation.

public void testConstructorValidation() throws IOException {
    try {
        new ElasticsearchHostsSniffer(null, 1, ElasticsearchHostsSniffer.Scheme.HTTP);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("restClient cannot be null", e.getMessage());
    }
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        try {
            new ElasticsearchHostsSniffer(restClient, 1, null);
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals(e.getMessage(), "scheme cannot be null");
        }
        try {
            new ElasticsearchHostsSniffer(restClient, RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0), ElasticsearchHostsSniffer.Scheme.HTTP);
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals(e.getMessage(), "sniffRequestTimeoutMillis must be greater than 0");
        }
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient)

Example 19 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class SnifferBuilderTests method testBuild.

public void testBuild() throws Exception {
    int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
    HttpHost[] hosts = new HttpHost[numNodes];
    for (int i = 0; i < numNodes; i++) {
        hosts[i] = new HttpHost("localhost", 9200 + i);
    }
    try (RestClient client = RestClient.builder(hosts).build()) {
        try {
            Sniffer.builder(null).build();
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("restClient cannot be null", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setHostsSniffer(null);
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("hostsSniffer cannot be null", e.getMessage());
        }
        try (Sniffer sniffer = Sniffer.builder(client).build()) {
            assertNotNull(sniffer);
        }
        SnifferBuilder builder = Sniffer.builder(client);
        if (getRandom().nextBoolean()) {
            builder.setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setHostsSniffer(new MockHostsSniffer());
        }
        try (Sniffer sniffer = builder.build()) {
            assertNotNull(sniffer);
        }
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient)

Example 20 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ResponseExceptionTests method testResponseException.

public void testResponseException() throws IOException {
    ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1);
    StatusLine statusLine = new BasicStatusLine(protocolVersion, 500, "Internal Server Error");
    HttpResponse httpResponse = new BasicHttpResponse(statusLine);
    String responseBody = "{\"error\":{\"root_cause\": {}}}";
    boolean hasBody = getRandom().nextBoolean();
    if (hasBody) {
        HttpEntity entity;
        if (getRandom().nextBoolean()) {
            entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON);
        } else {
            //test a non repeatable entity
            entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), ContentType.APPLICATION_JSON);
        }
        httpResponse.setEntity(entity);
    }
    RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
    HttpHost httpHost = new HttpHost("localhost", 9200);
    Response response = new Response(requestLine, httpHost, httpResponse);
    ResponseException responseException = new ResponseException(response);
    assertSame(response, responseException.getResponse());
    if (hasBody) {
        assertEquals(responseBody, EntityUtils.toString(responseException.getResponse().getEntity()));
    } else {
        assertNull(responseException.getResponse().getEntity());
    }
    String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri() + ": " + response.getStatusLine().toString();
    if (hasBody) {
        message += "\n" + responseBody;
    }
    assertEquals(message, responseException.getMessage());
}
Also used : HttpEntity(org.apache.http.HttpEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) InputStreamEntity(org.apache.http.entity.InputStreamEntity) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) BasicRequestLine(org.apache.http.message.BasicRequestLine) RequestLine(org.apache.http.RequestLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicRequestLine(org.apache.http.message.BasicRequestLine) HttpHost(org.apache.http.HttpHost)

Aggregations

HttpHost (org.apache.http.HttpHost)579 IOException (java.io.IOException)108 CredentialsProvider (org.apache.http.client.CredentialsProvider)101 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)98 AuthScope (org.apache.http.auth.AuthScope)97 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)95 HttpResponse (org.apache.http.HttpResponse)94 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)82 Test (org.junit.Test)78 URI (java.net.URI)67 HttpGet (org.apache.http.client.methods.HttpGet)66 Header (org.apache.http.Header)56 HttpRequest (org.apache.http.HttpRequest)53 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)52 HttpEntity (org.apache.http.HttpEntity)47 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)47 URISyntaxException (java.net.URISyntaxException)44 RequestConfig (org.apache.http.client.config.RequestConfig)44 BasicScheme (org.apache.http.impl.auth.BasicScheme)44 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)43