org.asynchttpclient.AsyncHttpClient

Here are the examples of the java api org.asynchttpclient.AsyncHttpClient taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

98 Examples 7

19 Source : HtmlAutoScrapperManagerBuilder.java
with MIT License
from whimtrip

/**
 * @param asyncHttpClient
 *         {@link AsyncHttpClient} from asynchttp lib. Default implementation uses
 *         {@link DefaultAsyncHttpClientConfig.Builder} builder clreplaced. You can provide
 *         an AsyncHttpClient with different default parameters if needed.
 * @return the same instance of {@link HtmlAutoScrapperManagerBuilder}.
 */
public HtmlAutoScrapperManagerBuilder setAsyncHttpClient(AsyncHttpClient asyncHttpClient) {
    this.asyncHttpClient = asyncHttpClient;
    return this;
}

19 Source : AutomaticScrapperManagerBuilder.java
with MIT License
from whimtrip

/**
 * @param asyncHttpClient
 *         {@link AsyncHttpClient} from asynchttp lib. Default implementation uses
 *         {@link DefaultAsyncHttpClientConfig.Builder} builder clreplaced. You can provide
 *         an AsyncHttpClient with different default parameters if needed.
 * @return the same instance of {@link AutomaticScrapperManagerBuilder}.
 */
public AutomaticScrapperManagerBuilder setAsyncHttpClient(AsyncHttpClient asyncHttpClient) {
    this.asyncHttpClient = asyncHttpClient;
    return this;
}

19 Source : VkStreamingApiClient.java
with MIT License
from VKCOM

public clreplaced VkStreamingApiClient implements Closeable {

    private static final int DEFAULT_MAX_BUFFER_SIZE = 1024000;

    private static final int DEFAULT_MAX_FRAME_SIZE = 1024000;

    private static final int DEFAULT_MAX_REQUEST_RETRY = 0;

    private Gson gson;

    private TransportClient transportClient;

    private AsyncHttpClient asyncHttpClient;

    public VkStreamingApiClient(TransportClient transportClient) {
        this(transportClient, new GsonBuilder().disableHtmlEscaping().create(), getDefaultAsyncClient());
    }

    public VkStreamingApiClient(TransportClient transportClient, Gson gson, AsyncHttpClient asyncHttpClient) {
        this.transportClient = transportClient;
        this.asyncHttpClient = asyncHttpClient;
        this.gson = gson;
    }

    public Gson getGson() {
        return gson;
    }

    public TransportClient getTransportClient() {
        return transportClient;
    }

    public AsyncHttpClient getAsyncHttpClient() {
        return asyncHttpClient;
    }

    public StreamingRules rules() {
        return new StreamingRules(this);
    }

    public StreamingStream stream() {
        return new StreamingStream(this);
    }

    @Override
    public void close() throws IOException {
        asyncHttpClient.close();
    }

    private static DefaultAsyncHttpClient getDefaultAsyncClient() {
        return new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().setMaxRequestRetry(DEFAULT_MAX_REQUEST_RETRY).setWebSocketMaxBufferSize(DEFAULT_MAX_BUFFER_SIZE).setWebSocketMaxFrameSize(DEFAULT_MAX_FRAME_SIZE).build());
    }
}

19 Source : MJpegHandler.java
with Apache License 2.0
from rc-dukes

/**
 * handler for MJPeg Streams
 * @author wf
 */
public clreplaced MJpegHandler {

    AsyncHttpClient asyncHttpClient;

    private PipedInputStream pipedInputStream;

    private PipedOutputStream pipedOutputStream;

    private BodyDeferringAsyncHandler outputHandler;

    BodyDeferringInputStream inputStream;

    public BodyDeferringInputStream getInputStream() {
        return inputStream;
    }

    /**
     * get an mjpeg stream from the given url
     *
     * @param url
     * @return - the MJPeg Stream
     * @throws Exception
     */
    public MJpegHandler(String url) throws Exception {
        // https://stackoverflow.com/a/50402629/1497139
        asyncHttpClient = asyncHttpClient();
        asyncHttpClient.prepareGet(url);
        pipedInputStream = new PipedInputStream();
        pipedOutputStream = new PipedOutputStream(pipedInputStream);
        outputHandler = new BodyDeferringAsyncHandler(pipedOutputStream);
        Future<Response> futureResponse = asyncHttpClient.prepareGet(url).execute(outputHandler);
        Response response = outputHandler.getResponse();
        if (response.getStatusCode() == 200) {
            inputStream = new BodyDeferringAsyncHandler.BodyDeferringInputStream(futureResponse, outputHandler, pipedInputStream);
        }
    }

    public MJpegHandler() {
    }

    /**
     * open me with the given bufferSize
     *
     * @param url
     * @return
     * @throws Exception
     */
    public MJpegDecoder open(int bufferSize) throws Exception {
        MJpegDecoder mjpegDecoder = new MJpegDecoder(this);
        mjpegDecoder.open(bufferSize);
        return mjpegDecoder;
    }

    /**
     * close this handler
     * @throws IOException
     */
    public void close() throws IOException {
        this.asyncHttpClient.close();
    }
}

19 Source : SampleMain.java
with Apache License 2.0
from provectus

public clreplaced SampleMain {

    final static AsyncHttpClient httpClient = asyncHttpClient(config());

    final static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        System.out.println("Starting generate sample data");
        SampleDataResult result = new SampleGenerator(50, 60, "https://x4e3w5eif9.execute-api.us-west-2.amazonaws.com/integration8009b5224f95441ab22", httpClient, objectMapper).generateSampleData();
        System.out.println("Data generation finished");
    }
}

19 Source : SampleGenerator.java
with Apache License 2.0
from provectus

public clreplaced SampleGenerator {

    private int minBids;

    private int maxBids;

    private String apiUrl;

    private AsyncHttpClient httpClient;

    private ObjectMapper objectMapper;

    public SampleGenerator(int minBids, int maxBids, String apiUrl, AsyncHttpClient httpClient, ObjectMapper objectMapper) {
        this.minBids = minBids;
        this.maxBids = maxBids;
        this.apiUrl = apiUrl;
        this.httpClient = httpClient;
        this.objectMapper = objectMapper;
    }

    <T> ListenableFuture<Response> sendRequest(String type, T model) throws JsonProcessingException {
        ListenableFuture<Response> future = httpClient.preparePost(String.format("%s/%s", apiUrl, type)).addHeader("Content-Type", "application/json").addHeader("User-Agent", USER_AGENT).setBody(objectMapper.writeValueAsBytes(model)).execute();
        return future;
    }

    int awaitSuccessfull(List<ListenableFuture<Response>> futures) {
        int n = 0;
        for (ListenableFuture<Response> future : futures) {
            try {
                Response response = future.get();
                // System.out.println(response.toString());
                if (response.getStatusCode() == 200) {
                    n++;
                }
            } catch (Throwable e) {
                System.out.println("ERROR: " + e.getMessage());
            }
        }
        return n;
    }

    SampleDataResult generateSampleData() throws JsonProcessingException {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int numberOfBids = random.nextInt(minBids, maxBids);
        int numberOfImps = random.nextInt(numberOfBids / 4, numberOfBids / 2);
        int numberOfClicks = random.nextInt(numberOfImps / 4, numberOfImps / 2);
        String domain = "www.google.com";
        String creativeCategory = "testCreativeCategory";
        String creativeId = UUID.randomUUID().toString();
        long campaignItemId = random.nextLong(1_000_000L, 2_000_000L);
        SampleDataResult result = new SampleDataResult(campaignItemId);
        Map<String, List<ListenableFuture<Response>>> futuresByType = new HashMap<>();
        for (int i = 0; i < numberOfBids; i++) {
            String txid = UUID.randomUUID().toString();
            String appuid = UUID.randomUUID().toString();
            long winPrice = random.nextInt(1_000, 2_000);
            BidBcn bid = BidBcn.builder().txId(txid).campaignItemId(campaignItemId).domain(domain).creativeId(creativeId).creativeCategory(creativeCategory).appUID(appuid).build();
            ImpressionBcn impressionBcn = new ImpressionBcn(txid, winPrice);
            ClickBcn clickBcn = new ClickBcn(txid);
            Location location = new Location(appuid, System.currentTimeMillis(), 55.796506, 49.108451);
            futuresByType.computeIfAbsent(BID_TYPE, (k) -> new ArrayList<>()).add(sendRequest(BID_TYPE, bid));
            futuresByType.computeIfAbsent(LOCATION_TYPE, (k) -> new ArrayList<>()).add(sendRequest(LOCATION_TYPE, location));
            if (numberOfImps > 0) {
                futuresByType.computeIfAbsent(IMP_TYPE, (k) -> new ArrayList<>()).add(sendRequest(IMP_TYPE, impressionBcn));
                numberOfImps--;
            }
            if (numberOfClicks > 0) {
                futuresByType.computeIfAbsent(CLICK_TYPE, (k) -> new ArrayList<>()).add(sendRequest(CLICK_TYPE, clickBcn));
                numberOfClicks--;
            }
        }
        result.setCountOfBids(awaitSuccessfull(futuresByType.get(BID_TYPE)));
        result.setCountOfImpressions(awaitSuccessfull(futuresByType.get(IMP_TYPE)));
        result.setCountOfClicks(awaitSuccessfull(futuresByType.get(CLICK_TYPE)));
        awaitSuccessfull(futuresByType.get(LOCATION_TYPE));
        return result;
    }
}

19 Source : AbstarctIntegration.java
with Apache License 2.0
from provectus

abstract clreplaced AbstarctIntegration {

    static final String URL_FOR_API = "UrlForAPI";

    static final String URL_FOR_REPORTS = "UrlForReports";

    static final String URL_FOR_PREDICTIONS = "UrlForPredictions";

    static CloudFormation cloudFormation;

    static String reportUrl;

    static String apiUrl;

    static String predictionsUrl;

    final AsyncHttpClient httpClient = asyncHttpClient(config());

    final ObjectMapper objectMapper = new ObjectMapper();

    SampleDataResult generateSampleData(int minBids, int maxBids) throws JsonProcessingException {
        return new SampleGenerator(minBids, maxBids, apiUrl, httpClient, objectMapper).generateSampleData();
    }
}

19 Source : AsyncHttpCf.java
with GNU General Public License v3.0
from javasync

public static CompletableFuture<Long> fetchAndCountLines(String url) {
    AsyncHttpClient ahc = Dsl.asyncHttpClient();
    return ahc.prepareGet(url).execute().toCompletableFuture().thenApply(Response::getResponseBodyreplacedtream).thenApply(in -> new BufferedReader(new InputStreamReader(in))).thenApply(reader -> reader.lines().count()).whenComplete((body, excep) -> close(ahc));
}

19 Source : AsyncHttpCf.java
with GNU General Public License v3.0
from javasync

private static void close(AsyncHttpClient ahc) {
    try {
        ahc.close();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

19 Source : WeatherWebApi.java
with GNU General Public License v3.0
from isel-leic-mpd

public clreplaced WeatherWebApi implements AutoCloseable {

    final String HOST = "http://api.worldweatheronline.com/premium/v1/";

    final String PATH_PAST_WEATHER = "past-weather.ashx?q=%s,%s&date=%s&enddate=%s&tp=24&format=json&key=%s";

    final String PATH_SEARCH = "search.ashx?query=%s&format=json&key=%s";

    final String WEATHER_KEY = "da5f2b8cc0a84eef8c6173655190905";

    private final Gson gson = new Gson();

    private final AsyncHttpClient ahc = Dsl.asyncHttpClient();

    public CompletableFuture<Stream<WeatherInfo>> pastWeather(double lat, double log, LocalDate from, LocalDate to) {
        String path = HOST + String.format(PATH_PAST_WEATHER, lat, log, from, to, WEATHER_KEY);
        System.out.println(path);
        return ahc.prepareGet(path).execute().toCompletableFuture().thenApply(Response::getResponseBody).thenApply(body -> {
            PastWeatherDto dto = gson.fromJson(body, PastWeatherDto.clreplaced);
            return fromDtoToWeatherInfo(dto);
        });
    }

    private static Stream<WeatherInfo> fromDtoToWeatherInfo(PastWeatherDto dto) {
        PastWeatherDataWeatherDto[] weather = dto.getData().getWeather();
        return Stream.of(weather).map(w -> {
            var h = w.getHourly()[0];
            return new WeatherInfo(LocalDate.parse(w.getDate()), h.getTempC(), h.getPrecipMM(), h.getWeatherDesc());
        });
    }

    @Override
    public void close() throws Exception {
        ahc.close();
    }
}

19 Source : WeatherWebApi.java
with GNU General Public License v3.0
from isel-leic-mpd

public clreplaced WeatherWebApi implements AutoCloseable {

    final String HOST = "http://api.worldweatheronline.com/premium/v1/";

    final String PATH_PAST_WEATHER = "past-weather.ashx?q=%s,%s&date=%s&enddate=%s&tp=24&format=json&key=%s";

    final String PATH_SEARCH = "search.ashx?query=%s&format=json&key=%s";

    final String WEATHER_KEY = "da5f2b8cc0a84eef8c6173655190905";

    private final Gson gson = new Gson();

    private final AsyncHttpClient ahc = Dsl.asyncHttpClient();

    public CompletableFuture<Stream<WeatherInfo>> pastWeather(double lat, double log, LocalDate from, LocalDate to) throws IOException {
        String path = HOST + String.format(PATH_PAST_WEATHER, lat, log, from, to, WEATHER_KEY);
        System.out.println(path);
        return ahc.prepareGet(path).execute().toCompletableFuture().thenApply(Response::getResponseBody).thenApply(body -> {
            PastWeatherDto dto = gson.fromJson(body, PastWeatherDto.clreplaced);
            return fromDtoToWeatherInfo(dto);
        });
    }

    private static Stream<WeatherInfo> fromDtoToWeatherInfo(PastWeatherDto dto) {
        PastWeatherDataWeatherDto[] weather = dto.getData().getWeather();
        return Stream.of(weather).map(w -> {
            var h = w.getHourly()[0];
            return new WeatherInfo(LocalDate.parse(w.getDate()), h.getTempC(), h.getPrecipMM(), h.getWeatherDesc());
        });
    }

    @Override
    public void close() throws Exception {
        ahc.close();
    }
}

19 Source : WeatherWebApi.java
with GNU General Public License v3.0
from isel-leic-mpd

public static CompletableFuture<List<WeatherInfo>> pastWeather(double lat, double log, LocalDate from, LocalDate to) throws IOException {
    String path = HOST + String.format(PATH_PAST_WEATHER, lat, log, from, to, WEATHER_KEY);
    AsyncHttpClient ahc = Dsl.asyncHttpClient();
    return ahc.prepareGet(path).execute().toCompletableFuture().thenApply(Response::getResponseBody).whenComplete((body, err) -> close(ahc)).thenApply(body -> {
        PastWeatherDto dto = gson.fromJson(body, PastWeatherDto.clreplaced);
        return fromDtoToWeatherInfo(dto);
    });
}

19 Source : WeatherWebApi.java
with GNU General Public License v3.0
from isel-leic-mpd

private static void close(AsyncHttpClient ahc) {
    try {
        ahc.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

19 Source : Fetcher.java
with GNU General Public License v3.0
from isel-leic-mpd

public clreplaced Fetcher implements AutoCloseable {

    final AsyncHttpClient asyncHttpClient = Dsl.asyncHttpClient();

    public CompletableFuture<Integer> fetchAndSumBodies(Path folder) throws IOException {
        return Files.walk(// Stream<Path>
        folder).filter(// Stream<Path>
        Files::isRegularFile).map(// Stream<CF<String>>
        path -> AsyncFiles.readAll(path)).map(// Stream<Cf<Integer>
        cf -> cf.thenCompose(url -> fetchBodyLength(url))).reduce((prev, curr) -> prev.thenCombine(curr, (l1, l2) -> l1 + l2)).get();
    }

    public CompletableFuture<Integer> fetchAndSumBodies(String... urls) {
        return Stream.of(urls).map(this::fetchBodyLength).reduce((prev, curr) -> prev.thenCombine(curr, (l1, l2) -> l1 + l2)).get();
    }

    private CompletableFuture<Integer> fetchBodyLength(String url) {
        System.out.println(url);
        return asyncHttpClient.prepareGet(url).execute().toCompletableFuture().thenApply(Response::getResponseBody).thenApply(body -> body.length());
    }

    @Override
    public void close() throws Exception {
        asyncHttpClient.close();
    }
}

19 Source : HttpRequest.java
with GNU General Public License v3.0
from isel-leic-mpd

static void closeAHC(AsyncHttpClient client) {
    try {
        client.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

19 Source : HttpRequest.java
with GNU General Public License v3.0
from isel-leic-mpd

@Override
public CompletableFuture<String> getContent(String uri) {
    AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
    return asyncHttpClient.prepareGet(uri).execute().toCompletableFuture().thenApply(// CF<String>
    Response::getResponseBody).whenComplete(// CF<String>
    (res, ex) -> closeAHC(asyncHttpClient));
/* <=>
                .thenApply(body -> {
                    closeAHC(asyncHttpClient);
                    return body;
                }); // CF<String>
                */
}

19 Source : App.java
with GNU General Public License v3.0
from isel-leic-mpd

static void getAsync(int leagueId) throws Exception {
    System.out.println("...... Requesting " + leagueId);
    String path = String.format(FOOTBALL_URI, leagueId);
    // 
    // Prepare Request
    // 
    AsyncHttpClient asyncHttpClient = asyncHttpClient();
    asyncHttpClient.prepareGet(path).addHeader("X-Auth-Token", "f8ca52bbe7a7436b832e939ed704394f").execute().toCompletableFuture().thenApply(Response::getResponseBody).whenComplete((res, ex) -> closeAHC(asyncHttpClient)).thenApply(App::fromJson).thenAccept(league -> System.out.printf("> %s --- leader: %s\n", league.leagueCaption, league.standing[0].teamName));
}

19 Source : App.java
with GNU General Public License v3.0
from isel-leic-mpd

static void getAsyncBad(int leagueId) throws Exception {
    System.out.println("...... Requesting " + leagueId);
    String path = String.format(FOOTBALL_URI, leagueId);
    // 
    // Prepare Request
    // 
    AsyncHttpClient asyncHttpClient = asyncHttpClient();
    CompletableFuture<Response> whenResponse = asyncHttpClient.prepareGet(path).addHeader("X-Auth-Token", "f8ca52bbe7a7436b832e939ed704394f").execute().toCompletableFuture();
    // 
    // Parse Response => MAL Bloqueante
    // 
    String content = whenResponse.join().getResponseBody();
    closeAHC(asyncHttpClient);
    LeagueTable league = fromJson(content);
    System.out.printf("> %s --- leader: %s\n", league.leagueCaption, league.standing[0].teamName);
}

19 Source : App.java
with GNU General Public License v3.0
from isel-leic-mpd

static void getAsync(int leagueId) throws Exception {
    System.out.println("...... Requesting " + leagueId);
    String path = String.format(FOOTBALL_URI, leagueId);
    // 
    // Prepare Request
    // 
    AsyncHttpClient asyncHttpClient = asyncHttpClient();
    asyncHttpClient.prepareGet(path).addHeader("X-Auth-Token", "f8ca52bbe7a7436b832e939ed704394f").execute().toCompletableFuture().whenComplete((res, ex) -> {
        // 
        // Parse Response
        // 
        String content = res.getResponseBody();
        closeAHC(asyncHttpClient);
        LeagueTable league = fromJson(content);
        System.out.printf("> %s --- leader: %s\n", league.leagueCaption, league.standing[0].teamName);
    });
}

19 Source : AsyncHttpRequest.java
with GNU General Public License v3.0
from isel-leic-mpd

public clreplaced AsyncHttpRequest implements AsyncRequest {

    AsyncHttpClient ahc = Dsl.asyncHttpClient();

    @Override
    public CompletableFuture<Reader> getReader(String path) {
        return ahc.prepareGet(path).execute().toCompletableFuture().thenApply(Response::getResponseBodyreplacedtream).thenApply(InputStreamReader::new);
    }

    @Override
    public void close() throws Exception {
        if (ahc != null)
            ahc.close();
    }
}

19 Source : HttpRequest.java
with GNU General Public License v3.0
from isel-leic-mpd

public clreplaced HttpRequest implements Request {

    private AsyncHttpClient asyncHttpClient;

    public HttpRequest() {
        this.asyncHttpClient = asyncHttpClient();
    }

    @Override
    public CompletableFuture<String> getContent(String url) throws IOException {
        return asyncHttpClient.prepareGet(url).execute().toCompletableFuture().thenApply(Response::getResponseBody);
    }
}

19 Source : AsyncHttpClientServiceShould.java
with Apache License 2.0
from Hakky54

@ExtendWith(MockitoExtension.clreplaced)
clreplaced AsyncHttpClientServiceShould {

    @InjectMocks
    private AsyncHttpClientService victim;

    @Mock
    private AsyncHttpClient httpClient;

    @Test
    @SuppressWarnings("unchecked")
    void executeRequest() throws Exception {
        Response response = mock(Response.clreplaced);
        ListenableFuture<Response> listenableFuture = mock(ListenableFuture.clreplaced);
        when(httpClient.executeRequest(any(RequestBuilder.clreplaced))).thenReturn(listenableFuture);
        when(listenableFuture.toCompletableFuture()).thenReturn(CompletableFuture.completedFuture(response));
        when(response.getStatusCode()).thenReturn(200);
        when(response.getResponseBody()).thenReturn("Hello");
        ArgumentCaptor<RequestBuilder> requestBuilderArgumentCaptor = ArgumentCaptor.forClreplaced(RequestBuilder.clreplaced);
        ClientResponse clientResponse = victim.executeRequest(HTTP_URL);
        replacedertThat(clientResponse.getStatusCode()).isEqualTo(200);
        replacedertThat(clientResponse.getResponseBody()).isEqualTo("Hello");
        verify(httpClient, times(1)).executeRequest(requestBuilderArgumentCaptor.capture());
        Request request = requestBuilderArgumentCaptor.getValue().build();
        replacedertThat(request.getUrl()).isEqualTo(HTTP_URL);
        replacedertThat(request.getMethod()).isEqualTo(GET_METHOD);
        replacedertThat(request.getHeaders().get(HEADER_KEY_CLIENT_TYPE)).isEqualTo(ASYNC_HTTP_CLIENT.getValue());
    }
}

19 Source : AsyncHttpClientService.java
with Apache License 2.0
from Hakky54

@Service
public clreplaced AsyncHttpClientService implements RequestService {

    private static final int TIMEOUT_AMOUNT_IN_SECONDS = 5;

    private final AsyncHttpClient httpClient;

    public AsyncHttpClientService(AsyncHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    @Override
    public ClientResponse executeRequest(String url) throws Exception {
        RequestBuilder requestBuilder = new RequestBuilder().setUrl(url).setHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());
        Response response = httpClient.executeRequest(requestBuilder).toCompletableFuture().get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS);
        return new ClientResponse(response.getResponseBody(), response.getStatusCode());
    }

    @Override
    public ClientType getClientType() {
        return ASYNC_HTTP_CLIENT;
    }
}

19 Source : HttpClient.java
with Apache License 2.0
from galeb

@Service
@Scope("prototype")
public clreplaced HttpClient {

    private static final boolean KEEP_ALIVE = false;

    private static final boolean FOLLOW_REDIRECT = false;

    private static final int TIMEOUT = 10000;

    private final AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setUseLaxCookieEncoder(true).setFollowRedirect(FOLLOW_REDIRECT).setKeepAlive(KEEP_ALIVE).setConnectTimeout(TIMEOUT).setPooledConnectionIdleTimeout(1).setMaxConnectionsPerHost(1).build());

    public Response get(String url) throws InterruptedException, ExecutionException {
        return asyncHttpClient.prepareGet(url).execute().get();
    }

    public Response execute(RequestBuilder builder) throws InterruptedException, java.util.concurrent.ExecutionException {
        Request request = builder.build();
        ListenableFuture<Response> listenableFuture = asyncHttpClient.executeRequest(request);
        return listenableFuture.get();
    }
}

19 Source : TestAzureAsyncReader.java
with Apache License 2.0
from dremio

@Test
public void testAsyncReaderWithRandomCharacterInPath() {
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    try {
        LocalDateTime versionDate = LocalDateTime.now(ZoneId.of("GMT")).minusDays(2);
        AzureAsyncReader azureAsyncReader = new AzureAsyncReader("account", new Path("/testdir/$#%&New Folder to test abc 123/0_0_0.parquet"), getMockAuthTokenProvider(), String.valueOf(versionDate.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()), false, client);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}

19 Source : TestAzureAsyncReader.java
with Apache License 2.0
from dremio

AzureAsyncReader getReader(String version, boolean isSecure, AsyncHttpClient client) {
    return spy(new AzureAsyncReader("account", new Path("container/directory/file_00.parquet"), getMockAuthTokenProvider(), version, isSecure, client));
}

19 Source : TestAzureAsyncReader.java
with Apache License 2.0
from dremio

@Test
public void testAsyncHttpClientClosedError() {
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    when(client.isClosed()).thenReturn(true);
    LocalDateTime versionDate = LocalDateTime.now(ZoneId.of("GMT")).minusDays(2);
    AzureAsyncReader azureAsyncReader = spy(new AzureAsyncReader("account", new Path("container/directory/file_00.parquet"), getMockAuthTokenProvider(), String.valueOf(versionDate.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()), false, client));
    try {
        azureAsyncReader.readFully(0, Unpooled.buffer(1), 0, 1);
        fail("Operation shouldn't proceed if client is closed");
    } catch (RuntimeException e) {
        replacedertEquals("AsyncHttpClient is closed", e.getMessage());
    }
}

19 Source : TestAzureAsyncReader.java
with Apache License 2.0
from dremio

private void testSuccessHttpMode(boolean isSecure, boolean checkVersion) {
    // Prepare response
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    HttpResponseStatus status = mock(HttpResponseStatus.clreplaced);
    when(status.getStatusCode()).thenReturn(206);
    CompletableFuture<Response> future = CompletableFuture.completedFuture(response);
    ListenableFuture<Response> resFuture = mock(ListenableFuture.clreplaced);
    when(resFuture.toCompletableFuture()).thenReturn(future);
    LocalDateTime versionDate = LocalDateTime.now(ZoneId.of("GMT")).minusDays(2);
    byte[] responseBytes = getRandomBytes(20);
    HttpResponseBodyPart responsePart = mock(HttpResponseBodyPart.clreplaced);
    when(responsePart.getBodyByteBuffer()).thenReturn(ByteBuffer.wrap(responseBytes));
    when(client.executeRequest(any(Request.clreplaced), any(AsyncCompletionHandler.clreplaced))).then(invocationOnMock -> {
        // Validate URL
        Request req = invocationOnMock.getArgument(0, Request.clreplaced);
        String expectedPrefix = isSecure ? "https" : "http";
        replacedertEquals("Invalid request url", expectedPrefix + "://account.dfs.core.windows.net/container/directory%2Ffile_00.parquet", req.getUrl());
        // Validate Headers
        replacedertEquals("Invalid blob range header", "bytes=0-19", req.getHeaders().get("Range"));
        LocalDateTime dateHeaderVal = LocalDateTime.parse(req.getHeaders().get("Date"), DATE_RFC1123_FORMATTER);
        long dateHeaderDiffInSecs = Math.abs(dateHeaderVal.until(LocalDateTime.now(ZoneId.of("GMT")), ChronoUnit.SECONDS));
        replacedertTrue("Date header not set correctly", dateHeaderDiffInSecs < 2);
        if (checkVersion) {
            LocalDateTime versionHeaderVal = LocalDateTime.parse(req.getHeaders().get("If-Unmodified-Since"), DATE_RFC1123_FORMATTER);
            replacedertEquals("Version header not set correctly", 0, versionHeaderVal.until(versionDate, ChronoUnit.SECONDS));
        }
        replacedertEquals("Authz header not set correctly", req.getHeaders().get("Authorization"), "Bearer testtoken");
        replacedertNotNull(req.getHeaders().get("x-ms-client-request-id"));
        // Fill in response
        AsyncCompletionHandler<Response> responseHandler = invocationOnMock.getArgument(1, AsyncCompletionHandler.clreplaced);
        replacedertEquals(responseHandler.getClreplaced(), BufferBasedCompletionHandler.clreplaced);
        responseHandler.onBodyPartReceived(responsePart);
        responseHandler.onStatusReceived(status);
        responseHandler.onCompleted(response);
        return resFuture;
    });
    AzureAsyncReader azureAsyncReader;
    if (checkVersion) {
        azureAsyncReader = getReader(String.valueOf(versionDate.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()), isSecure, client);
    } else {
        azureAsyncReader = getReader("0", isSecure, client);
    }
    try {
        ByteBuf buf = Unpooled.buffer(20);
        azureAsyncReader.readFully(0, buf, 0, 20).get();
        replacedertEquals(new String(buf.array()), new String(responseBytes));
        verify(azureAsyncReader).read(0, buf, 0, 20, 0);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}

19 Source : TestAzureAsyncContainerProvider.java
with Apache License 2.0
from dremio

@Test
public void testListContainersWithRetry() throws IOException, ExecutionException, InterruptedException {
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    when(response.getHeader(any(String.clreplaced))).thenReturn("");
    HttpResponseStatus status = mock(HttpResponseStatus.clreplaced);
    when(status.getStatusCode()).thenReturn(206);
    HttpResponseStatus failedStatus = mock(HttpResponseStatus.clreplaced);
    when(failedStatus.getStatusCode()).thenReturn(500);
    byte[] dfsCoreResponseBodyBytes = readStaticResponse("dfs-core-container-response-page1.json");
    RandomBytesResponseDispatcher dfsCoreResponseDispatcher = new RandomBytesResponseDispatcher(dfsCoreResponseBodyBytes);
    CompletableFuture<Response> future = CompletableFuture.completedFuture(response);
    ListenableFuture<Response> resFuture = mock(ListenableFuture.clreplaced);
    when(resFuture.toCompletableFuture()).thenReturn(future);
    when(resFuture.get()).thenReturn(response);
    AtomicInteger retryAttemptNo = new AtomicInteger(0);
    when(client.executeRequest(any(Request.clreplaced), any(AsyncCompletionHandler.clreplaced))).then(invocationOnMock -> {
        AsyncCompletionHandler handler = invocationOnMock.getArgument(1, AsyncCompletionHandler.clreplaced);
        if (retryAttemptNo.incrementAndGet() < 10) {
            handler.onStatusReceived(failedStatus);
        } else {
            while (dfsCoreResponseDispatcher.isNotFinished()) {
                handler.onBodyPartReceived(dfsCoreResponseDispatcher.getNextBodyPart());
            }
            handler.onStatusReceived(status);
        }
        handler.onCompleted(response);
        return resFuture;
    });
    AzureStorageFileSystem parentClreplaced = mock(AzureStorageFileSystem.clreplaced);
    AzureAuthTokenProvider authTokenProvider = getMockAuthTokenProvider();
    AzureAsyncContainerProvider containerProvider = new AzureAsyncContainerProvider(client, "azurestoragev2hier", authTokenProvider, parentClreplaced, true);
    List<String> receivedContainers = containerProvider.getContainerCreators().map(AzureStorageFileSystem.ContainerCreatorImpl.clreplaced::cast).map(AzureStorageFileSystem.ContainerCreatorImpl::getName).collect(Collectors.toList());
    List<String> expectedContainers = Arrays.asList("page1container1", "page1container2", "page1container3");
    replacedertEquals(expectedContainers, receivedContainers);
}

19 Source : TestAzureAsyncContainerProvider.java
with Apache License 2.0
from dremio

@Test
public void testListContainers() throws IOException, ExecutionException, InterruptedException {
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    when(response.getHeader(any(String.clreplaced))).thenReturn("");
    HttpResponseStatus status = mock(HttpResponseStatus.clreplaced);
    when(status.getStatusCode()).thenReturn(206);
    byte[] dfsCoreResponseBodyBytesPage1 = readStaticResponse("dfs-core-container-response-page1.json");
    RandomBytesResponseDispatcher dfsCoreResponseDispatcherPage1 = new RandomBytesResponseDispatcher(dfsCoreResponseBodyBytesPage1);
    byte[] dfsCoreResponseBodyBytesPage2 = readStaticResponse("dfs-core-container-response-page2.json");
    RandomBytesResponseDispatcher dfsCoreResponseDispatcherPage2 = new RandomBytesResponseDispatcher(dfsCoreResponseBodyBytesPage2);
    byte[] dfsCoreEmptyResponseBytes = readStaticResponse("dfs-core-container-empty.json");
    RandomBytesResponseDispatcher dfsCoreEmptyResponseDispatcher = new RandomBytesResponseDispatcher(dfsCoreEmptyResponseBytes);
    CompletableFuture<Response> future = CompletableFuture.completedFuture(response);
    ListenableFuture<Response> resFuture = mock(ListenableFuture.clreplaced);
    when(resFuture.toCompletableFuture()).thenReturn(future);
    when(resFuture.get()).thenReturn(response);
    when(client.executeRequest(any(Request.clreplaced), any(AsyncCompletionHandler.clreplaced))).then(invocationOnMock -> {
        Request req = invocationOnMock.getArgument(0, Request.clreplaced);
        AsyncCompletionHandler handler = invocationOnMock.getArgument(1, AsyncCompletionHandler.clreplaced);
        replacedertTrue(req.getUrl().startsWith("https://azurestoragev2hier.dfs.core.windows.net"));
        replacedertNotNull(req.getHeaders().get("Date"));
        replacedertNotNull(req.getHeaders().get("x-ms-client-request-id"));
        // edit only if you're upgrading client
        replacedertEquals("2019-07-07", req.getHeaders().get("x-ms-version"));
        replacedertEquals("Bearer testtoken", req.getHeaders().get("Authorization"));
        List<NameValuePair> queryParams = URLEncodedUtils.parse(new URI(req.getUrl()), StandardCharsets.UTF_8);
        String continuationKey = queryParams.stream().filter(param -> param.getName().equalsIgnoreCase("continuation")).findAny().map(NameValuePair::getValue).orElse("");
        // Return empty response if continuation key is not present. Return data in continuation call.
        // This is to ensure that the plugin makes another call when there's no data but continuation key present.
        if ("page1container1".equals(continuationKey)) {
            when(response.getHeader("x-ms-continuation")).thenReturn("page2container1");
            while (dfsCoreResponseDispatcherPage1.isNotFinished()) {
                handler.onBodyPartReceived(dfsCoreResponseDispatcherPage1.getNextBodyPart());
            }
        } else if ("page2container1".equals(continuationKey)) {
            when(response.getHeader("x-ms-continuation")).thenReturn("");
            while (dfsCoreResponseDispatcherPage2.isNotFinished()) {
                handler.onBodyPartReceived(dfsCoreResponseDispatcherPage2.getNextBodyPart());
            }
        } else {
            when(response.getHeader("x-ms-continuation")).thenReturn("page1container1");
            while (dfsCoreEmptyResponseDispatcher.isNotFinished()) {
                handler.onBodyPartReceived(dfsCoreEmptyResponseDispatcher.getNextBodyPart());
            }
        }
        handler.onStatusReceived(status);
        handler.onCompleted(response);
        return resFuture;
    });
    AzureStorageFileSystem parentClreplaced = mock(AzureStorageFileSystem.clreplaced);
    AzureAuthTokenProvider authTokenProvider = getMockAuthTokenProvider();
    AzureAsyncContainerProvider containerProvider = new AzureAsyncContainerProvider(client, "azurestoragev2hier", authTokenProvider, parentClreplaced, true);
    List<String> receivedContainers = containerProvider.getContainerCreators().map(AzureStorageFileSystem.ContainerCreatorImpl.clreplaced::cast).map(AzureStorageFileSystem.ContainerCreatorImpl::getName).collect(Collectors.toList());
    List<String> expectedContainers = Arrays.asList("page1container1", "page1container2", "page1container3", "page2container1", "page2container2", "page2container3");
    replacedertEquals(expectedContainers, receivedContainers);
}

19 Source : TestAzureAsyncContainerProvider.java
with Apache License 2.0
from dremio

@Test
public void testDoesContainerExistsAccessDenied() throws ExecutionException, InterruptedException {
    AzureStorageFileSystem parentClreplaced = mock(AzureStorageFileSystem.clreplaced);
    AzureAuthTokenProvider authTokenProvider = getMockAuthTokenProvider();
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    when(response.getHeader(any(String.clreplaced))).thenReturn("");
    when(response.getStatusCode()).thenReturn(403);
    when(response.getStatusText()).thenReturn("Forbidden, InsufficientAccountPermissions, \"The account being accessed does not have sufficient permissions to execute this operation.\"");
    ListenableFuture<Response> future = mock(ListenableFuture.clreplaced);
    when(future.get()).thenReturn(response);
    when(client.executeRequest(any(Request.clreplaced))).thenReturn(future);
    AzureAsyncContainerProvider containerProvider = new AzureAsyncContainerProvider(client, "azurestoragev2hier", authTokenProvider, parentClreplaced, true);
    try {
        containerProvider.replacedertContainerExists("container");
        fail("Expecting exception");
    } catch (Retryer.OperationFailedAfterRetriesException e) {
        replacedertTrue(e.getCause() instanceof ContainerAccessDeniedException);
        replacedertEquals("Access to container container denied - [403 Forbidden, InsufficientAccountPermissions, \"The account being accessed does not have sufficient permissions to execute this operation.\"]", e.getCause().getMessage());
    }
    // Ensure no retries attempted
    verify(client, times(1)).executeRequest(any(Request.clreplaced));
}

19 Source : TestAzureAsyncContainerProvider.java
with Apache License 2.0
from dremio

@Test
public void testDoesContainerExists() throws ExecutionException, InterruptedException {
    AzureStorageFileSystem parentClreplaced = mock(AzureStorageFileSystem.clreplaced);
    AzureAuthTokenProvider authTokenProvider = getMockAuthTokenProvider();
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    when(response.getHeader(any(String.clreplaced))).thenReturn("");
    when(response.getStatusCode()).thenReturn(200);
    ListenableFuture<Response> future = mock(ListenableFuture.clreplaced);
    when(future.get()).thenReturn(response);
    when(client.executeRequest(any(Request.clreplaced))).thenReturn(future);
    AzureAsyncContainerProvider containerProvider = new AzureAsyncContainerProvider(client, "azurestoragev2hier", authTokenProvider, parentClreplaced, true);
    containerProvider.replacedertContainerExists("container");
}

19 Source : TestAzureAsyncContainerProvider.java
with Apache License 2.0
from dremio

@Test
public void testDoesContainerExistsNotFound() throws ExecutionException, InterruptedException {
    AzureStorageFileSystem parentClreplaced = mock(AzureStorageFileSystem.clreplaced);
    AzureAuthTokenProvider authTokenProvider = getMockAuthTokenProvider();
    AsyncHttpClient client = mock(AsyncHttpClient.clreplaced);
    Response response = mock(Response.clreplaced);
    when(response.getHeader(any(String.clreplaced))).thenReturn("");
    when(response.getStatusCode()).thenReturn(404);
    when(response.getStatusText()).thenReturn("The specified filesystem does not exist.");
    ListenableFuture<Response> future = mock(ListenableFuture.clreplaced);
    when(future.get()).thenReturn(response);
    when(client.executeRequest(any(Request.clreplaced))).thenReturn(future);
    AzureAsyncContainerProvider containerProvider = new AzureAsyncContainerProvider(client, "azurestoragev2hier", authTokenProvider, parentClreplaced, true);
    try {
        containerProvider.replacedertContainerExists("container");
        fail("Expecting exception");
    } catch (Retryer.OperationFailedAfterRetriesException e) {
        replacedertEquals(ContainerNotFoundException.clreplaced, e.getCause().getClreplaced());
        replacedertEquals("Unable to find container container - [404 The specified filesystem does not exist.]", e.getCause().getMessage());
    }
    // Ensure no retries attempted
    verify(client, times(1)).executeRequest(any(Request.clreplaced));
}

19 Source : AsyncHttpClientManager.java
with Apache License 2.0
from dremio

/**
 * Helper clreplaced for constructor and maintaining AsyncHttpClients.
 */
public clreplaced AsyncHttpClientManager implements Closeable {

    private static final int DEFAULT_IDLE_TIME = 60000;

    private static final int DEFAULT_CLEANER_PERIOD = 1000;

    // Set 4 retry attempts. In the ADLS SDK, the ExponentialBackoffPolicy makes 4 retry attempts by default.
    private static final int DEFAULT_NUM_RETRY = 4;

    private AsyncHttpClient asyncHttpClient;

    private ADLStoreClient client;

    private ExecutorService utilityThreadPool;

    private final HashedWheelTimer poolTimer = new HashedWheelTimer();

    public AsyncHttpClientManager(String name, AzureDataLakeConf conf) throws IOException {
        final AccessTokenProvider tokenProvider;
        switch(conf.mode) {
            case CLIENT_KEY:
                tokenProvider = new ClientCredsTokenProvider(conf.clientKeyRefreshUrl, conf.clientId, conf.clientKeyPreplacedword);
                break;
            case REFRESH_TOKEN:
                tokenProvider = new RefreshTokenBasedTokenProvider(conf.clientId, conf.refreshTokenSecret);
                break;
            default:
                throw new RuntimeException("Failure creating ADLSg1 connection. Invalid credentials type.");
        }
        final SslContext sslContext = SslContextBuilder.forClient().build();
        // Configure our AsyncHttpClient to:
        // - use SSL (required for ADLS)
        // - use connection pooling
        // - generate response bodies lazily (leave them as Netty ByteBufs instead of convert them to byte[]).
        final DefaultAsyncHttpClientConfig.Builder configBuilder = config().setSslContext(sslContext).setThreadPoolName(name + "-adls-async-client").setChannelPool(new DefaultChannelPool(DEFAULT_IDLE_TIME, -1, poolTimer, DEFAULT_CLEANER_PERIOD)).setResponseBodyPartFactory(AsyncHttpClientConfig.ResponseBodyPartFactory.LAZY).setMaxRequestRetry(DEFAULT_NUM_RETRY);
        poolTimer.start();
        client = ADLStoreClient.createClient(conf.accountName + ".azuredatalakestore.net", tokenProvider);
        client.setOptions(new ADLStoreOptions().enableThrowingRemoteExceptions());
        asyncHttpClient = asyncHttpClient(configBuilder.build());
        utilityThreadPool = Executors.newCachedThreadPool(new NamedThreadFactory("adls-utility"));
    }

    public AsyncHttpClient getAsyncHttpClient() {
        return asyncHttpClient;
    }

    public ADLStoreClient getClient() {
        return client;
    }

    public ExecutorService getUtilityThreadPool() {
        return utilityThreadPool;
    }

    @Override
    public void close() throws IOException {
        AutoCloseables.close(IOException.clreplaced, asyncHttpClient, poolTimer::stop, utilityThreadPool::shutdown);
    }
}

19 Source : AdlsAsyncFileReader.java
with Apache License 2.0
from dremio

/**
 * Reads file content from ADLS Gen 1 asynchronously.
 */
public clreplaced AdlsAsyncFileReader extends ReusableAsyncByteReader implements ExponentialBackoff {

    static clreplaced RemoteException {

        private String exception;

        private String message;

        private String javaClreplacedName;

        public String getException() {
            return exception;
        }

        public String getMessage() {
            return message;
        }

        public String getJavaClreplacedName() {
            return javaClreplacedName;
        }

        @JsonProperty("RemoteException")
        private void unpackRemoteException(Map<String, String> remoteException) {
            // JSON error response is as follows:
            // { "RemoteException" : {
            // "exception" : ...,
            // "message"   : ...,
            // "javaClreplacedName" : ... }
            // }
            // We need to unpack the RemoteException property to get the meaningful attributes.
            exception = remoteException.get("exception");
            message = remoteException.get("message");
            javaClreplacedName = remoteException.get("javaClreplacedName");
        }
    }

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AdlsAsyncFileReader.clreplaced);

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, true);

    // set to the average latency of an async read
    private static final int BASE_MILLIS_TO_WAIT = 250;

    private static final int MAX_MILLIS_TO_WAIT = 10 * BASE_MILLIS_TO_WAIT;

    private static final int MAX_RETRIES = 4;

    private final ADLSClient client;

    private final AsyncHttpClient asyncHttpClient;

    private final String path;

    private final String sessionId = UUID.randomUUID().toString();

    private final DremioAdlFileSystem fs;

    private final Long cachedVersion;

    private volatile Long latestVersion;

    private final String threadName;

    private final ExecutorService threadPool;

    private int errCode = 0;

    /**
     * Helper clreplaced for processing ADLS responses. This will write to the given output buffer
     * for OK results and throw a detailed exception on failure.
     */
    private clreplaced AdlsResponseProcessor extends AsyncCompletionHandlerBase {

        private final ByteBuf outputBuffer;

        private boolean isErrorResponse = false;

        AdlsResponseProcessor(ByteBuf outputBuffer) {
            this.outputBuffer = outputBuffer;
        }

        @Override
        public State onStatusReceived(HttpResponseStatus status) throws Exception {
            // The REST service provides error information as part of the response
            // body when the response code is 400 or greater, and not a 401 (auth error).
            if (status.getStatusCode() >= io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST.code() && status.getStatusCode() != HttpConstants.ResponseStatusCodes.UNAUTHORIZED_401) {
                isErrorResponse = true;
                errCode = status.getStatusCode();
            }
            return super.onStatusReceived(status);
        }

        @Override
        public AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            if (isErrorResponse) {
                return super.onBodyPartReceived(content);
            }
            outputBuffer.writeBytes(content.getBodyByteBuffer());
            return AsyncHandler.State.CONTINUE;
        }

        @Override
        public Response onCompleted(Response response) throws Exception {
            if (isErrorResponse) {
                throwRemoteException(response.getResponseBody());
            }
            return response;
        }

        private void throwRemoteException(String responseString) throws Exception {
            final RemoteException remoteEx = OBJECT_MAPPER.readValue(responseString, RemoteException.clreplaced);
            final OperationResponse resp = new OperationResponse();
            resp.remoteExceptionName = remoteEx.getException();
            resp.remoteExceptionMessage = remoteEx.getMessage();
            resp.remoteExceptionJavaClreplacedName = remoteEx.getJavaClreplacedName();
            throw client.getExceptionFromResponse(resp, String.format("Error reading file %s", path));
        }
    }

    public AdlsAsyncFileReader(ADLSClient adlsClient, AsyncHttpClient asyncClient, String path, String cachedVersion, DremioAdlFileSystem fs, ExecutorService threadPool) {
        this.client = adlsClient;
        this.asyncHttpClient = asyncClient;
        this.path = path;
        this.fs = fs;
        this.cachedVersion = Long.parseLong(cachedVersion);
        this.latestVersion = (this.cachedVersion != 0) ? null : 0L;
        this.threadName = Thread.currentThread().getName();
        this.threadPool = threadPool;
    }

    @Override
    public int getBaseMillis() {
        return BASE_MILLIS_TO_WAIT;
    }

    @Override
    public int getMaxMillis() {
        return MAX_MILLIS_TO_WAIT;
    }

    @Override
    protected void onClose() {
        latestVersion = null;
    }

    private CompletableFuture<Void> readFullyFuture(long offset, ByteBuf dst, int dstOffset, int len) {
        if (latestVersion > cachedVersion) {
            logger.debug("[{}] File has been modified, metadata refresh is required", threadName);
            final CompletableFuture<Void> future = new CompletableFuture<>();
            future.completeExceptionally(new FileNotFoundException("Version of file changed " + path));
            return future;
        }
        final String clientRequestId = UUID.randomUUID().toString();
        final int capacityAtOffset = dst.capacity() - dstOffset;
        if (capacityAtOffset < len) {
            logger.debug("[{}] Buffer has {} bytes remaining. Attempted to write at offset {} with {} bytes", threadName, capacityAtOffset, dstOffset, len);
        }
        final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                return client.getAccessToken();
            } catch (IOException ex) {
                throw new CompletionException(ex);
            }
        }, threadPool);
        return future.thenCompose((authToken) -> {
            final ADLSClient.AdlsRequestBuilder builder = new ADLSClient.AdlsRequestBuilder(client.getClient(), authToken, logger).setOp(ADLSClient.getOpenOperation()).addQueryParam("read", "true").addQueryParam("filesessionid", sessionId).addHeader("x-ms-client-request-id", clientRequestId).setFilePath(path);
            if (offset > 0) {
                builder.addQueryParam("offset", Long.toString(offset));
            }
            if (len >= 0) {
                builder.addQueryParam("length", Long.toString(len));
            }
            return executeAsyncRequest(dst, dstOffset, clientRequestId, builder, 0);
        });
    }

    private CompletableFuture<Void> executeAsyncRequest(ByteBuf dst, int dstOffset, String clientRequestId, ADLSClient.AdlsRequestBuilder builder, int retryAttemptNum) {
        logger.debug("[{}] Sending request with clientRequestId: {}", threadName, clientRequestId);
        dst.writerIndex(dstOffset);
        final Stopwatch watch = Stopwatch.createStarted();
        return asyncHttpClient.executeRequest(builder.build(), new AdlsResponseProcessor(dst)).toCompletableFuture().whenComplete((response, throwable) -> {
            if (null == throwable) {
                logger.debug("[{}] Request completed for clientRequestId: {}, took {} ms", threadName, clientRequestId, watch.elapsed(TimeUnit.MILLISECONDS));
            } else if (retryAttemptNum < MAX_RETRIES) {
                logger.info("[{}] Retry #{}, request failed with {} clientRequestId: {}, took {} ms", threadName, retryAttemptNum + 1, errCode, clientRequestId, watch.elapsed(TimeUnit.MILLISECONDS), throwable);
            } else {
                logger.error("[{}] Request failed with {}, for clientRequestId: {}, took {} ms", threadName, errCode, clientRequestId, watch.elapsed(TimeUnit.MILLISECONDS), throwable);
            }
        }).thenAccept(// Discard the response, which has already been handled by AdlsResponseProcessor.
        response -> {
        }).thenApply(CompletableFuture::completedFuture).exceptionally(throwable -> {
            if (retryAttemptNum > MAX_RETRIES) {
                final CompletableFuture<Void> errorFuture = new CompletableFuture<>();
                errorFuture.completeExceptionally(throwable);
                return errorFuture;
            }
            backoffWait(retryAttemptNum);
            // Reset the index of the writer for the retry.
            dst.writerIndex(dstOffset);
            return executeAsyncRequest(dst, dstOffset, clientRequestId, builder, retryAttemptNum + 1);
        }).thenCompose(Function.idenreplacedy());
    }

    @Override
    public CompletableFuture<Void> readFully(long offset, ByteBuf dst, int dstOffset, int len) {
        if (latestVersion != null) {
            return readFullyFuture(offset, dst, dstOffset, len);
        }
        final CompletableFuture<Void> getStatusFuture = CompletableFuture.runAsync(() -> {
            try {
                if (latestVersion == null) {
                    synchronized (AdlsAsyncFileReader.this) {
                        if (latestVersion == null) {
                            latestVersion = fs.getFileStatus(new Path(path)).getModificationTime();
                        }
                    }
                }
            } catch (IOException ex) {
                throw new CompletionException(ex);
            }
        }, threadPool);
        return getStatusFuture.thenCompose(Void -> readFullyFuture(offset, dst, dstOffset, len));
    }
}

19 Source : BotSystem.java
with Apache License 2.0
from dialogs

public clreplaced BotSystem {

    private final ManagedChannel channel;

    private final AsyncHttpClient asyncHttpClient;

    private final boolean anonymousAuth;

    private BotSystem(ManagedChannel channel, AsyncHttpClient asyncHttpClient, boolean anonymousAuth) {
        this.channel = channel;
        this.asyncHttpClient = asyncHttpClient;
        this.anonymousAuth = anonymousAuth;
    }

    public static BotSystem createSystem(BotSystemConfig config) throws Exception {
        AsyncHttpClient asyncHttpClient = createAsyncHttpClient(config);
        ManagedChannel channel = createChannel(config);
        boolean anonymousAuth = config.getCertPath() != null && config.getCertPreplacedword() != null;
        return new BotSystem(channel, asyncHttpClient, anonymousAuth);
    }

    private static AsyncHttpClient createAsyncHttpClient(BotSystemConfig config) throws Exception {
        DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
        if (config.getCertPath() != null && config.getCertPreplacedword() != null) {
            File certFile = new File(config.getCertPath());
            SslContext sslContext = SslContextBuilder.forClient().keyManager(NetUtils.createKeyFactory(certFile, config.getCertPreplacedword())).build();
            builder.setSslContext(sslContext);
        }
        return asyncHttpClient(builder);
    }

    private static ManagedChannel createChannel(BotSystemConfig config) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        NettyChannelBuilder nettyChannelBuilder = (NettyChannelBuilder) ManagedChannelBuilder.forAddress(config.getHost(), config.getPort()).idleTimeout(15, SECONDS).keepAliveTime(30, SECONDS);
        if (config.getCertPath() != null && config.getCertPreplacedword() != null) {
            File certFile = new File(config.getCertPath());
            SslContext sslContext = GrpcSslContexts.forClient().keyManager(NetUtils.createKeyFactory(certFile, config.getCertPreplacedword())).build();
            nettyChannelBuilder.sslContext(sslContext);
        }
        if (!config.isSecure()) {
            nettyChannelBuilder.usePlaintext();
        }
        if (!config.isCompression()) {
            nettyChannelBuilder.decompressorRegistry(DecompressorRegistry.emptyInstance());
        }
        return nettyChannelBuilder.build();
    }

    public CompletableFuture<Bot> startBot(BotConfig config) {
        InternalBot internalBot = new InternalBot(channel, config, anonymousAuth);
        return internalBot.start().thenApply(v -> new Bot(channel, internalBot, asyncHttpClient));
    }

    public CompletableFuture<Bot> startBotWithToken(String name, String token) {
        BotConfig botConfig = BotConfig.Builder.newBuilder().withName(name).withCredentials(new BotCredentials(BotCredentials.Method.TOKEN, token)).build();
        return startBot(botConfig);
    }

    public CompletableFuture<Bot> startBotWithPreplacedword(String name, String preplacedword) {
        BotConfig botConfig = BotConfig.Builder.newBuilder().withName(name).withCredentials(new BotCredentials(BotCredentials.Method.PreplacedWORD, preplacedword)).build();
        return startBot(botConfig);
    }
}

19 Source : BotSystem.java
with Apache License 2.0
from dialogs

public static BotSystem createSystem(BotSystemConfig config) throws Exception {
    AsyncHttpClient asyncHttpClient = createAsyncHttpClient(config);
    ManagedChannel channel = createChannel(config);
    boolean anonymousAuth = config.getCertPath() != null && config.getCertPreplacedword() != null;
    return new BotSystem(channel, asyncHttpClient, anonymousAuth);
}

19 Source : MediaAndFilesApi.java
with Apache License 2.0
from dialogs

public clreplaced MediaAndFilesApi {

    private final ManagedChannel channel;

    private final InternalBot internalBot;

    private final AsyncHttpClient asyncHttpClient;

    public MediaAndFilesApi(ManagedChannel channel, InternalBot internalBot, AsyncHttpClient asyncHttpClient) {
        this.channel = channel;
        this.internalBot = internalBot;
        this.asyncHttpClient = asyncHttpClient;
    }

    /**
     * Uploads a file to server
     *
     * @param file     - file to upload
     * @return A CompletableFuture for FileLocation on server side (FileLocation for
     * Dialog internal api)
     */
    public CompletableFuture<FileLocation> uploadFile(@Nonnull File file) {
        if (!file.isFile()) {
            throw new IllegalArgumentException("Input isn't a file !");
        }
        String fileName = file.getName();
        int fileSize = (int) file.length();
        RequestGetFileUploadUrl.Builder requestGetUrl = RequestGetFileUploadUrl.newBuilder().setExpectedSize(fileSize);
        return internalBot.withToken(MediaAndFilesGrpc.newFutureStub(channel).withDeadlineAfter(1, TimeUnit.MINUTES), stub -> stub.getFileUploadUrl(requestGetUrl.build())).thenCompose(responseGetFileUploadUrl -> {
            RequestGetFileUploadPartUrl.Builder uploadPart = RequestGetFileUploadPartUrl.newBuilder().setPartNumber(0).setPartSize(fileSize).setUploadKey(responseGetFileUploadUrl.getUploadKey());
            return internalBot.withToken(MediaAndFilesGrpc.newFutureStub(channel).withDeadlineAfter(3, TimeUnit.MINUTES), stub -> stub.getFileUploadPartUrl(uploadPart.build())).thenApply(res -> new Pair<>(res.getUrl(), responseGetFileUploadUrl.getUploadKey()));
        }).thenCompose(respPair -> asyncHttpClient.preparePut(respPair.getValue0()).setBody(file).execute().toCompletableFuture().thenApply(val -> respPair.getValue1())).thenCompose(uploadKey -> internalBot.withToken(MediaAndFilesGrpc.newFutureStub(channel).withDeadlineAfter(3, TimeUnit.MINUTES), stub -> stub.commitFileUpload(RequestCommitFileUpload.newBuilder().setFileName(fileName).setUploadKey(uploadKey).build())).thenApply(ResponseCommitFileUpload::getUploadedFileLocation));
    }

    /**
     * Get Url to download a file from server
     *
     * @param fileLoc - file location (dialog internal API)
     * @return - A CompletableFuture for a String containing download url
     */
    public CompletableFuture<String> getFileUrl(FileLocation fileLoc) {
        return internalBot.withToken(MediaAndFilesGrpc.newFutureStub(channel).withDeadlineAfter(2, TimeUnit.MINUTES), stub -> stub.getFileUrls(RequestGetFileUrls.newBuilder().addFiles(fileLoc).build())).thenApply(resp -> resp.getFileUrlsList().get(0).getUrl());
    }
}

19 Source : ClientConfigurationUtils.java
with Apache License 2.0
from commercetools

/**
 * Gets an asynchronous {@link HttpClient} of `asynchttpclient` library, to be used by as an
 * underlying http client for the {@link SphereClient}.
 *
 * @return an asynchronous {@link HttpClient}
 */
private static HttpClient getHttpClient() {
    final AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().build());
    return AsyncHttpClientAdapter.of(asyncHttpClient);
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test(timeOut = 5000L)
public void testReadingNotOkResponse() throws Exception {
    CompletableFuture<Response> response = getOkFuture(getNotOkResponse(500));
    AsyncHttpClient mockClient = mockClientWith(response);
    pubscriber.setClient(mockClient);
    pubscriber.send(new PubSubMessage("foo", "bar"));
    // This is async (but practically still very fast since we mocked the response), so need a timeout.
    PubSubMessage actual = fetchAsync().get();
    replacedert.replacedertNotNull(actual);
    replacedert.replacedertEquals(actual.getId(), "foo");
    replacedert.replacedertEquals(actual.getContentreplacedtring(), CANNOT_REACH_DRPC.asJSONClip());
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test
public void testFailing() {
    AsyncHttpClient mockClient = mock(AsyncHttpClient.clreplaced);
    pubscriber.fail("foo");
    verifyZeroInteractions(mockClient);
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test
public void testClosingWithException() throws Exception {
    AsyncHttpClient mockClient = mock(AsyncHttpClient.clreplaced);
    doThrow(new IOException()).when(mockClient).close();
    pubscriber.setClient(mockClient);
    pubscriber.close();
    verify(mockClient, times(1)).close();
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

private AsyncHttpClient mockClientWith(CompletableFuture<Response> future) {
    ListenableFuture<Response> mockListenable = (ListenableFuture<Response>) mock(ListenableFuture.clreplaced);
    doReturn(future).when(mockListenable).toCompletableFuture();
    BoundRequestBuilder mockBuilder = mock(BoundRequestBuilder.clreplaced);
    doReturn(mockListenable).when(mockBuilder).execute();
    // Return itself
    doReturn(mockBuilder).when(mockBuilder).setBody(anyString());
    AsyncHttpClient mockClient = mock(AsyncHttpClient.clreplaced);
    doReturn(mockBuilder).when(mockClient).preparePost(anyString());
    return mockClient;
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test(timeOut = 5000L)
public void testReadingNullResponse() throws Exception {
    CompletableFuture<Response> response = getOkFuture(null);
    AsyncHttpClient mockClient = mockClientWith(response);
    pubscriber.setClient(mockClient);
    pubscriber.send(new PubSubMessage("foo", "bar"));
    // This is async (but practically still very fast since we mocked the response), so need a timeout.
    PubSubMessage actual = fetchAsync().get();
    replacedert.replacedertNotNull(actual);
    replacedert.replacedertEquals(actual.getId(), "foo");
    replacedert.replacedertEquals(actual.getContentreplacedtring(), CANNOT_REACH_DRPC.asJSONClip());
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test
public void testCommiting() {
    AsyncHttpClient mockClient = mock(AsyncHttpClient.clreplaced);
    pubscriber.commit("foo");
    verifyZeroInteractions(mockClient);
}

19 Source : DRPCQueryResultPubscriberTest.java
with Apache License 2.0
from bullet-db

@Test
public void testClosing() throws Exception {
    AsyncHttpClient mockClient = mock(AsyncHttpClient.clreplaced);
    pubscriber.setClient(mockClient);
    pubscriber.close();
    verify(mockClient, times(1)).close();
}

19 Source : DRPCQueryResultPubscriber.java
with Apache License 2.0
from bullet-db

/**
 * This clreplaced is both a query {@link Publisher} and a result {@link Subscriber}. It uses HTTP to do a request- response
 * call where the request is the Bullet query and the response is the result from the backend.
 */
@Slf4j
public clreplaced DRPCQueryResultPubscriber implements Publisher, Subscriber {

    private RandomPool<String> urls;

    private Queue<PubSubMessage> responses;

    @Setter(AccessLevel.PACKAGE)
    private AsyncHttpClient client;

    public static final int NO_TIMEOUT = -1;

    public static final int OK_200 = 200;

    // PROTOCOL://URL:PORT/DRPC_PATH/DRPC_FUNCTION
    private static final String URL_TEMPLATE = "%1$s://%2$s:%3$s/%4$s/%5$s";

    /**
     * Create an instance from a given {@link BulletConfig}. Requires {@link DRPCConfig#DRPC_HTTP_CONNECT_TIMEOUT},
     * {@link DRPCConfig#DRPC_HTTP_CONNECT_RETRY_LIMIT}, {@link DRPCConfig#DRPC_SERVERS}, {@link DRPCConfig#DRPC_HTTP_PORT},
     * and {@link DRPCConfig#DRPC_HTTP_PATH} to be set. To be used in PubSub.Context#QUERY_SUBMISSION mode.
     *
     * @param config A non-null config that contains the required settings.
     */
    public DRPCQueryResultPubscriber(BulletConfig config) {
        Objects.requireNonNull(config);
        Number connectTimeout = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_TIMEOUT, Number.clreplaced);
        Number retryLimit = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_CONNECT_RETRY_LIMIT, Number.clreplaced);
        List<String> servers = (List<String>) config.getRequiredConfigAs(DRPCConfig.DRPC_SERVERS, List.clreplaced);
        String protocol = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PROTOCOL, String.clreplaced);
        String port = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PORT, String.clreplaced);
        String path = config.getRequiredConfigAs(DRPCConfig.DRPC_HTTP_PATH, String.clreplaced);
        String function = config.getRequiredConfigAs(DRPCConfig.DRPC_FUNCTION, String.clreplaced);
        List<String> endpoints = servers.stream().map(url -> String.format(URL_TEMPLATE, protocol, url, port, path, function)).collect(Collectors.toList());
        this.urls = new RandomPool<>(endpoints);
        AsyncHttpClientConfig clientConfig = new DefaultAsyncHttpClientConfig.Builder().setConnectTimeout(connectTimeout.intValue()).setMaxRequestRetry(retryLimit.intValue()).setReadTimeout(NO_TIMEOUT).setRequestTimeout(NO_TIMEOUT).build();
        // This is thread safe
        client = new DefaultAsyncHttpClient(clientConfig);
        responses = new ConcurrentLinkedQueue<>();
    }

    @Override
    public PubSubMessage send(PubSubMessage message) {
        String url = urls.get();
        String id = message.getId();
        String json = message.asJSON();
        log.info("Posting to {} for id {}", url, id);
        log.debug("Posting to {} with body {}", url, json);
        client.preparePost(url).setBody(json).execute().toCompletableFuture().exceptionally(this::handleException).thenAcceptAsync(createResponseConsumer(id));
        return message;
    }

    @Override
    public PubSubMessage receive() {
        return responses.poll();
    }

    @Override
    public void commit(String id) {
    // Do nothing
    }

    @Override
    public void fail(String id) {
    // Do nothing
    }

    @Override
    public void close() {
        try {
            client.close();
        } catch (IOException ioe) {
            log.error("Error while closing AsyncHTTPClient", ioe);
        }
    }

    private Consumer<Response> createResponseConsumer(String id) {
        // Create a closure with id
        return response -> handleResponse(id, response);
    }

    private Response handleException(Throwable throwable) {
        // Just for logging
        log.error("Received error while posting query", throwable);
        return null;
    }

    private void handleResponse(String id, Response response) {
        if (response == null || response.getStatusCode() != OK_200) {
            log.error("Handling error for id {} with response {}", id, response);
            responses.offer(new PubSubMessage(id, DRPCError.CANNOT_REACH_DRPC.asJSONClip(), null));
            return;
        }
        log.info("Received for id {}: {} {}", response.getStatusCode(), id, response.getStatusText());
        String body = response.getResponseBody();
        PubSubMessage message = PubSubMessage.fromJSON(body);
        log.debug("Received for id {}:\n{}", message.getId(), message.getContent());
        responses.offer(message);
    }
}

19 Source : GetClient.java
with MIT License
from BUGS-NYU

public final clreplaced GetClient {

    private static AsyncHttpClient client;

    public static AsyncHttpClient getClient() {
        if (client == null)
            client = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().setCookieStore(BlackholeCookieStore.BLACK_HOLE).build());
        return client;
    }

    public static void close() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        client = null;
    }
}

19 Source : RecurlyTokenClient.java
with Apache License 2.0
from arcus-smart-home

public clreplaced RecurlyTokenClient {

    private static final String TOKEN_URL = "https://api.recurly.com/js/v1/token";

    private static final Gson GSON = new GsonBuilder().create();

    private final AsyncHttpClient client;

    public RecurlyTokenClient() {
        this.client = new DefaultAsyncHttpClient();
    }

    public ListenableFuture<String> getBillingToken(BillingInfoRequest request) {
        return doGetBillingToken(request);
    }

    private final ListenableFuture<String> doGetBillingToken(BillingInfoRequest billingInfoRequest) {
        final SettableFuture<String> future = SettableFuture.create();
        try {
            BoundRequestBuilder builder = client.preparePost(TOKEN_URL).addHeader(HttpHeaders.ACCEPT, "application/xml").addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
            for (Map.Entry<String, String> item : billingInfoRequest.getMappings().entrySet()) {
                builder.addFormParam(item.getKey(), item.getValue());
            }
            builder.execute(new AsyncCompletionHandler<Void>() {

                @Override
                public void onThrowable(Throwable throwable) {
                    future.setException(throwable);
                }

                @Override
                public Void onCompleted(Response response) throws Exception {
                    try {
                        RecurlyJSONResponse message = GSON.fromJson(response.getResponseBody(), RecurlyJSONResponse.clreplaced);
                        if (message.isError()) {
                            future.setException(new RecurlyAPIErrorException(message.getCode(), message.getMessage()));
                        } else {
                            future.set(message.getID());
                        }
                    } catch (Exception ex) {
                        future.setException(ex);
                    }
                    return null;
                }
            });
        } catch (Exception ex) {
            future.setException(ex);
        }
        return future;
    }
}

19 Source : AsyncHttpService.java
with Apache License 2.0
from arcus-smart-home

public final clreplaced AsyncHttpService {

    private static final int DEFAULT_HANDSHAKE_TIMEOUT = (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS);

    private static final int DEFAULT_REQUEST_TIMEOUT = (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS);

    @Nullable
    private static AsyncHttpClient client;

    private static EventLoopGroup evlg;

    private static boolean useEpoll;

    private static boolean useOpenSsl;

    private AsyncHttpService() {
    }

    static void start() {
        final AtomicLong counter = new AtomicLong();
        ThreadFactory tf = new ThreadFactory() {

            @Override
            public Thread newThread(@Nullable Runnable runnable) {
                Thread thr = new Thread(runnable);
                thr.setName("ahc" + counter.getAndIncrement());
                thr.setDaemon(true);
                return thr;
            }
        };
        useEpoll = Epoll.isAvailable();
        useOpenSsl = false;
        evlg = useEpoll ? new EpollEventLoopGroup(2, tf) : new NioEventLoopGroup(2, tf);
        DefaultAsyncHttpClientConfig config = builder().build();
        client = new DefaultAsyncHttpClient(config);
    }

    static void shutdown() {
        AsyncHttpClient clnt = client;
        if (clnt != null) {
            try {
                clnt.close();
            } catch (Exception ex) {
            }
        }
        client = null;
    }

    private static AsyncHttpClient get() {
        AsyncHttpClient result = client;
        if (result == null) {
            throw new IllegalStateException("async http service not started");
        }
        return result;
    }

    // ///////////////////////////////////////////////////////////////////////////
    // Custom Async Http Client Builder
    // ///////////////////////////////////////////////////////////////////////////
    public static DefaultAsyncHttpClientConfig.Builder builder() {
        try {
            return new DefaultAsyncHttpClientConfig.Builder().setUseOpenSsl(useOpenSsl).setUseNativeTransport(useEpoll).setEventLoopGroup(evlg).setChannelPool(NoopChannelPool.INSTANCE).setHandshakeTimeout(DEFAULT_HANDSHAKE_TIMEOUT).setRequestTimeout(DEFAULT_REQUEST_TIMEOUT);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static DefaultAsyncHttpClientConfig.Builder builderWithClientCertificates() {
        try {
            return new DefaultAsyncHttpClientConfig.Builder().setUseNativeTransport(useEpoll).setEventLoopGroup(evlg).setChannelPool(NoopChannelPool.INSTANCE).setHandshakeTimeout(DEFAULT_HANDSHAKE_TIMEOUT).setRequestTimeout(DEFAULT_REQUEST_TIMEOUT).setSslEngineFactory(new DefaultSslEngineFactory() {

                @Override
                protected SslContextBuilder configureSslContextBuilder(@Nullable SslContextBuilder builder) {
                    if (builder == null)
                        throw new NullPointerException();
                    SslContextBuilder updated = builder.trustManager(SslKeyStore.getFingerPrintTrustManagerFactory()).keyManager(SslKeyStore.getKeyManagerFactory());
                    return super.configureSslContextBuilder(updated);
                }
            });
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    // ///////////////////////////////////////////////////////////////////////////
    // HTTP request utility methods
    // ///////////////////////////////////////////////////////////////////////////
    public static BoundRequestBuilder get(String uri) {
        return get().prepareGet(uri);
    }

    public static BoundRequestBuilder get(URI uri) {
        return get(uri.toString());
    }
}

See More Examples