use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class ProxyServletTest method testProxyWithBigRequestContentIgnored.
@Test
public void testProxyWithBigRequestContentIgnored() throws Exception {
startServer(new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Give some time to the proxy to
// upload the content to the server.
Thread.sleep(1000);
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
startProxy();
startClient();
byte[] content = new byte[128 * 1024];
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).method(HttpMethod.POST).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDownstreamTransformationBufferedGzipped.
@Test
public void testDownstreamTransformationBufferedGzipped() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
int read;
while ((read = input.read()) >= 0) {
output.write(read);
output.flush();
}
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new BufferingContentTransformer());
}
});
startClient();
byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsBeforeCommittingProxyRequest.
@Test
public void testUpstreamTransformationThrowsBeforeCommittingProxyRequest() throws Exception {
startServer(new EchoHttpServlet());
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new ContentTransformer() {
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
throw new NullPointerException("explicitly_thrown_by_test");
}
};
}
});
startClient();
byte[] bytes = new byte[1024];
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(bytes)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(500, response.getStatus());
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project camel by apache.
the class DefaultBulkApiClient method marshalRequest.
private void marshalRequest(Object input, Request request, String contentType) throws SalesforceException {
try {
Marshaller marshaller = context.createMarshaller();
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
marshaller.marshal(input, byteStream);
request.content(new BytesContentProvider(contentType, byteStream.toByteArray()));
} catch (JAXBException e) {
throw new SalesforceException(String.format("Error marshaling request for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
} catch (IllegalArgumentException e) {
throw new SalesforceException(String.format("Error marshaling request for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
}
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project camel by apache.
the class DefaultBulkApiClient method createBatchQuery.
@Override
public void createBatchQuery(String jobId, String soqlQuery, ContentType jobContentType, final BatchInfoResponseCallback callback) {
final Request post = getRequest(HttpMethod.POST, batchUrl(jobId, null));
final byte[] queryBytes;
try {
queryBytes = soqlQuery.getBytes(StringUtil.__UTF8);
} catch (UnsupportedEncodingException e) {
callback.onResponse(null, new SalesforceException("Unexpected exception: " + e.getMessage(), e));
return;
}
post.content(new BytesContentProvider(queryBytes));
post.header(HttpHeader.CONTENT_TYPE, getContentType(jobContentType) + ";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);
}
});
}
Aggregations