fixes matrix params, checkstyle, spotbugs

This commit is contained in:
Jörg Prante 2019-07-14 21:38:57 +02:00
parent bc84cfce4a
commit 53e9469d18
21 changed files with 161 additions and 196 deletions

View file

@ -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
}

View file

@ -53,12 +53,10 @@ page at http://checkstyle.sourceforge.net/config.html -->
</module>
<module name="ImportOrder">
<!-- Checks for out of order import statements. -->
<property name="separated" value="true"/>
<property name="severity" value="warning"/>
<property name="groups" value="com,io,junit,net,org,javax,java"/>
<!-- This ensures that static imports go last. -->
<property name="option" value="under"/>
<property name="groups" value="*,javax,java"/>
<property name="option" value="bottom"/>
<property name="tokens" value="IMPORT, STATIC_IMPORT"/>
</module>

View file

@ -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<String, SortedSet<String>> {
private final LimitedStringMap wrappedMap;
private final LimitedTreeMap<String, String> 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, SortedSet<String>> {
String k = percentEncode ? percentEncoder.encode(key) : key;
SortedSet<String> 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<String, SortedSet<String>> {
for (String key : m.keySet()) {
SortedSet<String> vals = get(key);
if (vals == null) {
vals = new LimitedSortedStringSet();
vals = new LimitedSet<>();
put(key, vals);
}
vals.addAll(m.get(key));

View file

@ -3,6 +3,9 @@ package org.xbib.net.http;
import java.io.IOException;
import java.io.InputStream;
/**
*
*/
public interface HttpResponse {
int getStatusCode() throws IOException;

View file

@ -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<String, String> getAllHeaders() {
return Collections.emptyMap();
}
public InputStream getMessagePayload() {
return null;
}
public String getContentType() {
return null;
}
public Object unwrap() {
return url;
}
}

View file

@ -0,0 +1,4 @@
/**
* Net classes for HTTP.
*/
package org.xbib.net.http;

View file

@ -0,0 +1,34 @@
package org.xbib.net.http.util;
import java.util.Objects;
import java.util.TreeSet;
/**
* Limited set.
* @param <T> type
*/
@SuppressWarnings("serial")
public class LimitedSet<T extends CharSequence> extends TreeSet<T> {
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");
}
}

View file

@ -1,28 +0,0 @@
package org.xbib.net.http.util;
import java.util.SortedSet;
import java.util.TreeSet;
public class LimitedSortedStringSet extends TreeSet<String> implements SortedSet<String> {
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;
}
}

View file

@ -1,25 +0,0 @@
package org.xbib.net.http.util;
import java.util.SortedSet;
import java.util.TreeMap;
public class LimitedStringMap extends TreeMap<String, SortedSet<String>> {
private final int limit;
public LimitedStringMap() {
this(1024);
}
public LimitedStringMap(int limit) {
this.limit = limit;
}
@Override
public SortedSet<String> put(String key, SortedSet<String> value) {
if (size() < limit) {
return super.put(key, value);
}
return null;
}
}

View file

@ -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 <K> key
* @param <V> value
*/
@SuppressWarnings("serial")
public class LimitedTreeMap<K, V> extends TreeMap<K, SortedSet<V>> {
private final int limit;
public LimitedTreeMap() {
this(1024);
}
public LimitedTreeMap(int limit) {
this.limit = limit;
}
@Override
public SortedSet<V> put(K key, SortedSet<V> value) {
if (size() < limit) {
return super.put(key, value);
}
return null;
}
}

View file

@ -0,0 +1,4 @@
/**
* Utilities for Net HTTP classes.
*/
package org.xbib.net.http.util;

View file

@ -0,0 +1,31 @@
package org.xbib.net;
/**
* A pair of parameters.
* @param <K> the key type parameter
* @param <V> the value type parameter
*/
public class Pair<K, V> {
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;
}
}

View file

@ -2,6 +2,7 @@ package org.xbib.net;
import java.nio.charset.Charset;
import java.util.BitSet;
import static java.nio.charset.CodingErrorAction.REPORT;
/**

View file

@ -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<QueryParameters.Pair<String, String>> {
public class QueryParameters extends ArrayList<Pair<String, String>> {
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<QueryParameters.Pair<String, Stri
}
@Override
public boolean add(QueryParameters.Pair<String, String> element) {
public boolean add(Pair<String, String> 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<QueryParameters.Pair<String, Stri
String v = i >= 0 ? input.substring(i + 1) : null;
return new Pair<>(k, v);
}
/**
* A pair of query parameters.
* @param <K> the key type parameter
* @param <V> the value type parameter
*/
public static class Pair<K, V> {
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;
}
}
}

View file

@ -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<URL> {
return null;
}
Pair<String, String> 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<URL> {
return null;
}
Pair<String, String> p = indexOf(COLON_CHAR, builder.userInfo);
return decode(p.second);
return decode(p.getSecond());
}
/**
@ -668,9 +669,9 @@ public class URL implements Comparable<URL> {
if (withQuestionMark) {
sb.append(QUESTION_CHAR);
}
Iterator<QueryParameters.Pair<String, String>> it = builder.queryParams.iterator();
Iterator<Pair<String, String>> it = builder.queryParams.iterator();
while (it.hasNext()) {
QueryParameters.Pair<String, String> queryParam = it.next();
Pair<String, String> 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<URL> {
}
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<URL> {
}
}
private static class URLWithFragmentComparator implements Comparator<URL> {
@SuppressWarnings("serial")
private static class URLWithFragmentComparator implements Comparator<URL>, Serializable {
@Override
public int compare(URL o1, URL o2) {
@ -1369,7 +1370,8 @@ public class URL implements Comparable<URL> {
}
}
private static class URLWithoutFragmentComparator implements Comparator<URL> {
@SuppressWarnings("serial")
private static class URLWithoutFragmentComparator implements Comparator<URL>, Serializable {
@Override
public int compare(URL o1, URL o2) {
@ -1377,37 +1379,6 @@ public class URL implements Comparable<URL> {
}
}
/**
* A pair for matrix params.
*
* @param <K> key
* @param <V> value
*/
public static class Pair<K, V> {
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.
*/

View file

@ -3,6 +3,7 @@ package org.xbib.net;
/**
* URL syntax exception.
*/
@SuppressWarnings("serial")
public class URLSyntaxException extends Exception {
URLSyntaxException(String message) {

View file

@ -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<String> {
@SuppressWarnings("serial")
public class PathPatternComparator implements Comparator<String>, Serializable {
private final String path;

View file

@ -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 <T> type
*/
public class PathTrie<T> {
@ -100,6 +101,7 @@ public class PathTrie<T> {
/**
* 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 <T> type
*/
public static class Result<T> {
@ -151,6 +153,7 @@ public class PathTrie<T> {
/**
* A builder for creating a {@link PathTrie}, which is immutable.
* @param <T> type
*/
public static class Builder<T> {

View file

@ -0,0 +1,5 @@
/**
* Classes for parsing bodies.
*/
package org.xbib.net.body;

View file

@ -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