change Parameter domain handling
This commit is contained in:
parent
1dc1825672
commit
c37ff23865
10 changed files with 175 additions and 111 deletions
|
@ -1,5 +1,5 @@
|
||||||
group = org.xbib
|
group = org.xbib
|
||||||
name = net
|
name = net
|
||||||
version = 3.0.3
|
version = 3.0.4
|
||||||
|
|
||||||
org.gradle.warning.mode = ALL
|
org.gradle.warning.mode = ALL
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class PathDecoder {
|
||||||
String path = pos > 0 ? pathAndQuery.substring(0, pos) : pathAndQuery;
|
String path = pos > 0 ? pathAndQuery.substring(0, pos) : pathAndQuery;
|
||||||
this.query = pos > 0 ? pathAndQuery.substring(pos + 1) : null;
|
this.query = pos > 0 ? pathAndQuery.substring(pos + 1) : null;
|
||||||
this.path = PathNormalizer.normalize(path);
|
this.path = PathNormalizer.normalize(path);
|
||||||
this.params = Parameter.builder().enablePercentDeccoding();
|
this.params = Parameter.builder().enablePercentDecoding();
|
||||||
if (query != null) {
|
if (query != null) {
|
||||||
parse(query);
|
parse(query);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,15 +11,15 @@ class PathDecoderTest {
|
||||||
@Test
|
@Test
|
||||||
void testPlusSign() throws Exception {
|
void testPlusSign() throws Exception {
|
||||||
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("DEFAULT", "a").toString());
|
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString());
|
||||||
assertEquals("[e f]", decoder.getParameter().getAll("DEFAULT", "d").toString());
|
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSlash() throws Exception {
|
void testSlash() throws Exception {
|
||||||
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("DEFAULT", "a").toString());
|
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString());
|
||||||
assertEquals("[e f]", decoder.getParameter().getAll("DEFAULT", "d").toString());
|
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -32,16 +32,16 @@ class PathDecoderTest {
|
||||||
void testSlashes() throws Exception {
|
void testSlashes() throws Exception {
|
||||||
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("DEFAULT", "a").toString());
|
assertEquals("[b c]", decoder.getParameter().getAll("a", "DEFAULT").toString());
|
||||||
assertEquals("[e f]", decoder.getParameter().getAll("DEFAULT", "d").toString());
|
assertEquals("[e f]", decoder.getParameter().getAll("d", "DEFAULT").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPlusPercent() throws Exception {
|
void testPlusPercent() throws Exception {
|
||||||
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("DEFAULT", "a").toString());
|
assertEquals("[b+c]", decoder.getParameter().getAll("a", "DEFAULT").toString());
|
||||||
assertEquals("[e+f]", decoder.getParameter().getAll("DEFAULT", "d").toString());
|
assertEquals("[e+f]", decoder.getParameter().getAll("d", "DEFAULT").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -58,7 +58,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("DEFAULT", "x-fl-target").get(0).toString());
|
url = URL.from(decoder.getParameter().getAll("x-fl-target", "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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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("DEFAULT", "symbolicName").get(0));
|
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0));
|
||||||
assertEquals("1.0.0", result.getAll("DEFAULT", "version").get(0));
|
assertEquals("1.0.0", result.getAll("version", "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("DEFAULT", "symbolicName").get(0));
|
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0));
|
||||||
assertEquals("1.0.0", result.getAll("DEFAULT", "version").get(0));
|
assertEquals("1.0.0", result.getAll("version", "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("DEFAULT", "symbolicName").get(0));
|
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0));
|
||||||
assertEquals("1.0.0", result.getAll("DEFAULT", "version").get(0));
|
assertEquals("1.0.0", result.getAll("version", "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("DEFAULT", "symbolicName").get(0));
|
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0));
|
||||||
assertEquals("1.0.0", result.getAll("DEFAULT", "version").get(0));
|
assertEquals("1.0.0", result.getAll("version", "DEFAULT").get(0));
|
||||||
assertEquals("2010", result.getAll("DEFAULT", "year").get(0));
|
assertEquals("2010", result.getAll("year", "DEFAULT").get(0));
|
||||||
assertEquals("02", result.getAll("DEFAULT", "month").get(0));
|
assertEquals("02", result.getAll("month", "DEFAULT").get(0));
|
||||||
assertEquals("20", result.getAll("DEFAULT", "day").get(0));
|
assertEquals("20", result.getAll("day", "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("DEFAULT", "symbolicName").get(0));
|
assertEquals("com.example", result.getAll("symbolicName", "DEFAULT").get(0));
|
||||||
assertEquals("1.0.0.{12}", result.getAll("DEFAULT", "version").get(0));
|
assertEquals("1.0.0.{12}", result.getAll("version", "DEFAULT").get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -147,12 +147,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("PATH", "one")) {
|
if (result.getParameter().containsKey("one", "PATH")) {
|
||||||
assertThat(result.getValue(), is(1234));
|
assertThat(result.getValue(), is(1234));
|
||||||
assertThat(result.getParameter().get("PATH", "one"), is("foo"));
|
assertThat(result.getParameter().get("one", "PATH"), is("foo"));
|
||||||
} else {
|
} else {
|
||||||
assertThat(result.getValue(), is(4321));
|
assertThat(result.getValue(), is(4321));
|
||||||
assertThat(result.getParameter().get("PATH", "two"), is("foo"));
|
assertThat(result.getParameter().get("two", "PATH"), is("foo"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,6 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
@ -116,7 +115,7 @@ 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("PATH", "three")) {
|
if (result.getParameter().containsKey("three", "PATH")) {
|
||||||
assertThat(result.getParameter(), is(Parameter.of("PATH", Map.of("three", "three"))));
|
assertThat(result.getParameter(), is(Parameter.of("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("PATH", Map.of("one", "one", "two", "two", "four", "four"))));
|
||||||
|
@ -231,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.of("PATH"));
|
assertSuccessfulResolution(pathResolver, "GET", path, value, Parameter.builder().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,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
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;
|
||||||
|
@ -7,6 +8,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 java.util.stream.Stream;
|
||||||
import org.xbib.datastructures.common.ImmutableList;
|
import org.xbib.datastructures.common.ImmutableList;
|
||||||
|
@ -44,10 +46,6 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Parameter of(String domain) {
|
|
||||||
return Parameter.builder().domain(domain).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Parameter of(Map<String, Object> map) {
|
public static Parameter of(Map<String, Object> map) {
|
||||||
return Parameter.builder().enableSort().add(map).build();
|
return Parameter.builder().enableSort().add(map).build();
|
||||||
}
|
}
|
||||||
|
@ -75,31 +73,46 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Parameter o) {
|
public int compareTo(Parameter o) {
|
||||||
return toString().compareTo(o.toString());
|
return list.toString().compareTo(o.list.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return list.toString();
|
return allToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return builder.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public String getAsString(String domain, String key) {
|
public String getAsString(String key, String... domains) {
|
||||||
Object object = get(domain, key);
|
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;
|
||||||
object = collection.iterator().next();
|
Iterator<Object> iterator = collection.iterator();
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
object = iterator.next();
|
||||||
|
} else {
|
||||||
|
object = null;
|
||||||
|
}
|
||||||
return object != null ? object.toString() : null;
|
return object != null ? object.toString() : null;
|
||||||
}
|
}
|
||||||
return object != null ? object instanceof String ? (String) object : object.toString() : null;
|
return object != null ? object instanceof String ? (String) object : object.toString() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Integer getAsInteger(String domain, String key) {
|
public Integer getAsInteger(String key, String... domains) {
|
||||||
Object object = get(domain, key);
|
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;
|
||||||
object = collection.iterator().next();
|
Iterator<Object> iterator = collection.iterator();
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
object = iterator.next();
|
||||||
|
} else {
|
||||||
|
object = null;
|
||||||
|
}
|
||||||
return object != null ? Integer.parseInt(object.toString()) : null;
|
return object != null ? Integer.parseInt(object.toString()) : null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -110,11 +123,16 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Boolean getAsBoolean(String domain, String key) {
|
public Boolean getAsBoolean(String key, String... domains) {
|
||||||
Object object = get(domain, key);
|
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;
|
||||||
object = collection.iterator().next();
|
Iterator<Object> iterator = collection.iterator();
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
object = iterator.next();
|
||||||
|
} else {
|
||||||
|
object = null;
|
||||||
|
}
|
||||||
return object != null ? Boolean.parseBoolean(object.toString()) : null;
|
return object != null ? Boolean.parseBoolean(object.toString()) : null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -127,9 +145,7 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
|
||||||
public String allToString() {
|
public String allToString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(list.toString());
|
sb.append(list.toString());
|
||||||
if (!builder.parameterMap.isEmpty()) {
|
|
||||||
builder.parameterMap.forEach((key, value) -> sb.append(" ").append(key).append(" -> ").append(value));
|
builder.parameterMap.forEach((key, value) -> sb.append(" ").append(key).append(" -> ").append(value));
|
||||||
}
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,58 +178,103 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDomain() {
|
public List<Object> getAllDomain(String... domains) {
|
||||||
return builder.domain;
|
Parameter parameter = null;
|
||||||
|
for (String domain : domains) {
|
||||||
|
if (builder.parameterMap.containsKey(domain)) {
|
||||||
|
parameter = builder.parameterMap.get(domain);
|
||||||
}
|
}
|
||||||
|
if (parameter != null) {
|
||||||
public Stream<Pair<String, Object>> stream(String domain) {
|
List<Object> list = parameter.getAllDomain(domains);
|
||||||
if (!builder.domain.equals(domain)) {
|
if (list != null) {
|
||||||
throw new IllegalArgumentException("domain mismatch");
|
return list;
|
||||||
}
|
}
|
||||||
return list.stream();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Object> getAll(String domain, String key) {
|
|
||||||
Optional<Parameter> optional = builder.parameterMap.values().stream().filter(p -> domain.equals(p.getDomain())).findFirst();
|
|
||||||
if (optional.isPresent()) {
|
|
||||||
return optional.get().getAll(domain, key);
|
|
||||||
} else {
|
|
||||||
if (!builder.domain.equals(domain)) {
|
|
||||||
throw new IllegalArgumentException("domain mismatch");
|
|
||||||
}
|
}
|
||||||
return list.stream()
|
return list.stream()
|
||||||
.filter(p -> key.equals(p.getKey()))
|
|
||||||
.map(Pair::getValue)
|
.map(Pair::getValue)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsKey(String domain, String key) {
|
public boolean isPresent(String... domains) {
|
||||||
Optional<Parameter> optional = builder.parameterMap.values().stream().filter(p -> domain.equals(p.getDomain())).findFirst();
|
Parameter parameter = null;
|
||||||
if (optional.isPresent()) {
|
for (String domain : domains) {
|
||||||
return optional.get().containsKey(domain, key);
|
if (builder.parameterMap.containsKey(domain)) {
|
||||||
} else {
|
parameter = builder.parameterMap.get(domain);
|
||||||
if (!builder.domain.equals(domain)) {
|
|
||||||
throw new IllegalArgumentException("domain mismatch");
|
|
||||||
}
|
}
|
||||||
return list.stream().anyMatch(p -> key.equals(p.getKey()));
|
if (parameter != null) {
|
||||||
|
boolean b = parameter.isPresent(domains);
|
||||||
|
if (b) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return list.stream().findAny().isPresent();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public Object get(String domain, String key) {
|
public List<Object> getAll(String key, String... domains) {
|
||||||
Optional<Parameter> optional = builder.parameterMap.values().stream().filter(p -> domain.equals(p.getDomain())).findFirst();
|
Parameter parameter = null;
|
||||||
if (optional.isPresent()) {
|
for (String domain : domains) {
|
||||||
return optional.get().getAll(domain, key);
|
if (builder.parameterMap.containsKey(domain)) {
|
||||||
} else {
|
parameter = builder.parameterMap.get(domain);
|
||||||
if (!builder.domain.equals(domain)) {
|
}
|
||||||
throw new IllegalArgumentException("domain mismatch");
|
if (parameter != null) {
|
||||||
|
List<Object> list = parameter.getAll(key, domains);
|
||||||
|
if (list != null) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return list.stream()
|
return list.stream()
|
||||||
|
.filter(p -> p.getKey().equals(key))
|
||||||
|
.map(Pair::getValue)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsKey(String key, String... domains) {
|
||||||
|
Parameter parameter = null;
|
||||||
|
for (String domain : domains) {
|
||||||
|
if (builder.parameterMap.containsKey(domain)) {
|
||||||
|
parameter = builder.parameterMap.get(domain);
|
||||||
|
}
|
||||||
|
if (parameter != null) {
|
||||||
|
boolean b = parameter.containsKey(key, domains);
|
||||||
|
if (b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list.stream()
|
||||||
|
.anyMatch(p -> key.equals(p.getKey()));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(String key, String... domains) {
|
||||||
|
Parameter parameter = null;
|
||||||
|
for (String domain : domains) {
|
||||||
|
if (builder.parameterMap.containsKey(domain)) {
|
||||||
|
parameter = builder.parameterMap.get(domain);
|
||||||
|
}
|
||||||
|
if (parameter != null) {
|
||||||
|
Object object = parameter.get(key, domains);
|
||||||
|
if (object != null) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Optional<Object> optional = list.stream()
|
||||||
.filter(p -> key.equals(p.getKey()))
|
.filter(p -> key.equals(p.getKey()))
|
||||||
.map(Pair::getValue)
|
.map(Pair::getValue)
|
||||||
.findFirst().orElse(null);
|
.findFirst();
|
||||||
|
if (optional.isPresent()) {
|
||||||
|
return optional.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public String getAsQueryString() {
|
public String getAsQueryString() {
|
||||||
return queryString;
|
return queryString;
|
||||||
|
|
|
@ -32,7 +32,7 @@ public class ParameterBuilder implements PairValidator {
|
||||||
|
|
||||||
private final List<Pair<String, Object>> list;
|
private final List<Pair<String, Object>> list;
|
||||||
|
|
||||||
String domain;
|
protected String domain;
|
||||||
|
|
||||||
final Map<String, Parameter> parameterMap;
|
final Map<String, Parameter> parameterMap;
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ public class ParameterBuilder implements PairValidator {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterBuilder enablePercentDeccoding() {
|
public ParameterBuilder enablePercentDecoding() {
|
||||||
this.enablePercentDecoding = true;
|
this.enablePercentDecoding = true;
|
||||||
charset(StandardCharsets.UTF_8);
|
charset(StandardCharsets.UTF_8);
|
||||||
return this;
|
return this;
|
||||||
|
@ -172,11 +172,10 @@ public class ParameterBuilder implements PairValidator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterBuilder add(Parameter parameter) {
|
public ParameterBuilder add(Parameter parameter) {
|
||||||
if (domain.equals(parameter.getDomain())) {
|
if (parameterMap.containsKey(parameter.getDomain())) {
|
||||||
parameter.stream(domain).forEach(this::add);
|
throw new IllegalArgumentException("unable to add domain " + parameter.getDomain());
|
||||||
} else {
|
|
||||||
parameterMap.putIfAbsent(parameter.getDomain(), parameter);
|
|
||||||
}
|
}
|
||||||
|
parameterMap.putIfAbsent(parameter.getDomain(), parameter);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +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);
|
||||||
assertEquals("DEFAULT", parameter.getDomain());
|
assertFalse(parameter.containsKey("param1", "DEFAULT"));
|
||||||
assertFalse(parameter.containsKey("DEFAULT", "param1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,8 +26,7 @@ public class ParameterTest {
|
||||||
.add("Hello", "World")
|
.add("Hello", "World")
|
||||||
.build();
|
.build();
|
||||||
assertNotNull(parameter);
|
assertNotNull(parameter);
|
||||||
assertEquals("DEFAULT", parameter.getDomain());
|
assertTrue(parameter.containsKey("Hello", "DEFAULT"));
|
||||||
assertTrue(parameter.containsKey("DEFAULT", "Hello"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -40,9 +38,8 @@ public class ParameterTest {
|
||||||
.add("Hello", "World")
|
.add("Hello", "World")
|
||||||
.build();
|
.build();
|
||||||
assertNotNull(parameter);
|
assertNotNull(parameter);
|
||||||
assertEquals("DEFAULT", parameter.getDomain());
|
assertTrue(parameter.containsKey("Hello", "DEFAULT"));
|
||||||
assertTrue(parameter.containsKey("DEFAULT", "Hello"));
|
assertEquals(List.of("World", "World", "World"), parameter.getAll("Hello", "DEFAULT"));
|
||||||
assertEquals(List.of("World", "World", "World"), parameter.getAll("DEFAULT", "Hello"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -57,8 +54,8 @@ public class ParameterTest {
|
||||||
.build();
|
.build();
|
||||||
assertNotNull(parameter);
|
assertNotNull(parameter);
|
||||||
assertEquals("HEADER", parameter.getDomain());
|
assertEquals("HEADER", parameter.getDomain());
|
||||||
assertTrue(parameter.containsKey("HEADER", "content-type"));
|
assertTrue(parameter.containsKey("content-type", "HEADER"));
|
||||||
assertEquals(List.of("close"), parameter.getAll("HEADER", "connection"));
|
assertEquals(List.of("close"), parameter.getAll( "connection", "HEADER"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -112,7 +109,7 @@ public class ParameterTest {
|
||||||
.build();
|
.build();
|
||||||
URLBuilder mutator = url.mutator();
|
URLBuilder mutator = url.mutator();
|
||||||
mutator.path(requestPath);
|
mutator.path(requestPath);
|
||||||
httpParameter.stream("DEFAULT").forEach(e -> mutator.queryParam(e.getKey(), e.getValue()));
|
httpParameter.forEach(e -> mutator.queryParam(e.getKey(), e.getValue()));
|
||||||
url = mutator.build();
|
url = mutator.build();
|
||||||
assertEquals("http://localhost/path?operation=searchRetrieve&query=iss%20%3D%2000280836&recordSchema=MARC21plus-1-xml&version=1.1",
|
assertEquals("http://localhost/path?operation=searchRetrieve&query=iss%20%3D%2000280836&recordSchema=MARC21plus-1-xml&version=1.1",
|
||||||
url.toExternalForm());
|
url.toExternalForm());
|
||||||
|
@ -124,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("DEFAULT", "a").get(0));
|
assertEquals("b", parameter.getAll("a", "DEFAULT").get(0));
|
||||||
assertEquals("d", parameter.getAll("DEFAULT", "c").get(0));
|
assertEquals("d", parameter.getAll("c", "DEFAULT").get(0));
|
||||||
assertEquals("f", parameter.getAll("DEFAULT", "e").get(0));
|
assertEquals("f", parameter.getAll("e", "DEFAULT").get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -140,14 +137,14 @@ 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("DEFAULT", "a0").get(0));
|
assertEquals("b0", parameter.getAll("a0", "DEFAULT").get(0));
|
||||||
assertEquals("b99", parameter.getAll("DEFAULT", "a99").get(0));
|
assertEquals("b99", parameter.getAll("a99", "DEFAULT").get(0));
|
||||||
assertEquals("[]", parameter.getAll("DEFAULT", "a100").toString());
|
assertEquals("[]", parameter.getAll("a100", "DEFAULT").toString());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSubDomains() {
|
void testDomains() {
|
||||||
Parameter p1 = Parameter.builder().domain("A").add("a", "a").build();
|
Parameter p1 = Parameter.builder().domain("A").add("a", "a").build();
|
||||||
Parameter p2 = Parameter.builder().domain("B").add("b", "b").build();
|
Parameter p2 = Parameter.builder().domain("B").add("b", "b").build();
|
||||||
Parameter p3 = Parameter.builder().domain("C").add("c", "c").build();
|
Parameter p3 = Parameter.builder().domain("C").add("c", "c").build();
|
||||||
|
@ -156,8 +153,16 @@ public class ParameterTest {
|
||||||
.add(p2)
|
.add(p2)
|
||||||
.add(p3)
|
.add(p3)
|
||||||
.build();
|
.build();
|
||||||
assertEquals("[a]", p.get("A", "a").toString());
|
assertEquals("a", p.get("a", "A").toString());
|
||||||
assertEquals("[b]", p.get("B", "b").toString());
|
assertEquals("b", p.get("b", "B").toString());
|
||||||
assertEquals("[c]", p.get("C", "c").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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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("DEFAULT", "q1").get(0));
|
assertEquals("v3", url.getQueryParams().getAll("q1", "DEFAULT").get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue