Skip to content

Commit 221cf1e

Browse files
committed
Merge branch '3.4.x' of github.com:freefair/okhttp-spring-boot into 3.5.x
2 parents 0c05214 + 4e9984b commit 221cf1e

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

okhttp-spring-client/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ dependencies {
1212

1313
api "com.squareup.okhttp3:okhttp"
1414
api "org.springframework:spring-web"
15+
16+
testImplementation "org.springframework.boot:spring-boot-starter-test"
17+
}
18+
19+
tasks.named("test", Test) {
20+
useJUnitPlatform()
1521
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.freefair.spring.okhttp.client;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.RequestBody;
5+
import okio.BufferedSink;
6+
import okio.Okio;
7+
import org.springframework.core.io.ByteArrayResource;
8+
import org.springframework.core.io.FileSystemResource;
9+
import org.springframework.core.io.InputStreamResource;
10+
import org.springframework.core.io.Resource;
11+
import org.springframework.lang.Nullable;
12+
import org.springframework.util.MimeType;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
17+
/**
18+
* @author Lars Grefer
19+
*/
20+
public class ResourceRequestBody extends RequestBody {
21+
22+
private final Resource resource;
23+
24+
@Nullable
25+
private final MediaType mediaType;
26+
27+
public ResourceRequestBody(Resource resource) {
28+
this.resource = resource;
29+
this.mediaType = null;
30+
}
31+
32+
public ResourceRequestBody(Resource resource, MimeType springMimeType) {
33+
this.resource = resource;
34+
this.mediaType = MediaType.parse(springMimeType.toString());
35+
}
36+
37+
public ResourceRequestBody(Resource resource, MediaType okhttpMediaType) {
38+
this.resource = resource;
39+
this.mediaType = okhttpMediaType;
40+
}
41+
42+
@Override
43+
@Nullable
44+
public MediaType contentType() {
45+
return mediaType;
46+
}
47+
48+
@Override
49+
public void writeTo(BufferedSink bufferedSink) throws IOException {
50+
try (InputStream inputStream = resource.getInputStream()) {
51+
bufferedSink.writeAll(Okio.source(inputStream));
52+
}
53+
}
54+
55+
@Override
56+
public long contentLength() throws IOException {
57+
if (resource instanceof InputStreamResource && resource.getClass().equals(InputStreamResource.class)) {
58+
return -1;
59+
}
60+
return resource.contentLength();
61+
}
62+
63+
@Override
64+
public boolean isOneShot() {
65+
if (resource instanceof InputStreamResource) {
66+
return true;
67+
}
68+
69+
if (resource instanceof ByteArrayResource) {
70+
return false;
71+
}
72+
73+
if (resource instanceof FileSystemResource) {
74+
return false;
75+
}
76+
77+
return super.isOneShot();
78+
}
79+
}

okhttp-spring-client/src/main/java/io/freefair/spring/okhttp/client/StreamingBodyRequestBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public void writeTo(@NotNull BufferedSink bufferedSink) throws IOException {
4444

4545
@Override
4646
public boolean isOneShot() {
47-
return true;
47+
return !streamingBody.repeatable();
4848
}
4949
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.freefair.spring.okhttp.client;
2+
3+
import okhttp3.MediaType;
4+
import okio.ByteString;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.core.io.ByteArrayResource;
7+
import org.springframework.util.MimeType;
8+
import org.springframework.util.MimeTypeUtils;
9+
10+
import java.nio.charset.StandardCharsets;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
15+
class ResourceRequestBodyTest {
16+
17+
18+
@Test
19+
void testContentTypes() {
20+
21+
ByteArrayResource resource = new ByteArrayResource(ByteString.encodeUtf8("hello world").toByteArray());
22+
23+
assertThat(new ResourceRequestBody(resource).contentType())
24+
.isNull();
25+
26+
assertThat(new ResourceRequestBody(resource, MimeTypeUtils.IMAGE_PNG).contentType().toString())
27+
.isEqualTo("image/png");
28+
29+
assertThat(new ResourceRequestBody(resource, new MimeType("application", "foo", StandardCharsets.UTF_16)).contentType().toString())
30+
.isEqualTo("application/foo;charset=UTF-16");
31+
32+
assertThat(new ResourceRequestBody(resource, MediaType.parse("text/plain")).contentType().toString())
33+
.isEqualTo("text/plain");
34+
35+
}
36+
}

0 commit comments

Comments
 (0)