Search in sources :

Example 41 with HttpRequest

use of org.apache.http.HttpRequest in project docker-java-api by amihaiemil.

the class UnixHttpClientTestCase method executesRequestWithHostHandlerAndContext.

/**
 * UnixHttpClient can execute the HttpRequest with the given host,
 * response handler and context.
 * @throws IOException If something goes wrong.
 */
@Test
public void executesRequestWithHostHandlerAndContext() throws IOException {
    final HttpHost host = new HttpHost("127.0.0.1");
    final HttpRequest req = Mockito.mock(HttpRequest.class);
    final ResponseHandler<String> handler = Mockito.mock(ResponseHandler.class);
    final HttpContext context = Mockito.mock(HttpContext.class);
    final HttpClient decorated = Mockito.mock(HttpClient.class);
    Mockito.when(decorated.execute(host, req, handler, context)).thenReturn("executed");
    final HttpClient unix = new UnixHttpClient(decorated);
    MatcherAssert.assertThat(unix.execute(host, req, handler, context), Matchers.equalTo("executed"));
    Mockito.verify(decorated, Mockito.times(1)).execute(host, req, handler, context);
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) HttpContext(org.apache.http.protocol.HttpContext) Test(org.junit.Test)

Example 42 with HttpRequest

use of org.apache.http.HttpRequest in project stocator by CODAIT.

the class SwiftConnectionManager method getRetryHandler.

/**
 * Creates custom retry handler to be used if HTTP exception happens
 *
 * @return retry handler
 */
private HttpRequestRetryHandler getRetryHandler() {
    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= connectionConfiguration.getExecutionCount()) {
                // Do not retry if over max retry count
                LOG.debug("Execution count {} is bigger then threashold. Stop", executionCount);
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                LOG.debug("NoHttpResponseException exception. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof UnknownHostException) {
                LOG.debug("UnknownHostException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof ConnectTimeoutException) {
                LOG.debug("ConnectTimeoutException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SocketTimeoutException || exception.getClass() == SocketTimeoutException.class || exception.getClass().isInstance(SocketTimeoutException.class)) {
                // Connection refused
                LOG.debug("socketTimeoutException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                // Timeout
                LOG.debug("InterruptedIOException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SSLException) {
                LOG.debug("SSLException Retry count {}", executionCount);
                return true;
            }
            HttpClientContext clientContext = HttpClientContext.adapt(context);
            HttpRequest request = clientContext.getRequest();
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                LOG.debug("HttpEntityEnclosingRequest. Retry count {}", executionCount);
                return true;
            }
            LOG.debug("Retry stopped. Retry count {}", executionCount);
            return false;
        }
    };
    return myRetryHandler;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpRequest(org.apache.http.HttpRequest) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) HttpContext(org.apache.http.protocol.HttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 43 with HttpRequest

use of org.apache.http.HttpRequest in project kafka by apache.

the class RestServerTest method testDisableAdminEndpoint.

@Test
public void testDisableAdminEndpoint() throws IOException {
    Map<String, String> configMap = new HashMap<>(baseWorkerProps());
    configMap.put(ADMIN_LISTENERS_CONFIG, "");
    DistributedConfig workerConfig = new DistributedConfig(configMap);
    doReturn(KAFKA_CLUSTER_ID).when(herder).kafkaClusterId();
    doReturn(plugins).when(herder).plugins();
    doReturn(Collections.emptyList()).when(plugins).newPlugins(Collections.emptyList(), workerConfig, ConnectRestExtension.class);
    server = new RestServer(workerConfig);
    server.initializeServer();
    server.initializeResources(herder);
    assertNull(server.adminUrl());
    HttpRequest request = new HttpGet("/admin/loggers");
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpHost httpHost = new HttpHost(server.advertisedUrl().getHost(), server.advertisedUrl().getPort());
    CloseableHttpResponse response = httpClient.execute(httpHost, request);
    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
}
Also used : HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 44 with HttpRequest

use of org.apache.http.HttpRequest in project kafka by apache.

the class RestServerTest method testStandaloneConfig.

@Test
public void testStandaloneConfig() throws IOException {
    Map<String, String> workerProps = baseWorkerProps();
    workerProps.put("offset.storage.file.filename", "/tmp");
    WorkerConfig workerConfig = new StandaloneConfig(workerProps);
    doReturn(KAFKA_CLUSTER_ID).when(herder).kafkaClusterId();
    doReturn(plugins).when(herder).plugins();
    doReturn(Collections.emptyList()).when(plugins).newPlugins(Collections.emptyList(), workerConfig, ConnectRestExtension.class);
    doReturn(Arrays.asList("a", "b")).when(herder).connectors();
    server = new RestServer(workerConfig);
    server.initializeServer();
    server.initializeResources(herder);
    HttpRequest request = new HttpGet("/connectors");
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpHost httpHost = new HttpHost(server.advertisedUrl().getHost(), server.advertisedUrl().getPort());
    CloseableHttpResponse response = httpClient.execute(httpHost, request);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) StandaloneConfig(org.apache.kafka.connect.runtime.standalone.StandaloneConfig) Test(org.junit.Test)

Example 45 with HttpRequest

use of org.apache.http.HttpRequest in project kafka by apache.

the class RestServerTest method checkCORSRequest.

public void checkCORSRequest(String corsDomain, String origin, String expectedHeader, String method) throws IOException {
    Map<String, String> workerProps = baseWorkerProps();
    workerProps.put(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG, corsDomain);
    workerProps.put(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG, method);
    WorkerConfig workerConfig = new DistributedConfig(workerProps);
    doReturn(KAFKA_CLUSTER_ID).when(herder).kafkaClusterId();
    doReturn(plugins).when(herder).plugins();
    doReturn(Collections.emptyList()).when(plugins).newPlugins(Collections.emptyList(), workerConfig, ConnectRestExtension.class);
    doReturn(Arrays.asList("a", "b")).when(herder).connectors();
    server = new RestServer(workerConfig);
    server.initializeServer();
    server.initializeResources(herder);
    HttpRequest request = new HttpGet("/connectors");
    request.addHeader("Referer", origin + "/page");
    request.addHeader("Origin", origin);
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpHost httpHost = new HttpHost(server.advertisedUrl().getHost(), server.advertisedUrl().getPort());
    CloseableHttpResponse response = httpClient.execute(httpHost, request);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    if (expectedHeader != null) {
        Assert.assertEquals(expectedHeader, response.getFirstHeader("Access-Control-Allow-Origin").getValue());
    }
    request = new HttpOptions("/connector-plugins/FileStreamSource/validate");
    request.addHeader("Referer", origin + "/page");
    request.addHeader("Origin", origin);
    request.addHeader("Access-Control-Request-Method", method);
    response = httpClient.execute(httpHost, request);
    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
    if (expectedHeader != null) {
        Assert.assertEquals(expectedHeader, response.getFirstHeader("Access-Control-Allow-Origin").getValue());
    }
    if (method != null) {
        Assert.assertEquals(method, response.getFirstHeader("Access-Control-Allow-Methods").getValue());
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpHost(org.apache.http.HttpHost) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpOptions(org.apache.http.client.methods.HttpOptions) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14