update to Java 11 dependecies, remove URIBuilder and URIFormatter

This commit is contained in:
Jörg Prante 2019-08-08 22:18:15 +02:00
parent 251ba3762d
commit ab2ceaa579
4 changed files with 8 additions and 382 deletions

View file

@ -20,24 +20,19 @@ subprojects {
mavenCentral()
}
configurations {
asciidoclet
}
dependencies {
testCompile "junit:junit:${project.property('junit.version')}"
testCompile "org.xbib:bibliographic-character-sets:${project.property('xbib-bibliographic-character-sets.version')}"
asciidoclet "org.asciidoctor:asciidoclet:${project.property('asciidoclet.version')}"
}
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
compileTestJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
@ -169,5 +164,4 @@ subprojects {
property "sonar.junit.reportsPath", "build/test-results/test/"
}
}
}

View file

@ -2,14 +2,14 @@ group = org.xbib
name = oai
version = 2.0.0
xbib-content.version = 1.3.1
xbib-netty-http.version = 4.1.38.2
xbib-content.version = 2.0.0
xbib-netty-http.version = 4.1.38.3
tcnative.version = 2.0.25.Final
# test
junit.version 4.12
junit.version = 4.12
xbib-bibliographic-character-sets.version = 1.0.0
xbib-marc.version = 1.1.0
xbib-marc.version = 2.0.0
# doc
asciidoclet.version = 1.5.4

View file

@ -1,230 +0,0 @@
package org.xbib.oai.util;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
/**
*
*/
public class URIBuilder {
private URI uri;
private String scheme;
private String authority;
private String path;
private String fragment;
private Map<String, String> params;
public URIBuilder() {
this.params = new LinkedHashMap<>();
}
public URIBuilder(String base) {
this(URI.create(base));
}
public URIBuilder(URI base) {
this.uri = base;
this.scheme = uri.getScheme();
this.authority = uri.getAuthority();
this.path = uri.getPath();
this.fragment = uri.getFragment();
this.params = parseQueryString(uri);
}
public URIBuilder(URI base, Charset encoding) {
this.uri = base;
this.params = parseQueryString(uri, encoding);
}
public URIBuilder scheme(String scheme) {
this.scheme = scheme;
return this;
}
public URIBuilder authority(String authority) {
this.authority = authority;
return this;
}
public URIBuilder path(String path) {
this.path = path;
return this;
}
public URIBuilder fragment(String fragment) {
this.fragment = fragment;
return this;
}
/**
* This method adds a single key/value parameter to the query
* string of a given URI. Existing keys will be overwritten.
*
* @param key the key
* @param value the value
* @return this URI builder
*/
public URIBuilder addParameter(String key, String value) {
params.put(key, value);
return this;
}
public String buildGetPath() {
return path + (params.isEmpty() ? "" : "?" + URIFormatter.renderQueryString(params));
}
/**
* This method adds a single key/value parameter to the query
* string of a given URI, URI-escaped with the given encoding.
* Existing keys will be overwritten.
*
* @param key the key
* @param value the value
* @param encoding the encoding
* @return this URI builder
*/
public URIBuilder addParameter(String key, String value, Charset encoding) {
params.put(key, URIFormatter.encode(value, encoding));
return this;
}
public URI build() {
try {
return new URI(scheme, authority, path, URIFormatter.renderQueryString(params), fragment);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
public URI build(Charset encoding) {
try {
return new URI(scheme, authority, path, URIFormatter.renderQueryString(params, encoding), fragment);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
/**
* This method parses a query string and returns a map of decoded
* request parameters. We do not rely on java.net.URI because it does not
* decode plus characters. The encoding is UTF-8.
*
* @param uri the URI to examine for request parameters
* @return a map
*/
public static Map<String, String> parseQueryString(URI uri) {
return parseQueryString(uri, StandardCharsets.UTF_8);
}
/**
* This method parses a query string and returns a map of decoded
* request parameters. We do not rely on java.net.URI because it does not
* decode plus characters.
*
* @param uri the URI to examine for request parameters
* @param encoding the encoding
* @return a Map
*/
public static Map<String, String> parseQueryString(URI uri, Charset encoding) {
return parseQueryString(uri, encoding, null);
}
/**
* This method parses a query string and returns a map of decoded
* request parameters. We do not rely on java.net.URI because it does not
* decode plus characters. A listener can process the parameters in order.
*
* @param uri the URI to examine for request parameters
* @param encoding the encoding
* @param listener a listner for processing the URI parameters in order, or null
* @return a Map of parameters
*/
public static Map<String, String> parseQueryString(URI uri, Charset encoding, ParameterListener listener) {
if (uri == null) {
throw new IllegalArgumentException();
}
return parseQueryString(uri.getRawQuery(), encoding, listener);
}
public static Map<String, String> parseQueryString(String rawQuery, Charset encoding, ParameterListener listener) {
Map<String, String> m = new HashMap<>();
if (rawQuery == null) {
return m;
}
// we use getRawQuery because we do our decoding by ourselves
StringTokenizer st = new StringTokenizer(rawQuery, "&");
while (st.hasMoreTokens()) {
String pair = st.nextToken();
String k;
String v;
int pos = pair.indexOf('=');
if (pos < 0) {
k = pair;
v = null;
} else {
k = pair.substring(0, pos);
v = decode(pair.substring(pos + 1, pair.length()), encoding);
}
m.put(k, v);
if (listener != null) {
listener.received(k, v);
}
}
return m;
}
/**
* Decodes an octet according to RFC 2396. According to this spec,
* any characters outside the range 0x20 - 0x7E must be escaped because
* they are not printable characters, except for any characters in the
* fragment identifier. This method will translate any escaped characters
* back to the original.
*
* @param octet the octet to decode
* @param encoding the encoding to decode into
* @return The decoded URI
*/
public static String decode(String octet, Charset encoding) {
StringBuilder sb = new StringBuilder();
boolean fragment = false;
for (int i = 0; i < octet.length(); i++) {
char ch = octet.charAt(i);
switch (ch) {
case '+':
sb.append(' ');
break;
case '#':
sb.append(ch);
fragment = true;
break;
case '%':
if (!fragment) {
// fast hex decode
sb.append((char) ((Character.digit(octet.charAt(++i), 16) << 4)
| Character.digit(octet.charAt(++i), 16)));
} else {
sb.append(ch);
}
break;
default:
sb.append(ch);
break;
}
}
return new String(sb.toString().getBytes(StandardCharsets.ISO_8859_1), encoding);
}
/**
*
*/
@FunctionalInterface
public interface ParameterListener {
void received(String k, String v);
}
}

View file

@ -1,138 +0,0 @@
package org.xbib.oai.util;
import java.nio.charset.Charset;
import java.util.Map;
/**
*
*/
public class URIFormatter {
public static String renderQueryString(Map<String, String> m) {
return renderQueryString(m, null, false);
}
public static String renderQueryString(Map<String, String> m, Charset encoding) {
return renderQueryString(m, encoding, true);
}
/**
* This method takes a Map of key/value elements and converts it
* into a URL encoded querystring format.
*
* @param m a map of key/value arrays
* @param encoding the charset
* @param encode true if arameter must be encoded
* @return a string with the URL encoded data
*/
public static String renderQueryString(Map<String, String> m, Charset encoding, boolean encode) {
String key;
String value;
StringBuilder out = new StringBuilder();
for (Map.Entry<String, String> me : m.entrySet()) {
key = me.getKey();
value = encode ? encode(me.getValue(), encoding) : me.getValue();
if (key != null) {
if (out.length() > 0) {
out.append("&");
}
out.append(key);
if ((value != null) && (value.length() > 0)) {
out.append("=").append(value);
}
}
}
return out.toString();
}
/**
* <p>Encode a string into URI syntax</p>
* <p>This function applies the URI escaping rules defined in
* section 2 of [RFC 2396], as amended by [RFC 2732], to the string
* supplied as the first argument, which typically represents all or part
* of a URI, URI reference or IRI. The effect of the function is to
* replace any special character in the string by an escape sequence of
* the form %xx%yy..., where xxyy... is the hexadecimal representation of
* the octets used to represent the character in US-ASCII for characters
* in the ASCII repertoire, and a different character encoding for
* non-ASCII characters.</p>
* <p>If the second argument is true, all characters are escaped
* other than lower case letters a-z, upper case letters A-Z, digits 0-9,
* and the characters referred to in [RFC 2396] as "marks": specifically,
* "-" | "_" | "." | "!" | "~" | "" | "'" | "(" | ")". The "%" character
* itself is escaped only if it is not followed by two hexadecimal digits
* (that is, 0-9, a-f, and A-F).</p>
* <p>[RFC 2396] does not define whether escaped URIs should use
* lower case or upper case for hexadecimal digits. To ensure that escaped
* URIs can be compared using string comparison functions, this function
* must always use the upper-case letters A-F.</p>
* <p>The character encoding used as the basis for determining the
* octets depends on the setting of the second argument.</p>
*
* @param s the String to convert
* @param encoding The encoding to use for unsafe characters
* @return The converted String
*/
public static String encode(String s, Charset encoding) {
if (s == null) {
return null;
}
int length = s.length();
int start = 0;
int i = 0;
StringBuilder result = new StringBuilder(length);
while (true) {
while ((i < length) && isSafe(s.charAt(i))) {
i++;
}
// Safe character can just be added
result.append(s.substring(start, i));
// Are we done?
if (i >= length) {
return result.toString();
} else if (s.charAt(i) == ' ') {
result.append('+'); // Replace space char with plus symbol.
i++;
} else {
// Get all unsafe characters
start = i;
char c;
while ((i < length) && ((c = s.charAt(i)) != ' ') && !isSafe(c)) {
i++;
}
// Convert them to %XY encoded strings
String unsafe = s.substring(start, i);
byte[] bytes = unsafe.getBytes(encoding);
for (byte aByte : bytes) {
result.append('%');
result.append(hex.charAt(((int) aByte & 0xf0) >> 4));
result.append(hex.charAt((int) aByte & 0x0f));
}
}
start = i;
}
}
/**
* Returns true if the given char is
* either a uppercase or lowercase letter from 'a' till 'z', or a digit
* froim '0' till '9', or one of the characters '-', '_', '.' or ''. Such
* 'safe' character don't have to be url encoded.
*
* @param c the character
* @return true or false
*/
private static boolean isSafe(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9')) || (c == '-') || (c == '_') || (c == '.') || (c == '*');
}
/**
* Used to convert to hex. We don't use Integer.toHexString, since
* it converts to lower case (and the Sun docs pretty clearly specify
* upper case here), and because it doesn't provide a leading 0.
*/
private static final String hex = "0123456789ABCDEF";
}