@Override public void sendBatchActivities(List<Activity> activities) throws PXException, IOException { HttpAsyncRequestProducer producer = null; try { String requestBody = JsonUtils.writer.writeValueAsString(activities); logger.info("Sending Activity: {}", requestBody); HttpPost post = new HttpPost(this.pxConfiguration.getServerURL() + Constants.API_ACTIVITIES); post.setEntity(new StringEntity(requestBody, UTF_8)); post.setConfig(PXCommonUtils.getRequestConfig(pxConfiguration.getConnectionTimeout(),pxConfiguration.getApiTimeout())); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + pxConfiguration.getAuthToken()); producer = HttpAsyncMethods.create(post); asyncHttpClient.execute(producer, new BasicAsyncResponseConsumer(), new PxClientAsyncHandler()); } catch (Exception e) { throw new PXException(e); } finally { if (producer != null) { producer.close(); } } }
@Override public void sendEnforcerTelemetry(EnforcerTelemetry enforcerTelemetry) throws PXException, IOException{ HttpAsyncRequestProducer producer = null; try { String requestBody = JsonUtils.writer.writeValueAsString(enforcerTelemetry); logger.info("Sending enforcer telemetry: {}", requestBody); HttpPost post = new HttpPost(this.pxConfiguration.getServerURL() + Constants.API_ENFORCER_TELEMETRY); post.setEntity(new StringEntity(requestBody, UTF_8)); PXCommonUtils.getDefaultHeaders(pxConfiguration.getAuthToken()); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + pxConfiguration.getAuthToken()); post.setConfig(PXCommonUtils.getRequestConfig(pxConfiguration.getConnectionTimeout(),pxConfiguration.getApiTimeout())); producer = HttpAsyncMethods.create(post); asyncHttpClient.execute(producer, new BasicAsyncResponseConsumer(), new PxClientAsyncHandler()); } catch (Exception e) { e.printStackTrace(); } finally { if (producer != null) { producer.close(); } } }
private Future<Response> executeRequest(Request request, HttpRequestBase method, HttpContext context, HTTPCallback<HttpResponse> callback) { if (request.isDownload()) { HttpAsyncRequestProducer producer = HttpAsyncMethods.create(method); HttpAsyncResponseConsumer<HttpResponse> consumer = new BasicAsyncResponseConsumer(); return executeRequest(producer, consumer, context, callback); } else return executeRequest(method, context, callback); }
/** * Invokes the given method with the given arguments and invokes the * {@code JsonRpcCallback} with the result cast to the given * {@code returnType}, or null if void. The {@code extraHeaders} are added * to the request. * * @param methodName the name of the method to invoke * @param argument the arguments to the method * @param extraHeaders extra headers to add to the request * @param returnType the return type * @param callback the {@code JsonRpcCallback} */ @SuppressWarnings("unchecked") private <T> Future<T> doInvoke(String methodName, Object argument, Class<T> returnType, Map<String, String> extraHeaders, JsonRpcCallback<T> callback) { String path = serviceUrl.getPath() + (serviceUrl.getQuery() != null ? "?" + serviceUrl.getQuery() : ""); int port = serviceUrl.getPort() != -1 ? serviceUrl.getPort() : serviceUrl.getDefaultPort(); HttpRequest request = new BasicHttpEntityEnclosingRequest("POST", path); addHeaders(request, headers); addHeaders(request, extraHeaders); try { writeRequest(methodName, argument, request); } catch (IOException e) { callback.onError(e); } HttpHost target = new HttpHost(serviceUrl.getHost(), port, serviceUrl.getProtocol()); BasicAsyncRequestProducer asyncRequestProducer = new BasicAsyncRequestProducer(target, request); BasicAsyncResponseConsumer asyncResponseConsumer = new BasicAsyncResponseConsumer(); RequestAsyncFuture<T> futureCallback = new RequestAsyncFuture<>(returnType, callback); BasicHttpContext httpContext = new BasicHttpContext(); requester.execute(asyncRequestProducer, asyncResponseConsumer, pool, httpContext, futureCallback); return (callback instanceof JsonRpcFuture ? (Future<T>) callback : null); }
/** * Invokes the given method with the given arguments and invokes the * {@code JsonRpcCallback} with the result cast to the given * {@code returnType}, or null if void. The {@code extraHeaders} are added * to the request. * * @param methodName * the name of the method to invoke * @param arguments * the arguments to the method * @param extraHeaders * extra headers to add to the request * @param returnType * the return type * @param callback * the {@code JsonRpcCallback} */ @SuppressWarnings("unchecked") private <T> Future<T> doInvoke(String methodName, Object argument, Class<T> returnType, Map<String, String> extraHeaders, JsonRpcCallback<T> callback) { String path = serviceUrl.getPath() + (serviceUrl.getQuery() != null ? "?" + serviceUrl.getQuery() : ""); int port = serviceUrl.getPort() != -1 ? serviceUrl.getPort() : serviceUrl.getDefaultPort(); // create the HttpRequest HttpRequest request = new BasicHttpEntityEnclosingRequest("POST", path); addHeaders(request, headers); addHeaders(request, extraHeaders); // create the JSON payload try { writeRequest(methodName, argument, request); } catch (IOException e) { callback.onError(e); } HttpHost target = new HttpHost(serviceUrl.getHost(), port, serviceUrl.getProtocol()); BasicAsyncRequestProducer asyncRequestProducer = new BasicAsyncRequestProducer( target, request); BasicAsyncResponseConsumer asyncResponseConsumer = new BasicAsyncResponseConsumer(); RequestAsyncFuture<T> futureCallback = new RequestAsyncFuture<T>( returnType, callback); BasicHttpContext httpContext = new BasicHttpContext(); requester.execute(asyncRequestProducer, asyncResponseConsumer, pool, httpContext, futureCallback); return (callback instanceof JsonRpcFuture ? (Future<T>) callback : null); }
/** * execute methods to execute a request */ private HttpResponse execute(HttpUriRequest request) throws APIException { return execute(request, new BasicAsyncResponseConsumer()); }
@Override public Future<HttpResponse> execute(final HttpAsyncClient httpClient) throws FileNotFoundException { final HttpAsyncResponseConsumer<HttpResponse> consumer = new BasicAsyncResponseConsumer(); final HttpAsyncRequestProducer producer = this.getProducer(); return httpClient.execute(producer, consumer, null); }