Browse Source

Fix Class isAssignableFrom checks for Resource conversion

Issue: SPR-16606
pull/1744/head
Rossen Stoyanchev 7 years ago
parent
commit
30583a62cf
  1. 6
      spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java
  2. 27
      spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java
  3. 4
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

6
spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java

@ -51,9 +51,7 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> { @@ -51,9 +51,7 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
@Override
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
Class<?> clazz = elementType.getRawClass();
return (clazz != null &&
(InputStreamResource.class == clazz || clazz.isAssignableFrom(ByteArrayResource.class)) &&
super.canDecode(elementType, mimeType));
return clazz != null && Resource.class.isAssignableFrom(clazz) && super.canDecode(elementType, mimeType);
}
@Override
@ -77,7 +75,7 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> { @@ -77,7 +75,7 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
if (InputStreamResource.class == clazz) {
return new InputStreamResource(new ByteArrayInputStream(bytes));
}
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
else if (Resource.class.isAssignableFrom(clazz)) {
return new ByteArrayResource(bytes);
}
else {

27
spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,7 +23,6 @@ import org.junit.Test; @@ -23,7 +23,6 @@ import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
@ -32,9 +31,8 @@ import org.springframework.core.io.buffer.DataBuffer; @@ -32,9 +31,8 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
/**
* @author Arjen Poutsma
@ -44,25 +42,22 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase { @@ -44,25 +42,22 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
private final ResourceDecoder decoder = new ResourceDecoder();
@Test
public void canDecode() throws Exception {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteArrayResource.class),
MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(Resource.class),
MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
MimeTypeUtils.APPLICATION_JSON));
public void canDecode() {
assertTrue(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON));
assertFalse(this.decoder.canDecode(forClass(Object.class), MimeTypeUtils.APPLICATION_JSON));
}
@Test
public void decode() throws Exception {
public void decode() {
DataBuffer fooBuffer = stringBuffer("foo");
DataBuffer barBuffer = stringBuffer("bar");
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
Flux<Resource> result = this.decoder
.decode(source, ResolvableType.forClass(Resource.class), null, Collections.emptyMap());
.decode(source, forClass(Resource.class), null, Collections.emptyMap());
StepVerifier.create(result)
.consumeNextWith(resource -> {

4
spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -86,7 +86,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -86,7 +86,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
};
}
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) {
byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
return new ByteArrayResource(body) {
@Override

Loading…
Cancel
Save