use of org.apache.hc.core5.http.nio.entity.FileEntityProducer in project httpcomponents-core by apache.
the class TestFileAsyncEntityProducer method testTextContentRepeatable.
@Test
public void testTextContentRepeatable() throws Exception {
final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
Assertions.assertEquals(6, producer.getContentLength());
Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
Assertions.assertNull(producer.getContentEncoding());
for (int i = 0; i < 3; i++) {
final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
producer.produce(streamChannel);
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII));
producer.releaseResources();
}
}
use of org.apache.hc.core5.http.nio.entity.FileEntityProducer in project httpcomponents-core by apache.
the class TestFileAsyncEntityProducer method testTextContent.
@Test
public void testTextContent() throws Exception {
final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
Assertions.assertEquals(6, producer.getContentLength());
Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
Assertions.assertNull(producer.getContentEncoding());
final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
producer.produce(streamChannel);
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http.nio.entity.FileEntityProducer in project httpcomponents-core by apache.
the class BenchmarkWorker method createRequestProducer.
private AsyncRequestProducer createRequestProducer() {
String method = config.getMethod();
if (method == null) {
method = config.isHeadInsteadOfGet() ? Method.HEAD.name() : Method.GET.name();
}
final BasicHttpRequest request = new BasicHttpRequest(method, config.getUri());
final String[] headers = config.getHeaders();
if (headers != null) {
for (final String s : headers) {
final int pos = s.indexOf(':');
if (pos != -1) {
request.addHeader(new BasicHeader(s.substring(0, pos).trim(), s.substring(pos + 1)));
}
}
}
if (!config.isKeepAlive() && !config.isForceHttp2()) {
request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE));
}
if (config.isUseAcceptGZip()) {
request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
}
if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
request.addHeader(new BasicHeader("SOAPAction", config.getSoapAction()));
}
final AsyncEntityProducer entityProducer;
if (config.getPayloadFile() != null) {
entityProducer = new FileEntityProducer(config.getPayloadFile(), config.getContentType(), config.isUseChunking());
} else if (config.getPayloadText() != null) {
entityProducer = new BasicAsyncEntityProducer(config.getPayloadText(), config.getContentType(), config.isUseChunking());
} else {
entityProducer = null;
}
return new AsyncRequestProducer() {
@Override
public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
channel.sendRequest(request, entityProducer, context);
}
@Override
public boolean isRepeatable() {
return entityProducer == null || entityProducer.isRepeatable();
}
@Override
public int available() {
return entityProducer != null ? entityProducer.available() : 0;
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
if (entityProducer != null) {
entityProducer.produce(channel);
}
}
@Override
public void failed(final Exception cause) {
if (config.getVerbosity() >= 1) {
System.out.println("Failed HTTP request: " + cause.getMessage());
}
}
@Override
public void releaseResources() {
if (entityProducer != null) {
entityProducer.releaseResources();
}
}
};
}
Aggregations