com.imperva.shcf4j.httpcomponents.client4.ClosableAsyncHttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shcf4j-httpcomponents-client4 Show documentation
Show all versions of shcf4j-httpcomponents-client4 Show documentation
The Simple HTTP Client Facade for Java (SHCF4J) serves as a simple facade or abstraction for various HTTP client frameworks (e.g. java.net.HttpURLConnection, Apache HttpClient, etc.) allowing the end user to plug in the desired HTTP client framework at deployment time.
package com.imperva.shcf4j.httpcomponents.client4;
import com.imperva.shcf4j.HttpHost;
import com.imperva.shcf4j.HttpRequest;
import com.imperva.shcf4j.HttpResponse;
import com.imperva.shcf4j.client.AsyncHttpClient;
import com.imperva.shcf4j.client.protocol.ClientContext;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
/**
* InternalHttpAsyncClient
*
* @author Maxim Kirilov
*/
class ClosableAsyncHttpClient implements AsyncHttpClient {
private final CloseableHttpAsyncClient asyncClient;
ClosableAsyncHttpClient(CloseableHttpAsyncClient asyncClient) {
this.asyncClient = asyncClient;
this.asyncClient.start();
}
@Override
public CompletableFuture execute(HttpHost target, HttpRequest request) {
return execute(target, request, null);
}
@Override
public CompletableFuture execute(HttpHost target, HttpRequest request, ClientContext ctx) {
final CompletableFuture cf = new CompletableFuture<>();
this.asyncClient.execute(
ConversionUtils.convert(target),
ConversionUtils.convert(request),
ConversionUtils.convert(ctx),
new FutureCallback() {
@Override
public void cancelled() {
cf.cancel(false);
}
@Override
public void completed(org.apache.http.HttpResponse httpResponse) {
cf.complete(ConversionUtils.convert(httpResponse));
}
@Override
public void failed(Exception ex) {
cf.completeExceptionally(ex);
}
});
return cf;
}
@Override
public void close() throws IOException {
this.asyncClient.close();
}
}