From 7426a392f89c88ef243638a8c1dccd023c227fb9 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 14 Apr 2015 21:08:41 -0700 Subject: [PATCH] #172 PsByFlag with regex --- .../java/org/takes/facets/auth/PsByFlag.java | 35 ++++++++++++------- src/main/java/org/takes/rq/RqHref.java | 2 +- .../org/takes/facets/auth/PsByFlagTest.java | 5 ++- .../org/takes/facets/auth/TkAuthTest.java | 1 - src/test/java/org/takes/rq/RqHrefTest.java | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/takes/facets/auth/PsByFlag.java b/src/main/java/org/takes/facets/auth/PsByFlag.java index 4addb9840..0f97bb730 100644 --- a/src/main/java/org/takes/facets/auth/PsByFlag.java +++ b/src/main/java/org/takes/facets/auth/PsByFlag.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import org.takes.Request; import org.takes.Response; @@ -57,7 +58,7 @@ public final class PsByFlag implements Pass { /** * Flags and passes. */ - private final transient Map passes; + private final transient Map passes; /** * Ctor. @@ -72,7 +73,7 @@ public PsByFlag(final PsByFlag.Pair... pairs) { * Ctor. * @param map Map */ - public PsByFlag(final Map map) { + public PsByFlag(final Map map) { this(PsByFlag.class.getSimpleName(), map); } @@ -91,7 +92,7 @@ public PsByFlag(final String flg, final PsByFlag.Pair... pairs) { * @param flg Flag * @param map Map */ - public PsByFlag(final String flg, final Map map) { + public PsByFlag(final String flg, final Map map) { this.flag = flg; this.passes = Collections.unmodifiableMap(map); } @@ -102,9 +103,11 @@ public Iterator enter(final Request req) throws IOException { .param(this.flag).iterator(); final Collection users = new ArrayList(1); if (flg.hasNext()) { - final Pass pass = this.passes.get(flg.next()); - if (pass != null) { - users.add(pass.enter(req).next()); + final String value = flg.next(); + for (final Map.Entry ent : this.passes.entrySet()) { + if (ent.getKey().matcher(value).matches()) { + users.add(ent.getValue().enter(req).next()); + } } } return users.iterator(); @@ -121,11 +124,11 @@ public Response exit(final Response response, * @param entries Entries * @return Map */ - private static Map asMap( - final Map.Entry... entries) { - final ConcurrentMap map = - new ConcurrentHashMap(entries.length); - for (final Map.Entry ent : entries) { + private static Map asMap( + final Map.Entry... entries) { + final ConcurrentMap map = + new ConcurrentHashMap(entries.length); + for (final Map.Entry ent : entries) { map.put(ent.getKey(), ent.getValue()); } return map; @@ -135,7 +138,7 @@ private static Map asMap( * Pair of values. */ public static final class Pair - extends AbstractMap.SimpleEntry { + extends AbstractMap.SimpleEntry { /** * Serialization marker. */ @@ -146,6 +149,14 @@ public static final class Pair * @param pass Pass */ public Pair(final String key, final Pass pass) { + this(Pattern.compile(key), pass); + } + /** + * Ctor. + * @param key Key + * @param pass Pass + */ + public Pair(final Pattern key, final Pass pass) { super(key, pass); } } diff --git a/src/main/java/org/takes/rq/RqHref.java b/src/main/java/org/takes/rq/RqHref.java index 5f7192303..44c943edc 100644 --- a/src/main/java/org/takes/rq/RqHref.java +++ b/src/main/java/org/takes/rq/RqHref.java @@ -69,7 +69,7 @@ public Base(final Request req) { @Override public Href href() throws IOException { final Iterator host = new RqHeaders(this) - .header("Host").iterator(); + .header("host").iterator(); if (!host.hasNext()) { throw new IOException("Host header is absent"); } diff --git a/src/test/java/org/takes/facets/auth/PsByFlagTest.java b/src/test/java/org/takes/facets/auth/PsByFlagTest.java index d0b02ab0b..882282356 100644 --- a/src/test/java/org/takes/facets/auth/PsByFlagTest.java +++ b/src/test/java/org/takes/facets/auth/PsByFlagTest.java @@ -26,6 +26,7 @@ import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.regex.Pattern; import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.Warning; import org.hamcrest.MatcherAssert; @@ -94,7 +95,9 @@ public void exitTest() throws IOException { ); MatcherAssert.assertThat( new PsByFlag( - ImmutableMap.of("key", new PsFake(true)) + ImmutableMap.of( + Pattern.compile("key"), new PsFake(true) + ) ).exit(response, Mockito.mock(Identity.class)), Matchers.is(response) ); diff --git a/src/test/java/org/takes/facets/auth/TkAuthTest.java b/src/test/java/org/takes/facets/auth/TkAuthTest.java index 19562fedf..33d6478c8 100644 --- a/src/test/java/org/takes/facets/auth/TkAuthTest.java +++ b/src/test/java/org/takes/facets/auth/TkAuthTest.java @@ -97,7 +97,6 @@ public void logsInUserViaCookie() throws IOException { Matchers.startsWith("HTTP/1.1 200 "), Matchers.containsString("Set-Cookie: PsCookie=urn%3Atest%3A0") ) - ); } diff --git a/src/test/java/org/takes/rq/RqHrefTest.java b/src/test/java/org/takes/rq/RqHrefTest.java index b7ce29a31..f85d2e960 100644 --- a/src/test/java/org/takes/rq/RqHrefTest.java +++ b/src/test/java/org/takes/rq/RqHrefTest.java @@ -139,7 +139,7 @@ public void extractsParamByDefault() throws IOException { ) ) ).param("absent", "def-5"), - Matchers.startsWith("def-5") + Matchers.startsWith("def-") ); } }