Skip to content

Commit

Permalink
Remove usage of obsolete NestedIOException
Browse files Browse the repository at this point in the history
Related to spring-projects/spring-framework#28198

Replaces its usage with the standard `IOException`
which has supported a root cause since Java 6.
  • Loading branch information
garyrussell authored and artembilan committed Mar 18, 2022
1 parent 0ccfcbe commit d8aafda
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,6 +34,7 @@
*
* @author Dave Syer
* @author Gary Russell
* @author Artem Bilan
*/
public final class SerializationUtils {

Expand Down Expand Up @@ -111,22 +111,22 @@ public static Object deserialize(InputStream inputStream, Set<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,35 @@

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;

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
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}

}

0 comments on commit d8aafda

Please sign in to comment.