Skip to content

Commit

Permalink
#172 PsByFlag with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Apr 15, 2015
1 parent 611526e commit 7426a39
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
35 changes: 23 additions & 12 deletions src/main/java/org/takes/facets/auth/PsByFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,7 +58,7 @@ public final class PsByFlag implements Pass {
/**
* Flags and passes.
*/
private final transient Map<String, Pass> passes;
private final transient Map<Pattern, Pass> passes;

/**
* Ctor.
Expand All @@ -72,7 +73,7 @@ public PsByFlag(final PsByFlag.Pair... pairs) {
* Ctor.
* @param map Map
*/
public PsByFlag(final Map<String, Pass> map) {
public PsByFlag(final Map<Pattern, Pass> map) {
this(PsByFlag.class.getSimpleName(), map);
}

Expand All @@ -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<String, Pass> map) {
public PsByFlag(final String flg, final Map<Pattern, Pass> map) {
this.flag = flg;
this.passes = Collections.unmodifiableMap(map);
}
Expand All @@ -102,9 +103,11 @@ public Iterator<Identity> enter(final Request req) throws IOException {
.param(this.flag).iterator();
final Collection<Identity> users = new ArrayList<Identity>(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<Pattern, Pass> ent : this.passes.entrySet()) {
if (ent.getKey().matcher(value).matches()) {
users.add(ent.getValue().enter(req).next());
}
}
}
return users.iterator();
Expand All @@ -121,11 +124,11 @@ public Response exit(final Response response,
* @param entries Entries
* @return Map
*/
private static Map<String, Pass> asMap(
final Map.Entry<String, Pass>... entries) {
final ConcurrentMap<String, Pass> map =
new ConcurrentHashMap<String, Pass>(entries.length);
for (final Map.Entry<String, Pass> ent : entries) {
private static Map<Pattern, Pass> asMap(
final Map.Entry<Pattern, Pass>... entries) {
final ConcurrentMap<Pattern, Pass> map =
new ConcurrentHashMap<Pattern, Pass>(entries.length);
for (final Map.Entry<Pattern, Pass> ent : entries) {
map.put(ent.getKey(), ent.getValue());
}
return map;
Expand All @@ -135,7 +138,7 @@ private static Map<String, Pass> asMap(
* Pair of values.
*/
public static final class Pair
extends AbstractMap.SimpleEntry<String, Pass> {
extends AbstractMap.SimpleEntry<Pattern, Pass> {
/**
* Serialization marker.
*/
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqHref.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Base(final Request req) {
@Override
public Href href() throws IOException {
final Iterator<String> host = new RqHeaders(this)
.header("Host").iterator();
.header("host").iterator();
if (!host.hasNext()) {
throw new IOException("Host header is absent");
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/takes/facets/auth/PsByFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,7 +95,9 @@ public void exitTest() throws IOException {
);
MatcherAssert.assertThat(
new PsByFlag(
ImmutableMap.<String, Pass>of("key", new PsFake(true))
ImmutableMap.<Pattern, Pass>of(
Pattern.compile("key"), new PsFake(true)
)
).exit(response, Mockito.mock(Identity.class)),
Matchers.is(response)
);
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/facets/auth/TkAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public void logsInUserViaCookie() throws IOException {
Matchers.startsWith("HTTP/1.1 200 "),
Matchers.containsString("Set-Cookie: PsCookie=urn%3Atest%3A0")
)

);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqHrefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void extractsParamByDefault() throws IOException {
)
)
).param("absent", "def-5"),
Matchers.startsWith("def-5")
Matchers.startsWith("def-")
);
}
}

0 comments on commit 7426a39

Please sign in to comment.