use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithDeferredContentProviderRacingWithIterator.
@Test
public void testUploadWithDeferredContentProviderRacingWithIterator() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final CountDownLatch latch = new CountDownLatch(1);
final byte[] data = new byte[512];
final AtomicReference<DeferredContentProvider> contentRef = new AtomicReference<>();
final DeferredContentProvider content = new DeferredContentProvider() {
@Override
public Iterator<ByteBuffer> iterator() {
return new Iterator<ByteBuffer>() {
// Data for the deferred content iterator:
// [0] => deferred
// [1] => deferred
// [2] => data
private final byte[][] iteratorData = new byte[3][];
private final AtomicInteger index = new AtomicInteger();
{
iteratorData[0] = null;
iteratorData[1] = null;
iteratorData[2] = data;
}
@Override
public boolean hasNext() {
return index.get() < iteratorData.length;
}
@Override
public ByteBuffer next() {
byte[] chunk = iteratorData[index.getAndIncrement()];
ByteBuffer result = chunk == null ? null : ByteBuffer.wrap(chunk);
if (index.get() < iteratorData.length) {
contentRef.get().offer(result == null ? BufferUtil.EMPTY_BUFFER : result);
contentRef.get().close();
}
return result;
}
@Override
public void remove() {
}
};
}
};
contentRef.set(content);
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientSynchronizationTest method testSynchronizationOnComplete.
@Test
public void testSynchronizationOnComplete() throws Exception {
start(new EmptyServerHandler());
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme);
synchronized (this) {
request.send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
synchronized (HttpClientSynchronizationTest.this) {
Assert.assertFalse(result.isFailed());
latch.countDown();
}
}
});
}
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpSender method terminateRequest.
private void terminateRequest(HttpExchange exchange) {
// In abort(), the state is updated before the failure is recorded
// to avoid to overwrite it, so here we may read a null failure.
Throwable failure = this.failure;
if (failure == null)
failure = new HttpRequestException("Concurrent failure", exchange.getRequest());
Result result = exchange.terminateRequest();
terminateRequest(exchange, failure, result);
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpChannelOverHTTP method exchangeTerminating.
@Override
public Result exchangeTerminating(HttpExchange exchange, Result result) {
if (result.isFailed())
return result;
HttpResponse response = exchange.getResponse();
if ((response.getVersion() == HttpVersion.HTTP_1_1) && (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)) {
String connection = response.getHeaders().get(HttpHeader.CONNECTION);
if ((connection == null) || !connection.toLowerCase(Locale.US).contains("upgrade")) {
return new Result(result, new HttpResponseException("101 Switching Protocols without Connection: Upgrade not supported", response));
}
// Upgrade Response
HttpRequest request = exchange.getRequest();
if (request instanceof HttpConnectionUpgrader) {
HttpConnectionUpgrader listener = (HttpConnectionUpgrader) request;
try {
listener.upgrade(response, getHttpConnection());
} catch (Throwable x) {
return new Result(result, x);
}
}
}
return result;
}
use of org.eclipse.jetty.client.api.Result in project jetty.project by eclipse.
the class HttpClientAsyncContentTest method testSmallAsyncContent.
@Test
public void testSmallAsyncContent() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ServletOutputStream output = response.getOutputStream();
output.write(65);
output.flush();
output.write(66);
}
});
final AtomicInteger contentCount = new AtomicInteger();
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final AtomicReference<CountDownLatch> contentLatch = new AtomicReference<>(new CountDownLatch(1));
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContentAsync(new Response.AsyncContentListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
contentCount.incrementAndGet();
callbackRef.set(callback);
contentLatch.get().countDown();
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completeLatch.countDown();
}
});
Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
Callback callback = callbackRef.get();
// Wait a while to be sure that the parsing does not proceed.
TimeUnit.MILLISECONDS.sleep(1000);
Assert.assertEquals(1, contentCount.get());
// Succeed the content callback to proceed with parsing.
callbackRef.set(null);
contentLatch.set(new CountDownLatch(1));
callback.succeeded();
Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
callback = callbackRef.get();
// Wait a while to be sure that the parsing does not proceed.
TimeUnit.MILLISECONDS.sleep(1000);
Assert.assertEquals(2, contentCount.get());
Assert.assertEquals(1, completeLatch.getCount());
// Succeed the content callback to proceed with parsing.
callbackRef.set(null);
contentLatch.set(new CountDownLatch(1));
callback.succeeded();
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(2, contentCount.get());
}
Aggregations