From 53e9469d18e2cca61f740146f6c814b4fba47fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Sun, 14 Jul 2019 21:38:57 +0200 Subject: [PATCH] fixes matrix params, checkstyle, spotbugs --- build.gradle | 6 ++- config/checkstyle/checkstyle.xml | 8 ++- .../org/xbib/net/http/HttpParameters.java | 12 ++--- .../java/org/xbib/net/http/HttpResponse.java | 3 ++ .../net/http/UrlStringRequestAdapter.java | 49 ------------------ .../java/org/xbib/net/http/package-info.java | 4 ++ .../org/xbib/net/http/util/LimitedSet.java | 34 +++++++++++++ .../net/http/util/LimitedSortedStringSet.java | 28 ---------- .../xbib/net/http/util/LimitedStringMap.java | 25 --------- .../xbib/net/http/util/LimitedTreeMap.java | 32 ++++++++++++ .../org/xbib/net/http/util/package-info.java | 4 ++ net-url/src/main/java/org/xbib/net/Pair.java | 31 +++++++++++ .../java/org/xbib/net/PercentEncoders.java | 1 + .../java/org/xbib/net/QueryParameters.java | 39 +++----------- net-url/src/main/java/org/xbib/net/URL.java | 51 ++++--------------- .../java/org/xbib/net/URLSyntaxException.java | 1 + .../xbib/net/path/PathPatternComparator.java | 4 +- .../main/java/org/xbib/net/path/PathTrie.java | 5 +- .../java/org/xbib/net/body/ParseBodyTest.java | 2 +- .../java/org/xbib/net/body/package-info.java | 5 ++ .../xbib/net/template/URITemplateTest.java | 13 +++-- 21 files changed, 161 insertions(+), 196 deletions(-) delete mode 100644 net-http/src/main/java/org/xbib/net/http/UrlStringRequestAdapter.java create mode 100644 net-http/src/main/java/org/xbib/net/http/package-info.java create mode 100644 net-http/src/main/java/org/xbib/net/http/util/LimitedSet.java delete mode 100644 net-http/src/main/java/org/xbib/net/http/util/LimitedSortedStringSet.java delete mode 100644 net-http/src/main/java/org/xbib/net/http/util/LimitedStringMap.java create mode 100644 net-http/src/main/java/org/xbib/net/http/util/LimitedTreeMap.java create mode 100644 net-http/src/main/java/org/xbib/net/http/util/package-info.java create mode 100644 net-url/src/main/java/org/xbib/net/Pair.java create mode 100644 net-url/src/test/java/org/xbib/net/body/package-info.java diff --git a/build.gradle b/build.gradle index 1d65cbf..c7a5cd1 100644 --- a/build.gradle +++ b/build.gradle @@ -37,12 +37,14 @@ subprojects { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } + compileTestJava { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } + tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:all,-serial" + options.compilerArgs << "-Xlint:all" } jar { @@ -130,7 +132,7 @@ subprojects { } checkstyle { - configFile = rootProject.file('config/checkstyle/checkstyle.xml') + //configFile = rootProject.file('config/checkstyle/checkstyle.xml') ignoreFailures = true showViolations = true } diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 3dc3e16..157f848 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -53,12 +53,10 @@ page at http://checkstyle.sourceforge.net/config.html --> - - + - - - + + diff --git a/net-http/src/main/java/org/xbib/net/http/HttpParameters.java b/net-http/src/main/java/org/xbib/net/http/HttpParameters.java index 4f3eecc..a1f2703 100644 --- a/net-http/src/main/java/org/xbib/net/http/HttpParameters.java +++ b/net-http/src/main/java/org/xbib/net/http/HttpParameters.java @@ -3,8 +3,8 @@ package org.xbib.net.http; import org.xbib.net.PercentDecoder; import org.xbib.net.PercentEncoder; import org.xbib.net.PercentEncoders; -import org.xbib.net.http.util.LimitedSortedStringSet; -import org.xbib.net.http.util.LimitedStringMap; +import org.xbib.net.http.util.LimitedSet; +import org.xbib.net.http.util.LimitedTreeMap; import java.nio.charset.MalformedInputException; import java.nio.charset.StandardCharsets; @@ -28,14 +28,14 @@ import java.util.SortedSet; */ public class HttpParameters implements Map> { - private final LimitedStringMap wrappedMap; + private final LimitedTreeMap wrappedMap; private final PercentEncoder percentEncoder; private final PercentDecoder percentDecoder; public HttpParameters() { - this.wrappedMap = new LimitedStringMap(); + this.wrappedMap = new LimitedTreeMap<>(); this.percentEncoder = PercentEncoders.getQueryEncoder(StandardCharsets.UTF_8); this.percentDecoder = new PercentDecoder(); } @@ -155,7 +155,7 @@ public class HttpParameters implements Map> { String k = percentEncode ? percentEncoder.encode(key) : key; SortedSet values = wrappedMap.get(k); if (values == null) { - values = new LimitedSortedStringSet(); + values = new LimitedSet<>(); wrappedMap.put(k, values); } String v = null; @@ -208,7 +208,7 @@ public class HttpParameters implements Map> { for (String key : m.keySet()) { SortedSet vals = get(key); if (vals == null) { - vals = new LimitedSortedStringSet(); + vals = new LimitedSet<>(); put(key, vals); } vals.addAll(m.get(key)); diff --git a/net-http/src/main/java/org/xbib/net/http/HttpResponse.java b/net-http/src/main/java/org/xbib/net/http/HttpResponse.java index 6dfd13b..74da62c 100644 --- a/net-http/src/main/java/org/xbib/net/http/HttpResponse.java +++ b/net-http/src/main/java/org/xbib/net/http/HttpResponse.java @@ -3,6 +3,9 @@ package org.xbib.net.http; import java.io.IOException; import java.io.InputStream; +/** + * + */ public interface HttpResponse { int getStatusCode() throws IOException; diff --git a/net-http/src/main/java/org/xbib/net/http/UrlStringRequestAdapter.java b/net-http/src/main/java/org/xbib/net/http/UrlStringRequestAdapter.java deleted file mode 100644 index 51a68f7..0000000 --- a/net-http/src/main/java/org/xbib/net/http/UrlStringRequestAdapter.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.xbib.net.http; - -import java.io.InputStream; -import java.util.Collections; -import java.util.Map; - -public class UrlStringRequestAdapter implements HttpRequest { - - private String url; - - public UrlStringRequestAdapter(String url) { - this.url = url; - } - - public String getMethod() { - return "GET"; - } - - public String getRequestUrl() { - return url; - } - - public void setRequestUrl(String url) { - this.url = url; - } - - public void setHeader(String name, String value) { - } - - public String getHeader(String name) { - return null; - } - - public Map getAllHeaders() { - return Collections.emptyMap(); - } - - public InputStream getMessagePayload() { - return null; - } - - public String getContentType() { - return null; - } - - public Object unwrap() { - return url; - } -} diff --git a/net-http/src/main/java/org/xbib/net/http/package-info.java b/net-http/src/main/java/org/xbib/net/http/package-info.java new file mode 100644 index 0000000..29afdf0 --- /dev/null +++ b/net-http/src/main/java/org/xbib/net/http/package-info.java @@ -0,0 +1,4 @@ +/** + * Net classes for HTTP. + */ +package org.xbib.net.http; diff --git a/net-http/src/main/java/org/xbib/net/http/util/LimitedSet.java b/net-http/src/main/java/org/xbib/net/http/util/LimitedSet.java new file mode 100644 index 0000000..31973bd --- /dev/null +++ b/net-http/src/main/java/org/xbib/net/http/util/LimitedSet.java @@ -0,0 +1,34 @@ +package org.xbib.net.http.util; + +import java.util.Objects; +import java.util.TreeSet; + +/** + * Limited set. + * @param type + */ +@SuppressWarnings("serial") +public class LimitedSet extends TreeSet { + + private final int sizeLimit; + + private final int elementSizeLimit; + + public LimitedSet() { + this(1024, 65536); + } + + public LimitedSet(int sizeLimit, int elementSizeLimit) { + this.sizeLimit = sizeLimit; + this.elementSizeLimit = elementSizeLimit; + } + + @Override + public boolean add(T t) { + Objects.requireNonNull(t); + if (size() < sizeLimit && t.length() <= elementSizeLimit) { + return super.add(t); + } + throw new IllegalArgumentException("limit exceeded"); + } +} diff --git a/net-http/src/main/java/org/xbib/net/http/util/LimitedSortedStringSet.java b/net-http/src/main/java/org/xbib/net/http/util/LimitedSortedStringSet.java deleted file mode 100644 index a2ed914..0000000 --- a/net-http/src/main/java/org/xbib/net/http/util/LimitedSortedStringSet.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.xbib.net.http.util; - -import java.util.SortedSet; -import java.util.TreeSet; - -public class LimitedSortedStringSet extends TreeSet implements SortedSet { - - private final int sizeLimit; - - private final int elementSizeLimit; - - public LimitedSortedStringSet() { - this(1024, 65536); - } - - public LimitedSortedStringSet(int sizeLimit, int elementSizeLimit) { - this.sizeLimit = sizeLimit; - this.elementSizeLimit = elementSizeLimit; - } - - @Override - public boolean add(String string) { - if (size() < sizeLimit && string.length() <= elementSizeLimit ) { - return super.add(string); - } - return false; - } -} diff --git a/net-http/src/main/java/org/xbib/net/http/util/LimitedStringMap.java b/net-http/src/main/java/org/xbib/net/http/util/LimitedStringMap.java deleted file mode 100644 index c947437..0000000 --- a/net-http/src/main/java/org/xbib/net/http/util/LimitedStringMap.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.xbib.net.http.util; - -import java.util.SortedSet; -import java.util.TreeMap; - -public class LimitedStringMap extends TreeMap> { - - private final int limit; - - public LimitedStringMap() { - this(1024); - } - - public LimitedStringMap(int limit) { - this.limit = limit; - } - - @Override - public SortedSet put(String key, SortedSet value) { - if (size() < limit) { - return super.put(key, value); - } - return null; - } -} diff --git a/net-http/src/main/java/org/xbib/net/http/util/LimitedTreeMap.java b/net-http/src/main/java/org/xbib/net/http/util/LimitedTreeMap.java new file mode 100644 index 0000000..efce901 --- /dev/null +++ b/net-http/src/main/java/org/xbib/net/http/util/LimitedTreeMap.java @@ -0,0 +1,32 @@ +package org.xbib.net.http.util; + +import java.util.SortedSet; +import java.util.TreeMap; + +/** + * Tree Map with a limited number of elements. + * + * @param key + * @param value + */ +@SuppressWarnings("serial") +public class LimitedTreeMap extends TreeMap> { + + private final int limit; + + public LimitedTreeMap() { + this(1024); + } + + public LimitedTreeMap(int limit) { + this.limit = limit; + } + + @Override + public SortedSet put(K key, SortedSet value) { + if (size() < limit) { + return super.put(key, value); + } + return null; + } +} diff --git a/net-http/src/main/java/org/xbib/net/http/util/package-info.java b/net-http/src/main/java/org/xbib/net/http/util/package-info.java new file mode 100644 index 0000000..8f6ecd2 --- /dev/null +++ b/net-http/src/main/java/org/xbib/net/http/util/package-info.java @@ -0,0 +1,4 @@ +/** + * Utilities for Net HTTP classes. + */ +package org.xbib.net.http.util; diff --git a/net-url/src/main/java/org/xbib/net/Pair.java b/net-url/src/main/java/org/xbib/net/Pair.java new file mode 100644 index 0000000..27b293d --- /dev/null +++ b/net-url/src/main/java/org/xbib/net/Pair.java @@ -0,0 +1,31 @@ +package org.xbib.net; + +/** + * A pair of parameters. + * @param the key type parameter + * @param the value type parameter + */ +public class Pair { + + private final K first; + + private final V second; + + Pair(K first, V second) { + this.first = first; + this.second = second; + } + + public K getFirst() { + return first; + } + + public V getSecond() { + return second; + } + + @Override + public String toString() { + return first + ":" + second; + } +} diff --git a/net-url/src/main/java/org/xbib/net/PercentEncoders.java b/net-url/src/main/java/org/xbib/net/PercentEncoders.java index f3613a8..2e53b6f 100644 --- a/net-url/src/main/java/org/xbib/net/PercentEncoders.java +++ b/net-url/src/main/java/org/xbib/net/PercentEncoders.java @@ -2,6 +2,7 @@ package org.xbib.net; import java.nio.charset.Charset; import java.util.BitSet; + import static java.nio.charset.CodingErrorAction.REPORT; /** diff --git a/net-url/src/main/java/org/xbib/net/QueryParameters.java b/net-url/src/main/java/org/xbib/net/QueryParameters.java index 128642f..ef06ec0 100644 --- a/net-url/src/main/java/org/xbib/net/QueryParameters.java +++ b/net-url/src/main/java/org/xbib/net/QueryParameters.java @@ -10,13 +10,13 @@ import java.util.stream.Collectors; * Query parameter list, of limited size. Default is 1024 pairs. */ @SuppressWarnings("serial") -public class QueryParameters extends ArrayList> { +public class QueryParameters extends ArrayList> { private static final char AMPERSAND_CHAR = '&'; private static final char EQUAL_CHAR = '='; - private final PercentDecoder percentDecoder; + private transient final PercentDecoder percentDecoder; private final int max; @@ -42,10 +42,15 @@ public class QueryParameters extends ArrayList element) { + public boolean add(Pair element) { return size() < max && super.add(element); } + @Override + public boolean equals(Object o) { + return o instanceof QueryParameters && super.equals(o); + } + public QueryParameters addPercentEncodedBody(String body) throws MalformedInputException, UnmappableCharacterException { String s = body; while (s != null) { @@ -73,32 +78,4 @@ public class QueryParameters extends ArrayList= 0 ? input.substring(i + 1) : null; return new Pair<>(k, v); } - - /** - * A pair of query parameters. - * @param the key type parameter - * @param the value type parameter - */ - public static class Pair { - private final K first; - private final V second; - - Pair(K first, V second) { - this.first = first; - this.second = second; - } - - public K getFirst() { - return first; - } - - public V getSecond() { - return second; - } - - @Override - public String toString() { - return first + ":" + second; - } - } } diff --git a/net-url/src/main/java/org/xbib/net/URL.java b/net-url/src/main/java/org/xbib/net/URL.java index 0ae19d9..c9007a8 100755 --- a/net-url/src/main/java/org/xbib/net/URL.java +++ b/net-url/src/main/java/org/xbib/net/URL.java @@ -3,6 +3,7 @@ package org.xbib.net; import org.xbib.net.scheme.Scheme; import org.xbib.net.scheme.SchemeRegistry; +import java.io.Serializable; import java.net.IDN; import java.net.Inet4Address; import java.net.Inet6Address; @@ -361,7 +362,7 @@ public class URL implements Comparable { return null; } Pair p = indexOf(COLON_CHAR, builder.userInfo); - return decode(p.first); + return decode(p.getFirst()); } public String getPassword() { @@ -369,7 +370,7 @@ public class URL implements Comparable { return null; } Pair p = indexOf(COLON_CHAR, builder.userInfo); - return decode(p.second); + return decode(p.getSecond()); } /** @@ -668,9 +669,9 @@ public class URL implements Comparable { if (withQuestionMark) { sb.append(QUESTION_CHAR); } - Iterator> it = builder.queryParams.iterator(); + Iterator> it = builder.queryParams.iterator(); while (it.hasNext()) { - QueryParameters.Pair queryParam = it.next(); + Pair queryParam = it.next(); try { sb.append(withEncoding ? queryParamEncoder.encode(queryParam.getFirst()) : queryParam.getFirst()); if (queryParam.getSecond() != null) { @@ -916,11 +917,10 @@ public class URL implements Comparable { } public Builder pathSegment(String segment) { - if (pathSegments.isEmpty() && !isNullOrEmpty(host) && isNullOrEmpty(segment)) { + if (pathSegments.isEmpty() && !isNullOrEmpty(host) && !isNullOrEmpty(segment)) { pathSegments.add(EMPTY_SEGMENT); - } else { - pathSegments.add(new PathSegment(segment)); } + pathSegments.add(new PathSegment(segment)); return this; } @@ -1361,7 +1361,8 @@ public class URL implements Comparable { } } - private static class URLWithFragmentComparator implements Comparator { + @SuppressWarnings("serial") + private static class URLWithFragmentComparator implements Comparator, Serializable { @Override public int compare(URL o1, URL o2) { @@ -1369,7 +1370,8 @@ public class URL implements Comparable { } } - private static class URLWithoutFragmentComparator implements Comparator { + @SuppressWarnings("serial") + private static class URLWithoutFragmentComparator implements Comparator, Serializable { @Override public int compare(URL o1, URL o2) { @@ -1377,37 +1379,6 @@ public class URL implements Comparable { } } - /** - * A pair for matrix params. - * - * @param key - * @param value - */ - public static class Pair { - - private final K first; - - private final V second; - - Pair(K first, V second) { - this.first = first; - this.second = second; - } - - public K getFirst() { - return first; - } - - public V getSecond() { - return second; - } - - @Override - public String toString() { - return first + "=" + second; - } - } - /** * A path segment with associated matrix params, if any. */ diff --git a/net-url/src/main/java/org/xbib/net/URLSyntaxException.java b/net-url/src/main/java/org/xbib/net/URLSyntaxException.java index 40a13b0..2f0a91f 100644 --- a/net-url/src/main/java/org/xbib/net/URLSyntaxException.java +++ b/net-url/src/main/java/org/xbib/net/URLSyntaxException.java @@ -3,6 +3,7 @@ package org.xbib.net; /** * URL syntax exception. */ +@SuppressWarnings("serial") public class URLSyntaxException extends Exception { URLSyntaxException(String message) { diff --git a/net-url/src/main/java/org/xbib/net/path/PathPatternComparator.java b/net-url/src/main/java/org/xbib/net/path/PathPatternComparator.java index e7c8096..757d7a1 100644 --- a/net-url/src/main/java/org/xbib/net/path/PathPatternComparator.java +++ b/net-url/src/main/java/org/xbib/net/path/PathPatternComparator.java @@ -1,11 +1,13 @@ package org.xbib.net.path; +import java.io.Serializable; import java.util.Comparator; /** * Path pattern comparator. */ -public class PathPatternComparator implements Comparator { +@SuppressWarnings("serial") +public class PathPatternComparator implements Comparator, Serializable { private final String path; diff --git a/net-url/src/main/java/org/xbib/net/path/PathTrie.java b/net-url/src/main/java/org/xbib/net/path/PathTrie.java index e7823a9..8a25a59 100644 --- a/net-url/src/main/java/org/xbib/net/path/PathTrie.java +++ b/net-url/src/main/java/org/xbib/net/path/PathTrie.java @@ -20,6 +20,7 @@ import java.util.regex.Pattern; * path is resolved, a map from parameter names to raw String values is returned as part of the * result. Null values are not acceptable values in this trie. Parameter names can only contain * alphanumeric characters or underscores, and cannot start with a numeric. + * @param type */ public class PathTrie { @@ -100,6 +101,7 @@ public class PathTrie { /** * The resulting information for a successful path resolution, which includes the value to which * the path maps, as well as the raw (but URL decoded) string values of all path parameters. + * @param type */ public static class Result { @@ -151,12 +153,13 @@ public class PathTrie { /** * A builder for creating a {@link PathTrie}, which is immutable. + * @param type */ public static class Builder { private final Map> subBuilders = new LinkedHashMap<>(); - private final Map> httpMethodMap =new LinkedHashMap<>(); + private final Map> httpMethodMap = new LinkedHashMap<>(); private final boolean throwOnConflict; diff --git a/net-url/src/test/java/org/xbib/net/body/ParseBodyTest.java b/net-url/src/test/java/org/xbib/net/body/ParseBodyTest.java index 908b7ac..be3f1ea 100644 --- a/net-url/src/test/java/org/xbib/net/body/ParseBodyTest.java +++ b/net-url/src/test/java/org/xbib/net/body/ParseBodyTest.java @@ -27,7 +27,7 @@ class ParseBodyTest { QueryParameters queryParameters = new QueryParameters(100); List list = new ArrayList<>(); for (int i = 0; i < 200; i++) { - list.add("a" + i + "=b" + i ); + list.add("a" + i + "=b" + i); } String body = String.join("&", list); queryParameters.addPercentEncodedBody(body); diff --git a/net-url/src/test/java/org/xbib/net/body/package-info.java b/net-url/src/test/java/org/xbib/net/body/package-info.java new file mode 100644 index 0000000..732a06d --- /dev/null +++ b/net-url/src/test/java/org/xbib/net/body/package-info.java @@ -0,0 +1,5 @@ +/** + * Classes for parsing bodies. + */ +package org.xbib.net.body; + diff --git a/net-url/src/test/java/org/xbib/net/template/URITemplateTest.java b/net-url/src/test/java/org/xbib/net/template/URITemplateTest.java index 772c5d7..a1487de 100644 --- a/net-url/src/test/java/org/xbib/net/template/URITemplateTest.java +++ b/net-url/src/test/java/org/xbib/net/template/URITemplateTest.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; - import org.junit.jupiter.api.Test; import org.xbib.net.template.expression.ExpressionType; import org.xbib.net.template.expression.TemplateExpression; @@ -24,12 +23,6 @@ import org.xbib.net.template.vars.values.NullValue; import org.xbib.net.template.vars.values.ScalarValue; import org.xbib.net.template.vars.values.VariableValue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - import java.nio.CharBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -39,6 +32,12 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + class URITemplateTest { @Test