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);
}
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;
}
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());
}
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());
}
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());
}
}
Aggregations