ARTEMIS-4239 use StandardCharsets.UTF_8

This commit is contained in:
Alexey Markevich 2023-04-08 16:24:25 +02:00 committed by Justin Bertram
parent bc0b75d25a
commit 383345a4f6
8 changed files with 83 additions and 115 deletions

View File

@ -19,14 +19,13 @@ package org.apache.activemq.artemis.api.core;
import static org.apache.activemq.artemis.utils.uri.URISupport.appendParameters;
import static org.apache.activemq.artemis.utils.uri.URISupport.parseQuery;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.activemq.artemis.utils.uri.URISupport;
public class ParameterisedAddress {
public static SimpleString toParameterisedAddress(SimpleString address, Map<String, String> parameters) throws URISyntaxException {
public static SimpleString toParameterisedAddress(SimpleString address, Map<String, String> parameters) {
if (parameters != null && !parameters.isEmpty()) {
return SimpleString.toSimpleString(toParameterisedAddress(address.toString(), parameters));
} else {
@ -34,7 +33,7 @@ public class ParameterisedAddress {
}
}
public static String toParameterisedAddress(String address, Map<String, String> parameters) throws URISyntaxException {
public static String toParameterisedAddress(String address, Map<String, String> parameters) {
if (parameters != null && !parameters.isEmpty()) {
return appendParameters(new StringBuilder(address), parameters).toString();
} else {
@ -90,11 +89,7 @@ public class ParameterisedAddress {
} else {
this.address = SimpleString.toSimpleString(address.substring(0, index));
QueueConfiguration queueConfiguration = new QueueConfiguration(address);
try {
parseQuery(address).forEach(queueConfiguration::set);
} catch (URISyntaxException use) {
throw new IllegalArgumentException("Malformed parameters in address " + address);
}
parseQuery(address).forEach(queueConfiguration::set);
this.queueConfiguration = queueConfiguration;
}
}

View File

@ -17,11 +17,11 @@
package org.apache.activemq.artemis.utils.uri;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -204,12 +204,12 @@ public class BeanSupport {
(type == String.class);
}
public static String decodeURI(String value) throws UnsupportedEncodingException {
return URLDecoder.decode(value, "UTF-8");
public static String decodeURI(String value) {
return URLDecoder.decode(value, StandardCharsets.UTF_8);
}
public static String encodeURI(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, "UTF-8");
public static String encodeURI(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}
}

View File

@ -16,9 +16,7 @@
*/
package org.apache.activemq.artemis.utils.uri;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
@ -104,34 +102,30 @@ public abstract class URISchema<T, P> {
}
public static Map<String, String> parseQuery(String uri,
Map<String, String> propertyOverrides) throws URISyntaxException {
try {
Map<String, String> rc = new HashMap<>();
if (uri != null && !uri.isEmpty()) {
String[] parameters = uri.split("&");
for (String parameter : parameters) {
int p = parameter.indexOf("=");
if (p >= 0) {
String name = BeanSupport.decodeURI(parameter.substring(0, p));
String value = BeanSupport.decodeURI(parameter.substring(p + 1));
rc.put(name, value);
} else {
if (!parameter.trim().isEmpty()) {
rc.put(parameter, null);
}
Map<String, String> propertyOverrides) {
Map<String, String> rc = new HashMap<>();
if (uri != null && !uri.isEmpty()) {
String[] parameters = uri.split("&");
for (String parameter : parameters) {
int p = parameter.indexOf("=");
if (p >= 0) {
String name = BeanSupport.decodeURI(parameter.substring(0, p));
String value = BeanSupport.decodeURI(parameter.substring(p + 1));
rc.put(name, value);
} else {
if (!parameter.trim().isEmpty()) {
rc.put(parameter, null);
}
}
}
if (propertyOverrides != null) {
for (Map.Entry<String, String> entry : propertyOverrides.entrySet()) {
rc.put(entry.getKey(), entry.getValue());
}
}
return rc;
} catch (UnsupportedEncodingException e) {
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
}
if (propertyOverrides != null) {
for (Map.Entry<String, String> entry : propertyOverrides.entrySet()) {
rc.put(entry.getKey(), entry.getValue());
}
}
return rc;
}
protected String printQuery(Map<String, String> query) {

View File

@ -16,11 +16,11 @@
*/
package org.apache.activemq.artemis.utils.uri;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -109,7 +109,7 @@ public class URISupport {
}
}
public static StringBuilder appendParameters(StringBuilder sb, Map<String, String> parameters) throws URISyntaxException {
public static StringBuilder appendParameters(StringBuilder sb, Map<String, String> parameters) {
if (!parameters.isEmpty()) {
sb.append('?');
sb.append(createQueryString(parameters));
@ -122,19 +122,14 @@ public class URISupport {
*
* @param uri The URI whose query should be extracted and processed.
* @return A Mapping of the URI options.
* @throws java.net.URISyntaxException
*/
public static Map<String, String> parseQuery(String uri) throws URISyntaxException {
try {
uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the relevant part of the query
Map<String, String> rc = new HashMap<>();
if (uri != null && !uri.isEmpty()) {
parseParameters(rc, uri.split("[&;]"));
}
return rc;
} catch (UnsupportedEncodingException e) {
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
public static Map<String, String> parseQuery(String uri) {
uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the relevant part of the query
Map<String, String> rc = new HashMap<>();
if (uri != null && !uri.isEmpty()) {
parseParameters(rc, uri.split("[&;]"));
}
return rc;
}
public static boolean containsQuery(String uri) {
@ -145,13 +140,12 @@ public class URISupport {
return uri.contains('?');
}
private static void parseParameters(Map<String, String> rc,
String[] parameters) throws UnsupportedEncodingException {
private static void parseParameters(Map<String, String> rc, String[] parameters) {
for (String parameter : parameters) {
int p = parameter.indexOf("=");
if (p >= 0) {
String name = URLDecoder.decode(parameter.substring(0, p), "UTF-8");
String value = URLDecoder.decode(parameter.substring(p + 1), "UTF-8");
String name = URLDecoder.decode(parameter.substring(0, p), StandardCharsets.UTF_8);
String value = URLDecoder.decode(parameter.substring(p + 1), StandardCharsets.UTF_8);
rc.put(name, value);
} else {
rc.put(parameter, null);
@ -473,33 +467,28 @@ public class URISupport {
*
* @param options The Mapping that will create the new Query string.
* @return a URI formatted query string.
* @throws java.net.URISyntaxException
*/
public static String createQueryString(Map<String, ? extends Object> options) throws URISyntaxException {
try {
if (options.size() > 0) {
StringBuilder rc = new StringBuilder();
boolean first = true;
List<String> keys = new ArrayList<>();
keys.addAll(options.keySet());
Collections.sort(keys);
for (String key : keys) {
if (first) {
first = false;
} else {
rc.append("&");
}
String value = (String) options.get(key);
rc.append(URLEncoder.encode(key, "UTF-8"));
rc.append("=");
rc.append(URLEncoder.encode(value, "UTF-8"));
public static String createQueryString(Map<String, ? extends Object> options) {
if (options.size() > 0) {
StringBuilder rc = new StringBuilder();
boolean first = true;
List<String> keys = new ArrayList<>();
keys.addAll(options.keySet());
Collections.sort(keys);
for (String key : keys) {
if (first) {
first = false;
} else {
rc.append("&");
}
return rc.toString();
} else {
return "";
String value = (String) options.get(key);
rc.append(URLEncoder.encode(key, StandardCharsets.UTF_8));
rc.append("=");
rc.append(URLEncoder.encode(value, StandardCharsets.UTF_8));
}
} catch (UnsupportedEncodingException e) {
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
return rc.toString();
} else {
return "";
}
}

View File

@ -18,8 +18,8 @@ package org.apache.activemq.artemis.protocol.amqp.util;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import org.apache.qpid.proton.codec.ReadableBuffer;
import org.apache.qpid.proton.codec.WritableBuffer;
@ -34,8 +34,6 @@ import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
*/
public class NettyReadable implements ReadableBuffer {
private static final Charset Charset_UTF8 = Charset.forName("UTF-8");
private final ByteBuf buffer;
public NettyReadable(ByteBuf buffer) {
@ -145,7 +143,7 @@ public class NettyReadable implements ReadableBuffer {
@Override
public String readUTF8() {
return buffer.readCharSequence(buffer.readableBytes(), Charset_UTF8).toString();
return buffer.readCharSequence(buffer.readableBytes(), StandardCharsets.UTF_8).toString();
}
@Override

View File

@ -21,7 +21,6 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
@ -656,7 +655,7 @@ public class ConfigurationImplTest extends ActiveMQTestBase {
} else if (type.equals(String.class)) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
String generatedString = new String(array, StandardCharsets.UTF_8);
properties.put(prop, generatedString);
}

View File

@ -16,7 +16,7 @@
*/
package org.apache.activemq.artemis.mqtt.example;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import io.netty.handler.codec.mqtt.MqttConnectMessage;
import io.netty.handler.codec.mqtt.MqttMessage;
@ -39,7 +39,7 @@ public class SimpleMQTTInterceptor implements MQTTInterceptor {
MqttPublishMessage message = (MqttPublishMessage) mqttMessage;
String originalMessage = message.payload().toString(Charset.forName("UTF-8"));
String originalMessage = message.payload().toString(StandardCharsets.UTF_8);
System.out.println("Original message: " + originalMessage);
// The new message content must not be bigger that the original content.

View File

@ -20,13 +20,13 @@ import javax.net.ssl.SSLContext;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@ -99,29 +99,24 @@ public class PropertyUtil {
*
* @param options The Mapping that will create the new Query string.
* @return a URI formatted query string.
* @throws URISyntaxException if the given URI is invalid.
*/
public static String createQueryString(Map<String, ?> options) throws URISyntaxException {
try {
if (options.size() > 0) {
StringBuffer rc = new StringBuffer();
boolean first = true;
for (Entry<String, ?> entry : options.entrySet()) {
if (first) {
first = false;
} else {
rc.append("&");
}
rc.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
rc.append("=");
rc.append(URLEncoder.encode((String) entry.getValue(), "UTF-8"));
public static String createQueryString(Map<String, ?> options) {
if (options.size() > 0) {
StringBuffer rc = new StringBuffer();
boolean first = true;
for (Entry<String, ?> entry : options.entrySet()) {
if (first) {
first = false;
} else {
rc.append("&");
}
return rc.toString();
} else {
return "";
rc.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
rc.append("=");
rc.append(URLEncoder.encode((String) entry.getValue(), StandardCharsets.UTF_8));
}
} catch (UnsupportedEncodingException e) {
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
return rc.toString();
} else {
return "";
}
}
@ -163,17 +158,15 @@ public class PropertyUtil {
*
* @param queryString the string value returned from a call to the URI class getQuery method.
* @return <Code>Map</Code> of properties from the parsed string.
* @throws Exception if an error occurs while parsing the query options.
*/
public static Map<String, String> parseQuery(String queryString) throws Exception {
public static Map<String, String> parseQuery(String queryString) {
if (queryString != null && !queryString.isEmpty()) {
Map<String, String> rc = new HashMap<>();
String[] parameters = queryString.split("&");
for (String parameter : parameters) {
for (String parameter : queryString.split("&")) {
int p = parameter.indexOf("=");
if (p >= 0) {
String name = URLDecoder.decode(parameter.substring(0, p), "UTF-8");
String value = URLDecoder.decode(parameter.substring(p + 1), "UTF-8");
String name = URLDecoder.decode(parameter.substring(0, p), StandardCharsets.UTF_8);
String value = URLDecoder.decode(parameter.substring(p + 1), StandardCharsets.UTF_8);
rc.put(name, value);
} else {
rc.put(parameter, null);