Search in sources :

Example 51 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 52 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 53 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)

Example 54 with HttpHost

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

the class RestClientBuilderTests method testBuild.

public void testBuild() throws IOException {
    try {
        RestClient.builder((HttpHost[]) null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("hosts must not be null", e.getMessage());
    }
    try {
        RestClient.builder();
        fail("should have failed");
    } catch (IllegalArgumentException e) {
        assertEquals("no hosts provided", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200), null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("host cannot be null", e.getMessage());
    }
    try (RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build()) {
        assertNotNull(restClient);
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setMaxRetryTimeoutMillis(randomIntBetween(Integer.MIN_VALUE, 0));
        fail("should have failed");
    } catch (IllegalArgumentException e) {
        assertEquals("maxRetryTimeoutMillis must be greater than 0", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setDefaultHeaders(null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("defaultHeaders must not be null", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setDefaultHeaders(new Header[] { null });
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("default header must not be null", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setFailureListener(null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("failureListener must not be null", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("httpClientConfigCallback must not be null", e.getMessage());
    }
    try {
        RestClient.builder(new HttpHost("localhost", 9200)).setRequestConfigCallback(null);
        fail("should have failed");
    } catch (NullPointerException e) {
        assertEquals("requestConfigCallback must not be null", e.getMessage());
    }
    int numNodes = randomIntBetween(1, 5);
    HttpHost[] hosts = new HttpHost[numNodes];
    for (int i = 0; i < numNodes; i++) {
        hosts[i] = new HttpHost("localhost", 9200 + i);
    }
    RestClientBuilder builder = RestClient.builder(hosts);
    if (randomBoolean()) {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder;
            }
        });
    }
    if (randomBoolean()) {
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                return requestConfigBuilder;
            }
        });
    }
    if (randomBoolean()) {
        int numHeaders = randomIntBetween(1, 5);
        Header[] headers = new Header[numHeaders];
        for (int i = 0; i < numHeaders; i++) {
            headers[i] = new BasicHeader("header" + i, "value");
        }
        builder.setDefaultHeaders(headers);
    }
    if (randomBoolean()) {
        builder.setMaxRetryTimeoutMillis(randomIntBetween(1, Integer.MAX_VALUE));
    }
    if (randomBoolean()) {
        String pathPrefix = (randomBoolean() ? "/" : "") + randomAsciiOfLengthBetween(2, 5);
        while (pathPrefix.length() < 20 && randomBoolean()) {
            pathPrefix += "/" + randomAsciiOfLengthBetween(3, 6);
        }
        builder.setPathPrefix(pathPrefix + (randomBoolean() ? "/" : ""));
    }
    try (RestClient restClient = builder.build()) {
        assertNotNull(restClient);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) BasicHeader(org.apache.http.message.BasicHeader) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) BasicHeader(org.apache.http.message.BasicHeader)

Example 55 with HttpHost

use of org.apache.http.HttpHost in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method setWxCpConfigStorage.

public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
    this.wxCpConfigStorage = wxConfigProvider;
    String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host();
    int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port();
    String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username();
    String http_proxy_password = wxCpConfigStorage.getHttp_proxy_password();
    if (StringUtils.isNotBlank(http_proxy_host)) {
        // 使用代理服务器
        if (StringUtils.isNotBlank(http_proxy_username)) {
            // 需要用户认证的代理服务器
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(http_proxy_host, http_proxy_port), new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
            httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        } else {
            // 无需用户认证的代理服务器
            httpClient = HttpClients.createDefault();
        }
        httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
    } else {
        httpClient = HttpClients.createDefault();
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

HttpHost (org.apache.http.HttpHost)258 HttpResponse (org.apache.http.HttpResponse)50 IOException (java.io.IOException)44 URI (java.net.URI)35 Header (org.apache.http.Header)35 HttpEntity (org.apache.http.HttpEntity)32 AuthScope (org.apache.http.auth.AuthScope)31 HttpGet (org.apache.http.client.methods.HttpGet)29 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)28 CredentialsProvider (org.apache.http.client.CredentialsProvider)28 HttpRequest (org.apache.http.HttpRequest)27 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)25 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)23 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23 HttpParams (org.apache.http.params.HttpParams)23 Test (org.junit.Test)23 URISyntaxException (java.net.URISyntaxException)21 ProtocolVersion (org.apache.http.ProtocolVersion)21 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)19 Before (org.junit.Before)18