use of org.apache.hc.core5.http.nio.support.BasicPushProducer in project httpcomponents-core by apache.
the class H2IntegrationTest method testPushRefused.
@Test
public void testPushRefused() throws Exception {
final BlockingQueue<Exception> pushResultQueue = new LinkedBlockingDeque<>();
final InetSocketAddress serverEndpoint = server.start();
server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
@Override
public AsyncServerExchangeHandler get() {
return new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
@Override
protected void handle(final Message<HttpRequest, Void> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/stuff")), context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) {
@Override
public void failed(final Exception cause) {
pushResultQueue.add(cause);
super.failed(cause);
}
});
responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/more-stuff")), context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) {
@Override
public void failed(final Exception cause) {
pushResultQueue.add(cause);
super.failed(cause);
}
});
responseTrigger.submitResponse(new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")), context);
}
};
}
});
client.start(H2Config.custom().setPushEnabled(true).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
final String entity1 = result1.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi there", entity1);
final Object result2 = pushResultQueue.poll(5, TimeUnit.SECONDS);
Assertions.assertNotNull(result2);
Assertions.assertTrue(result2 instanceof H2StreamResetException);
Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result2).getCode());
final Object result3 = pushResultQueue.poll(5, TimeUnit.SECONDS);
Assertions.assertNotNull(result3);
Assertions.assertTrue(result3 instanceof H2StreamResetException);
Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result3).getCode());
}
use of org.apache.hc.core5.http.nio.support.BasicPushProducer in project httpcomponents-core by apache.
the class AsyncPushBuilder method build.
@Override
public AsyncPushProducer build() {
final HttpResponse response = new BasicHttpResponse(getStatus());
response.setVersion(getVersion());
response.setHeaders(getHeaders());
return new BasicPushProducer(response, entityProducer);
}
use of org.apache.hc.core5.http.nio.support.BasicPushProducer in project httpcomponents-core by apache.
the class H2IntegrationTest method testPush.
@Test
public void testPush() throws Exception {
final InetSocketAddress serverEndpoint = server.start();
server.register("/hello", () -> new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
@Override
protected void handle(final Message<HttpRequest, Void> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/stuff")), context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)));
responseTrigger.submitResponse(AsyncResponseBuilder.create(HttpStatus.SC_OK).setEntity("Hi there", ContentType.TEXT_PLAIN).build(), context);
}
});
client.start(H2Config.custom().setPushEnabled(true).build());
final BlockingQueue<Message<HttpResponse, String>> pushMessageQueue = new LinkedBlockingDeque<>();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), (request, context) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
@Override
protected void handleResponse(final HttpRequest promise, final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
try {
pushMessageQueue.put(responseMessage);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}, null, null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
final String entity1 = result1.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi there", entity1);
final Message<HttpResponse, String> result2 = pushMessageQueue.poll(5, TimeUnit.SECONDS);
Assertions.assertNotNull(result2);
final HttpResponse response2 = result2.getHead();
final String entity2 = result2.getBody();
Assertions.assertEquals(200, response2.getCode());
Assertions.assertNotNull(entity2);
final StringTokenizer t1 = new StringTokenizer(entity2, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
}
}
Aggregations