use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InTwoBuffers.
@Test
public void test_Send_SmallRequestContent_InTwoBuffers() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
destination.start();
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
Request request = client.newRequest(URI.create("http://localhost/"));
String content1 = "0123456789";
String content2 = "abcdef";
request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))));
final CountDownLatch headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(1);
request.listener(new Request.Listener.Adapter() {
@Override
public void onHeaders(Request request) {
headersLatch.countDown();
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
});
connection.send(request, null);
String requestString = endPoint.takeOutputString();
Assert.assertTrue(requestString.startsWith("GET "));
Assert.assertThat(requestString, Matchers.endsWith("\r\n\r\n" + content1 + content2));
Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.
the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InOneBuffer.
@Test
public void test_Send_SmallRequestContent_InOneBuffer() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
destination.start();
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
Request request = client.newRequest(URI.create("http://localhost/"));
String content = "abcdef";
request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8))));
final CountDownLatch headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(1);
request.listener(new Request.Listener.Adapter() {
@Override
public void onHeaders(Request request) {
headersLatch.countDown();
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
});
connection.send(request, null);
String requestString = endPoint.takeOutputString();
Assert.assertTrue(requestString.startsWith("GET "));
Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.ByteBufferContentProvider 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();
}
});
}
Aggregations