use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project zm-mailbox by Zimbra.
the class SoapHttpTransport method invokeAsync.
@Override
public Future<HttpResponse> invokeAsync(Element document, boolean raw, boolean noSession, String requestedAccountId, String changeToken, String tokenType, NotificationFormat nFormat, String curWaitSetID, FutureCallback<HttpResponse> cb) throws IOException {
HttpPost post = new HttpPost(getUriWithPath(document));
// Set user agent if it's specified.
String agentName = getUserAgentName();
if (agentName != null) {
String agentVersion = getUserAgentVersion();
if (agentVersion != null)
agentName += " " + agentVersion;
post.setHeader("User-Agent", agentName);
}
// request headers
post.setHeader("Content-Type", getRequestProtocol().getContentType());
if (getClientIp() != null) {
post.setHeader(RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp());
if (ZimbraLog.misc.isDebugEnabled()) {
ZimbraLog.misc.debug("set remote IP header [%s] to [%s]", RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp());
}
}
if (getRequestProtocol().hasSOAPActionHeader())
post.setHeader("SOAPAction", mUri);
if (mCustomHeaders != null) {
for (Map.Entry<String, String> entry : mCustomHeaders.entrySet()) post.setHeader(entry.getKey(), entry.getValue());
}
// SOAP message
Element soapReq = generateSoapMessage(document, raw, noSession, requestedAccountId, changeToken, tokenType, nFormat, curWaitSetID);
String soapMessage = SoapProtocol.toString(soapReq, getPrettyPrint());
post.setEntity(new ByteArrayEntity(soapMessage.getBytes("UTF-8")));
HttpClientContext context = HttpClientContext.create();
String host = post.getURI().getHost();
CookieStore cookieStore = HttpClientUtil.newCookieStore(getAuthToken(), host, isAdmin());
String trustedToken = getTrustedToken();
if (trustedToken != null) {
BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.COOKIE_ZM_TRUST_TOKEN, trustedToken);
cookie.setDomain(post.getURI().getHost());
cookie.setPath("/");
cookie.setSecure(false);
cookieStore.addCookie(cookie);
}
context.setCookieStore(cookieStore);
CloseableHttpAsyncClient httpClient = ZimbraHttpClientManager.getInstance().getInternalAsyncHttpClient();
return httpClient.execute(post, context, cb);
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project newts by OpenNMS.
the class ImportRunner method restPoster.
private Observable<Boolean> restPoster(Observable<List<Sample>> samples, MetricRegistry metrics) {
final CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
httpClient.start();
return samples.map(toJSON()).map(meter(metrics.meter("posts"), String.class)).mergeMap(postJSON(m_restUrl, httpClient)).map(meter(metrics.meter("responses"), ObservableHttpResponse.class)).map(meter(metrics.meter("samples-completed"), m_samplesPerBatch, ObservableHttpResponse.class)).all(successful()).doOnCompleted(new Action0() {
@Override
public void call() {
try {
httpClient.close();
} catch (IOException e) {
System.err.println("Failed to close httpClient!");
e.printStackTrace();
}
}
});
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project metrics by dropwizard.
the class InstrumentedHttpClientsTest method setUp.
@Before
public void setUp() throws Exception {
CloseableHttpAsyncClient chac = new InstrumentedNHttpClientBuilder(metricRegistry, metricNameStrategy).build();
chac.start();
asyncHttpClient = chac;
metricRegistry.addListener(registryListener);
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project cxf by apache.
the class ApacheHttpAsyncClient method main.
public static void main(final String[] args) throws Exception {
try (final CatalogTracing tracing = new CatalogTracing("catalog-client")) {
final CloseableHttpAsyncClient httpclient = TracingHttpAsyncClientBuilder.create(tracing.getHttpTracing()).build();
final HttpGet request = new HttpGet("http://localhost:9000/catalog");
request.setHeader("Accept", "application/json");
httpclient.start();
final Future<HttpResponse> response = httpclient.execute(request, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
try {
System.out.println(EntityUtils.toString(result.getEntity(), StandardCharsets.UTF_8));
} catch (final IOException ex) {
System.out.println(ex);
}
}
@Override
public void failed(Exception ex) {
System.out.println(ex);
}
@Override
public void cancelled() {
}
});
response.get(1000, TimeUnit.MILLISECONDS);
httpclient.close();
}
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project elasticsearch by elastic.
the class RestClientBuilder method build.
/**
* Creates a new {@link RestClient} based on the provided configuration.
*/
public RestClient build() {
if (failureListener == null) {
failureListener = new RestClient.FailureListener();
}
CloseableHttpAsyncClient httpClient = AccessController.doPrivileged(new PrivilegedAction<CloseableHttpAsyncClient>() {
@Override
public CloseableHttpAsyncClient run() {
return createHttpClient();
}
});
RestClient restClient = new RestClient(httpClient, maxRetryTimeout, defaultHeaders, hosts, pathPrefix, failureListener);
httpClient.start();
return restClient;
}
Aggregations