use of com.ning.http.client.RequestBuilder 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, ""));
}
});
}
use of com.ning.http.client.RequestBuilder in project tez by apache.
the class AsyncHttpConnection method connect.
/**
* Connect to source
*
* @return true if connection was successful
* false if connection was previously cleaned up
* @throws IOException upon connection failure
*/
public boolean connect() throws IOException, InterruptedException {
computeEncHash();
RequestBuilder rb = new RequestBuilder();
rb.setHeader(SecureShuffleUtils.HTTP_HEADER_URL_HASH, encHash);
rb.setHeader(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
rb.setHeader(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
Request request = rb.setUrl(url.toString()).build();
// for debugging
LOG.debug("Request url={}, encHash={}, id={}", url, encHash);
try {
// Blocks calling thread until it receives headers, but have the option to defer response body
responseFuture = httpAsyncClient.executeRequest(request, handler);
// BodyDeferringAsyncHandler would automatically manage producer and consumer frequency mismatch
dis = new TezBodyDeferringAsyncHandler.BodyDeferringInputStream(responseFuture, handler, pis);
response = dis.getAsapResponse();
if (response == null) {
throw new IOException("Response is null");
}
} catch (IOException e) {
throw e;
}
// verify the response
int rc = response.getStatusCode();
if (rc != HttpURLConnection.HTTP_OK) {
LOG.debug("Request url={}, id={}", response.getUri());
throw new IOException("Got invalid response code " + rc + " from " + url + ": " + response.getStatusText());
}
return true;
}
use of com.ning.http.client.RequestBuilder in project cdap by caskdata.
the class NettyRouterPipelineTest method testChunkRequestSuccess.
@Test
public void testChunkRequestSuccess() throws Exception {
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
byte[] requestBody = generatePostData();
final Request request = new RequestBuilder("POST").setUrl(String.format("http://%s:%d%s", HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME), "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
return null;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
// TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
content.writeTo(byteArrayOutputStream);
return super.onBodyPartReceived(content);
}
});
future.get();
Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
use of com.ning.http.client.RequestBuilder in project cdap by caskdata.
the class NettyRouterTestBase method testRouterAsync.
@Test
public void testRouterAsync() throws Exception {
int numElements = 123;
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
final CountDownLatch latch = new CountDownLatch(numElements);
final AtomicInteger numSuccessfulRequests = new AtomicInteger(0);
for (int i = 0; i < numElements; ++i) {
final int elem = i;
final Request request = new RequestBuilder("GET").setUrl(resolveURI(DEFAULT_SERVICE, String.format("%s/%s-%d", "/v1/echo", "async", i))).build();
asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
latch.countDown();
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
String responseBody = response.getResponseBody();
LOG.trace("Got response {}", responseBody);
Assert.assertEquals("async-" + elem, responseBody);
numSuccessfulRequests.incrementAndGet();
return null;
}
@Override
public void onThrowable(Throwable t) {
LOG.error("Got exception while posting {}", elem, t);
latch.countDown();
}
});
// Sleep so as not to overrun the server.
TimeUnit.MILLISECONDS.sleep(1);
}
latch.await();
asyncHttpClient.close();
Assert.assertEquals(numElements, numSuccessfulRequests.get());
// we use sticky endpoint strategy so the sum of requests from the two gateways should be NUM_ELEMENTS
Assert.assertTrue(numElements == (defaultServer1.getNumRequests() + defaultServer2.getNumRequests()));
}
use of com.ning.http.client.RequestBuilder in project cdap by caskdata.
the class NettyRouterTestBase method testUpload.
@Test
public void testUpload() throws Exception {
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
byte[] requestBody = generatePostData();
final Request request = new RequestBuilder("POST").setUrl(resolveURI(DEFAULT_SERVICE, "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
return null;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
// TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
content.writeTo(byteArrayOutputStream);
return super.onBodyPartReceived(content);
}
});
future.get();
Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Aggregations