Skip to content

Commit 5d78ede

Browse files
committed
Merge branch 'feature/master/apache5x' into joviegas/post-surface-api-review
2 parents 1acd510 + dfcfdcb commit 5d78ede

File tree

6 files changed

+30
-52
lines changed

6 files changed

+30
-52
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Improve error message for the error case where a request using RequestBody#fromInputStream failed to retry due to lack of mark and reset support. See [#6174](https://github.com/aws/aws-sdk-java-v2/issues/6174)"
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"type": "feature",
3-
"category": "AWS SDK for Java v2",
3+
"category": "Apache HTTP Client 5",
44
"contributor": "",
55
"description": "Preview Release of AWS SDK Apache5 HttpClient with Apache HttpClient 5.5.x"
66
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/sync/RequestBody.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import software.amazon.awssdk.http.ContentStreamProvider;
3939
import software.amazon.awssdk.http.Header;
4040
import software.amazon.awssdk.utils.BinaryUtils;
41-
import software.amazon.awssdk.utils.IoUtils;
4241

4342
/**
4443
* Represents the body of an HTTP request. Must be provided for operations that have a streaming input.
@@ -120,38 +119,29 @@ public static RequestBody fromFile(File file) {
120119
* Creates a {@link RequestBody} from an input stream. {@value Header#CONTENT_LENGTH} must
121120
* be provided so that the SDK does not have to make two passes of the data.
122121
* <p>
123-
* The stream will not be closed by the SDK. It is up to to caller of this method to close the stream. The stream
124-
* should not be read outside of the SDK (by another thread) as it will change the state of the {@link InputStream} and
122+
* The stream will not be closed by the SDK. It is up to caller of this method to close the stream. The stream
123+
* should not be read outside the SDK (by another thread) as it will change the state of the {@link InputStream} and
125124
* could tamper with the sending of the request.
126125
* <p>
127126
* To support resetting via {@link ContentStreamProvider}, this uses {@link InputStream#reset()} and uses a read limit of
128127
* 128 KiB. If you need more control, use {@link #fromContentProvider(ContentStreamProvider, long, String)} or
129128
* {@link #fromContentProvider(ContentStreamProvider, String)}.
130129
*
130+
* <p>
131+
* It is recommended to provide a stream that supports mark and reset for retry. If the stream does not support mark and
132+
* reset, an {@link IllegalStateException} will be thrown during retry.
133+
*
131134
* @param inputStream Input stream to send to the service. The stream will not be closed by the SDK.
132135
* @param contentLength Content length of data in input stream. If a content length smaller than the actual size of the
133136
* object is set, the client will truncate the stream to the specified content length and only send
134137
* exactly the number of bytes equal to the content length.
135138
* @return RequestBody instance.
136139
*/
137140
public static RequestBody fromInputStream(InputStream inputStream, long contentLength) {
138-
IoUtils.markStreamWithMaxReadLimit(inputStream);
139-
InputStream nonCloseable = nonCloseableInputStream(inputStream);
140-
ContentStreamProvider provider = new ContentStreamProvider() {
141-
@Override
142-
public InputStream newStream() {
143-
if (nonCloseable.markSupported()) {
144-
invokeSafely(nonCloseable::reset);
145-
}
146-
return nonCloseable;
147-
}
148-
149-
@Override
150-
public String name() {
151-
return ProviderType.STREAM.getName();
152-
}
153-
};
154-
return fromContentProvider(provider, contentLength, Mimetype.MIMETYPE_OCTET_STREAM);
141+
ContentStreamProvider contentStreamProvider = ContentStreamProvider.fromInputStream(
142+
nonCloseableInputStream(inputStream));
143+
return fromContentProvider(contentStreamProvider,
144+
contentLength, Mimetype.MIMETYPE_OCTET_STREAM);
155145
}
156146

157147
/**

core/sdk-core/src/test/java/software/amazon/awssdk/core/sync/RequestBodyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import software.amazon.awssdk.checksums.SdkChecksum;
4040
import software.amazon.awssdk.core.internal.sync.BufferingContentStreamProvider;
4141
import software.amazon.awssdk.core.internal.util.Mimetype;
42+
import software.amazon.awssdk.testutils.RandomInputStream;
4243
import software.amazon.awssdk.utils.BinaryUtils;
4344
import software.amazon.awssdk.utils.IoUtils;
4445
import software.amazon.awssdk.utils.StringInputStream;
@@ -163,6 +164,17 @@ public void fromInputStream_streamSupportsReset_resetsTheStream() {
163164
assertThat(getCrc32(requestBody.contentStreamProvider().newStream())).isEqualTo(streamCrc32);
164165
}
165166

167+
@Test
168+
public void fromInputStream_streamNotSupportReset_shouldThrowException() {
169+
RandomInputStream stream = new RandomInputStream(100);
170+
assertThat(stream.markSupported()).isFalse();
171+
RequestBody requestBody = RequestBody.fromInputStream(stream, 100);
172+
IoUtils.drainInputStream(requestBody.contentStreamProvider().newStream());
173+
assertThatThrownBy(() -> requestBody.contentStreamProvider().newStream())
174+
.isInstanceOf(IllegalStateException.class)
175+
.hasMessageContaining("Content input stream does not support mark/reset");
176+
}
177+
166178
private static String getCrc32(InputStream inputStream) {
167179
byte[] buff = new byte[1024];
168180
int read;

http-clients/apache5-client/src/main/resources/META-INF/services/software.amazon.awssdk.http.SdkHttpService

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,4 @@
1313
# permissions and limitations under the License.
1414
#
1515

16-
#
17-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
18-
#
19-
# Licensed under the Apache License, Version 2.0 (the "License").
20-
# You may not use this file except in compliance with the License.
21-
# A copy of the License is located at
22-
#
23-
# http://aws.amazon.com/apache2.0
24-
#
25-
# or in the "license" file accompanying this file. This file is distributed
26-
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
27-
# express or implied. See the License for the specific language governing
28-
# permissions and limitations under the License.
29-
#
30-
31-
#
32-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33-
#
34-
# Licensed under the Apache License, Version 2.0 (the "License").
35-
# You may not use this file except in compliance with the License.
36-
# A copy of the License is located at
37-
#
38-
# http://aws.amazon.com/apache2.0
39-
#
40-
# or in the "license" file accompanying this file. This file is distributed
41-
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
42-
# express or implied. See the License for the specific language governing
43-
# permissions and limitations under the License.
44-
#
45-
4616
software.amazon.awssdk.http.apache5.Apache5SdkHttpService

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@
825825
<spotbugs.skip>true</spotbugs.skip>
826826
<skip.unit.tests>true</skip.unit.tests>
827827
<mdep.analyze.skip>true</mdep.analyze.skip>
828-
<japicmp.skip>false</japicmp.skip>
828+
<japicmp.skip>true</japicmp.skip>
829829
<maven.javadoc.skip>true</maven.javadoc.skip>
830830
</properties>
831831
</profile>

0 commit comments

Comments
 (0)