Search in sources :

Example 11 with BoundRequestBuilder

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

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 12 with BoundRequestBuilder

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

the class AbstractLeaderAwareResource method maybeProxyToLeader.

protected <T, Q> T maybeProxyToLeader(HttpServletRequest request, Class<T> clazz, Q body, Supplier<T> runnable) {
    if (leaderLatch.hasLeadership()) {
        return runnable.get();
    }
    String leaderUri;
    try {
        leaderUri = leaderLatch.getLeader().getId();
    } catch (Exception e) {
        throw new RuntimeException("Could not get leader uri to proxy request");
    }
    if (leaderUri.equals(leaderLatch.getId())) {
        LOG.warn("Got own leader id when not the leader! There is likely no leader, will not proxy");
        return runnable.get();
    }
    String url = "http://" + leaderUri + request.getContextPath() + request.getPathInfo();
    LOG.debug("Not the leader, proxying request to {}", url);
    BoundRequestBuilder requestBuilder;
    switch(request.getMethod().toUpperCase()) {
        case "POST":
            requestBuilder = httpClient.preparePost(url);
            break;
        case "PUT":
            requestBuilder = httpClient.preparePut(url);
            break;
        case "DELETE":
            requestBuilder = httpClient.prepareDelete(url);
            break;
        default:
            throw new WebApplicationException(String.format("Not meant to proxy request of method %s", request.getMethod()), 400);
    }
    try {
        if (body != null) {
            requestBuilder.setBody(objectMapper.writeValueAsBytes(body));
            LOG.trace("Added body {} to reqeust", body);
        }
    } catch (JsonProcessingException jpe) {
        LOG.error("Could not write body from object {}", body);
        throw new WebApplicationException(jpe, 500);
    }
    copyHeadersAndParams(requestBuilder, request);
    Request httpRequest = requestBuilder.build();
    Response response;
    try {
        LOG.trace("Sending request to leader: {}", httpRequest);
        response = httpClient.executeRequest(httpRequest).get();
    } catch (IOException | ExecutionException | InterruptedException e) {
        LOG.error("Could not proxy request {} to leader", e);
        throw new WebApplicationException(e, 500);
    }
    try {
        if (response.getStatusCode() > 399) {
            throw new WebApplicationException(response.getResponseBody(Charsets.UTF_8.toString()), response.getStatusCode());
        } else {
            return objectMapper.readValue(response.getResponseBodyAsStream(), clazz);
        }
    } catch (IOException ioe) {
        String message = String.format("Request to leader succeeded with status %s, but could not interpret response", response.getStatusCode());
        LOG.error(message, ioe);
        throw new WebApplicationException(message, ioe, 500);
    }
}
Also used : Response(com.ning.http.client.Response) BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) Request(com.ning.http.client.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 13 with BoundRequestBuilder

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

the class LoadBalancerClientImpl method sendBaragonRequest.

private SingularityLoadBalancerUpdate sendBaragonRequest(LoadBalancerRequestId loadBalancerRequestId, BaragonRequest loadBalancerRequest, LoadBalancerMethod method) {
    try {
        LOG.trace("Preparing to send request {}", loadBalancerRequest);
        final BoundRequestBuilder requestBuilder = httpClient.preparePost(loadBalancerUri).addHeader(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON).setBody(objectMapper.writeValueAsBytes(loadBalancerRequest));
        if (loadBalancerQueryParams.isPresent()) {
            addAllQueryParams(requestBuilder, loadBalancerQueryParams.get());
        }
        return sendRequestWrapper(loadBalancerRequestId, method, requestBuilder.build(), BaragonRequestState.FAILED);
    } catch (IOException e) {
        return new SingularityLoadBalancerUpdate(BaragonRequestState.UNKNOWN, loadBalancerRequestId, Optional.of(e.getMessage()), System.currentTimeMillis(), method, Optional.of(loadBalancerUri));
    }
}
Also used : SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) IOException(java.io.IOException)

Example 14 with BoundRequestBuilder

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

the class LoadBalancerClientImpl method getState.

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

Example 15 with BoundRequestBuilder

use of com.ning.http.client.AsyncHttpClient.BoundRequestBuilder in project new-cloud by xie-summer.

the class AsyncHttpUtils method getUrlAsString.

/**
 * 通过GET方法请求url
 *
 * @param url
 * @param params
 */
public static void getUrlAsString(String url, Map<String, String> params, HttpResultCallback callback) {
    if (StringUtils.isBlank(url)) {
        return;
    }
    String gurl = getFullUrl(url, params, "utf-8");
    try {
        BoundRequestBuilder rb = getDefault().prepareGet(gurl);
        rb.setHeader("Accept-Encoding", "gzip,deflate");
        rb.execute(new AsynchHandler(callback));
    } catch (IOException e) {
        DB_LOGGER.error(e, 30);
    }
}
Also used : BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) IOException(java.io.IOException)

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