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

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

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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -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()); byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
return new ByteArrayResource(body) { return new ByteArrayResource(body) {
@Override @Override

Loading…
Cancel
Save