Skip to content

Commit

Permalink
Merge pull request #24292 from ofaizulin/rd-httpmsgconv-fix-isr-conte…
Browse files Browse the repository at this point in the history
…ntlength

Closes gh-24292
  • Loading branch information
rstoyanchev committed Jan 8, 2020
2 parents e3e7d90 + 0182738 commit 1dd5db4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
@@ -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.
Expand Down Expand Up @@ -83,6 +83,10 @@ 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
@@ -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.
Expand Down Expand Up @@ -82,10 +82,7 @@ public void decode() {
@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 -> {
Expand All @@ -105,4 +102,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());
}

}
@@ -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.
Expand Down 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() throws IOException {
long length = inputMessage.getHeaders().getContentLength();
return (length != -1 ? length : super.contentLength());
}
};
}
else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) {
Expand Down
@@ -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.
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

0 comments on commit 1dd5db4

Please sign in to comment.