use enum for parameter domains

This commit is contained in:
Jörg Prante 2023-03-24 14:06:48 +01:00
parent c37ff23865
commit 0a72c5dae8
12 changed files with 127 additions and 112 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = net name = net
version = 3.0.4 version = 3.0.5
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -83,7 +83,9 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
} }
} else if (builder.infoMap.containsKey(method)) { } else if (builder.infoMap.containsKey(method)) {
Info<T> info = builder.infoMap.get(method); Info<T> info = builder.infoMap.get(method);
ParameterBuilder parameterBuilder = Parameter.builder().domain("PATH").enableSort(); ParameterBuilder parameterBuilder = Parameter.builder()
.domain(Parameter.Domain.PATH)
.enableSort();
for (int i = 0; i < info.parameterNames.size(); i++) { for (int i = 0; i < info.parameterNames.size(); i++) {
parameterBuilder.add(info.parameterNames.get(i), parameters.get(i)); parameterBuilder.add(info.parameterNames.get(i), parameters.get(i));
} }

View file

@ -30,7 +30,7 @@ public class PathMatcher extends Path {
public PathMatcher() { public PathMatcher() {
this(null, "/", true, true, true, this(null, "/", true, true, true,
Parameter.builder().domain("PATH").enableSort()); Parameter.builder().domain(Parameter.Domain.PATH).enableSort());
} }
public PathMatcher(String pathSpec, public PathMatcher(String pathSpec,
@ -47,7 +47,7 @@ public class PathMatcher extends Path {
public static PathMatcher of(String pathSpec) { public static PathMatcher of(String pathSpec) {
return new PathMatcher(pathSpec, "/", true, true, true, return new PathMatcher(pathSpec, "/", true, true, true,
Parameter.builder().domain("PATH").enableSort()); Parameter.builder().domain(Parameter.Domain.PATH).enableSort());
} }
public void setPathSeparator(String pathSeparator) { public void setPathSeparator(String pathSeparator) {

View file

@ -24,8 +24,6 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
private static final Logger logger = Logger.getLogger(PathResolver.class.getName()); private static final Logger logger = Logger.getLogger(PathResolver.class.getName());
private static final String PATH_DOMAIN = "PATH";
private final Builder<T> builder; private final Builder<T> builder;
private final Map<PathSegment, PathResolver<T>> children; private final Map<PathSegment, PathResolver<T>> children;
@ -44,7 +42,7 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
Objects.requireNonNull(path, "path"); Objects.requireNonNull(path, "path");
List<PathSegment> pathSegments = PathMatcher.tokenize(PathNormalizer.normalize(path), List<PathSegment> pathSegments = PathMatcher.tokenize(PathNormalizer.normalize(path),
builder.pathSeparator, builder.trimTokens, builder.caseSensitive); builder.pathSeparator, builder.trimTokens, builder.caseSensitive);
ParameterBuilder parameterBuilder = Parameter.builder().domain(PATH_DOMAIN).enableSort(); ParameterBuilder parameterBuilder = Parameter.builder().domain(Parameter.Domain.PATH).enableSort();
resolve(pathSegments, 0, parameterBuilder, listener); resolve(pathSegments, 0, parameterBuilder, listener);
} }
@ -63,7 +61,7 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
List<PathResolver<T>> list = new ArrayList<>(); List<PathResolver<T>> list = new ArrayList<>();
boolean shortCircuit = match(segment, list, pb); boolean shortCircuit = match(segment, list, pb);
if (list.isEmpty()) { if (list.isEmpty()) {
pb = Parameter.builder().domain(PATH_DOMAIN).enableSort(); pb = Parameter.builder().domain(Parameter.Domain.PATH).enableSort();
} }
if (shortCircuit) { if (shortCircuit) {
PathResolver<T> pathResolver = list.get(list.size() - 1); PathResolver<T> pathResolver = list.get(list.size() - 1);
@ -71,7 +69,7 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
T value = pathResolver.builder.value; T value = pathResolver.builder.value;
if (listener != null) { if (listener != null) {
listener.onResult(new Result<>(value, pb.build(), pathResolver.builder.method)); listener.onResult(new Result<>(value, pb.build(), pathResolver.builder.method));
pb = Parameter.builder().domain(PATH_DOMAIN).enableSort(); pb = Parameter.builder().domain(Parameter.Domain.PATH).enableSort();
} }
} }
} else { } else {
@ -84,7 +82,7 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
T value = builder.value; T value = builder.value;
if (listener != null) { if (listener != null) {
listener.onResult(new Result<>(value, pb.build(), builder.method)); listener.onResult(new Result<>(value, pb.build(), builder.method));
pb = Parameter.builder().domain(PATH_DOMAIN).enableSort(); pb = Parameter.builder().domain(Parameter.Domain.PATH).enableSort();
} }
} }
} }
@ -221,7 +219,7 @@ public class PathResolver<T> implements org.xbib.net.path.PathResolver<T> {
Objects.requireNonNull(pathSpec, "pathSpec"); Objects.requireNonNull(pathSpec, "pathSpec");
Objects.requireNonNull(value, "value"); Objects.requireNonNull(value, "value");
PathMatcher pathMatcher = new PathMatcher(pathSpec, pathSeparator, trimTokens, caseSensitive, true, PathMatcher pathMatcher = new PathMatcher(pathSpec, pathSeparator, trimTokens, caseSensitive, true,
Parameter.builder().domain(PATH_DOMAIN).enableSort()); Parameter.builder().domain(Parameter.Domain.PATH).enableSort());
add(pathMatcher.getAnalyzedSegments(), value, method, 0); add(pathMatcher.getAnalyzedSegments(), value, method, 0);
return this; return this;
} }

View file

@ -1,6 +1,7 @@
package org.xbib.net.path; package org.xbib.net.path;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.net.Parameter;
import org.xbib.net.URL; import org.xbib.net.URL;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -9,39 +10,39 @@ import static org.junit.jupiter.api.Assertions.assertNull;
class PathDecoderTest { class PathDecoderTest {
@Test @Test
void testPlusSign() throws Exception { void testPlusSign() {
PathDecoder decoder = new PathDecoder("/path?a=b+c", "d=e+f"); PathDecoder decoder = new PathDecoder("/path?a=b+c", "d=e+f");
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString());
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString());
} }
@Test @Test
void testSlash() throws Exception { void testSlash() {
PathDecoder decoder = new PathDecoder("path/foo/bar/?a=b+c", "d=e+f"); PathDecoder decoder = new PathDecoder("path/foo/bar/?a=b+c", "d=e+f");
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString());
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString());
} }
@Test @Test
void testDoubleSlashes() throws Exception { void testDoubleSlashes() {
PathDecoder decoder = new PathDecoder("//path", ""); PathDecoder decoder = new PathDecoder("//path", "");
assertEquals("/path", decoder.path()); assertEquals("/path", decoder.path());
} }
@Test @Test
void testSlashes() throws Exception { void testSlashes() {
PathDecoder decoder = new PathDecoder("//path?a=b+c", "d=e+f"); PathDecoder decoder = new PathDecoder("//path?a=b+c", "d=e+f");
assertEquals("/path", decoder.path()); assertEquals("/path", decoder.path());
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString());
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString());
} }
@Test @Test
void testPlusPercent() throws Exception { void testPlusPercent() {
PathDecoder decoder = new PathDecoder("//path?a=b%2Bc", "d=e%2Bf"); PathDecoder decoder = new PathDecoder("//path?a=b%2Bc", "d=e%2Bf");
assertEquals("/path", decoder.path()); assertEquals("/path", decoder.path());
assertEquals("[b+c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); assertEquals("[b+c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString());
assertEquals("[e+f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); assertEquals("[e+f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString());
} }
@Test @Test
@ -58,7 +59,7 @@ class PathDecoderTest {
} }
assertEquals("x-fl-key=20190035592&x-source=ftp://dummy@xbib.org/upload/20190035592/20190035592.pdf&x-fl-target=ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf&x-fl-copy=&x-fl-ack=https://xbib.org/ack/&x-fl-pages=1-", url.getDecodedQuery()); assertEquals("x-fl-key=20190035592&x-source=ftp://dummy@xbib.org/upload/20190035592/20190035592.pdf&x-fl-target=ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf&x-fl-copy=&x-fl-ack=https://xbib.org/ack/&x-fl-pages=1-", url.getDecodedQuery());
assertEquals("[x-fl-key=20190035592, x-source=ftp://dummy@xbib.org/upload/20190035592/20190035592.pdf, x-fl-target=ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf, x-fl-copy=, x-fl-ack=https://xbib.org/ack/, x-fl-pages=1-]", decoder.getParameter().toString()); assertEquals("[x-fl-key=20190035592, x-source=ftp://dummy@xbib.org/upload/20190035592/20190035592.pdf, x-fl-target=ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf, x-fl-copy=, x-fl-ack=https://xbib.org/ack/, x-fl-pages=1-]", decoder.getParameter().toString());
url = URL.from(decoder.getParameter().getAll("x-fl-target", "DEFAULT").get(0).toString()); url = URL.from(decoder.getParameter().getAll("x-fl-target", Parameter.Domain.DEFAULT).get(0).toString());
assertEquals("ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf", url.toString()); assertEquals("ftp://dummy@xbib.org/fl/download/20190035592/Fernleihe_Kopienlieferung_null_FB201900373_BLQDMT62_20190035592_20190035592.pdf", url.toString());
} }
} }

View file

@ -314,13 +314,13 @@ class PathMatcherTest {
void extractUriTemplateVariablesRegex() { void extractUriTemplateVariablesRegex() {
Parameter result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar", Parameter result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar",
"com.example-1.0.0.jar"); "com.example-1.0.0.jar");
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0));
assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0));
result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar", result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar",
"com.example-sources-1.0.0.jar"); "com.example-sources-1.0.0.jar");
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0));
assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0));
} }
@Test @Test
@ -328,21 +328,21 @@ class PathMatcherTest {
Parameter result = pathMatcher.extractUriTemplateVariables( Parameter result = pathMatcher.extractUriTemplateVariables(
"{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar", "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar",
"com.example-sources-1.0.0.jar"); "com.example-sources-1.0.0.jar");
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0));
assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0));
result = pathMatcher.extractUriTemplateVariables( result = pathMatcher.extractUriTemplateVariables(
"{symbolicName:[\\w\\.]+}-sources-{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar", "{symbolicName:[\\w\\.]+}-sources-{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar",
"com.example-sources-1.0.0-20100220.jar"); "com.example-sources-1.0.0-20100220.jar");
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0));
assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0));
assertEquals("2010", result.getAll("year", "DEFAULT").get(0)); assertEquals("2010", result.getAll("year", Parameter.Domain.DEFAULT).get(0));
assertEquals("02", result.getAll("month", "DEFAULT").get(0)); assertEquals("02", result.getAll("month", Parameter.Domain.DEFAULT).get(0));
assertEquals("20", result.getAll("day", "DEFAULT").get(0)); assertEquals("20", result.getAll("day", Parameter.Domain.DEFAULT).get(0));
result = pathMatcher.extractUriTemplateVariables( result = pathMatcher.extractUriTemplateVariables(
"{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.\\{\\}]+}.jar", "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.\\{\\}]+}.jar",
"com.example-sources-1.0.0.{12}.jar"); "com.example-sources-1.0.0.{12}.jar");
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0));
assertEquals("1.0.0.{12}", result.getAll("version", "DEFAULT").get(0)); assertEquals("1.0.0.{12}", result.getAll("version", Parameter.Domain.DEFAULT).get(0));
} }
@Test @Test

View file

@ -1,6 +1,7 @@
package org.xbib.net.path.simple; package org.xbib.net.path.simple;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.net.Parameter;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -147,12 +148,12 @@ class PathResolverTest {
.add("GET", "test/{two}", 4321) .add("GET", "test/{two}", 4321)
.build(); .build();
pathResolver.resolve("GET", "test/foo", result -> { pathResolver.resolve("GET", "test/foo", result -> {
if (result.getParameter().containsKey("one", "PATH")) { if (result.getParameter().containsKey("one", Parameter.Domain.PATH)) {
assertThat(result.getValue(), is(1234)); assertThat(result.getValue(), is(1234));
assertThat(result.getParameter().get("one", "PATH"), is("foo")); assertThat(result.getParameter().get("one", Parameter.Domain.PATH), is("foo"));
} else { } else {
assertThat(result.getValue(), is(4321)); assertThat(result.getValue(), is(4321));
assertThat(result.getParameter().get("two", "PATH"), is("foo")); assertThat(result.getParameter().get("two", Parameter.Domain.PATH), is("foo"));
} }
}); });
} }

View file

@ -25,7 +25,7 @@ class PathResolverTest {
.add( "GET", "/static/{file}", 1234) .add( "GET", "/static/{file}", 1234)
.build(); .build();
assertSuccessfulResolution(pathResolver, "GET", "/static/test.txt", 1234, assertSuccessfulResolution(pathResolver, "GET", "/static/test.txt", 1234,
Parameter.of("PATH", Map.of("file", "test.txt"))); Parameter.of(Parameter.Domain.PATH, Map.of("file", "test.txt")));
} }
@Test @Test
@ -68,7 +68,7 @@ class PathResolverTest {
.add("GET", "discovery/{version}/rest", 1234) .add("GET", "discovery/{version}/rest", 1234)
.build(); .build();
assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rest", 1234, assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rest", 1234,
Parameter.of("PATH", Map.of("version", "v1"))); Parameter.of(Parameter.Domain.PATH, Map.of("version", "v1")));
} }
@Test @Test
@ -77,7 +77,7 @@ class PathResolverTest {
.add("GET", "discovery/{discovery_version}/apis/{api}/{format}", 1234) .add("GET", "discovery/{discovery_version}/apis/{api}/{format}", 1234)
.build(); .build();
assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/apis/test/rest", 1234, assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/apis/test/rest", 1234,
Parameter.of("PATH", Map.of("discovery_version", "v1", "api", "test", "format", "rest"))); Parameter.of(Parameter.Domain.PATH, Map.of("discovery_version", "v1", "api", "test", "format", "rest")));
} }
@Test @Test
@ -87,13 +87,13 @@ class PathResolverTest {
.add("GET", "discovery/{version}/rpc", 4321) .add("GET", "discovery/{version}/rpc", 4321)
.build(); .build();
assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rest", 1234, assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rest", 1234,
Parameter.of("PATH", Map.of("version", "v1"))); Parameter.of(Parameter.Domain.PATH, Map.of("version", "v1")));
assertSuccessfulResolution(pathResolver, "GET", "discovery/v2/rest", 1234, assertSuccessfulResolution(pathResolver, "GET", "discovery/v2/rest", 1234,
Parameter.of("PATH", Map.of("version", "v2"))); Parameter.of(Parameter.Domain.PATH, Map.of("version", "v2")));
assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rpc", 4321, assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rpc", 4321,
Parameter.of("PATH", Map.of("version", "v1"))); Parameter.of(Parameter.Domain.PATH, Map.of("version", "v1")));
assertSuccessfulResolution(pathResolver, "GET", "discovery/v2/rpc", 4321, assertSuccessfulResolution(pathResolver, "GET", "discovery/v2/rpc", 4321,
Parameter.of("PATH", Map.of("version", "v2"))); Parameter.of(Parameter.Domain.PATH, Map.of("version", "v2")));
} }
@Test @Test
@ -103,7 +103,7 @@ class PathResolverTest {
.add("GET", "one/two", 4321) .add("GET", "one/two", 4321)
.build(); .build();
assertSuccessfulResolution(pathResolver, "GET", "one/three", 1234, assertSuccessfulResolution(pathResolver, "GET", "one/three", 1234,
Parameter.of("PATH", Map.of("one", "one"))); Parameter.of(Parameter.Domain.PATH, Map.of("one", "one")));
assertSuccessfulResolution(pathResolver, "one/two", 4321); assertSuccessfulResolution(pathResolver, "one/two", 4321);
} }
@ -115,15 +115,15 @@ class PathResolverTest {
.build(); .build();
pathResolver.resolve("GET", "one/two/three/four", result -> { pathResolver.resolve("GET", "one/two/three/four", result -> {
assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321)));
if (result.getParameter().containsKey("three", "PATH")) { if (result.getParameter().containsKey("three", Parameter.Domain.PATH)) {
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("three", "three")))); assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("three", "three"))));
} else { } else {
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("one", "one", "two", "two", "four", "four")))); assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("one", "one", "two", "two", "four", "four"))));
} }
}); });
pathResolver.resolve("GET", "one/two/three/five", result -> { pathResolver.resolve("GET", "one/two/three/five", result -> {
assertThat(result.getValue(), equalTo(1234)); assertThat(result.getValue(), equalTo(1234));
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("one", "one", "two", "two", "four", "five")))); assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("one", "one", "two", "two", "four", "five"))));
}); });
} }
@ -137,10 +137,10 @@ class PathResolverTest {
pathResolver.resolve("GET", "test/foo", result -> { pathResolver.resolve("GET", "test/foo", result -> {
assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321)));
if (result.getValue() == 1234) { if (result.getValue() == 1234) {
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("one", "foo")))); assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("one", "foo"))));
} }
if (result.getValue() == 4321) { if (result.getValue() == 4321) {
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("two", "foo")))); assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("two", "foo"))));
} }
count.incrementAndGet(); count.incrementAndGet();
}); });
@ -156,11 +156,11 @@ class PathResolverTest {
AtomicInteger count = new AtomicInteger(); AtomicInteger count = new AtomicInteger();
pathResolver.resolve("GET", "test/foo", result -> { pathResolver.resolve("GET", "test/foo", result -> {
assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321)));
if (result.getParameter().containsKey("PATH", "one")) { if (result.getParameter().containsKey( "one", Parameter.Domain.PATH)) {
assertThat(result.getParameter().get("PATH", "one"), is("foo")); assertThat(result.getParameter().get( "one", Parameter.Domain.PATH), is("foo"));
} }
if (result.getParameter().containsKey("PATH", "two")) { if (result.getParameter().containsKey("two", Parameter.Domain.PATH)) {
assertThat(result.getParameter().get("PATH", "two"), is("foo")); assertThat(result.getParameter().get( "two", Parameter.Domain.PATH), is("foo"));
} }
count.incrementAndGet(); count.incrementAndGet();
}); });
@ -230,7 +230,7 @@ class PathResolverTest {
} }
private void assertSuccessfulResolution(PathResolver<Integer> pathResolver, String path, Integer value) { private void assertSuccessfulResolution(PathResolver<Integer> pathResolver, String path, Integer value) {
assertSuccessfulResolution(pathResolver, "GET", path, value, Parameter.builder().domain("PATH").build()); assertSuccessfulResolution(pathResolver, "GET", path, value, Parameter.builder().domain(Parameter.Domain.PATH).build());
} }
private void assertSuccessfulResolution(PathResolver<Integer> pathResolver, String method, String path, Integer value, private void assertSuccessfulResolution(PathResolver<Integer> pathResolver, String method, String path, Integer value,

View file

@ -1,6 +1,5 @@
package org.xbib.net; package org.xbib.net;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -8,9 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.xbib.datastructures.common.ImmutableList; import org.xbib.datastructures.common.ImmutableList;
import org.xbib.datastructures.common.LinkedHashSetMultiMap; import org.xbib.datastructures.common.LinkedHashSetMultiMap;
import org.xbib.datastructures.common.MultiMap; import org.xbib.datastructures.common.MultiMap;
@ -18,6 +15,15 @@ import org.xbib.datastructures.common.Pair;
public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Parameter> { public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Parameter> {
public enum Domain {
DEFAULT,
QUERY,
FORM,
PATH,
HEADER,
COOKIE
}
private static final Parameter EMPTY = Parameter.builder().build(); private static final Parameter EMPTY = Parameter.builder().build();
private final ParameterBuilder builder; private final ParameterBuilder builder;
@ -50,7 +56,7 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return Parameter.builder().enableSort().add(map).build(); return Parameter.builder().enableSort().add(map).build();
} }
public static Parameter of(String domain, Map<String, Object> map) { public static Parameter of(Domain domain, Map<String, Object> map) {
return Parameter.builder().domain(domain).enableSort().add(map).build(); return Parameter.builder().domain(domain).enableSort().add(map).build();
} }
@ -81,13 +87,12 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return allToString(); return allToString();
} }
public String getDomain() { public Domain getDomain() {
return builder.domain; return builder.domain;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String getAsString(String key, String... domains) { public String getAsString(String key, Domain... domains) {
Object object = get(key, domains); Object object = get(key, domains);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
@ -103,7 +108,7 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Integer getAsInteger(String key, String... domains) { public Integer getAsInteger(String key, Domain... domains) {
Object object = get(key, domains); Object object = get(key, domains);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
@ -123,7 +128,7 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Boolean getAsBoolean(String key, String... domains) { public Boolean getAsBoolean(String key, Domain... domains) {
Object object = get(key, domains); Object object = get(key, domains);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
@ -178,9 +183,9 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return object; return object;
} }
public List<Object> getAllDomain(String... domains) { public List<Object> getAllDomain(Domain... domains) {
Parameter parameter = null; Parameter parameter = null;
for (String domain : domains) { for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) { if (builder.parameterMap.containsKey(domain)) {
parameter = builder.parameterMap.get(domain); parameter = builder.parameterMap.get(domain);
} }
@ -197,9 +202,9 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return null; return null;
} }
public boolean isPresent(String... domains) { public boolean isPresent(Domain... domains) {
Parameter parameter = null; Parameter parameter = null;
for (String domain : domains) { for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) { if (builder.parameterMap.containsKey(domain)) {
parameter = builder.parameterMap.get(domain); parameter = builder.parameterMap.get(domain);
} }
@ -214,9 +219,18 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return false; return false;
} }
public List<Object> getAll(String key, String... domains) { public Parameter getDomain(Domain... domains) {
for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) {
return builder.parameterMap.get(domain);
}
}
return null;
}
public List<Object> getAll(String key, Domain... domains) {
Parameter parameter = null; Parameter parameter = null;
for (String domain : domains) { for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) { if (builder.parameterMap.containsKey(domain)) {
parameter = builder.parameterMap.get(domain); parameter = builder.parameterMap.get(domain);
} }
@ -234,9 +248,9 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return null; return null;
} }
public boolean containsKey(String key, String... domains) { public boolean containsKey(String key, Domain... domains) {
Parameter parameter = null; Parameter parameter = null;
for (String domain : domains) { for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) { if (builder.parameterMap.containsKey(domain)) {
parameter = builder.parameterMap.get(domain); parameter = builder.parameterMap.get(domain);
} }
@ -252,9 +266,9 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return false; return false;
} }
public Object get(String key, String... domains) { public Object get(String key, Domain... domains) {
Parameter parameter = null; Parameter parameter = null;
for (String domain : domains) { for (Domain domain : domains) {
if (builder.parameterMap.containsKey(domain)) { if (builder.parameterMap.containsKey(domain)) {
parameter = builder.parameterMap.get(domain); parameter = builder.parameterMap.get(domain);
} }

View file

@ -32,9 +32,9 @@ public class ParameterBuilder implements PairValidator {
private final List<Pair<String, Object>> list; private final List<Pair<String, Object>> list;
protected String domain; protected Parameter.Domain domain;
final Map<String, Parameter> parameterMap; final Map<Parameter.Domain, Parameter> parameterMap;
private Charset charset; private Charset charset;
@ -65,7 +65,7 @@ public class ParameterBuilder implements PairValidator {
ParameterBuilder() { ParameterBuilder() {
this.list = new ArrayList<>(); this.list = new ArrayList<>();
this.parameterMap = new HashMap<>(); this.parameterMap = new HashMap<>();
this.domain = "DEFAULT"; this.domain = Parameter.Domain.DEFAULT;
this.limit = 0; this.limit = 0;
} }
@ -139,7 +139,7 @@ public class ParameterBuilder implements PairValidator {
return this; return this;
} }
public ParameterBuilder domain(String domain) { public ParameterBuilder domain(Parameter.Domain domain) {
this.domain = domain; this.domain = domain;
return this; return this;
} }

View file

@ -17,7 +17,7 @@ public class ParameterTest {
public void testEmptyBuilder() { public void testEmptyBuilder() {
Parameter parameter = Parameter.builder().build(); Parameter parameter = Parameter.builder().build();
assertNotNull(parameter); assertNotNull(parameter);
assertFalse(parameter.containsKey("param1", "DEFAULT")); assertFalse(parameter.containsKey("param1", Parameter.Domain.DEFAULT));
} }
@Test @Test
@ -26,7 +26,7 @@ public class ParameterTest {
.add("Hello", "World") .add("Hello", "World")
.build(); .build();
assertNotNull(parameter); assertNotNull(parameter);
assertTrue(parameter.containsKey("Hello", "DEFAULT")); assertTrue(parameter.containsKey("Hello", Parameter.Domain.DEFAULT));
} }
@Test @Test
@ -38,8 +38,8 @@ public class ParameterTest {
.add("Hello", "World") .add("Hello", "World")
.build(); .build();
assertNotNull(parameter); assertNotNull(parameter);
assertTrue(parameter.containsKey("Hello", "DEFAULT")); assertTrue(parameter.containsKey("Hello", Parameter.Domain.DEFAULT));
assertEquals(List.of("World", "World", "World"), parameter.getAll("Hello", "DEFAULT")); assertEquals(List.of("World", "World", "World"), parameter.getAll("Hello", Parameter.Domain.DEFAULT));
} }
@Test @Test
@ -47,15 +47,15 @@ public class ParameterTest {
Parameter parameter = Parameter.builder() Parameter parameter = Parameter.builder()
.charset(StandardCharsets.US_ASCII) .charset(StandardCharsets.US_ASCII)
.lowercase() .lowercase()
.domain("HEADER") .domain(Parameter.Domain.HEADER)
.add("Content-Type", "text/plain") .add("Content-Type", "text/plain")
.add("Accept", "*/*") .add("Accept", "*/*")
.add("Connection", "close") .add("Connection", "close")
.build(); .build();
assertNotNull(parameter); assertNotNull(parameter);
assertEquals("HEADER", parameter.getDomain()); assertEquals(Parameter.Domain.HEADER, parameter.getDomain());
assertTrue(parameter.containsKey("content-type", "HEADER")); assertTrue(parameter.containsKey("content-type", Parameter.Domain.HEADER));
assertEquals(List.of("close"), parameter.getAll( "connection", "HEADER")); assertEquals(List.of("close"), parameter.getAll( "connection", Parameter.Domain.HEADER));
} }
@Test @Test
@ -121,9 +121,9 @@ public class ParameterTest {
String body = "a=b&c=d&e=f"; String body = "a=b&c=d&e=f";
queryParameters.addPercentEncodedBody(body); queryParameters.addPercentEncodedBody(body);
Parameter parameter = queryParameters.build(); Parameter parameter = queryParameters.build();
assertEquals("b", parameter.getAll("a", "DEFAULT").get(0)); assertEquals("b", parameter.getAll("a", Parameter.Domain.DEFAULT).get(0));
assertEquals("d", parameter.getAll("c", "DEFAULT").get(0)); assertEquals("d", parameter.getAll("c", Parameter.Domain.DEFAULT).get(0));
assertEquals("f", parameter.getAll("e", "DEFAULT").get(0)); assertEquals("f", parameter.getAll("e", Parameter.Domain.DEFAULT).get(0));
} }
@Test @Test
@ -137,32 +137,31 @@ public class ParameterTest {
String body = String.join("&", list); String body = String.join("&", list);
queryParameters.addPercentEncodedBody(body); queryParameters.addPercentEncodedBody(body);
Parameter parameter = queryParameters.build(); Parameter parameter = queryParameters.build();
assertEquals("b0", parameter.getAll("a0", "DEFAULT").get(0)); assertEquals("b0", parameter.getAll("a0", Parameter.Domain.DEFAULT).get(0));
assertEquals("b99", parameter.getAll("a99", "DEFAULT").get(0)); assertEquals("b99", parameter.getAll("a99", Parameter.Domain.DEFAULT).get(0));
assertEquals("[]", parameter.getAll("a100", "DEFAULT").toString()); assertEquals("[]", parameter.getAll("a100", Parameter.Domain.DEFAULT).toString());
}); });
} }
@Test @Test
void testDomains() { void testDomains() {
Parameter p1 = Parameter.builder().domain("A").add("a", "a").build(); Parameter p1 = Parameter.builder().domain(Parameter.Domain.QUERY).add("a", "a").build();
Parameter p2 = Parameter.builder().domain("B").add("b", "b").build(); Parameter p2 = Parameter.builder().domain(Parameter.Domain.FORM).add("b", "b").build();
Parameter p3 = Parameter.builder().domain("C").add("c", "c").build(); Parameter p3 = Parameter.builder().domain(Parameter.Domain.HEADER).add("c", "c").build();
Parameter p = Parameter.builder() Parameter p = Parameter.builder()
.add(p1) .add(p1)
.add(p2) .add(p2)
.add(p3) .add(p3)
.build(); .build();
assertEquals("a", p.get("a", "A").toString()); assertEquals("a", p.get("a", Parameter.Domain.QUERY).toString());
assertEquals("b", p.get("b", "B").toString()); assertEquals("b", p.get("b", Parameter.Domain.FORM).toString());
assertEquals("c", p.get("c", "C").toString()); assertEquals("c", p.get("c", Parameter.Domain.HEADER).toString());
assertEquals("[] A -> [a=a] B -> [b=b] C -> [c=c]", p.toString()); assertEquals("a", p.get("a", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER));
assertEquals("a", p.get("a", "A", "B", "C")); assertEquals("b", p.get("b", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER));
assertEquals("b", p.get("b", "A", "B", "C")); assertEquals("c", p.get("c", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER));
assertEquals("c", p.get("c", "A", "B", "C")); assertTrue(p.isPresent(Parameter.Domain.QUERY));
assertTrue(p.isPresent("A")); assertTrue(p.isPresent(Parameter.Domain.FORM));
assertTrue(p.isPresent("B")); assertTrue(p.isPresent(Parameter.Domain.HEADER));
assertTrue(p.isPresent("C")); assertFalse(p.isPresent(Parameter.Domain.DEFAULT));
assertFalse(p.isPresent("D"));
} }
} }

View file

@ -327,7 +327,7 @@ class URLParserTest {
assertEquals("path", pathSegment.getSegment()); assertEquals("path", pathSegment.getSegment());
assertEquals("p2", pathSegment.getMatrixParams().get(0).getKey()); assertEquals("p2", pathSegment.getMatrixParams().get(0).getKey());
assertEquals("v2", pathSegment.getMatrixParams().get(0).getValue()); assertEquals("v2", pathSegment.getMatrixParams().get(0).getValue());
assertEquals("v3", url.getQueryParams().getAll("q1", "DEFAULT").get(0)); assertEquals("v3", url.getQueryParams().getAll("q1", Parameter.Domain.DEFAULT).get(0));
} }
@Test @Test