Search in sources :

Example 1 with AbstractAsyncServerAuthFilter

use of org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter 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);
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) Message(org.apache.hc.core5.http.Message) BasicRequestConsumer(org.apache.hc.core5.http.nio.support.BasicRequestConsumer) InetSocketAddress(java.net.InetSocketAddress) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) AsyncFilterChain(org.apache.hc.core5.http.nio.AsyncFilterChain) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) IOException(java.io.IOException) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) AbstractAsyncServerAuthFilter(org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 EntityDetails (org.apache.hc.core5.http.EntityDetails)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 Message (org.apache.hc.core5.http.Message)1 HttpAsyncServer (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)1 AsyncFilterChain (org.apache.hc.core5.http.nio.AsyncFilterChain)1 AsyncPushProducer (org.apache.hc.core5.http.nio.AsyncPushProducer)1 AsyncServerRequestHandler (org.apache.hc.core5.http.nio.AsyncServerRequestHandler)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 AbstractAsyncServerAuthFilter (org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter)1 BasicRequestConsumer (org.apache.hc.core5.http.nio.support.BasicRequestConsumer)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1 URIAuthority (org.apache.hc.core5.net.URIAuthority)1 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)1 ListenerEndpoint (org.apache.hc.core5.reactor.ListenerEndpoint)1