Search in sources :

Example 6 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method actualRequestCredentials.

@Test
public void actualRequestCredentials() throws Exception {
    ServerWebExchange exchange = actualRequest();
    this.conf.addAllowedOrigin("http://domain1.com");
    this.conf.addAllowedOrigin("http://domain2.com");
    this.conf.addAllowedOrigin("http://domain3.com");
    this.conf.setAllowCredentials(true);
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 7 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method preflightRequestValidRequestAndConfig.

@Test
public void preflightRequestValidRequestAndConfig() throws Exception {
    ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1").toExchange();
    this.conf.addAllowedOrigin("*");
    this.conf.addAllowedMethod("GET");
    this.conf.addAllowedMethod("PUT");
    this.conf.addAllowedHeader("header1");
    this.conf.addAllowedHeader("header2");
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
    assertEquals("GET,PUT", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
    assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 8 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class AbstractMessageWriterResultHandler method writeBody.

@SuppressWarnings("unchecked")
protected Mono<Void> writeBody(Object body, MethodParameter bodyParameter, ServerWebExchange exchange) {
    ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
    Class<?> bodyClass = bodyType.resolve();
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyClass, body);
    Publisher<?> publisher;
    ResolvableType elementType;
    if (adapter != null) {
        publisher = adapter.toPublisher(body);
        elementType = adapter.isNoValue() ? ResolvableType.forClass(Void.class) : bodyType.getGeneric(0);
    } else {
        publisher = Mono.justOrEmpty(body);
        elementType = (bodyClass == null && body != null ? ResolvableType.forInstance(body) : bodyType);
    }
    if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) {
        return Mono.from((Publisher<Void>) publisher);
    }
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    MediaType bestMediaType = selectMediaType(exchange, () -> getProducibleMediaTypes(elementType));
    if (bestMediaType != null) {
        for (HttpMessageWriter<?> messageWriter : getMessageWriters()) {
            if (messageWriter.canWrite(elementType, bestMediaType)) {
                return (messageWriter instanceof ServerHttpMessageWriter ? ((ServerHttpMessageWriter<?>) messageWriter).write((Publisher) publisher, bodyType, elementType, bestMediaType, request, response, Collections.emptyMap()) : messageWriter.write((Publisher) publisher, elementType, bestMediaType, response, Collections.emptyMap()));
            }
        }
    } else {
        if (getProducibleMediaTypes(elementType).isEmpty()) {
            return Mono.error(new IllegalStateException("No converter for return value type: " + elementType));
        }
    }
    return Mono.error(new NotAcceptableStatusException(getProducibleMediaTypes(elementType)));
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ServerHttpMessageWriter(org.springframework.http.codec.ServerHttpMessageWriter) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 9 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class RedirectView method sendRedirect.

/**
	 * Send a redirect back to the HTTP client
	 * @param targetUrl the target URL to redirect to
	 * @param exchange current exchange
	 */
protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) {
    ServerHttpResponse response = exchange.getResponse();
    String encodedURL = (isRemoteHost(targetUrl) ? targetUrl : response.encodeUrl(targetUrl));
    response.getHeaders().setLocation(URI.create(encodedURL));
    response.setStatusCode(getStatusCode());
    return Mono.empty();
}
Also used : ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 10 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultEntityResponseBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
    MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
    HandlerStrategies strategies = HandlerStrategies.empty().messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())).build();
    StepVerifier.create(result).consumeNextWith(response -> {
        StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
        response.writeTo(exchange, strategies);
    }).expectComplete().verify();
    assertNotNull(exchange.getResponse().getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) CacheControl(org.springframework.http.CacheControl) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Assert.assertSame(org.junit.Assert.assertSame) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ResolvableType(org.springframework.core.ResolvableType) EnumSet(java.util.EnumSet) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Collections(java.util.Collections) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)30 Test (org.junit.Test)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)20 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)6 ResolvableType (org.springframework.core.ResolvableType)4 MediaType (org.springframework.http.MediaType)4 Flux (reactor.core.publisher.Flux)4 IOException (java.io.IOException)3 Collections (java.util.Collections)3 Mono (reactor.core.publisher.Mono)3 List (java.util.List)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ReactiveAdapter (org.springframework.core.ReactiveAdapter)2 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)2 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)2