use of org.eclipse.jetty.client.api.ContentProvider in project camel by apache.
the class DefaultCompositeApiClient method submitCompositeTree.
@Override
public void submitCompositeTree(final SObjectTree tree, final ResponseCallback<SObjectTreeResponse> callback) throws SalesforceException {
final String url = versionUrl() + "composite/tree/" + tree.getObjectType();
final Request post = createRequest(HttpMethod.POST, url);
final ContentProvider content = serialize(tree, tree.objectTypes());
post.content(content);
doHttpRequest(post, (response, exception) -> callback.onResponse(tryToReadResponse(SObjectTreeResponse.class, response), exception));
}
use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.
the class HttpClientTest method test_ExchangeIsComplete_WhenRequestFailsMidway_WithResponse.
@Test
public void test_ExchangeIsComplete_WhenRequestFailsMidway_WithResponse() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Echo back
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new ContentProvider() {
@Override
public long getLength() {
return -1;
}
@Override
public Iterator<ByteBuffer> iterator() {
return new Iterator<ByteBuffer>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public ByteBuffer next() {
throw new NoSuchElementException("explicitly_thrown_by_test");
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testClientRequestContentKnownLengthGzipped.
private void testClientRequestContentKnownLengthGzipped(int length, final boolean expectChunked) throws Exception {
byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
startServer(new EchoHttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String transferEncoding = request.getHeader(HttpHeader.TRANSFER_ENCODING.asString());
if (expectChunked)
Assert.assertNotNull(transferEncoding);
else
Assert.assertNull(transferEncoding);
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
super.service(request, response);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new GZIPContentTransformer(ContentTransformer.IDENTITY);
}
});
startClient();
byte[] gzipBytes = gzip(bytes);
ContentProvider gzipContent = new BytesContentProvider(gzipBytes);
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(gzipContent).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.api.ContentProvider in project jetty.project by eclipse.
the class ProxyServletFailureTest method testProxyRequestStallsContentServerIdlesTimeout.
@Test
public void testProxyRequestStallsContentServerIdlesTimeout() throws Exception {
final byte[] content = new byte[] { 'C', '0', 'F', 'F', 'E', 'E' };
int expected;
if (proxyServlet instanceof AsyncProxyServlet) {
// TODO should this be a 502 also???
expected = 500;
proxyServlet = new AsyncProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
DeferredContentProvider provider = new DeferredContentProvider() {
@Override
public boolean offer(ByteBuffer buffer, Callback callback) {
// Send less content to trigger the test condition.
buffer.limit(buffer.limit() - 1);
return super.offer(buffer.slice(), callback);
}
};
request.getInputStream().setReadListener(newReadListener(request, response, proxyRequest, provider));
return provider;
}
};
} else {
expected = 502;
proxyServlet = new ProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
return new BytesContentProvider(content) {
@Override
public long getLength() {
// Increase the content length to trigger the test condition.
return content.length + 1;
}
};
}
};
}
prepareProxy();
prepareServer(new EchoHttpServlet());
long idleTimeout = 1000;
serverConnector.setIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(content)).send();
Assert.assertEquals(expected, response.getStatus());
}
}
use of org.eclipse.jetty.client.api.ContentProvider 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