Remove usage of obsolete NestedIOException

Related to https://github.com/spring-projects/spring-framework/issues/28198

Replaces its usage with the standard `IOException`
which has supported a root cause since Java 6.
This commit is contained in:
Gary Russell
2021-12-15 15:22:20 -05:00
committed by Artem Bilan
parent 0ccfcbe283
commit d8aafdafff
3 changed files with 27 additions and 23 deletions

View File

@@ -22,16 +22,17 @@ import java.io.IOException;
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 @@ import org.springframework.core.serializer.Serializer;
*
* @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 class SerializerMessageConverter extends AllowedListDeserializingMessageC
*
* @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 @@ public class SerializerMessageConverter extends AllowedListDeserializingMessageC
return objectInputStream.readObject();
}
catch (ClassNotFoundException ex) {
throw new NestedIOException("Failed to deserialize object type", ex);
throw new IOException("Failed to deserialize object type", ex);
}
}

View File

@@ -26,7 +26,6 @@ import java.io.ObjectStreamClass;
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 @@ import org.springframework.util.PatternMatchUtils;
*
* @author Dave Syer
* @author Gary Russell
* @author Artem Bilan
*/
public final class SerializationUtils {
@@ -111,22 +111,22 @@ public final class SerializationUtils {
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);
}
}

View File

@@ -23,9 +23,11 @@ import static org.mockito.Mockito.verify;
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.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
@@ -65,7 +67,7 @@ public class SerializerMessageConverterTests extends AllowedListDeserializingMes
@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 class SerializerMessageConverterTests extends AllowedListDeserializingMes
@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 class SerializerMessageConverterTests extends AllowedListDeserializingMes
}
@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 class SerializerMessageConverterTests extends AllowedListDeserializingMes
byte[] body = message.getBody();
body[10] = 'z';
assertThatThrownBy(() -> converter.fromMessage(message))
.isExactlyInstanceOf(MessageConversionException.class)
.hasCauseExactlyInstanceOf(NestedIOException.class);
.isExactlyInstanceOf(MessageConversionException.class)
.hasCauseExactlyInstanceOf(IOException.class);
}
}