Search in sources :

Example 1 with WebClient

use of cn.taketoday.web.reactive.function.client.WebClient in project today-infrastructure by TAKETODAY.

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link WebClient WebClient} and
 * {@link cn.taketoday.web.reactive.function.server.ServerResponse ServerResponse}.
 *
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementClass the class of values to be produced
 * @return the inserter to write a producer
 * @since 4.0
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementClass, "'elementClass' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.fromClass(elementClass), adapter);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Assert(cn.taketoday.lang.Assert) WebClient(cn.taketoday.web.reactive.function.client.WebClient) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Publisher(org.reactivestreams.Publisher) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) List(java.util.List) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Nullable(cn.taketoday.lang.Nullable) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) TypeReference(cn.taketoday.core.TypeReference) MediaType(cn.taketoday.http.MediaType) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter)

Example 2 with WebClient

use of cn.taketoday.web.reactive.function.client.WebClient in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method noCompressionForResponseWithInvalidContentType.

@Test
void noCompressionForResponseWithInvalidContentType() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMimeTypes(new String[] { "application/json" });
    WebClient client = prepareCompressionTest(compression, "test~plain");
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsNotCompressed(response);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 3 with WebClient

use of cn.taketoday.web.reactive.function.client.WebClient in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method compressionOfResponseToGetRequest.

@Test
protected void compressionOfResponseToGetRequest() {
    WebClient client = prepareCompressionTest();
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsCompressed(response);
}
Also used : WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 4 with WebClient

use of cn.taketoday.web.reactive.function.client.WebClient in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method testClientAuthSuccess.

protected void testClientAuthSuccess(Ssl sslConfiguration, ReactorClientHttpConnector clientConnector) {
    AbstractReactiveWebServerFactory factory = getFactory();
    factory.setSsl(sslConfiguration);
    this.webServer = factory.getWebServer(new EchoHandler());
    this.webServer.start();
    WebClient client = WebClient.builder().baseUrl("https://localhost:" + this.webServer.getPort()).clientConnector(clientConnector).build();
    Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
    assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}
Also used : WebClient(cn.taketoday.web.reactive.function.client.WebClient)

Example 5 with WebClient

use of cn.taketoday.web.reactive.function.client.WebClient in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method noCompressionForSmallResponse.

@Test
void noCompressionForSmallResponse() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMinResponseSize(DataSize.ofBytes(3001));
    WebClient client = prepareCompressionTest(compression);
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsNotCompressed(response);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebClient (cn.taketoday.web.reactive.function.client.WebClient)36 Test (org.junit.jupiter.api.Test)22 Compression (cn.taketoday.framework.web.server.Compression)8 Ssl (cn.taketoday.framework.web.server.Ssl)6 ReactorClientHttpConnector (cn.taketoday.http.client.reactive.ReactorClientHttpConnector)6 ConnectException (java.net.ConnectException)6 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)4 MultiValueMap (cn.taketoday.core.MultiValueMap)4 ReactiveAdapter (cn.taketoday.core.ReactiveAdapter)4 ReactiveAdapterRegistry (cn.taketoday.core.ReactiveAdapterRegistry)4 ResolvableType (cn.taketoday.core.ResolvableType)4 TypeReference (cn.taketoday.core.TypeReference)4 Resource (cn.taketoday.core.io.Resource)4 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)4 HttpEntity (cn.taketoday.http.HttpEntity)4 MediaType (cn.taketoday.http.MediaType)4 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)4 MultipartBodyBuilder (cn.taketoday.http.client.MultipartBodyBuilder)4 ClientHttpRequest (cn.taketoday.http.client.reactive.ClientHttpRequest)4 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)4