Example usage for org.apache.http.impl.nio.client HttpAsyncClients createDefault

List of usage examples for org.apache.http.impl.nio.client HttpAsyncClients createDefault

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client HttpAsyncClients createDefault.

Prototype

public static CloseableHttpAsyncClient createDefault() 

Source Link

Document

Creates CloseableHttpAsyncClient instance with default configuration.

Usage

From source file:com.vmware.photon.controller.client.RestClientTest.java

@Test
public void testAddAuthHeader() {
    RestClient restClient = new RestClient(endpoint, HttpAsyncClients.createDefault(), "shared-secret");
    HttpUriRequest request = new HttpGet(endpoint + path);
    request = restClient.addAuthHeader(request);
    assertEquals(request.getHeaders(RestClient.AUTHORIZATION_HEADER)[0].toString(),
            RestClient.AUTHORIZATION_HEADER + ": " + RestClient.AUTHORIZATION_METHOD + "shared-secret");
}

From source file:com.vmware.photon.controller.client.RestClientTest.java

@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "HTTP request failed with: 404")
public void testCheckResponseMatchFail() {
    RestClient restClient = new RestClient(endpoint, HttpAsyncClients.createDefault());

    HttpResponse httpResponse = mock(HttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(404);

    restClient.checkResponse(httpResponse, 200);
}

From source file:com.dilmus.dilshad.scabi.core.async.DComputeNoBlock.java

public DComputeNoBlock(String jsonString) throws IOException {

    m_djson = new DMJson(jsonString);
    m_computeHost = m_djson.getString("ComputeHost");
    m_computePort = m_djson.getString("ComputePort");

    try {/*from   w  ww . j  a  v  a  2s  .  c o m*/
        m_httpClient = HttpAsyncClients.createDefault();
        m_httpClient.start();
        m_target = new HttpHost(m_computeHost, Integer.parseInt(m_computePort), "http");
    } catch (Exception e) {
        //e.printStackTrace();
        if (null != m_httpClient)
            m_httpClient.close();
        throw e;
    }

    m_jsonString = jsonString;
    m_jsonStrInput = DMJsonHelper.empty();

    m_isFaulty = false;

    m_jarFilePathList = new ArrayList<String>();
}

From source file:com.vmware.photon.controller.client.RestClientTest.java

@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "HTTP request failed with: 400, Response Body")
public void testCheckResponseMatchFailWithBody() {
    RestClient restClient = new RestClient(endpoint, HttpAsyncClients.createDefault());

    HttpResponse httpResponse = mock(HttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(400);
    when(httpResponse.getEntity()).thenReturn(new HttpEntity() {
        @Override/*from  w  w  w .  j a  v  a2  s .c  om*/
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public boolean isChunked() {
            return false;
        }

        @Override
        public long getContentLength() {
            return 0;
        }

        @Override
        public Header getContentType() {
            return null;
        }

        @Override
        public Header getContentEncoding() {
            return null;
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            return new ByteArrayInputStream("Response Body".getBytes());
        }

        @Override
        public void writeTo(OutputStream outstream) throws IOException {
        }

        @Override
        public boolean isStreaming() {
            return false;
        }

        @Override
        public void consumeContent() throws IOException {
        }
    });

    restClient.checkResponse(httpResponse, 200);
}

From source file:com.dilmus.dilshad.scabi.core.async.DComputeNoBlock.java

public DComputeNoBlock(DMeta meta) throws Exception {

    String jsonCompute = null;//from  w ww . j a  v a2  s  . co  m
    try {
        m_httpClient = HttpAsyncClients.createDefault();
        m_httpClient.start();
        Future<HttpResponse> futureHttpResponse = computeAlloc(meta);
        HttpResponse httpResponse = DComputeNoBlock.get(futureHttpResponse);
        jsonCompute = DComputeNoBlock.getResult(httpResponse);
        m_djson = new DMJson(jsonCompute);
        m_computeHost = m_djson.getString("ComputeHost");
        m_computePort = m_djson.getString("ComputePort");
        m_target = new HttpHost(m_computeHost, Integer.parseInt(m_computePort), "http");
        m_jsonString = jsonCompute;
        m_meta = meta;
        m_jsonStrInput = DMJsonHelper.empty();

    } catch (Exception e) {
        //e.printStackTrace();
        if (null != m_httpClient)
            m_httpClient.close();
        throw e;
    }

    m_isFaulty = false;

    m_jarFilePathList = new ArrayList<String>();
}

From source file:org.opennms.newts.gsod.ImportRunner.java

private Observable<Boolean> restPoster(Observable<List<Sample>> samples, MetricRegistry metrics) {

    final CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
    httpClient.start();/*from ww  w.j  a va  2 s .com*/

    return samples

            // turn each batch into json
            .map(toJSON())

            // meter them as the go into the post code
            .map(meter(metrics.meter("posts"), String.class))

            // post the json to the REST server
            .mergeMap(postJSON(m_restUrl, httpClient))

            // meter the responses
            .map(meter(metrics.meter("responses"), ObservableHttpResponse.class))

            // count sample completions
            .map(meter(metrics.meter("samples-completed"), m_samplesPerBatch, ObservableHttpResponse.class))

            // make sure every request has a successful return code
            .all(successful())

            .doOnCompleted(new Action0() {

                @Override
                public void call() {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        System.err.println("Failed to close httpClient!");
                        e.printStackTrace();
                    }
                }

            });
}

From source file:org.apache.http.impl.client.cache.CachingHttpAsyncClient.java

public CachingHttpAsyncClient() throws IOReactorException {
    this(HttpAsyncClients.createDefault(), new BasicHttpCache(), CacheConfig.DEFAULT);
}

From source file:org.apache.http.impl.client.cache.CachingHttpAsyncClient.java

public CachingHttpAsyncClient(final CacheConfig config) throws IOReactorException {
    this(HttpAsyncClients.createDefault(), new BasicHttpCache(config), config);
}

From source file:org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService.java

private String sendCommandViaHTTP(final String deviceIp, int deviceServerPort, String callUrlPattern,
        boolean fireAndForgot) throws DeviceManagementException {

    if (deviceServerPort == 0) {
        deviceServerPort = 80;/*from  www .  j av a  2 s  . com*/
    }

    String responseMsg = "";
    String urlString = URL_PREFIX + deviceIp + ":" + deviceServerPort + callUrlPattern;

    if (log.isDebugEnabled()) {
        log.debug(urlString);
    }

    if (!fireAndForgot) {
        HttpURLConnection httpConnection = getHttpConnection(urlString);

        try {
            httpConnection.setRequestMethod(HttpMethod.GET);
        } catch (ProtocolException e) {
            String errorMsg = "Protocol specific error occurred when trying to set method to GET" + " for:"
                    + urlString;
            log.error(errorMsg);
            throw new DeviceManagementException(errorMsg, e);
        }

        responseMsg = readResponseFromGetRequest(httpConnection);

    } else {
        CloseableHttpAsyncClient httpclient = null;
        try {

            httpclient = HttpAsyncClients.createDefault();
            httpclient.start();
            HttpGet request = new HttpGet(urlString);
            final CountDownLatch latch = new CountDownLatch(1);
            Future<HttpResponse> future = httpclient.execute(request, new FutureCallback<HttpResponse>() {
                @Override
                public void completed(HttpResponse httpResponse) {
                    latch.countDown();
                }

                @Override
                public void failed(Exception e) {
                    latch.countDown();
                }

                @Override
                public void cancelled() {
                    latch.countDown();
                }
            });

            latch.await();

        } catch (InterruptedException e) {
            if (log.isDebugEnabled()) {
                log.debug("Sync Interrupted");
            }
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();

                }
            } catch (IOException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed on close");
                }
            }
        }

    }

    return responseMsg;
}