Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ContentLength support for InputStreamResource created in ResourceHttpMessageConverter and ResourceDecoder #24292

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -83,6 +83,11 @@ public Resource decode(DataBuffer dataBuffer, ResolvableType elementType,
public String getFilename() {
return filename;
}

@Override
public long contentLength() {
return bytes.length;
}
};
}
else if (Resource.class.isAssignableFrom(clazz)) {
Expand Down
Expand Up @@ -104,4 +104,22 @@ public void decodeToMono() {
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());
}

}
Expand Up @@ -84,6 +84,11 @@ protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessag
public String getFilename() {
return inputMessage.getHeaders().getContentDisposition().getFilename();
}

@Override
public long contentLength() {
return inputMessage.getHeaders().getContentLength();
}
};
}
else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) {
Expand Down
Expand Up @@ -80,10 +80,12 @@ public void shouldReadInputStreamResource() throws IOException {
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);
}
}

Expand Down