This is an automated email from the ASF dual-hosted git repository.

jbertram pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new 383345a4f6 ARTEMIS-4239 use StandardCharsets.UTF_8
383345a4f6 is described below

commit 383345a4f602d79c415f72b8ad66b8218cbac467
Author: Alexey Markevich <[email protected]>
AuthorDate: Sat Apr 8 16:24:25 2023 +0200

    ARTEMIS-4239 use StandardCharsets.UTF_8
---
 .../artemis/api/core/ParameterisedAddress.java     | 11 +---
 .../activemq/artemis/utils/uri/BeanSupport.java    | 10 +--
 .../activemq/artemis/utils/uri/URISchema.java      | 42 ++++++-------
 .../activemq/artemis/utils/uri/URISupport.java     | 71 +++++++++-------------
 .../artemis/protocol/amqp/util/NettyReadable.java  |  6 +-
 .../core/config/impl/ConfigurationImplTest.java    |  3 +-
 .../mqtt/example/SimpleMQTTInterceptor.java        |  4 +-
 .../transport/amqp/client/util/PropertyUtil.java   | 47 ++++++--------
 8 files changed, 81 insertions(+), 113 deletions(-)

diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
index 2b5c8a5d00..4d25e70db3 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
@@ -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;
       }
    }
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java
index 9b6dbbd966..1d7623c28b 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java
@@ -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);
    }
 
 }
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISchema.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISchema.java
index 0a9fd97d05..d20a5c98ec 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISchema.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISchema.java
@@ -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());
-            }
+      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);
       }
+      return rc;
    }
 
    protected String printQuery(Map<String, String> query) {
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
index b6b4707c84..53ed8ab4ff 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
@@ -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 "";
       }
    }
 
diff --git 
a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/util/NettyReadable.java
 
b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/util/NettyReadable.java
index 6b14575f07..351b288c0b 100644
--- 
a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/util/NettyReadable.java
+++ 
b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/util/NettyReadable.java
@@ -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
diff --git 
a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
index 018ac4183d..b55b4baeda 100644
--- 
a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
+++ 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
@@ -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);
             }
diff --git 
a/examples/features/standard/interceptor-mqtt/src/main/java/org/apache/activemq/artemis/mqtt/example/SimpleMQTTInterceptor.java
 
b/examples/features/standard/interceptor-mqtt/src/main/java/org/apache/activemq/artemis/mqtt/example/SimpleMQTTInterceptor.java
index 5297e61d8d..ae9dacfa0b 100644
--- 
a/examples/features/standard/interceptor-mqtt/src/main/java/org/apache/activemq/artemis/mqtt/example/SimpleMQTTInterceptor.java
+++ 
b/examples/features/standard/interceptor-mqtt/src/main/java/org/apache/activemq/artemis/mqtt/example/SimpleMQTTInterceptor.java
@@ -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.
diff --git 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/util/PropertyUtil.java
 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/util/PropertyUtil.java
index 1e18904d44..a417ed0696 100644
--- 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/util/PropertyUtil.java
+++ 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/util/PropertyUtil.java
@@ -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);

Reply via email to