Browse Source

Merge pull request #24292 from ofaizulin/rd-httpmsgconv-fix-isr-contentlength

Closes gh-24292
pull/24332/head
Rossen Stoyanchev 6 years ago
parent
commit
1dd5db42ab
  1. 6
      spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java
  2. 25
      spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java
  3. 7
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java
  4. 4
      spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -83,6 +83,10 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> { @@ -83,6 +83,10 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
public String getFilename() {
return filename;
}
@Override
public long contentLength() {
return bytes.length;
}
};
}
else if (Resource.class.isAssignableFrom(clazz)) {

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -82,10 +82,7 @@ class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> { @@ -82,10 +82,7 @@ class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> {
@Override
@Test
public void decodeToMono() {
Flux<DataBuffer> input = Flux.concat(
dataBuffer(this.fooBytes),
dataBuffer(this.barBytes));
Flux<DataBuffer> input = Flux.concat(dataBuffer(this.fooBytes), dataBuffer(this.barBytes));
testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class),
step -> step
.consumeNextWith(value -> {
@ -105,4 +102,22 @@ class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> { @@ -105,4 +102,22 @@ class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> {
Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
}
@Test
public void decodeInputStreamResource() {
Flux<DataBuffer> input = Flux.concat(dataBuffer(this.fooBytes), dataBuffer(this.barBytes));
testDecodeAll(input, InputStreamResource.class, step -> step
.consumeNextWith(resource -> {
try {
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
assertThat(new String(bytes)).isEqualTo("foobar");
assertThat(resource.contentLength()).isEqualTo(fooBytes.length + barBytes.length);
}
catch (IOException ex) {
throw new AssertionError(ex.getMessage(), ex);
}
})
.expectComplete()
.verify());
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@ -84,6 +84,11 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -84,6 +84,11 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
public String getFilename() {
return inputMessage.getHeaders().getContentDisposition().getFilename();
}
@Override
public long contentLength() throws IOException {
long length = inputMessage.getHeaders().getContentLength();
return (length != -1 ? length : super.contentLength());
}
};
}
else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) {

4
spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -80,10 +80,12 @@ public class ResourceHttpMessageConverterTests { @@ -80,10 +80,12 @@ public class ResourceHttpMessageConverterTests {
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
inputMessage.getHeaders().setContentDisposition(
ContentDisposition.builder("attachment").filename("yourlogo.jpg").build());
inputMessage.getHeaders().setContentLength(123);
Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
assertThat(actualResource).isInstanceOf(InputStreamResource.class);
assertThat(actualResource.getInputStream()).isEqualTo(body);
assertThat(actualResource.getFilename()).isEqualTo("yourlogo.jpg");
assertThat(actualResource.contentLength()).isEqualTo(123);
}
}

Loading…
Cancel
Save