Search in sources :

Example 11 with CloseableHttpAsyncClient

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with CloseableHttpAsyncClient

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();
            }
        }
    });
}
Also used : Action0(rx.functions.Action0) ObservableHttpResponse(rx.apache.http.ObservableHttpResponse) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) IOException(java.io.IOException)

Example 13 with CloseableHttpAsyncClient

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);
}
Also used : CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) Before(org.junit.Before)

Example 14 with CloseableHttpAsyncClient

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();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpResponse(org.apache.http.HttpResponse) CatalogTracing(demo.jaxrs.tracing.server.CatalogTracing) IOException(java.io.IOException) IOException(java.io.IOException)

Example 15 with CloseableHttpAsyncClient

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;
}
Also used : CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient)

Aggregations

CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)34 HttpResponse (org.apache.http.HttpResponse)18 IOException (java.io.IOException)13 HttpGet (org.apache.http.client.methods.HttpGet)12 Test (org.junit.Test)8 HttpHost (org.apache.http.HttpHost)7 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)6 Future (java.util.concurrent.Future)5 InputStreamReader (java.io.InputStreamReader)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 FutureCallback (org.apache.http.concurrent.FutureCallback)4 HttpAsyncRequestProducer (org.apache.http.nio.protocol.HttpAsyncRequestProducer)4 Before (org.junit.Before)4 URL (java.net.URL)3 HttpEntity (org.apache.http.HttpEntity)3