Skip to content

Commit 8b81845

Browse files
committed
Merge branch '3.5.x' of github.com:freefair/okhttp-spring-boot into 4.0.x
2 parents f3e2f4e + be2dea8 commit 8b81845

File tree

8 files changed

+127
-22
lines changed

8 files changed

+127
-22
lines changed

.github/workflows/gradle-deploy.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
deploy-docs:
1313
runs-on: ubuntu-latest
1414
env:
15-
ORG_GRADLE_PROJECT_freefairBuildCachePassword: ${{ secrets.BUILD_CACHE_PASSWORD }}
1615
ORG_GRADLE_PROJECT_freefairDocsUser: ${{ secrets.FREEFAIR_DOCS_USER }}
1716
ORG_GRADLE_PROJECT_freefairDocsPass: ${{ secrets.FREEFAIR_DOCS_PASS }}
1817
steps:
@@ -23,5 +22,5 @@ jobs:
2322
java-version: 17
2423
distribution: 'temurin'
2524
- name: Setup Gradle
26-
uses: gradle/gradle-build-action@v3
25+
uses: gradle/actions/setup-gradle@v4
2726
- run: ./gradlew uploadDocumentation -s

.github/workflows/gradle-publish.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ on:
77
jobs:
88
publish:
99
runs-on: ubuntu-latest
10-
env:
11-
ORG_GRADLE_PROJECT_freefairBuildCachePassword: ${{ secrets.BUILD_CACHE_PASSWORD }}
1210
steps:
1311
- uses: actions/checkout@v5
1412
- name: Set up JDK 17
@@ -17,17 +15,17 @@ jobs:
1715
java-version: 17
1816
distribution: 'temurin'
1917
- name: Setup Gradle
20-
uses: gradle/gradle-build-action@v3
18+
uses: gradle/actions/setup-gradle@v4
2119
- name: Publish local
2220
run: ./gradlew publish
2321
env:
2422
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.FREEFAIR_SIGNING_KEY }}
2523
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.FREEFAIR_SIGNING_PASSWORD }}
2624
- name: Publish package
27-
run: ./gradlew --no-parallel uploadDocumentation jreleaserFullRelease
25+
run: ./gradlew --no-parallel uploadDocumentation jreleaserDeploy
2826
env:
2927
JRELEASER_MAVENCENTRAL_USERNAME: ${{ secrets.MAVENCENTRAL_USERNAME }}
30-
JRELEASER_MAVENCENTRAL_PASSWORD: ${{ secrets.MAVENCENTRAL_PASSWORD }}
28+
JRELEASER_MAVENCENTRAL_TOKEN: ${{ secrets.MAVENCENTRAL_PASSWORD }}
3129
JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3230
ORG_GRADLE_PROJECT_freefairDocsUser: ${{ secrets.FREEFAIR_DOCS_USER }}
3331
ORG_GRADLE_PROJECT_freefairDocsPass: ${{ secrets.FREEFAIR_DOCS_PASS }}

.github/workflows/gradle.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ jobs:
1111
build:
1212
name: build
1313
runs-on: ubuntu-latest
14-
env:
15-
ORG_GRADLE_PROJECT_freefairBuildCachePassword: ${{ secrets.BUILD_CACHE_PASSWORD }}
1614
strategy:
1715
fail-fast: false
1816
matrix:
@@ -27,7 +25,7 @@ jobs:
2725
distribution: 'temurin'
2826

2927
- name: Setup Gradle
30-
uses: gradle/gradle-build-action@v3
28+
uses: gradle/actions/setup-gradle@v4
3129

3230
- run: ./gradlew -V assemble -s
3331
- run: ./gradlew -V check -s

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+
}

settings.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ develocity {
2020
}
2121
}
2222

23-
buildCache {
24-
remote(HttpBuildCache) {
25-
url = 'https://build-cache.grefer-hosting.de/cache/'
26-
push = isCiServer
27-
credentials {
28-
username = "freefair"
29-
password = providers.gradleProperty('freefairBuildCachePassword').getOrElse(null)
30-
}
31-
}
32-
}
33-
3423
dependencyResolutionManagement {
3524
repositories {
3625
mavenCentral()

0 commit comments

Comments
 (0)