You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2019/02/07 16:43:16 UTC

[httpcomponents-core] branch HTTPCORE-570 updated: Drop uriPatternType and merge from master.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch HTTPCORE-570
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/HTTPCORE-570 by this push:
     new 8594308  Drop uriPatternType and merge from master.
8594308 is described below

commit 85943085100dbe882af05a2fb947a996eeb76802
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Feb 7 11:43:15 2019 -0500

    Drop uriPatternType and merge from master.
---
 .../http/impl/bootstrap/AsyncServerBootstrap.java  | 14 +------
 .../hc/core5/http/protocol/UriPatternMatcher.java  | 44 ++++++++++++++++++--
 .../hc/core5/http/protocol/UriPatternType.java     | 20 ++++++---
 .../core5/http/protocol/TestUriPatternMatcher.java | 13 ++++++
 .../core5/http/protocol/TestUriPatternType.java}   | 48 ++++++++++++++--------
 5 files changed, 100 insertions(+), 39 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
index 08448ab..cb3b5f4 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
@@ -77,7 +77,6 @@ public class AsyncServerBootstrap {
     private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
     private final List<FilterEntry<AsyncFilterHandler>> filters;
     private String canonicalHostName;
-    private UriPatternType uriPatternType;
     private LookupRegistry<AsyncServerExchangeHandler> lookupRegistry;
     private IOReactorConfig ioReactorConfig;
     private H1Config h1Config;
@@ -201,14 +200,6 @@ public class AsyncServerBootstrap {
     }
 
     /**
-     * Assigns {@link UriPatternType} for handler registration.
-     */
-    public final AsyncServerBootstrap setUriPatternType(final UriPatternType uriPatternType) {
-        this.uriPatternType = uriPatternType;
-        return this;
-    }
-
-    /**
      * Registers the given {@link AsyncServerExchangeHandler} {@link Supplier} as a default handler for URIs
      * matching the given pattern.
      *
@@ -335,16 +326,13 @@ public class AsyncServerBootstrap {
     }
 
     public HttpAsyncServer create() {
-        if (lookupRegistry != null && uriPatternType != null) {
-            throw new IllegalStateException("You MUST only set one of lookupRegistry or uriPatternType.");
-        }
         final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
                 canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
                 new Supplier() {
 
                     @Override
                     public LookupRegistry get() {
-                        return lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(uriPatternType);
+                        return lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN);
                     }
 
                 });
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternMatcher.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternMatcher.java
index 6f3f01d..ce4b5e3 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternMatcher.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternMatcher.java
@@ -38,15 +38,31 @@ import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.util.Args;
 
 /**
- * Maintains a map of objects keyed by a request URI pattern. <br>
+ * Maintains a map of objects keyed by a request URI pattern.
+ * <p>
  * Patterns may have three formats:
+ * </p>
  * <ul>
  * <li>{@code *}</li>
  * <li>{@code *<uri>}</li>
  * <li>{@code <uri>*}</li>
  * </ul>
- * <br>
- * This class can be used to resolve an object matching a particular request URI.
+ * <p>
+ * This class can be used to resolve an object matching a particular request
+ * URI.
+ * </p>
+ * <p>
+ * By default, exact matches override other patterns: "*" and patterns that
+ * start and end with a "*".
+ * </p>
+ * <p>
+ * The order of pattern registration matters for patterns: "*" and patterns that
+ * start and end with a "*".
+ * </p>
+ * <p>
+ * You can disable exact matches by passing {@code false} to
+ * {@link #UriPatternMatcher(boolean)}.
+ * </p>
  *
  * @param <T> The type of registered objects.
  * @since 4.0
@@ -55,10 +71,19 @@ import org.apache.hc.core5.util.Args;
 public class UriPatternMatcher<T> implements LookupRegistry<T> {
 
     private final Map<String, T> map;
+    private final boolean exactMatchOverride;
 
     public UriPatternMatcher() {
+        this(true);
+    }
+
+    /**
+     * @since 5.0
+     */
+    public UriPatternMatcher(final boolean exactMatchOverride) {
         super();
         this.map = new LinkedHashMap<>();
+        this.exactMatchOverride = exactMatchOverride;
     }
 
     /**
@@ -74,6 +99,17 @@ public class UriPatternMatcher<T> implements LookupRegistry<T> {
     }
 
     /**
+     * Returns whether this exact matches override other patterns; the "*", and the
+     * ones starting and ending with a "*".
+     *
+     * @return whether this exact matches override other patterns
+     * @since 5.0
+     */
+    boolean isExactMatchOverride() {
+        return exactMatchOverride;
+    }
+
+    /**
      * Registers the given object for URIs matching the given pattern.
      *
      * @param pattern
@@ -112,7 +148,7 @@ public class UriPatternMatcher<T> implements LookupRegistry<T> {
     public synchronized T lookup(final String path) {
         Args.notNull(path, "Request path");
         // direct match?
-        T obj = this.map.get(path);
+        T obj = exactMatchOverride ? this.map.get(path) : null;
         if (obj == null) {
             // pattern match?
             String bestMatch = null;
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java
index d06d642..2aeb2aa 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java
@@ -32,17 +32,27 @@ package org.apache.hc.core5.http.protocol;
  */
 public enum UriPatternType {
 
-    BASIC, REGEX;
+    /**
+     * @deprecated Use {@link #URI_PATTERN_ORDER_EXACT_OVERRIDE}
+     */
+    @Deprecated
+    BASIC,
+
+    REGEX, URI_PATTERN, URI_PATTERN_IN_ORDER;
 
     public static <T> LookupRegistry<T> newMatcher(final UriPatternType type) {
         if (type == null) {
             return new UriPatternMatcher<>();
         }
         switch (type) {
-            case REGEX:
-                return new UriRegexMatcher<>();
-            default:
-                return new UriPatternMatcher<>();
+        case REGEX:
+            return new UriRegexMatcher<>();
+        case URI_PATTERN:
+            return new UriPatternMatcher<>();
+        case URI_PATTERN_IN_ORDER:
+            return new UriPatternMatcher<>(false);
+        default:
+            return new UriPatternMatcher<>(true);
         }
     }
 
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternMatcher.java b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternMatcher.java
index ef45281..3a41664 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternMatcher.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternMatcher.java
@@ -166,4 +166,17 @@ public class TestUriPatternMatcher {
         matcher.lookup(null);
     }
 
+    @Test
+    public void testNoExactMatchOverride() {
+        final Object h1 = new Object();
+        final Object h2 = new Object();
+
+        final LookupRegistry<Object> matcher = new UriPatternMatcher<>(false);
+        matcher.register("*", h1);
+        matcher.register("exact", h2);
+
+        final Object h = matcher.lookup("/exact");
+        Assert.assertNotNull(h);
+        Assert.assertTrue(h1 == h);
+    }
 }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternType.java
similarity index 50%
copy from httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java
copy to httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternType.java
index d06d642..ccc426a 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/UriPatternType.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/protocol/TestUriPatternType.java
@@ -27,23 +27,37 @@
 
 package org.apache.hc.core5.http.protocol;
 
-/**
- * @since 5.0
- */
-public enum UriPatternType {
-
-    BASIC, REGEX;
-
-    public static <T> LookupRegistry<T> newMatcher(final UriPatternType type) {
-        if (type == null) {
-            return new UriPatternMatcher<>();
-        }
-        switch (type) {
-            case REGEX:
-                return new UriRegexMatcher<>();
-            default:
-                return new UriPatternMatcher<>();
-        }
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUriPatternType {
+
+    @Test
+    public void testBasic() {
+        @SuppressWarnings("deprecation")
+        final LookupRegistry<Object> matcher = UriPatternType.newMatcher(UriPatternType.BASIC);
+        Assert.assertTrue(matcher instanceof UriPatternMatcher);
+        Assert.assertTrue(((UriPatternMatcher<?>) matcher).isExactMatchOverride());
+    }
+
+    @Test
+    public void testRegex() {
+        final LookupRegistry<Object> matcher = UriPatternType.newMatcher(UriPatternType.REGEX);
+        Assert.assertTrue(matcher instanceof UriRegexMatcher);
+    }
+
+    @Test
+    public void testUriPattern() {
+        final LookupRegistry<Object> matcher = UriPatternType.newMatcher(UriPatternType.URI_PATTERN);
+        Assert.assertTrue(matcher instanceof UriPatternMatcher);
+        Assert.assertTrue(((UriPatternMatcher<?>) matcher).isExactMatchOverride());
+    }
+
+    @Test
+    public void testUriPatternInOrder() {
+        final LookupRegistry<Object> matcher = UriPatternType.newMatcher(UriPatternType.URI_PATTERN_IN_ORDER);
+        Assert.assertTrue(matcher instanceof UriPatternMatcher);
+        Assert.assertFalse(((UriPatternMatcher<?>) matcher).isExactMatchOverride());
     }
 
 }