From 0a72c5dae8546f6db61f84d521b06ba7875cc18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 24 Mar 2023 14:06:48 +0100 Subject: [PATCH] use enum for parameter domains --- gradle.properties | 2 +- .../xbib/net/path/simple/PathResolver.java | 4 +- .../xbib/net/path/structure/PathMatcher.java | 4 +- .../xbib/net/path/structure/PathResolver.java | 12 ++-- .../org/xbib/net/path/PathDecoderTest.java | 29 +++++----- .../xbib/net/path/simple/PathMatcherTest.java | 26 ++++----- .../net/path/simple/PathResolverTest.java | 7 ++- .../net/path/structure/PathResolverTest.java | 38 ++++++------- net/src/main/java/org/xbib/net/Parameter.java | 52 +++++++++++------- .../java/org/xbib/net/ParameterBuilder.java | 8 +-- .../test/java/org/xbib/net/ParameterTest.java | 55 +++++++++---------- .../test/java/org/xbib/net/URLParserTest.java | 2 +- 12 files changed, 127 insertions(+), 112 deletions(-) diff --git a/gradle.properties b/gradle.properties index 51c9ca7..fb7d296 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = net -version = 3.0.4 +version = 3.0.5 org.gradle.warning.mode = ALL diff --git a/net-path/src/main/java/org/xbib/net/path/simple/PathResolver.java b/net-path/src/main/java/org/xbib/net/path/simple/PathResolver.java index 5224553..aa80180 100644 --- a/net-path/src/main/java/org/xbib/net/path/simple/PathResolver.java +++ b/net-path/src/main/java/org/xbib/net/path/simple/PathResolver.java @@ -83,7 +83,9 @@ public class PathResolver implements org.xbib.net.path.PathResolver { } } else if (builder.infoMap.containsKey(method)) { Info 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++) { parameterBuilder.add(info.parameterNames.get(i), parameters.get(i)); } diff --git a/net-path/src/main/java/org/xbib/net/path/structure/PathMatcher.java b/net-path/src/main/java/org/xbib/net/path/structure/PathMatcher.java index 5a81a13..89ab3a1 100644 --- a/net-path/src/main/java/org/xbib/net/path/structure/PathMatcher.java +++ b/net-path/src/main/java/org/xbib/net/path/structure/PathMatcher.java @@ -30,7 +30,7 @@ public class PathMatcher extends Path { public PathMatcher() { this(null, "/", true, true, true, - Parameter.builder().domain("PATH").enableSort()); + Parameter.builder().domain(Parameter.Domain.PATH).enableSort()); } public PathMatcher(String pathSpec, @@ -47,7 +47,7 @@ public class PathMatcher extends Path { public static PathMatcher of(String pathSpec) { return new PathMatcher(pathSpec, "/", true, true, true, - Parameter.builder().domain("PATH").enableSort()); + Parameter.builder().domain(Parameter.Domain.PATH).enableSort()); } public void setPathSeparator(String pathSeparator) { diff --git a/net-path/src/main/java/org/xbib/net/path/structure/PathResolver.java b/net-path/src/main/java/org/xbib/net/path/structure/PathResolver.java index fc06e1e..7e70ac2 100644 --- a/net-path/src/main/java/org/xbib/net/path/structure/PathResolver.java +++ b/net-path/src/main/java/org/xbib/net/path/structure/PathResolver.java @@ -24,8 +24,6 @@ public class PathResolver implements org.xbib.net.path.PathResolver { private static final Logger logger = Logger.getLogger(PathResolver.class.getName()); - private static final String PATH_DOMAIN = "PATH"; - private final Builder builder; private final Map> children; @@ -44,7 +42,7 @@ public class PathResolver implements org.xbib.net.path.PathResolver { Objects.requireNonNull(path, "path"); List pathSegments = PathMatcher.tokenize(PathNormalizer.normalize(path), 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); } @@ -63,7 +61,7 @@ public class PathResolver implements org.xbib.net.path.PathResolver { List> list = new ArrayList<>(); boolean shortCircuit = match(segment, list, pb); if (list.isEmpty()) { - pb = Parameter.builder().domain(PATH_DOMAIN).enableSort(); + pb = Parameter.builder().domain(Parameter.Domain.PATH).enableSort(); } if (shortCircuit) { PathResolver pathResolver = list.get(list.size() - 1); @@ -71,7 +69,7 @@ public class PathResolver implements org.xbib.net.path.PathResolver { T value = pathResolver.builder.value; if (listener != null) { 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 { @@ -84,7 +82,7 @@ public class PathResolver implements org.xbib.net.path.PathResolver { T value = builder.value; if (listener != null) { 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 implements org.xbib.net.path.PathResolver { Objects.requireNonNull(pathSpec, "pathSpec"); Objects.requireNonNull(value, "value"); 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); return this; } diff --git a/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java b/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java index 572c0f7..b13374a 100644 --- a/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java +++ b/net-path/src/test/java/org/xbib/net/path/PathDecoderTest.java @@ -1,6 +1,7 @@ package org.xbib.net.path; import org.junit.jupiter.api.Test; +import org.xbib.net.Parameter; import org.xbib.net.URL; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -9,39 +10,39 @@ import static org.junit.jupiter.api.Assertions.assertNull; class PathDecoderTest { @Test - void testPlusSign() throws Exception { + void testPlusSign() { PathDecoder decoder = new PathDecoder("/path?a=b+c", "d=e+f"); - assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); - assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); + assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString()); + assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString()); } @Test - void testSlash() throws Exception { + void testSlash() { PathDecoder decoder = new PathDecoder("path/foo/bar/?a=b+c", "d=e+f"); - assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); - assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); + assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString()); + assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString()); } @Test - void testDoubleSlashes() throws Exception { + void testDoubleSlashes() { PathDecoder decoder = new PathDecoder("//path", ""); assertEquals("/path", decoder.path()); } @Test - void testSlashes() throws Exception { + void testSlashes() { PathDecoder decoder = new PathDecoder("//path?a=b+c", "d=e+f"); assertEquals("/path", decoder.path()); - assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); - assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); + assertEquals("[b c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString()); + assertEquals("[e f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString()); } @Test - void testPlusPercent() throws Exception { + void testPlusPercent() { PathDecoder decoder = new PathDecoder("//path?a=b%2Bc", "d=e%2Bf"); assertEquals("/path", decoder.path()); - assertEquals("[b+c]", decoder.getParameter().getAll("a", "DEFAULT").toString()); - assertEquals("[e+f]", decoder.getParameter().getAll("d", "DEFAULT").toString()); + assertEquals("[b+c]", decoder.getParameter().getAll("a", Parameter.Domain.DEFAULT).toString()); + assertEquals("[e+f]", decoder.getParameter().getAll("d", Parameter.Domain.DEFAULT).toString()); } @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-]", 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()); } } diff --git a/net-path/src/test/java/org/xbib/net/path/simple/PathMatcherTest.java b/net-path/src/test/java/org/xbib/net/path/simple/PathMatcherTest.java index 6935516..7c3a1de 100644 --- a/net-path/src/test/java/org/xbib/net/path/simple/PathMatcherTest.java +++ b/net-path/src/test/java/org/xbib/net/path/simple/PathMatcherTest.java @@ -314,13 +314,13 @@ class PathMatcherTest { void extractUriTemplateVariablesRegex() { Parameter result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar", "com.example-1.0.0.jar"); - assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); - assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); + assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0)); + assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0)); result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar", "com.example-sources-1.0.0.jar"); - assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); - assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); + assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0)); + assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0)); } @Test @@ -328,21 +328,21 @@ class PathMatcherTest { Parameter result = pathMatcher.extractUriTemplateVariables( "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar", "com.example-sources-1.0.0.jar"); - assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); - assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); + assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0)); + assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0)); result = pathMatcher.extractUriTemplateVariables( "{symbolicName:[\\w\\.]+}-sources-{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar", "com.example-sources-1.0.0-20100220.jar"); - assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); - assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0)); - assertEquals("2010", result.getAll("year", "DEFAULT").get(0)); - assertEquals("02", result.getAll("month", "DEFAULT").get(0)); - assertEquals("20", result.getAll("day", "DEFAULT").get(0)); + assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0)); + assertEquals("1.0.0", result.getAll("version", Parameter.Domain.DEFAULT).get(0)); + assertEquals("2010", result.getAll("year", Parameter.Domain.DEFAULT).get(0)); + assertEquals("02", result.getAll("month", Parameter.Domain.DEFAULT).get(0)); + assertEquals("20", result.getAll("day", Parameter.Domain.DEFAULT).get(0)); result = pathMatcher.extractUriTemplateVariables( "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.\\{\\}]+}.jar", "com.example-sources-1.0.0.{12}.jar"); - assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0)); - assertEquals("1.0.0.{12}", result.getAll("version", "DEFAULT").get(0)); + assertEquals("com.example", result.getAll("symbolicName", Parameter.Domain.DEFAULT).get(0)); + assertEquals("1.0.0.{12}", result.getAll("version", Parameter.Domain.DEFAULT).get(0)); } @Test diff --git a/net-path/src/test/java/org/xbib/net/path/simple/PathResolverTest.java b/net-path/src/test/java/org/xbib/net/path/simple/PathResolverTest.java index bbe40f2..c35078b 100644 --- a/net-path/src/test/java/org/xbib/net/path/simple/PathResolverTest.java +++ b/net-path/src/test/java/org/xbib/net/path/simple/PathResolverTest.java @@ -1,6 +1,7 @@ package org.xbib.net.path.simple; import org.junit.jupiter.api.Test; +import org.xbib.net.Parameter; import java.util.Collections; import java.util.Map; @@ -147,12 +148,12 @@ class PathResolverTest { .add("GET", "test/{two}", 4321) .build(); 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.getParameter().get("one", "PATH"), is("foo")); + assertThat(result.getParameter().get("one", Parameter.Domain.PATH), is("foo")); } else { assertThat(result.getValue(), is(4321)); - assertThat(result.getParameter().get("two", "PATH"), is("foo")); + assertThat(result.getParameter().get("two", Parameter.Domain.PATH), is("foo")); } }); } diff --git a/net-path/src/test/java/org/xbib/net/path/structure/PathResolverTest.java b/net-path/src/test/java/org/xbib/net/path/structure/PathResolverTest.java index 856a731..6b5696b 100644 --- a/net-path/src/test/java/org/xbib/net/path/structure/PathResolverTest.java +++ b/net-path/src/test/java/org/xbib/net/path/structure/PathResolverTest.java @@ -25,7 +25,7 @@ class PathResolverTest { .add( "GET", "/static/{file}", 1234) .build(); 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 @@ -68,7 +68,7 @@ class PathResolverTest { .add("GET", "discovery/{version}/rest", 1234) .build(); assertSuccessfulResolution(pathResolver, "GET", "discovery/v1/rest", 1234, - Parameter.of("PATH", Map.of("version", "v1"))); + Parameter.of(Parameter.Domain.PATH, Map.of("version", "v1"))); } @Test @@ -77,7 +77,7 @@ class PathResolverTest { .add("GET", "discovery/{discovery_version}/apis/{api}/{format}", 1234) .build(); 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 @@ -87,13 +87,13 @@ class PathResolverTest { .add("GET", "discovery/{version}/rpc", 4321) .build(); 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, - Parameter.of("PATH", Map.of("version", "v2"))); + Parameter.of(Parameter.Domain.PATH, Map.of("version", "v2"))); 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, - Parameter.of("PATH", Map.of("version", "v2"))); + Parameter.of(Parameter.Domain.PATH, Map.of("version", "v2"))); } @Test @@ -103,7 +103,7 @@ class PathResolverTest { .add("GET", "one/two", 4321) .build(); 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); } @@ -115,15 +115,15 @@ class PathResolverTest { .build(); pathResolver.resolve("GET", "one/two/three/four", result -> { assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); - if (result.getParameter().containsKey("three", "PATH")) { - assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("three", "three")))); + if (result.getParameter().containsKey("three", Parameter.Domain.PATH)) { + assertThat(result.getParameter(), is(Parameter.of(Parameter.Domain.PATH, Map.of("three", "three")))); } 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 -> { 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 -> { assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); 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) { - 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(); }); @@ -156,11 +156,11 @@ class PathResolverTest { AtomicInteger count = new AtomicInteger(); pathResolver.resolve("GET", "test/foo", result -> { assertThat(result.getValue(), anyOf(equalTo(1234), equalTo(4321))); - if (result.getParameter().containsKey("PATH", "one")) { - assertThat(result.getParameter().get("PATH", "one"), is("foo")); + if (result.getParameter().containsKey( "one", Parameter.Domain.PATH)) { + assertThat(result.getParameter().get( "one", Parameter.Domain.PATH), is("foo")); } - if (result.getParameter().containsKey("PATH", "two")) { - assertThat(result.getParameter().get("PATH", "two"), is("foo")); + if (result.getParameter().containsKey("two", Parameter.Domain.PATH)) { + assertThat(result.getParameter().get( "two", Parameter.Domain.PATH), is("foo")); } count.incrementAndGet(); }); @@ -230,7 +230,7 @@ class PathResolverTest { } private void assertSuccessfulResolution(PathResolver 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 pathResolver, String method, String path, Integer value, diff --git a/net/src/main/java/org/xbib/net/Parameter.java b/net/src/main/java/org/xbib/net/Parameter.java index ef90bb5..9e9b172 100644 --- a/net/src/main/java/org/xbib/net/Parameter.java +++ b/net/src/main/java/org/xbib/net/Parameter.java @@ -1,6 +1,5 @@ package org.xbib.net; -import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; @@ -8,9 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.logging.Logger; import java.util.stream.Collectors; -import java.util.stream.Stream; import org.xbib.datastructures.common.ImmutableList; import org.xbib.datastructures.common.LinkedHashSetMultiMap; import org.xbib.datastructures.common.MultiMap; @@ -18,6 +15,15 @@ import org.xbib.datastructures.common.Pair; public class Parameter implements Iterable>, Comparable { + public enum Domain { + DEFAULT, + QUERY, + FORM, + PATH, + HEADER, + COOKIE + } + private static final Parameter EMPTY = Parameter.builder().build(); private final ParameterBuilder builder; @@ -50,7 +56,7 @@ public class Parameter implements Iterable>, Comparable map) { + public static Parameter of(Domain domain, Map map) { return Parameter.builder().domain(domain).enableSort().add(map).build(); } @@ -81,13 +87,12 @@ public class Parameter implements Iterable>, Comparable collection = (Collection) object; @@ -103,7 +108,7 @@ public class Parameter implements Iterable>, Comparable collection = (Collection) object; @@ -123,7 +128,7 @@ public class Parameter implements Iterable>, Comparable collection = (Collection) object; @@ -178,9 +183,9 @@ public class Parameter implements Iterable>, Comparable getAllDomain(String... domains) { + public List getAllDomain(Domain... domains) { Parameter parameter = null; - for (String domain : domains) { + for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain); } @@ -197,9 +202,9 @@ public class Parameter implements Iterable>, Comparable>, Comparable 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 getAll(String key, Domain... domains) { Parameter parameter = null; - for (String domain : domains) { + for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain); } @@ -234,9 +248,9 @@ public class Parameter implements Iterable>, Comparable>, Comparable> list; - protected String domain; + protected Parameter.Domain domain; - final Map parameterMap; + final Map parameterMap; private Charset charset; @@ -65,7 +65,7 @@ public class ParameterBuilder implements PairValidator { ParameterBuilder() { this.list = new ArrayList<>(); this.parameterMap = new HashMap<>(); - this.domain = "DEFAULT"; + this.domain = Parameter.Domain.DEFAULT; this.limit = 0; } @@ -139,7 +139,7 @@ public class ParameterBuilder implements PairValidator { return this; } - public ParameterBuilder domain(String domain) { + public ParameterBuilder domain(Parameter.Domain domain) { this.domain = domain; return this; } diff --git a/net/src/test/java/org/xbib/net/ParameterTest.java b/net/src/test/java/org/xbib/net/ParameterTest.java index 1d5611c..e6756a0 100644 --- a/net/src/test/java/org/xbib/net/ParameterTest.java +++ b/net/src/test/java/org/xbib/net/ParameterTest.java @@ -17,7 +17,7 @@ public class ParameterTest { public void testEmptyBuilder() { Parameter parameter = Parameter.builder().build(); assertNotNull(parameter); - assertFalse(parameter.containsKey("param1", "DEFAULT")); + assertFalse(parameter.containsKey("param1", Parameter.Domain.DEFAULT)); } @Test @@ -26,7 +26,7 @@ public class ParameterTest { .add("Hello", "World") .build(); assertNotNull(parameter); - assertTrue(parameter.containsKey("Hello", "DEFAULT")); + assertTrue(parameter.containsKey("Hello", Parameter.Domain.DEFAULT)); } @Test @@ -38,8 +38,8 @@ public class ParameterTest { .add("Hello", "World") .build(); assertNotNull(parameter); - assertTrue(parameter.containsKey("Hello", "DEFAULT")); - assertEquals(List.of("World", "World", "World"), parameter.getAll("Hello", "DEFAULT")); + assertTrue(parameter.containsKey("Hello", Parameter.Domain.DEFAULT)); + assertEquals(List.of("World", "World", "World"), parameter.getAll("Hello", Parameter.Domain.DEFAULT)); } @Test @@ -47,15 +47,15 @@ public class ParameterTest { Parameter parameter = Parameter.builder() .charset(StandardCharsets.US_ASCII) .lowercase() - .domain("HEADER") + .domain(Parameter.Domain.HEADER) .add("Content-Type", "text/plain") .add("Accept", "*/*") .add("Connection", "close") .build(); assertNotNull(parameter); - assertEquals("HEADER", parameter.getDomain()); - assertTrue(parameter.containsKey("content-type", "HEADER")); - assertEquals(List.of("close"), parameter.getAll( "connection", "HEADER")); + assertEquals(Parameter.Domain.HEADER, parameter.getDomain()); + assertTrue(parameter.containsKey("content-type", Parameter.Domain.HEADER)); + assertEquals(List.of("close"), parameter.getAll( "connection", Parameter.Domain.HEADER)); } @Test @@ -121,9 +121,9 @@ public class ParameterTest { String body = "a=b&c=d&e=f"; queryParameters.addPercentEncodedBody(body); Parameter parameter = queryParameters.build(); - assertEquals("b", parameter.getAll("a", "DEFAULT").get(0)); - assertEquals("d", parameter.getAll("c", "DEFAULT").get(0)); - assertEquals("f", parameter.getAll("e", "DEFAULT").get(0)); + assertEquals("b", parameter.getAll("a", Parameter.Domain.DEFAULT).get(0)); + assertEquals("d", parameter.getAll("c", Parameter.Domain.DEFAULT).get(0)); + assertEquals("f", parameter.getAll("e", Parameter.Domain.DEFAULT).get(0)); } @Test @@ -137,32 +137,31 @@ public class ParameterTest { String body = String.join("&", list); queryParameters.addPercentEncodedBody(body); Parameter parameter = queryParameters.build(); - assertEquals("b0", parameter.getAll("a0", "DEFAULT").get(0)); - assertEquals("b99", parameter.getAll("a99", "DEFAULT").get(0)); - assertEquals("[]", parameter.getAll("a100", "DEFAULT").toString()); + assertEquals("b0", parameter.getAll("a0", Parameter.Domain.DEFAULT).get(0)); + assertEquals("b99", parameter.getAll("a99", Parameter.Domain.DEFAULT).get(0)); + assertEquals("[]", parameter.getAll("a100", Parameter.Domain.DEFAULT).toString()); }); } @Test void testDomains() { - Parameter p1 = Parameter.builder().domain("A").add("a", "a").build(); - Parameter p2 = Parameter.builder().domain("B").add("b", "b").build(); - Parameter p3 = Parameter.builder().domain("C").add("c", "c").build(); + Parameter p1 = Parameter.builder().domain(Parameter.Domain.QUERY).add("a", "a").build(); + Parameter p2 = Parameter.builder().domain(Parameter.Domain.FORM).add("b", "b").build(); + Parameter p3 = Parameter.builder().domain(Parameter.Domain.HEADER).add("c", "c").build(); Parameter p = Parameter.builder() .add(p1) .add(p2) .add(p3) .build(); - assertEquals("a", p.get("a", "A").toString()); - assertEquals("b", p.get("b", "B").toString()); - assertEquals("c", p.get("c", "C").toString()); - assertEquals("[] A -> [a=a] B -> [b=b] C -> [c=c]", p.toString()); - assertEquals("a", p.get("a", "A", "B", "C")); - assertEquals("b", p.get("b", "A", "B", "C")); - assertEquals("c", p.get("c", "A", "B", "C")); - assertTrue(p.isPresent("A")); - assertTrue(p.isPresent("B")); - assertTrue(p.isPresent("C")); - assertFalse(p.isPresent("D")); + assertEquals("a", p.get("a", Parameter.Domain.QUERY).toString()); + assertEquals("b", p.get("b", Parameter.Domain.FORM).toString()); + assertEquals("c", p.get("c", Parameter.Domain.HEADER).toString()); + assertEquals("a", p.get("a", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); + assertEquals("b", p.get("b", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); + assertEquals("c", p.get("c", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); + assertTrue(p.isPresent(Parameter.Domain.QUERY)); + assertTrue(p.isPresent(Parameter.Domain.FORM)); + assertTrue(p.isPresent(Parameter.Domain.HEADER)); + assertFalse(p.isPresent(Parameter.Domain.DEFAULT)); } } diff --git a/net/src/test/java/org/xbib/net/URLParserTest.java b/net/src/test/java/org/xbib/net/URLParserTest.java index 852062e..826857d 100644 --- a/net/src/test/java/org/xbib/net/URLParserTest.java +++ b/net/src/test/java/org/xbib/net/URLParserTest.java @@ -327,7 +327,7 @@ class URLParserTest { assertEquals("path", pathSegment.getSegment()); assertEquals("p2", pathSegment.getMatrixParams().get(0).getKey()); 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