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();
}
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);
}
}
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));
}
}
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);
}
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);
}
}
Aggregations