use of com.ning.http.client.HttpResponseHeaders in project connect-java-library by urbanairship.
the class StreamConnectionResponseHandlerTest method testBodyReceivedBeforeBodyConsumeLatchReleasedHandlerClosesProperly.
@Test
public void testBodyReceivedBeforeBodyConsumeLatchReleasedHandlerClosesProperly() throws Exception {
int code = HttpURLConnection.HTTP_PAYMENT_REQUIRED;
String message = RandomStringUtils.randomAlphabetic(20);
HttpResponseStatus status = mock(HttpResponseStatus.class);
when(status.getStatusCode()).thenReturn(code);
when(status.getStatusText()).thenReturn(message);
HttpResponseHeaders headers = mock(HttpResponseHeaders.class);
when(headers.getHeaders()).thenReturn(new FluentCaseInsensitiveStringsMap(Collections.<String, Collection<String>>emptyMap()));
handler.onStatusReceived(status);
handler.onHeadersReceived(headers);
final CountDownLatch bodyReceivedThreadStarted = new CountDownLatch(1);
final CountDownLatch bodyReceivedThreadExit = new CountDownLatch(1);
final HttpResponseBodyPart part = mock(HttpResponseBodyPart.class);
ExecutorService bodyReceivedThread = Executors.newSingleThreadExecutor();
Runnable bodyReceivedRunnable = new Runnable() {
@Override
public void run() {
try {
bodyReceivedThreadStarted.countDown();
handler.onBodyPartReceived(part);
bodyReceivedThreadExit.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
};
try {
bodyReceivedThread.submit(bodyReceivedRunnable);
assertTrue(bodyReceivedThreadStarted.await(10, TimeUnit.SECONDS));
// Wait just an exta little bit to try to guarantee the body received call gets made...
Thread.sleep(2000L);
handler.stop();
assertTrue(bodyReceivedThreadExit.await(10, TimeUnit.SECONDS));
} finally {
bodyReceivedThread.shutdownNow();
}
}
use of com.ning.http.client.HttpResponseHeaders in project connect-java-library by urbanairship.
the class StreamConnectionResponseHandlerTest method testConnectFlow.
@Test
public void testConnectFlow() throws Exception {
int code = HttpURLConnection.HTTP_OK;
String message = RandomStringUtils.randomAlphabetic(20);
HttpResponseStatus status = mock(HttpResponseStatus.class);
when(status.getStatusCode()).thenReturn(code);
when(status.getStatusText()).thenReturn(message);
AsyncHandler.STATE state = handler.onStatusReceived(status);
assertEquals(AsyncHandler.STATE.CONTINUE, state);
HttpResponseHeaders headers = mock(HttpResponseHeaders.class);
Map<String, Collection<String>> headerMap = ImmutableMap.of(RandomStringUtils.randomAlphanumeric(5), ImmutableList.of(RandomStringUtils.randomAlphabetic(10)), RandomStringUtils.randomAlphanumeric(5), (Collection<String>) ImmutableList.of(RandomStringUtils.randomAlphabetic(10), RandomStringUtils.randomAlphabetic(5)));
when(headers.getHeaders()).thenReturn(new FluentCaseInsensitiveStringsMap(headerMap));
state = handler.onHeadersReceived(headers);
assertEquals(AsyncHandler.STATE.CONTINUE, state);
ArgumentCaptor<StatusAndHeaders> captor = ArgumentCaptor.forClass(StatusAndHeaders.class);
verify(connectCallback).connected(captor.capture());
StatusAndHeaders received = captor.getValue();
assertEquals(code, received.getStatusCode());
assertEquals(message, received.getStatusMessage());
assertEquals(headerMap, received.getHeaders());
}
use of com.ning.http.client.HttpResponseHeaders 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.HttpResponseHeaders 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.HttpResponseHeaders in project connect-java-library by urbanairship.
the class StreamConnectionResponseHandlerTest method testExceptionAfterConnect.
@Test
public void testExceptionAfterConnect() throws Exception {
int code = HttpURLConnection.HTTP_OK;
String message = RandomStringUtils.randomAlphabetic(20);
HttpResponseStatus status = mock(HttpResponseStatus.class);
when(status.getStatusCode()).thenReturn(code);
when(status.getStatusText()).thenReturn(message);
HttpResponseHeaders headers = mock(HttpResponseHeaders.class);
when(headers.getHeaders()).thenReturn(new FluentCaseInsensitiveStringsMap(Collections.<String, Collection<String>>emptyMap()));
handler.onStatusReceived(status);
handler.onHeadersReceived(headers);
Throwable exception = new RuntimeException("boom");
handler.onThrowable(exception);
Optional<Throwable> error = handler.getError();
assertTrue(error.isPresent());
assertEquals(exception, error.get());
}
Aggregations