use of org.apache.hc.core5.http.nio.AsyncPushProducer in project httpcomponents-core by apache.
the class TerminalAsyncServerFilter method handle.
@Override
public AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
if (exchangeHandler != null) {
exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {
@Override
public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.sendInformation(response);
}
@Override
public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {
@Override
public void failed(final Exception cause) {
exchangeHandler.failed(cause);
}
@Override
public boolean isRepeatable() {
return false;
}
@Override
public long getContentLength() {
return entityDetails.getContentLength();
}
@Override
public String getContentType() {
return entityDetails.getContentType();
}
@Override
public String getContentEncoding() {
return entityDetails.getContentEncoding();
}
@Override
public boolean isChunked() {
return entityDetails.isChunked();
}
@Override
public Set<String> getTrailerNames() {
return entityDetails.getTrailerNames();
}
@Override
public int available() {
return exchangeHandler.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
exchangeHandler.produce(channel);
}
@Override
public void releaseResources() {
exchangeHandler.releaseResources();
}
} : null);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.pushPromise(promise, pushProducer);
}
}, context);
return exchangeHandler;
}
responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
return null;
}
use of org.apache.hc.core5.http.nio.AsyncPushProducer 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.AsyncPushProducer in project httpcomponents-core by apache.
the class AsyncServerFilterChainExchangeHandlerFactory method create.
@Override
public AsyncServerExchangeHandler create(final HttpRequest request, final HttpContext context) throws HttpException {
return new AsyncServerExchangeHandler() {
private final AtomicReference<AsyncDataConsumer> dataConsumerRef = new AtomicReference<>();
private final AtomicReference<AsyncResponseProducer> responseProducerRef = new AtomicReference<>();
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
dataConsumerRef.set(filterChain.handle(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
@Override
public void sendInformation(final HttpResponse response) throws HttpException, IOException {
responseChannel.sendInformation(response, context);
}
@Override
public void submitResponse(final HttpResponse response, final AsyncEntityProducer entityProducer) throws HttpException, IOException {
final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, entityProducer);
responseProducerRef.set(responseProducer);
responseProducer.sendResponse(responseChannel, context);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
responseChannel.pushPromise(promise, responseProducer, context);
}
}));
}
@Override
public void failed(final Exception cause) {
if (exceptionCallback != null) {
exceptionCallback.execute(cause);
}
final AsyncResponseProducer handler = responseProducerRef.get();
if (handler != null) {
handler.failed(cause);
}
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.updateCapacity(capacityChannel);
} else {
capacityChannel.update(Integer.MAX_VALUE);
}
}
@Override
public void consume(final ByteBuffer src) throws IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.consume(src);
}
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.streamEnd(trailers);
}
}
@Override
public int available() {
final AsyncResponseProducer responseProducer = responseProducerRef.get();
Asserts.notNull(responseProducer, "Response producer");
return responseProducer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
final AsyncResponseProducer responseProducer = responseProducerRef.get();
Asserts.notNull(responseProducer, "Response producer");
responseProducer.produce(channel);
}
@Override
public void releaseResources() {
final AsyncDataConsumer dataConsumer = dataConsumerRef.getAndSet(null);
if (dataConsumer != null) {
dataConsumer.releaseResources();
}
final AsyncResponseProducer responseProducer = responseProducerRef.getAndSet(null);
if (responseProducer != null) {
responseProducer.releaseResources();
}
}
};
}
use of org.apache.hc.core5.http.nio.AsyncPushProducer in project httpcomponents-core by apache.
the class AsyncServerFilterExample method main.
public static void main(final String[] args) throws Exception {
int port = 8080;
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
final HttpAsyncServer server = AsyncServerBootstrap.bootstrap().setIOReactorConfig(config).replaceFilter(StandardFilter.EXPECT_CONTINUE.name(), new AbstractAsyncServerAuthFilter<String>(true) {
@Override
protected String parseChallengeResponse(final String authorizationValue, final HttpContext context) throws HttpException {
return authorizationValue;
}
@Override
protected boolean authenticate(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
return "let me pass".equals(challengeResponse);
}
@Override
protected String generateChallenge(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
return "who goes there?";
}
}).addFilterFirst("my-filter", (request, entityDetails, context, responseTrigger, chain) -> {
if (request.getRequestUri().equals("/back-door")) {
responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_OK), AsyncEntityProducers.create("Welcome"));
return null;
}
return chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
@Override
public void sendInformation(final HttpResponse response) throws HttpException, IOException {
responseTrigger.sendInformation(response);
}
@Override
public void submitResponse(final HttpResponse response, final AsyncEntityProducer entityProducer) throws HttpException, IOException {
response.addHeader("X-Filter", "My-Filter");
responseTrigger.submitResponse(response, entityProducer);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
responseTrigger.pushPromise(promise, responseProducer);
}
});
}).register("*", new AsyncServerRequestHandler<Message<HttpRequest, String>>() {
@Override
public AsyncRequestConsumer<Message<HttpRequest, String>> prepare(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context) throws HttpException {
return new BasicRequestConsumer<>(entityDetails != null ? new StringAsyncEntityConsumer() : null);
}
@Override
public void handle(final Message<HttpRequest, String> requestMessage, final ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException {
// do something useful
responseTrigger.submitResponse(AsyncResponseBuilder.create(HttpStatus.SC_OK).setEntity("Hello").build(), context);
}
}).create();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("HTTP server shutting down");
server.close(CloseMode.GRACEFUL);
}));
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port), URIScheme.HTTP);
final ListenerEndpoint listenerEndpoint = future.get();
System.out.print("Listening on " + listenerEndpoint.getAddress());
server.awaitShutdown(TimeValue.MAX_VALUE);
}
use of org.apache.hc.core5.http.nio.AsyncPushProducer in project httpcomponents-core by apache.
the class ServerPushH2StreamHandler method produceOutput.
@Override
public void produceOutput() throws HttpException, IOException {
switch(responseState) {
case IDLE:
responseState = MessageState.HEADERS;
pushProducer.produceResponse(new ResponseChannel() {
@Override
public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
commitInformation(response);
}
@Override
public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
commitResponse(response, entityDetails);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
commitPromise(promise, pushProducer);
}
}, context);
break;
case BODY:
pushProducer.produce(dataChannel);
break;
}
}
Aggregations