-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
462 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/main/java/io/github/nstdio/http/ext/BodyPublishers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.nstdio.http.ext; | ||
|
||
import io.github.nstdio.http.ext.spi.JsonMappingProvider; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.net.http.HttpRequest.BodyPublisher; | ||
import java.nio.ByteBuffer; | ||
import java.util.Optional; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Flow; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Implementations of various useful {@link BodyPublisher}s. | ||
*/ | ||
public final class BodyPublishers { | ||
private BodyPublishers() { | ||
} | ||
|
||
/** | ||
* Returns a request body publisher whose body is JSON representation of {@code body}. The conversion will be done | ||
* using {@code JsonMappingProvider} default provider retrieved using {@link JsonMappingProvider#provider()}. | ||
* | ||
* @param body The body. | ||
* | ||
* @return a BodyPublisher | ||
*/ | ||
public static BodyPublisher ofJson(Object body) { | ||
return ofJson(body, JsonMappingProvider.provider()); | ||
} | ||
|
||
/** | ||
* Returns a request body publisher whose body is JSON representation of {@code body}. The conversion will be done | ||
* using {@code jsonProvider}. | ||
* | ||
* @param body The body. | ||
* @param jsonProvider The JSON mapping provider to use when creating JSON presentation of {@code body}. | ||
* | ||
* @return a BodyPublisher | ||
*/ | ||
public static BodyPublisher ofJson(Object body, JsonMappingProvider jsonProvider) { | ||
return ofJson(body, jsonProvider, null); | ||
} | ||
|
||
/** | ||
* Returns a request body publisher whose body is JSON representation of {@code body}. The conversion will be done | ||
* using {@code JsonMappingProvider} default provider retrieved using {@link JsonMappingProvider#provider()}. | ||
* | ||
* @param body The body. | ||
* @param executor The scheduler to use to publish body to subscriber. If {@code null} the * | ||
* {@link ForkJoinPool#commonPool()} will be used. | ||
* | ||
* @return a BodyPublisher | ||
*/ | ||
public static BodyPublisher ofJson(Object body, Executor executor) { | ||
return ofJson(body, JsonMappingProvider.provider(), executor); | ||
} | ||
|
||
/** | ||
* Returns a request body publisher whose body is JSON representation of {@code body}. The conversion will be done * | ||
* using {@code jsonProvider}. | ||
* | ||
* @param body The body. | ||
* @param jsonProvider The JSON mapping provider to use when creating JSON presentation of {@code body}. | ||
* @param executor The scheduler to use to publish body to subscriber. If {@code null} the | ||
* {@link ForkJoinPool#commonPool()} will be used. | ||
* | ||
* @return a BodyPublisher | ||
*/ | ||
public static BodyPublisher ofJson(Object body, JsonMappingProvider jsonProvider, Executor executor) { | ||
return new JsonPublisher(body, jsonProvider, Optional.ofNullable(executor).orElseGet(ForkJoinPool::commonPool)); | ||
} | ||
|
||
/** | ||
* The {@code BodyPublisher} that converts objects to JSON. | ||
*/ | ||
static final class JsonPublisher implements BodyPublisher { | ||
private final Object body; | ||
private final JsonMappingProvider provider; | ||
private final Executor executor; | ||
|
||
JsonPublisher(Object body, JsonMappingProvider provider, Executor executor) { | ||
this.body = body; | ||
this.provider = provider; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { | ||
var subscription = ByteArraySubscription.ofByteBuffer(subscriber, bytesSupplier(), executor); | ||
|
||
subscriber.onSubscribe(subscription); | ||
} | ||
|
||
private Supplier<byte[]> bytesSupplier() { | ||
return () -> { | ||
var os = new ByteArrayOutputStream(); | ||
try { | ||
provider.get().write(body, os); | ||
} catch (Throwable e) { | ||
Throwables.sneakyThrow(e); | ||
} | ||
|
||
return os.toByteArray(); | ||
}; | ||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
return -1; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/io/github/nstdio/http/ext/ContentTypeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.nstdio.http.ext; | ||
|
||
import io.github.nstdio.http.ext.BodyPublishers.JsonPublisher; | ||
|
||
import java.net.http.HttpRequest.BodyPublisher; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static io.github.nstdio.http.ext.Headers.HEADER_CONTENT_TYPE; | ||
import static java.util.function.Predicate.not; | ||
|
||
class ContentTypeInterceptor implements Interceptor { | ||
private final Interceptor headersAdding; | ||
|
||
ContentTypeInterceptor(String contentType) { | ||
headersAdding = new HeadersAddingInterceptor(Map.of(HEADER_CONTENT_TYPE, contentType)); | ||
} | ||
|
||
@Override | ||
public <T> Chain<T> intercept(Chain<T> in) { | ||
if (!isJsonPublisher(in.request().bodyPublisher())) { | ||
return in; | ||
} | ||
|
||
return headersAdding.intercept(in); | ||
} | ||
|
||
private static boolean isJsonPublisher(Optional<BodyPublisher> bodyPublisher) { | ||
return bodyPublisher.filter(not(JsonPublisher.class::isInstance)).isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/spiTest/kotlin/io/github/nstdio/http/ext/BodyPublishersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.nstdio.http.ext | ||
|
||
import io.github.nstdio.http.ext.spi.Classpath | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import mockwebserver3.MockResponse | ||
import mockwebserver3.MockWebServer | ||
import mockwebserver3.junit5.internal.MockWebServerExtension | ||
import org.junit.jupiter.api.Assumptions.assumeTrue | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse.BodyHandlers.discarding | ||
import java.nio.charset.StandardCharsets.UTF_8 | ||
|
||
@ExtendWith(MockWebServerExtension::class) | ||
class BodyPublishersTest(private val mockWebServer: MockWebServer) { | ||
@Nested | ||
internal inner class OfJsonTest { | ||
@BeforeEach | ||
fun setUp() { | ||
assumeTrue { ALL_JSON.any { Classpath.isPresent(it) } } | ||
} | ||
|
||
@Test | ||
fun `Should publish body as JSON`() { | ||
//given | ||
val client = ExtendedHttpClient.newHttpClient() | ||
val body = mapOf("a" to 1, "b" to 2) | ||
val request = HttpRequest.newBuilder(mockWebServer.url("/test").toUri()) | ||
.POST(BodyPublishers.ofJson(body)) | ||
.build() | ||
|
||
mockWebServer.enqueue(MockResponse().setResponseCode(200)) | ||
|
||
//when | ||
client.send(request, discarding()) | ||
|
||
//then | ||
val actual = mockWebServer.takeRequest() | ||
|
||
actual.body.readString(UTF_8) | ||
.shouldBe("{\"a\":1,\"b\":2}") | ||
|
||
actual.headers["Content-Type"] | ||
.shouldNotBeNull() | ||
.shouldBe("application/json") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters