use of org.eclipse.jetty.client.util.InputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamContentProviderThrowingWhileReading.
@Test(expected = ExecutionException.class)
public void testInputStreamContentProviderThrowingWhileReading() throws Exception {
start(new AbstractHandler.ErrorDispatchHandler() {
@Override
public void doNonErrorHandle(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 byte[] data = new byte[] { 0, 1, 2, 3 };
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new InputStreamContentProvider(new InputStream() {
private int index = 0;
@Override
public int read() throws IOException {
// Will eventually throw ArrayIndexOutOfBounds
return data[index++];
}
}, data.length / 2)).timeout(5, TimeUnit.SECONDS).send();
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project jetty.project by eclipse.
the class HttpClientStreamTest method testUploadWithConnectFailureClosesStream.
@Test
public void testUploadWithConnectFailureClosesStream() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch closeLatch = new CountDownLatch(1);
InputStream stream = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)) {
@Override
public void close() throws IOException {
super.close();
closeLatch.countDown();
}
};
InputStreamContentProvider content = new InputStreamContentProvider(stream);
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("0.0.0.1", connector.getLocalPort()).scheme(getScheme()).content(content).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 camel by apache.
the class AbstractClientBase method doHttpRequest.
protected void doHttpRequest(final Request request, final ClientResponseCallback callback) {
// Highly memory inefficient,
// but buffer the request content to allow it to be replayed for authentication retries
final ContentProvider content = request.getContent();
if (content instanceof InputStreamContentProvider) {
final List<ByteBuffer> buffers = new ArrayList<>();
for (ByteBuffer buffer : content) {
buffers.add(buffer);
}
request.content(new ByteBufferContentProvider(buffers.toArray(new ByteBuffer[buffers.size()])));
buffers.clear();
}
// execute the request
request.send(new BufferingResponseListener(httpClient.getMaxContentLength()) {
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
if (result.isFailed()) {
// Failure!!!
// including Salesforce errors reported as exception from SalesforceSecurityHandler
Throwable failure = result.getFailure();
if (failure instanceof SalesforceException) {
callback.onResponse(null, (SalesforceException) failure);
} else {
final String msg = String.format("Unexpected error {%s:%s} executing {%s:%s}", response.getStatus(), response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, response.getStatus(), failure));
}
} else {
// HTTP error status
final int status = response.getStatus();
SalesforceHttpRequest request = (SalesforceHttpRequest) ((SalesforceHttpRequest) result.getRequest()).getConversation().getAttribute(SalesforceSecurityHandler.AUTHENTICATION_REQUEST_ATTRIBUTE);
if (status == HttpStatus.BAD_REQUEST_400 && request != null) {
// parse login error
ContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding());
try {
session.parseLoginResponse(contentResponse, getContentAsString());
final String msg = String.format("Unexpected Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, null));
} catch (SalesforceException e) {
final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, response.getStatus(), e));
}
} else if (status < HttpStatus.OK_200 || status >= HttpStatus.MULTIPLE_CHOICES_300) {
// Salesforce HTTP failure!
request = (SalesforceHttpRequest) result.getRequest();
final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
final SalesforceException cause = createRestException(response, getContentAsInputStream());
// for APIs that return body on status 400, such as Composite API we need content as well
callback.onResponse(getContentAsInputStream(), new SalesforceException(msg, response.getStatus(), cause));
} else {
// Success!!!
callback.onResponse(getContentAsInputStream(), null);
}
}
}
@Override
public InputStream getContentAsInputStream() {
if (getContent().length == 0) {
return null;
}
return super.getContentAsInputStream();
}
});
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.
the class DefaultRestClient method upsertSObject.
@Override
public void upsertSObject(String sObjectName, String fieldName, String fieldValue, InputStream sObject, ResponseCallback callback) {
final Request patch = getRequest("PATCH", sobjectsExternalIdUrl(sObjectName, fieldName, fieldValue));
// requires authorization token
setAccessToken(patch);
// input stream as entity content
patch.content(new InputStreamContentProvider(sObject));
// TODO will the encoding always be UTF-8??
patch.header(HttpHeader.CONTENT_TYPE, PayloadFormat.JSON.equals(format) ? APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8);
doHttpRequest(patch, new DelegatingClientCallback(callback));
}
use of org.eclipse.jetty.client.util.InputStreamContentProvider in project camel by apache.
the class DefaultRestClient method approval.
@Override
public void approval(final InputStream request, final ResponseCallback callback) {
final Request post = getRequest(HttpMethod.POST, versionUrl() + "process/approvals/");
// authorization
setAccessToken(post);
// input stream as entity content
post.content(new InputStreamContentProvider(request));
post.header(HttpHeader.CONTENT_TYPE, PayloadFormat.JSON.equals(format) ? APPLICATION_JSON_UTF8 : APPLICATION_XML_UTF8);
doHttpRequest(post, new DelegatingClientCallback(callback));
}
Aggregations