Search in sources :

Example 1 with AuthenticationDataProvider

use of com.yahoo.pulsar.client.api.AuthenticationDataProvider in project pulsar by yahoo.

the class HttpClient method get.

public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
    final CompletableFuture<T> future = new CompletableFuture<>();
    try {
        String requestUrl = new URL(url, path).toString();
        AuthenticationDataProvider authData = authentication.getAuthData();
        BoundRequestBuilder builder = httpClient.prepareGet(requestUrl);
        // Add headers for authentication if any
        if (authData.hasDataForHttp()) {
            for (Map.Entry<String, String> header : authData.getHttpHeaders()) {
                builder.setHeader(header.getKey(), header.getValue());
            }
        }
        final ListenableFuture<Response> responseFuture = builder.setHeader("Accept", "application/json").execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                log.warn("[{}] Failed to perform http request: {}", requestUrl, t.getMessage());
                future.completeExceptionally(new PulsarClientException(t));
            }
        });
        responseFuture.addListener(() -> {
            try {
                Response response = responseFuture.get();
                if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
                    log.warn("[{}] HTTP get request failed: {}", requestUrl, response.getStatusText());
                    future.completeExceptionally(new PulsarClientException("HTTP get request failed: " + response.getStatusText()));
                    return;
                }
                T data = ObjectMapperFactory.getThreadLocal().readValue(response.getResponseBodyAsBytes(), clazz);
                future.complete(data);
            } catch (Exception e) {
                log.warn("[{}] Error during HTTP get request: {}", requestUrl, e.getMessage());
                future.completeExceptionally(new PulsarClientException(e));
            }
        }, MoreExecutors.sameThreadExecutor());
    } catch (Exception e) {
        log.warn("[{}] Failed to get authentication data for lookup: {}", path, e.getMessage());
        if (e instanceof PulsarClientException) {
            future.completeExceptionally(e);
        } else {
            future.completeExceptionally(new PulsarClientException(e));
        }
    }
    return future;
}
Also used : AuthenticationDataProvider(com.yahoo.pulsar.client.api.AuthenticationDataProvider) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) HttpResponse(io.netty.handler.codec.http.HttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) Map(java.util.Map)

Aggregations

AuthenticationDataProvider (com.yahoo.pulsar.client.api.AuthenticationDataProvider)1 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1