Search in sources :

Example 1 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project Singularity by HubSpot.

the class LoadBalancerClientImpl method cancel.

@Override
public SingularityLoadBalancerUpdate cancel(LoadBalancerRequestId loadBalancerRequestId) {
    final String uri = getLoadBalancerUri(loadBalancerRequestId);
    final BoundRequestBuilder requestBuilder = httpClient.prepareDelete(uri);
    if (loadBalancerQueryParams.isPresent()) {
        addAllQueryParams(requestBuilder, loadBalancerQueryParams.get());
    }
    return sendRequestWrapper(loadBalancerRequestId, LoadBalancerMethod.CANCEL, requestBuilder.build(), BaragonRequestState.UNKNOWN);
}
Also used : BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder)

Example 2 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project Singularity by HubSpot.

the class SingularityWebhookSender method executeWebhookAsync.

// TODO handle retries, errors.
private <T> CompletableFuture<Response> executeWebhookAsync(SingularityWebhook webhook, Object payload, AbstractSingularityWebhookAsyncHandler<T> handler) {
    LOG.trace("Sending {} to {}", payload, webhook.getUri());
    BoundRequestBuilder postRequest = http.preparePost(webhook.getUri());
    postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    try {
        postRequest.setBody(objectMapper.writeValueAsBytes(payload));
    } catch (JsonProcessingException e) {
        throw Throwables.propagate(e);
    }
    CompletableFuture<Response> webhookFuture = new CompletableFuture<>();
    try {
        handler.setCompletableFuture(webhookFuture);
        postRequest.execute(handler);
    } catch (IOException e) {
        LOG.warn("Couldn't execute webhook to {}", webhook.getUri(), e);
        if (handler.shouldDeleteUpdateDueToQueueAboveCapacity()) {
            handler.deleteWebhookUpdate();
        }
        webhookFuture.completeExceptionally(e);
    }
    return webhookFuture;
}
Also used : Response(com.ning.http.client.Response) BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project traccar by traccar.

the class EventForwarder method forwardEvent.

public final void forwardEvent(Event event, Position position, Set<Long> users) {
    BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url);
    requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name());
    requestBuilder.addHeader("Content-Type", getContentType());
    if (header != null && !header.isEmpty()) {
        FluentCaseInsensitiveStringsMap params = new FluentCaseInsensitiveStringsMap();
        params.putAll(splitIntoKeyValues(header, ":"));
        requestBuilder.setHeaders(params);
    }
    setContent(event, position, users, requestBuilder);
    requestBuilder.execute();
}
Also used : BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) FluentCaseInsensitiveStringsMap(com.ning.http.client.FluentCaseInsensitiveStringsMap)

Example 4 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project apex-core by apache.

the class PubSubWebSocketClient method openConnection.

/**
 * <p>openConnection.</p>
 *
 * @param timeoutMillis
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 * @throws TimeoutException
 */
public void openConnection(long timeoutMillis) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    throwable.set(null);
    List<Cookie> cookies = null;
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        Response response = client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute().get();
        cookies = response.getCookies();
    }
    BoundRequestBuilder brb = client.prepareGet(uri.toString());
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            brb.addCookie(cookie);
        }
    }
    connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get(timeoutMillis, TimeUnit.MILLISECONDS);
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)

Example 5 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project apex-core by apache.

the class PubSubWebSocketClient method openConnectionAsync.

public void openConnectionAsync() throws IOException {
    throwable.set(null);
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                List<Cookie> cookies = response.getCookies();
                BoundRequestBuilder brb = client.prepareGet(uri.toString());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        brb.addCookie(cookie);
                    }
                }
                connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get();
                return response;
            }
        });
    } else {
        final PubSubWebSocket webSocket = new PubSubWebSocket() {

            @Override
            public void onOpen(WebSocket ws) {
                connection = ws;
                super.onOpen(ws);
            }
        };
        client.prepareGet(uri.toString()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(webSocket).build());
    }
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler) TimeoutException(java.util.concurrent.TimeoutException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(org.codehaus.jackson.JsonParseException) JSONException(org.codehaus.jettison.json.JSONException) WebSocket(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List)

Aggregations

BoundRequestBuilder (com.ning.http.client.AsyncHttpClient.BoundRequestBuilder)15 IOException (java.io.IOException)12 Response (com.ning.http.client.Response)3 ExecutionException (java.util.concurrent.ExecutionException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 FluentCaseInsensitiveStringsMap (com.ning.http.client.FluentCaseInsensitiveStringsMap)2 BoundRequestBuilder (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder)2 Response (org.apache.apex.shaded.ning19.com.ning.http.client.Response)2 Cookie (org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie)2 WebSocketUpgradeHandler (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)2 JsonParseException (org.codehaus.jackson.JsonParseException)2 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)2 JSONException (org.codehaus.jettison.json.JSONException)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 SingularityLoadBalancerUpdate (com.hubspot.singularity.SingularityLoadBalancerUpdate)1 Request (com.ning.http.client.Request)1 FilePart (com.ning.http.multipart.FilePart)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1