[java-support] branch master updated: Support to enforce unique handler instances and optionally unique type
Brent Putman
putmanb at georgetown.edu
Sun Feb 23 21:45:48 EST 2020
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch master
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=74db2de08d1a9c7fff9129fd8c06f55ab7e78042
The following commit(s) were added to refs/heads/master by this push:
new 74db2de Support to enforce unique handler instances and optionally unique type
74db2de is described below
commit 74db2de08d1a9c7fff9129fd8c06f55ab7e78042
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Sun Feb 23 18:44:00 2020 -0500
Support to enforce unique handler instances and optionally unique type
---
.../java/support/httpclient/HttpClientSupport.java | 40 +++++++++++-
.../support/httpclient/HttpClientSupportTest.java | 71 ++++++++++++++++++++++
2 files changed, 109 insertions(+), 2 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
index c84c5ef..b857e5a 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
@@ -157,8 +157,26 @@ public final class HttpClientSupport {
*/
public static void addDynamicContextHandlerFirst(@Nonnull final HttpClientContext context,
@Nonnull final HttpClientContextHandler handler) {
+ addDynamicContextHandlerFirst(context, handler, false);
+ }
+
+ /**
+ * Add the specified instance of {@link HttpClientContextHandler}
+ * to the {@link HttpClientContext} in the first handler list position.
+ *
+ * @param context the client context
+ * @param handler the handler to add
+ * @param uniqueType whether to only add the handler if an instance of its (exact) class is not already present
+ */
+ public static void addDynamicContextHandlerFirst(@Nonnull final HttpClientContext context,
+ @Nonnull final HttpClientContextHandler handler, final boolean uniqueType) {
Constraint.isNotNull(handler, "HttpClientContextHandler was null");
- getDynamicContextHandlerList(context).add(0, handler);
+ final List<HttpClientContextHandler> list = getDynamicContextHandlerList(context);
+ if (list.contains(handler)
+ || (uniqueType && list.stream().anyMatch(h -> handler.getClass().equals(h.getClass())))) {
+ return;
+ }
+ list.add(0, handler);
}
/**
@@ -170,8 +188,26 @@ public final class HttpClientSupport {
*/
public static void addDynamicContextHandlerLast(@Nonnull final HttpClientContext context,
@Nonnull final HttpClientContextHandler handler) {
+ addDynamicContextHandlerLast(context, handler, false);
+ }
+
+ /**
+ * Add the specified instance of {@link HttpClientContextHandler}
+ * to the {@link HttpClientContext} in the last handler list position.
+ *
+ * @param context the client context
+ * @param handler the handler to add
+ * @param uniqueType whether to only add the handler if an instance of its (exact) class is not already present
+ */
+ public static void addDynamicContextHandlerLast(@Nonnull final HttpClientContext context,
+ @Nonnull final HttpClientContextHandler handler, final boolean uniqueType) {
Constraint.isNotNull(handler, "HttpClientContextHandler was null");
- getDynamicContextHandlerList(context).add(handler);
+ final List<HttpClientContextHandler> list = getDynamicContextHandlerList(context);
+ if (list.contains(handler)
+ || (uniqueType && list.stream().anyMatch(h -> handler.getClass().equals(h.getClass())))) {
+ return;
+ }
+ list.add(handler);
}
// Checkstyle: CyclomaticComplexity OFF
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupportTest.java
index 4eb37ee..ebd71ab 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupportTest.java
@@ -28,6 +28,7 @@ import com.google.common.collect.Lists;
public class HttpClientSupportTest {
+
@Test
public void testAddDynamicContextHandlerFirst() {
HttpClientContext context = HttpClientContext.create();
@@ -55,6 +56,76 @@ public class HttpClientSupportTest {
Assert.assertEquals(HttpClientSupport.getDynamicContextHandlerList(context), Lists.newArrayList(one, two, three));
}
+
+ @Test
+ public void testAddDynamicContextHandlerFirstUniqueInstances() {
+ HttpClientContext context = HttpClientContext.create();
+ HttpClientContextHandler one = new TestContextHandler();
+ HttpClientContextHandler two = new TestContextHandler();
+ HttpClientContextHandler three = new TestContextHandler();
+
+ HttpClientSupport.addDynamicContextHandlerFirst(context, one);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, one);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, two);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, one);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, two);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, three);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, two);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, three);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, two);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, one);
+
+ Assert.assertEquals(HttpClientSupport.getDynamicContextHandlerList(context), Lists.newArrayList(three, two, one));
+ }
+
+ @Test
+ public void testAddDynamicContextHandlerLastUniqueInstances() {
+ HttpClientContext context = HttpClientContext.create();
+ HttpClientContextHandler one = new TestContextHandler();
+ HttpClientContextHandler two = new TestContextHandler();
+ HttpClientContextHandler three = new TestContextHandler();
+
+ HttpClientSupport.addDynamicContextHandlerLast(context, one);
+ HttpClientSupport.addDynamicContextHandlerLast(context, one);
+ HttpClientSupport.addDynamicContextHandlerLast(context, two);
+ HttpClientSupport.addDynamicContextHandlerLast(context, one);
+ HttpClientSupport.addDynamicContextHandlerLast(context, two);
+ HttpClientSupport.addDynamicContextHandlerLast(context, three);
+ HttpClientSupport.addDynamicContextHandlerLast(context, two);
+ HttpClientSupport.addDynamicContextHandlerLast(context, three);
+ HttpClientSupport.addDynamicContextHandlerLast(context, two);
+ HttpClientSupport.addDynamicContextHandlerLast(context, one);
+
+ Assert.assertEquals(HttpClientSupport.getDynamicContextHandlerList(context), Lists.newArrayList(one, two, three));
+ }
+
+ @Test
+ public void testAddDynamicContextHandlerFirstUniqueType() {
+ HttpClientContext context = HttpClientContext.create();
+ HttpClientContextHandler one = new TestContextHandler();
+ HttpClientContextHandler two = new TestContextHandler();
+ HttpClientContextHandler three = new TestContextHandler();
+
+ HttpClientSupport.addDynamicContextHandlerFirst(context, one, true);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, two, true);
+ HttpClientSupport.addDynamicContextHandlerFirst(context, three, true);
+
+ Assert.assertEquals(HttpClientSupport.getDynamicContextHandlerList(context), Lists.newArrayList(one));
+ }
+
+ @Test
+ public void testAddDynamicContextHandlerLastUniqueType() {
+ HttpClientContext context = HttpClientContext.create();
+ HttpClientContextHandler one = new TestContextHandler();
+ HttpClientContextHandler two = new TestContextHandler();
+ HttpClientContextHandler three = new TestContextHandler();
+
+ HttpClientSupport.addDynamicContextHandlerLast(context, one, true);
+ HttpClientSupport.addDynamicContextHandlerLast(context, two, true);
+ HttpClientSupport.addDynamicContextHandlerLast(context, three, true);
+
+ Assert.assertEquals(HttpClientSupport.getDynamicContextHandlerList(context), Lists.newArrayList(one));
+ }
//Helpers
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list