You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/09/04 08:20:03 UTC

[1/3] httpcomponents-core git commit: Examples of using classic and asynchronous I/O request filters with embedded HTTP/1.1 servers

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master dc2e9285a -> 572fd1700


Examples of using classic and asynchronous I/O request filters with embedded HTTP/1.1 servers


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/7f7f4998
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/7f7f4998
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/7f7f4998

Branch: refs/heads/master
Commit: 7f7f499805d75f13fa924bbcb3a7b546a83818ef
Parents: dc2e928
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Sep 4 09:52:35 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Sep 4 09:52:35 2017 +0200

----------------------------------------------------------------------
 .../http/examples/AsyncServerFilterExample.java | 189 +++++++++++++++++++
 .../examples/ClassicServerFilterExample.java    | 157 +++++++++++++++
 2 files changed, 346 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/7f7f4998/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
new file mode 100644
index 0000000..cc07a66
--- /dev/null
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
@@ -0,0 +1,189 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.examples;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap;
+import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
+import org.apache.hc.core5.http.impl.bootstrap.StandardFilters;
+import org.apache.hc.core5.http.message.BasicHttpResponse;
+import org.apache.hc.core5.http.nio.AsyncDataConsumer;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.http.nio.AsyncFilterChain;
+import org.apache.hc.core5.http.nio.AsyncFilterHandler;
+import org.apache.hc.core5.http.nio.AsyncPushProducer;
+import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
+import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
+import org.apache.hc.core5.http.nio.BasicRequestConsumer;
+import org.apache.hc.core5.http.nio.BasicResponseProducer;
+import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
+import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.reactor.IOReactorConfig;
+import org.apache.hc.core5.reactor.ListenerEndpoint;
+import org.apache.hc.core5.util.TimeValue;
+
+/**
+ * Example of using asynchronous I/O request filters with an embedded HTTP/1.1 server.
+ */
+public class AsyncServerFilterExample {
+
+    public static void main(String[] args) throws Exception {
+        int port = 8080;
+        if (args.length >= 1) {
+            port = Integer.parseInt(args[0]);
+        }
+
+        IOReactorConfig config = IOReactorConfig.custom()
+                .setSoTimeout(15, TimeUnit.SECONDS)
+                .setTcpNoDelay(true)
+                .build();
+
+        final HttpAsyncServer server = AsyncServerBootstrap.bootstrap()
+                .setIOReactorConfig(config)
+
+                // Replace standard expect-continue handling with a custom auth filter
+
+                .replaceFilter(StandardFilters.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 HttpContext context) {
+                        return "let me pass".equals(challengeResponse);
+                    }
+
+                    @Override
+                    protected String generateChallenge(
+                            final String challengeResponse, final HttpContext context) {
+                        return "who goes there?";
+                    }
+
+                })
+
+                // Add a custom request filter at the beginning of the processing pipeline
+
+                .addFilterFirst("my-filter", new AsyncFilterHandler() {
+
+                    @Override
+                    public AsyncDataConsumer handle(
+                            final HttpRequest request,
+                            final EntityDetails entityDetails,
+                            final HttpContext context,
+                            final AsyncFilterChain.ResponseTrigger responseTrigger,
+                            final AsyncFilterChain chain) throws HttpException, IOException {
+                        if (request.getRequestUri().equals("/back-door")) {
+                            responseTrigger.submitResponse(
+                                    new BasicHttpResponse(HttpStatus.SC_OK),
+                                    new BasicAsyncEntityProducer("Welcome", ContentType.TEXT_PLAIN));
+                            return null;
+                        } else {
+                            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);
+                                }
+
+                            });
+                        }
+                    }
+
+                })
+
+                // Application request handler
+
+                .register("*", new AsyncServerRequestHandler<Message<HttpRequest, String>>() {
+
+                    @Override
+                    public AsyncRequestConsumer<Message<HttpRequest, String>> prepare(
+                            final HttpRequest request,
+                            final HttpContext context) throws HttpException {
+                        return new BasicRequestConsumer<>(new StringAsyncEntityConsumer());
+                    }
+
+                    @Override
+                    public void handle(
+                            final Message<HttpRequest, String> requestMessage,
+                            final ResponseTrigger responseTrigger,
+                            final HttpContext context) throws HttpException, IOException {
+                        // do something useful
+                        responseTrigger.submitResponse(new BasicResponseProducer(
+                                HttpStatus.SC_OK,
+                                new BasicAsyncEntityProducer("Hello", ContentType.TEXT_PLAIN)));
+                    }
+                })
+                .create();
+
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                System.out.println("HTTP server shutting down");
+                server.shutdown(ShutdownType.GRACEFUL);
+            }
+        });
+
+        server.start();
+        Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port));
+        ListenerEndpoint listenerEndpoint = future.get();
+        System.out.print("Listening on " + listenerEndpoint.getAddress());
+        server.awaitShutdown(TimeValue.ofDays(Long.MAX_VALUE));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/7f7f4998/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
new file mode 100644
index 0000000..a147716
--- /dev/null
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
@@ -0,0 +1,157 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.examples;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.config.SocketConfig;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.impl.bootstrap.StandardFilters;
+import org.apache.hc.core5.http.io.HttpFilterChain;
+import org.apache.hc.core5.http.io.HttpFilterHandler;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter;
+import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.util.TimeValue;
+
+/**
+ * Example of using classic I/O request filters with an embedded HTTP/1.1 server.
+ */
+public class ClassicServerFilterExample {
+
+    public static void main(String[] args) throws Exception {
+        int port = 8080;
+        if (args.length >= 1) {
+            port = Integer.parseInt(args[0]);
+        }
+        SocketConfig socketConfig = SocketConfig.custom()
+                .setSoTimeout(15, TimeUnit.SECONDS)
+                .setTcpNoDelay(true)
+                .build();
+
+        final HttpServer server = ServerBootstrap.bootstrap()
+                .setListenerPort(port)
+                .setSocketConfig(socketConfig)
+
+                // Replace standard expect-continue handling with a custom auth filter
+
+                .replaceFilter(StandardFilters.EXPECT_CONTINUE.name(), new AbstractHttpServerAuthFilter<String>(false) {
+
+                    @Override
+                    protected String parseChallengeResponse(
+                            final String authorizationValue, final HttpContext context) throws HttpException {
+                        return authorizationValue;
+                    }
+
+                    @Override
+                    protected boolean authenticate(
+                            final String challengeResponse, final HttpContext context) {
+                        return "let me pass".equals(challengeResponse);
+                    }
+
+                    @Override
+                    protected String generateChallenge(
+                            final String challengeResponse, final HttpContext context) {
+                        return "who goes there?";
+                    }
+
+                })
+
+                // Add a custom request filter at the beginning of the processing pipeline
+
+                .addFilterFirst("my-filter", new HttpFilterHandler() {
+
+                    @Override
+                    public void handle(final ClassicHttpRequest request,
+                                       final HttpFilterChain.ResponseTrigger responseTrigger,
+                                       final HttpContext context, HttpFilterChain chain) throws HttpException, IOException {
+                        if (request.getRequestUri().equals("/back-door")) {
+                            final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
+                            response.setEntity(new StringEntity("Welcome", ContentType.TEXT_PLAIN));
+                            responseTrigger.submitResponse(response);
+                        } else {
+                            chain.proceed(request, new HttpFilterChain.ResponseTrigger() {
+
+                                @Override
+                                public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException {
+                                    responseTrigger.sendInformation(response);
+                                }
+
+                                @Override
+                                public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
+                                    response.addHeader("X-Filter", "My-Filter");
+                                    responseTrigger.submitResponse(response);
+                                }
+
+                            }, context);
+                        }
+                    }
+
+                })
+
+                // Application request handler
+
+                .register("*", new HttpRequestHandler() {
+
+                    @Override
+                    public void handle(
+                            final ClassicHttpRequest request,
+                            final ClassicHttpResponse response,
+                            final HttpContext context) throws HttpException, IOException {
+                        // do something useful
+                        response.setCode(HttpStatus.SC_OK);
+                        response.setEntity(new StringEntity("Hello"));
+                    }
+
+                })
+                .create();
+
+        server.start();
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                server.shutdown(ShutdownType.GRACEFUL);
+            }
+        });
+        System.out.println("Listening on port " + port);
+
+        server.awaitTermination(TimeValue.ofDays(Long.MAX_VALUE));
+
+    }
+
+}


[2/3] httpcomponents-core git commit: Auth server filter to pass URI authority and request URI to #authenticate and #generateChallenge methods

Posted by ol...@apache.org.
Auth server filter to pass URI authority and request URI to #authenticate and #generateChallenge methods


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/59bff65e
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/59bff65e
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/59bff65e

Branch: refs/heads/master
Commit: 59bff65effd5d004cde9ff465ea53d97c8b6a5d2
Parents: 7f7f499
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Sep 4 10:01:02 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Sep 4 10:17:21 2017 +0200

----------------------------------------------------------------------
 .../testing/classic/ClassicAuthenticationTest.java   | 12 ++++++++++--
 .../core5/testing/nio/Http1AuthenticationTest.java   | 12 ++++++++++--
 .../http/examples/AsyncServerFilterExample.java      | 11 +++++++++--
 .../http/examples/ClassicServerFilterExample.java    | 11 +++++++++--
 .../io/support/AbstractHttpServerAuthFilter.java     | 15 ++++++++++-----
 .../nio/support/AbstractAsyncServerAuthFilter.java   | 15 ++++++++++-----
 6 files changed, 58 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
index 93e4fe5..2f9d9b3 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.util.Timeout;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -111,12 +112,19 @@ public class ClassicAuthenticationTest {
 
                         @Override
                         protected boolean authenticate(
-                                final String challengeResponse, final HttpContext context) {
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return challengeResponse != null && challengeResponse.equals("let me pass");
                         }
 
                         @Override
-                        protected String generateChallenge(final String challengeResponse, final HttpContext context) {
+                        protected String generateChallenge(
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return "who goes there?";
                         }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
index aa5a696..90e6b96 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
@@ -59,6 +59,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
 import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
@@ -125,12 +126,19 @@ public class Http1AuthenticationTest {
 
                         @Override
                         protected boolean authenticate(
-                                final String challengeResponse, final HttpContext context) {
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return challengeResponse != null && challengeResponse.equals("let me pass");
                         }
 
                         @Override
-                        protected String generateChallenge(final String challengeResponse, final HttpContext context) {
+                        protected String generateChallenge(
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return "who goes there?";
                         }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
index cc07a66..1ee1218 100644
--- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
 import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.util.TimeValue;
@@ -91,13 +92,19 @@ public class AsyncServerFilterExample {
 
                     @Override
                     protected boolean authenticate(
-                            final String challengeResponse, final HttpContext context) {
+                            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 HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "who goes there?";
                     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
index a147716..c94447a 100644
--- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
@@ -47,6 +47,7 @@ import org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.util.TimeValue;
 
 /**
@@ -80,13 +81,19 @@ public class ClassicServerFilterExample {
 
                     @Override
                     protected boolean authenticate(
-                            final String challengeResponse, final HttpContext context) {
+                            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 HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "who goes there?";
                     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
index 1024f94..a000c27 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
@@ -44,6 +44,7 @@ import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.net.URIAuthority;
 
 /**
  * @since 5.0
@@ -57,11 +58,11 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
         this.respondImmediately = respondImmediately;
     }
 
-    protected abstract T parseChallengeResponse(String challenge, HttpContext context) throws HttpException;
+    protected abstract T parseChallengeResponse(String authorizationValue, HttpContext context) throws HttpException;
 
-    protected abstract boolean authenticate(T challengeResponse, HttpContext context);
+    protected abstract boolean authenticate(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
-    protected abstract String generateChallenge(T challengeResponse, HttpContext context);
+    protected abstract String generateChallenge(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
     protected HttpEntity generateResponseContent(final HttpResponse unauthorized) {
         return new StringEntity("Unauthorized");
@@ -75,7 +76,11 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
             final HttpFilterChain chain) throws HttpException, IOException {
         final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
         final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
-        final boolean authenticated = authenticate(challengeResponse, context);
+
+        final URIAuthority authority = request.getAuthority();
+        final String requestUri = request.getRequestUri();
+
+        final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
         final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
         final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
 
@@ -86,7 +91,7 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
             chain.proceed(request, responseTrigger, context);
         } else {
             final ClassicHttpResponse unauthorized = new BasicClassicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
-            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, context));
+            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
             final HttpEntity responseContent = generateResponseContent(unauthorized);
             unauthorized.setEntity(responseContent);
             if (respondImmediately || expectContinue || request.getEntity() == null) {

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
index cb2fb26..443af98 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
@@ -48,6 +48,7 @@ import org.apache.hc.core5.http.nio.AsyncFilterHandler;
 import org.apache.hc.core5.http.nio.CapacityChannel;
 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.net.URIAuthority;
 
 /**
  * @since 5.0
@@ -61,11 +62,11 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
         this.respondImmediately = respondImmediately;
     }
 
-    protected abstract T parseChallengeResponse(String challenge, HttpContext context) throws HttpException;
+    protected abstract T parseChallengeResponse(String authorizationValue, HttpContext context) throws HttpException;
 
-    protected abstract boolean authenticate(T challengeResponse, HttpContext context);
+    protected abstract boolean authenticate(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
-    protected abstract String generateChallenge(T challengeResponse, HttpContext context);
+    protected abstract String generateChallenge(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
     protected AsyncEntityProducer generateResponseContent(final HttpResponse unauthorized) {
         return new BasicAsyncEntityProducer("Unauthorized");
@@ -80,7 +81,11 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
             final AsyncFilterChain chain) throws HttpException, IOException {
         final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
         final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
-        final boolean authenticated = authenticate(challengeResponse, context);
+
+        final URIAuthority authority = request.getAuthority();
+        final String requestUri = request.getRequestUri();
+
+        final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
         final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
         final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
 
@@ -91,7 +96,7 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
             return chain.proceed(request, entityDetails, context, responseTrigger);
         } else {
             final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
-            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, context));
+            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
             final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
             if (respondImmediately || expectContinue || entityDetails == null) {
                 responseTrigger.submitResponse(unauthorized, responseContentProducer);


[3/3] httpcomponents-core git commit: Added #getRoutes method to ConnPoolControl interface

Posted by ol...@apache.org.
Added #getRoutes method to ConnPoolControl interface


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/572fd170
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/572fd170
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/572fd170

Branch: refs/heads/master
Commit: 572fd170016a27d0893bc19a9fe7d202706d62fa
Parents: 59bff65
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Sep 4 10:17:56 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Sep 4 10:17:56 2017 +0200

----------------------------------------------------------------------
 .../hc/core5/http/impl/bootstrap/HttpAsyncRequester.java       | 6 ++++++
 .../org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java | 6 ++++++
 .../main/java/org/apache/hc/core5/pool/ConnPoolControl.java    | 4 ++++
 .../src/main/java/org/apache/hc/core5/pool/LaxConnPool.java    | 1 +
 .../src/main/java/org/apache/hc/core5/pool/StrictConnPool.java | 1 +
 5 files changed, 18 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/572fd170/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
index 8c37d31..5c63611 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http.impl.bootstrap;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -150,6 +151,11 @@ public class HttpAsyncRequester extends AsyncRequester implements ConnPoolContro
         connPool.closeExpired();
     }
 
+    @Override
+    public Set<HttpHost> getRoutes() {
+        return connPool.getRoutes();
+    }
+
     public Future<AsyncClientEndpoint> connect(
             final HttpHost host,
             final TimeValue timeout,

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/572fd170/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
index 0b8c3ce..b6489d6 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
@@ -32,6 +32,7 @@ import java.io.InterruptedIOException;
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeoutException;
@@ -151,6 +152,11 @@ public class HttpRequester implements ConnPoolControl<HttpHost>, GracefullyClose
         connPool.closeExpired();
     }
 
+    @Override
+    public Set<HttpHost> getRoutes() {
+        return connPool.getRoutes();
+    }
+
     public ClassicHttpResponse execute(
             final HttpClientConnection connection,
             final ClassicHttpRequest request,

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/572fd170/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolControl.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolControl.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolControl.java
index ac83c39..00019a1 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolControl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolControl.java
@@ -26,6 +26,8 @@
  */
 package org.apache.hc.core5.pool;
 
+import java.util.Set;
+
 import org.apache.hc.core5.util.TimeValue;
 
 /**
@@ -55,4 +57,6 @@ public interface ConnPoolControl<T> extends ConnPoolStats<T> {
 
     void closeExpired();
 
+    Set<T> getRoutes();
+
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/572fd170/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
index 06e2b9f..12347e2 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java
@@ -234,6 +234,7 @@ public class LaxConnPool<T, C extends GracefullyCloseable> implements ManagedCon
                 routePool.getMax());
     }
 
+    @Override
     public Set<T> getRoutes() {
         return new HashSet<>(routeToPool.keySet());
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/572fd170/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
index 8fc9ed0..a776747 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
@@ -507,6 +507,7 @@ public class StrictConnPool<T, C extends GracefullyCloseable> implements Managed
      *
      * @since 4.4
      */
+    @Override
     public Set<T> getRoutes() {
         this.lock.lock();
         try {