diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java index 5b12f288b6..bdbe2bde0d 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java @@ -22,16 +22,17 @@ import java.io.ObjectInputStream; import java.io.ObjectStreamClass; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.beans.DirectFieldAccessor; import org.springframework.core.ConfigurableObjectInputStream; -import org.springframework.core.NestedIOException; import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.core.serializer.DefaultSerializer; import org.springframework.core.serializer.Deserializer; import org.springframework.core.serializer.Serializer; +import org.springframework.lang.Nullable; /** * Implementation of {@link MessageConverter} that can work with Strings or native objects @@ -47,10 +48,11 @@ * * @author Dave Syer * @author Gary Russell + * @author Artem Bilan */ public class SerializerMessageConverter extends AllowedListDeserializingMessageConverter { - public static final String DEFAULT_CHARSET = "UTF-8"; + public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name(); private volatile String defaultCharset = DEFAULT_CHARSET; @@ -80,7 +82,7 @@ public void setIgnoreContentType(boolean ignoreContentType) { * * @param defaultCharset The default charset. */ - public void setDefaultCharset(String defaultCharset) { + public void setDefaultCharset(@Nullable String defaultCharset) { this.defaultCharset = (defaultCharset != null) ? defaultCharset : DEFAULT_CHARSET; } @@ -182,7 +184,7 @@ protected Class resolveClass(ObjectStreamClass classDesc) return objectInputStream.readObject(); } catch (ClassNotFoundException ex) { - throw new NestedIOException("Failed to deserialize object type", ex); + throw new IOException("Failed to deserialize object type", ex); } } diff --git a/spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java b/spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java index ccdb9ce6b3..d7e69f455e 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java @@ -26,7 +26,6 @@ import java.util.Set; import org.springframework.core.ConfigurableObjectInputStream; -import org.springframework.core.NestedIOException; import org.springframework.util.ObjectUtils; import org.springframework.util.PatternMatchUtils; @@ -35,6 +34,7 @@ * * @author Dave Syer * @author Gary Russell + * @author Artem Bilan */ public final class SerializationUtils { @@ -111,22 +111,22 @@ public static Object deserialize(InputStream inputStream, Set allowedLis throws IOException { try ( - ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, classLoader) { + ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, classLoader) { - @Override - protected Class resolveClass(ObjectStreamClass classDesc) - throws IOException, ClassNotFoundException { - Class clazz = super.resolveClass(classDesc); - checkAllowedList(clazz, allowedListPatterns); - return clazz; - } + @Override + protected Class resolveClass(ObjectStreamClass classDesc) + throws IOException, ClassNotFoundException { + Class clazz = super.resolveClass(classDesc); + checkAllowedList(clazz, allowedListPatterns); + return clazz; + } - }) { + }) { return objectInputStream.readObject(); } catch (ClassNotFoundException ex) { - throw new NestedIOException("Failed to deserialize object type", ex); + throw new IOException("Failed to deserialize object type", ex); } } diff --git a/spring-amqp/src/test/java/org/springframework/amqp/support/converter/SerializerMessageConverterTests.java b/spring-amqp/src/test/java/org/springframework/amqp/support/converter/SerializerMessageConverterTests.java index 74eb71cf96..744adb5748 100644 --- a/spring-amqp/src/test/java/org/springframework/amqp/support/converter/SerializerMessageConverterTests.java +++ b/spring-amqp/src/test/java/org/springframework/amqp/support/converter/SerializerMessageConverterTests.java @@ -23,9 +23,11 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -33,23 +35,23 @@ import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.utils.test.TestUtils; -import org.springframework.core.NestedIOException; import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.core.serializer.Deserializer; /** * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan */ public class SerializerMessageConverterTests extends AllowedListDeserializingMessageConverterTests { @Test - public void bytesAsDefaultMessageBodyType() throws Exception { + public void bytesAsDefaultMessageBodyType() { SerializerMessageConverter converter = new SerializerMessageConverter(); Message message = new Message("test".getBytes(), new MessageProperties()); Object result = converter.fromMessage(message); assertThat(result.getClass()).isEqualTo(byte[].class); - assertThat(new String((byte[]) result, "UTF-8")).isEqualTo("test"); + assertThat(new String((byte[]) result, StandardCharsets.UTF_8)).isEqualTo("test"); } @Test @@ -65,7 +67,7 @@ public void messageToString() { @Test public void messageToBytes() { SerializerMessageConverter converter = new SerializerMessageConverter(); - Message message = new Message(new byte[] { 1, 2, 3 }, new MessageProperties()); + Message message = new Message(new byte[]{ 1, 2, 3 }, new MessageProperties()); message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_BYTES); Object result = converter.fromMessage(message); assertThat(result.getClass()).isEqualTo(byte[].class); @@ -126,7 +128,7 @@ public void stringToMessage() throws Exception { @Test public void bytesToMessage() throws Exception { SerializerMessageConverter converter = new SerializerMessageConverter(); - Message message = converter.toMessage(new byte[] { 1, 2, 3 }, new MessageProperties()); + Message message = converter.toMessage(new byte[]{ 1, 2, 3 }, new MessageProperties()); String contentType = message.getMessageProperties().getContentType(); byte[] body = message.getBody(); assertThat(contentType).isEqualTo("application/octet-stream"); @@ -168,7 +170,7 @@ public void testDefaultDeserializerClassLoader() throws Exception { } @Test - public void messageConversionExceptionForClassNotFound() throws Exception { + public void messageConversionExceptionForClassNotFound() { SerializerMessageConverter converter = new SerializerMessageConverter(); TestBean testBean = new TestBean("foo"); Message message = converter.toMessage(testBean, new MessageProperties()); @@ -177,8 +179,8 @@ public void messageConversionExceptionForClassNotFound() throws Exception { byte[] body = message.getBody(); body[10] = 'z'; assertThatThrownBy(() -> converter.fromMessage(message)) - .isExactlyInstanceOf(MessageConversionException.class) - .hasCauseExactlyInstanceOf(NestedIOException.class); + .isExactlyInstanceOf(MessageConversionException.class) + .hasCauseExactlyInstanceOf(IOException.class); } }