use of com.ning.http.client.Request in project traccar by traccar.
the class StatisticsManager method checkSplit.
private void checkSplit() {
int currentUpdate = Calendar.getInstance().get(SPLIT_MODE);
if (lastUpdate.getAndSet(currentUpdate) != currentUpdate) {
Statistics statistics = new Statistics();
statistics.setCaptureTime(new Date());
statistics.setActiveUsers(users.size());
statistics.setActiveDevices(devices.size());
statistics.setRequests(requests);
statistics.setMessagesReceived(messagesReceived);
statistics.setMessagesStored(messagesStored);
statistics.setMailSent(mailSent);
statistics.setSmsSent(smsSent);
statistics.setGeocoderRequests(geocoderRequests);
statistics.setGeolocationRequests(geolocationRequests);
try {
Context.getDataManager().addObject(statistics);
} catch (SQLException e) {
Log.warning(e);
}
String url = Context.getConfig().getString("server.statistics");
if (url != null) {
String time = ISODateTimeFormat.dateTime().print(statistics.getCaptureTime().getTime());
Request request = new RequestBuilder("POST").setUrl(url).addHeader("Content-Type", "application/x-www-form-urlencoded").addFormParam("version", Log.getAppVersion()).addFormParam("captureTime", time).addFormParam("activeUsers", String.valueOf(statistics.getActiveUsers())).addFormParam("activeDevices", String.valueOf(statistics.getActiveDevices())).addFormParam("requests", String.valueOf(statistics.getRequests())).addFormParam("messagesReceived", String.valueOf(statistics.getMessagesReceived())).addFormParam("messagesStored", String.valueOf(statistics.getMessagesStored())).addFormParam("mailSent", String.valueOf(statistics.getMailSent())).addFormParam("smsSent", String.valueOf(statistics.getSmsSent())).addFormParam("geocoderRequests", String.valueOf(statistics.getGeocoderRequests())).addFormParam("geolocationRequests", String.valueOf(statistics.getGeolocationRequests())).build();
Context.getAsyncHttpClient().prepareRequest(request).execute();
}
users.clear();
devices.clear();
requests = 0;
messagesReceived = 0;
messagesStored = 0;
mailSent = 0;
smsSent = 0;
geocoderRequests = 0;
geolocationRequests = 0;
}
}
use of com.ning.http.client.Request in project riposte by Nike-Inc.
the class AsyncHttpClientHelperTest method verifyRequestBuilderWrapperGeneratedAsExpected.
private void verifyRequestBuilderWrapperGeneratedAsExpected(RequestBuilderWrapper rbw, String url, String method, Optional<CircuitBreaker<Response>> customCb, boolean disableCb) {
assertThat(rbw.url).isEqualTo(url);
assertThat(rbw.httpMethod).isEqualTo(method);
assertThat(rbw.customCircuitBreaker).isEqualTo(customCb);
assertThat(rbw.disableCircuitBreaker).isEqualTo(disableCb);
Request req = rbw.requestBuilder.build();
assertThat(req.getMethod()).isEqualTo(method);
assertThat(req.getUri()).isEqualTo(Uri.create(url));
assertThat(req.getUrl()).isEqualTo(url);
assertThat(req.getNameResolver()).isEqualTo(MultiIpAwareNameResolver.INSTANCE);
}
use of com.ning.http.client.Request in project jersey by jersey.
the class GrizzlyConnector method apply.
@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
final Request connectorRequest = translate(request);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
final ByteBufferInputStream entityStream = new ByteBufferInputStream();
final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
Throwable failure;
try {
return grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {
private volatile HttpResponseStatus status = null;
@Override
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
status = responseStatus;
return STATE.CONTINUE;
}
@Override
public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
if (!callbackInvoked.compareAndSet(false, true)) {
return STATE.ABORT;
}
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
// hand-off to grizzly's application thread pool for response processing
processResponse(new Runnable() {
@Override
public void run() {
callback.response(translate(request, status, headers, entityStream));
}
});
return STATE.CONTINUE;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
entityStream.put(bodyPart.getBodyByteBuffer());
return STATE.CONTINUE;
}
@Override
public Void onCompleted() throws Exception {
entityStream.closeQueue();
return null;
}
@Override
public void onThrowable(Throwable t) {
entityStream.closeQueue(t);
if (callbackInvoked.compareAndSet(false, true)) {
t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
callback.failure(t);
}
}
});
} catch (Throwable t) {
failure = t;
}
if (callbackInvoked.compareAndSet(false, true)) {
callback.failure(failure);
}
CompletableFuture<Object> future = new CompletableFuture<>();
future.completeExceptionally(failure);
return future;
}
use of com.ning.http.client.Request in project jersey by jersey.
the class GrizzlyConnector method apply.
/**
* Sends the {@link javax.ws.rs.core.Request} via Grizzly transport and returns the {@link javax.ws.rs.core.Response}.
*
* @param request Jersey client request to be sent.
* @return received response.
*/
@Override
public ClientResponse apply(final ClientRequest request) {
final Request connectorRequest = translate(request);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<>();
final ByteBufferInputStream entityStream = new ByteBufferInputStream();
final AtomicBoolean futureSet = new AtomicBoolean(false);
try {
grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {
private volatile HttpResponseStatus status = null;
@Override
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
status = responseStatus;
return STATE.CONTINUE;
}
@Override
public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
if (!futureSet.compareAndSet(false, true)) {
return STATE.ABORT;
}
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
responseFuture.complete(translate(request, this.status, headers, entityStream));
return STATE.CONTINUE;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
entityStream.put(bodyPart.getBodyByteBuffer());
return STATE.CONTINUE;
}
@Override
public Void onCompleted() throws Exception {
entityStream.closeQueue();
return null;
}
@Override
public void onThrowable(Throwable t) {
entityStream.closeQueue(t);
if (futureSet.compareAndSet(false, true)) {
t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
responseFuture.completeExceptionally(t);
}
}
});
return responseFuture.get();
} catch (ExecutionException ex) {
Throwable e = ex.getCause() == null ? ex : ex.getCause();
throw new ProcessingException(e.getMessage(), e);
} catch (InterruptedException ex) {
throw new ProcessingException(ex.getMessage(), ex);
}
}
use of com.ning.http.client.Request in project cdap by caskdata.
the class WorkflowClient method getWorkflowStatus.
public void getWorkflowStatus(String namespaceId, String appId, String workflowId, String runId, final Callback callback) throws IOException {
// determine the service provider for the given path
String serviceName = String.format("workflow.%s.%s.%s.%s", namespaceId, appId, workflowId, runId);
Discoverable discoverable = new RandomEndpointStrategy(discoveryServiceClient.discover(serviceName)).pick();
if (discoverable == null) {
LOG.debug("No endpoint for service {}", serviceName);
callback.handle(new Status(Status.Code.NOT_FOUND, ""));
return;
}
// make HTTP call to workflow service.
InetSocketAddress endpoint = discoverable.getSocketAddress();
// Construct request
String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME;
String url = String.format("%s%s:%d/status", scheme, endpoint.getHostName(), endpoint.getPort());
Request workflowRequest = new RequestBuilder("GET").setUrl(url).build();
httpClient.executeRequest(workflowRequest, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
callback.handle(new Status(Status.Code.OK, response.getResponseBody(Charsets.UTF_8.name())));
return null;
}
@Override
public void onThrowable(Throwable t) {
LOG.warn("Failed to request for workflow status", t);
callback.handle(new Status(Status.Code.ERROR, ""));
}
});
}
Aggregations