use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.
the class TestBasicAsyncEntityProducer method testTextContentRepeatable.
@Test
public void testTextContentRepeatable() throws Exception {
final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
Assertions.assertEquals(3, 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);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
producer.releaseResources();
}
}
use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project mercury by yellow013.
the class AsyncClientFullDuplexExchange method main.
public static void main(final String[] args) throws Exception {
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.NEGOTIATE, H2Config.DEFAULT, Http1Config.DEFAULT, ioReactorConfig);
client.start();
final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
final BasicRequestProducer requestProducer = new BasicRequestProducer(request, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new StringAsyncEntityConsumer());
System.out.println("Executing request " + request);
final CountDownLatch latch = new CountDownLatch(1);
client.execute(new AsyncClientExchangeHandler() {
@Override
public void releaseResources() {
requestProducer.releaseResources();
responseConsumer.releaseResources();
latch.countDown();
}
@Override
public void cancel() {
System.out.println(request + " cancelled");
}
@Override
public void failed(final Exception cause) {
System.out.println(request + "->" + cause);
}
@Override
public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
requestProducer.sendRequest(channel, context);
}
@Override
public int available() {
return requestProducer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
requestProducer.produce(channel);
}
@Override
public void consumeInformation(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
System.out.println(request + "->" + new StatusLine(response));
}
@Override
public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException {
System.out.println(request + "->" + new StatusLine(response));
responseConsumer.consumeResponse(response, entityDetails, context, null);
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
responseConsumer.updateCapacity(capacityChannel);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
responseConsumer.consume(src);
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
responseConsumer.streamEnd(trailers);
}
});
latch.await(1, TimeUnit.MINUTES);
System.out.println("Shutting down");
client.close(CloseMode.GRACEFUL);
}
use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.
the class TestBasicAsyncEntityProducer method testTextContent.
@Test
public void testTextContent() throws Exception {
final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
Assertions.assertEquals(3, 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);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.
the class TestBasicAsyncEntityProducer method testBinaryContent.
@Test
public void testBinaryContent() throws Exception {
final AsyncEntityProducer producer = new BasicAsyncEntityProducer(new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY);
Assertions.assertEquals(3, producer.getContentLength());
Assertions.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType());
Assertions.assertNull(producer.getContentEncoding());
final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer 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