use of org.eclipse.jetty.client.util.InputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithConcurrentServerCloseClosesStream.
@Test
public void testUploadWithConcurrentServerCloseClosesStream() throws Exception {
final CountDownLatch serverLatch = new CountDownLatch(1);
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);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
serverLatch.countDown();
}
});
final AtomicBoolean commit = new AtomicBoolean();
final CountDownLatch closeLatch = new CountDownLatch(1);
InputStream stream = new InputStream() {
@Override
public int read() throws IOException {
if (commit.get()) {
try {
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
connector.stop();
return 0;
} catch (Throwable x) {
throw new IOException(x);
}
} else {
return connector.isStopped() ? -1 : 0;
}
}
@Override
public void close() throws IOException {
super.close();
closeLatch.countDown();
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream, 1);
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(provider).onRequestCommit(request -> commit.set(true)).send(result -> {
Assert.assertTrue(result.isFailed());
completeLatch.countDown();
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccess.
@Slow
@Test
public void testTimeoutIsCancelledOnSuccess() throws Exception {
long timeout = 1000;
start(new TimeoutHandler(timeout));
final CountDownLatch latch = new CountDownLatch(1);
final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new InputStreamContentProvider(new ByteArrayInputStream(content))).timeout(2 * timeout, TimeUnit.MILLISECONDS);
request.send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
Assert.assertArrayEquals(content, getContent());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
TimeUnit.MILLISECONDS.sleep(2 * timeout);
Assert.assertNull(request.getAbortCause());
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project jetty.project by eclipse.
the class Usage method testRequestInputStream.
@Test
public void testRequestInputStream() throws Exception {
HttpClient client = new HttpClient();
client.start();
InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8));
ContentResponse response = client.newRequest("localhost", 8080).content(new InputStreamContentProvider(input)).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.
the class DefaultBulkApiClient method createBatch.
@Override
public void createBatch(InputStream batchStream, String jobId, ContentType contentTypeEnum, final BatchInfoResponseCallback callback) {
final Request post = getRequest(HttpMethod.POST, batchUrl(jobId, null));
post.content(new InputStreamContentProvider(batchStream));
post.header(HttpHeader.CONTENT_TYPE, getContentType(contentTypeEnum) + ";charset=" + StringUtil.__UTF8);
// make the call and parse the result
doHttpRequest(post, new ClientResponseCallback() {
@Override
public void onResponse(InputStream response, SalesforceException ex) {
BatchInfo value = null;
try {
value = unmarshalResponse(response, post, BatchInfo.class);
} catch (SalesforceException e) {
ex = e;
}
callback.onResponse(value, ex);
}
});
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.
the class DefaultRestClient method updateSObject.
@Override
public void updateSObject(String sObjectName, String id, InputStream sObject, ResponseCallback callback) {
final Request patch = getRequest("PATCH", sobjectsUrl(sObjectName + "/" + id));
// requires authorization token
setAccessToken(patch);
// input stream as entity content
patch.content(new InputStreamContentProvider(sObject));
patch.header(HttpHeader.CONTENT_TYPE, PayloadFormat.JSON.equals(format) ? APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8);
doHttpRequest(patch, new DelegatingClientCallback(callback));
}
Aggregations