From f508a25566c915e511eb3523248dc9427d3f10f4 Mon Sep 17 00:00:00 2001 From: Jiri Danek Date: Fri, 26 Apr 2019 23:02:49 +0200 Subject: [PATCH] ARTEMIS-2320 Standard Charset object can be used --- .../core/protocol/mqtt/MQTTPublishManager.java | 16 ++++++---------- .../amqp/client/AmqpTransferTagGenerator.java | 9 ++------- .../amqp/client/sasl/CramMD5Mechanism.java | 9 +++------ .../tests/integration/stomp/StompTest.java | 2 +- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/artemis-protocols/artemis-mqtt-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/mqtt/MQTTPublishManager.java b/artemis-protocols/artemis-mqtt-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/mqtt/MQTTPublishManager.java index 667f5be46a..ffd4eeb733 100644 --- a/artemis-protocols/artemis-mqtt-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/mqtt/MQTTPublishManager.java +++ b/artemis-protocols/artemis-mqtt-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/mqtt/MQTTPublishManager.java @@ -17,7 +17,7 @@ package org.apache.activemq.artemis.core.protocol.mqtt; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; @@ -268,15 +268,11 @@ public class MQTTPublishManager { ByteBuf payload; switch (message.getType()) { case Message.TEXT_TYPE: - try { - SimpleString text = message.getDataBuffer().readNullableSimpleString(); - byte[] stringPayload = text.toString().getBytes("UTF-8"); - payload = ByteBufAllocator.DEFAULT.buffer(stringPayload.length); - payload.writeBytes(stringPayload); - break; - } catch (UnsupportedEncodingException e) { - log.warn("Unable to send message: " + message.getMessageID() + " Cause: " + e.getMessage(), e); - } + SimpleString text = message.getDataBuffer().readNullableSimpleString(); + byte[] stringPayload = text.toString().getBytes(StandardCharsets.UTF_8); + payload = ByteBufAllocator.DEFAULT.buffer(stringPayload.length); + payload.writeBytes(stringPayload); + break; default: ActiveMQBuffer bodyBuffer = message.getDataBuffer(); payload = ByteBufAllocator.DEFAULT.buffer(bodyBuffer.writerIndex()); diff --git a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpTransferTagGenerator.java b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpTransferTagGenerator.java index 48b9fe51e0..c05b43de2d 100644 --- a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpTransferTagGenerator.java +++ b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/AmqpTransferTagGenerator.java @@ -16,7 +16,7 @@ */ package org.apache.activemq.transport.amqp.client; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; @@ -58,12 +58,7 @@ public final class AmqpTransferTagGenerator { rc = iterator.next(); iterator.remove(); } else { - try { - rc = Long.toHexString(nextTagId++).getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - // This should never happen since we control the input. - throw new RuntimeException(e); - } + rc = Long.toHexString(nextTagId++).getBytes(StandardCharsets.UTF_8); } return rc; } diff --git a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/sasl/CramMD5Mechanism.java b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/sasl/CramMD5Mechanism.java index 8573306d86..51f01c2b5d 100644 --- a/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/sasl/CramMD5Mechanism.java +++ b/tests/artemis-test-support/src/main/java/org/apache/activemq/transport/amqp/client/sasl/CramMD5Mechanism.java @@ -19,7 +19,7 @@ package org.apache.activemq.transport.amqp.client.sasl; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.security.sasl.SaslException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; @@ -30,7 +30,6 @@ import java.security.NoSuchAlgorithmException; */ public class CramMD5Mechanism extends AbstractMechanism { - private static final String ASCII = "ASCII"; private static final String HMACMD5 = "HMACMD5"; private boolean sentResponse; @@ -53,7 +52,7 @@ public class CramMD5Mechanism extends AbstractMechanism { public byte[] getChallengeResponse(byte[] challenge) throws SaslException { if (!sentResponse && challenge != null && challenge.length != 0) { try { - SecretKeySpec key = new SecretKeySpec(getPassword().getBytes(ASCII), HMACMD5); + SecretKeySpec key = new SecretKeySpec(getPassword().getBytes(StandardCharsets.US_ASCII), HMACMD5); Mac mac = Mac.getInstance(HMACMD5); mac.init(key); @@ -70,9 +69,7 @@ public class CramMD5Mechanism extends AbstractMechanism { } sentResponse = true; - return hash.toString().getBytes(ASCII); - } catch (UnsupportedEncodingException e) { - throw new SaslException("Unable to utilise required encoding", e); + return hash.toString().getBytes(StandardCharsets.US_ASCII); } catch (InvalidKeyException e) { throw new SaslException("Unable to utilise key", e); } catch (NoSuchAlgorithmException e) { diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompTest.java index ba0616f066..3bc332bb08 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompTest.java @@ -261,7 +261,7 @@ public class StompTest extends StompTestBase { byte[] mqttPayload = clientProvider.receive(10000); clientProvider.disconnect(); - assertEquals(stompPayload, new String(mqttPayload, "UTF-8")); + assertEquals(stompPayload, new String(mqttPayload, StandardCharsets.UTF_8)); clientProvider.disconnect(); }