Search in sources :

Example 36 with ServerHttpRequest

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

the class ReactorNettyRequestUpgradeStrategy method getHandshakeInfo.

private HandshakeInfo getHandshakeInfo(ServerWebExchange exchange, Optional<String> protocol) {
    ServerHttpRequest request = exchange.getRequest();
    Mono<Principal> principal = exchange.getPrincipal();
    return new HandshakeInfo(request.getURI(), request.getHeaders(), principal, protocol);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 37 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project FredBoat by Frederikam.

the class ApiExceptionHandler method onException.

@ExceptionHandler
public ResponseEntity<String> onException(Exception exception, ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    log.error(request.getMethod() + " " + request.getURI().getPath(), exception);
    return new ResponseEntity<>("Sorry! An error happened.\n" + exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 38 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-integration by spring-projects.

the class WebFluxInboundEndpoint method extractRequestBody.

@SuppressWarnings("unchecked")
private <T> Mono<T> extractRequestBody(ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    if (isReadable(request)) {
        MediaType contentType;
        if (request.getHeaders().getContentType() == null) {
            contentType = MediaType.APPLICATION_OCTET_STREAM;
        } else {
            contentType = request.getHeaders().getContentType();
        }
        if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
            return (Mono<T>) exchange.getFormData();
        } else if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
            return (Mono<T>) exchange.getMultipartData();
        } else {
            ResolvableType bodyType = getRequestPayloadType();
            if (bodyType == null) {
                bodyType = "text".equals(contentType.getType()) ? ResolvableType.forClass(String.class) : ResolvableType.forClass(byte[].class);
            }
            Class<?> resolvedType = bodyType.resolve();
            ReactiveAdapter adapter = (resolvedType != null ? this.adapterRegistry.getAdapter(resolvedType) : null);
            ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
            HttpMessageReader<?> httpMessageReader = this.codecConfigurer.getReaders().stream().filter(reader -> reader.canRead(elementType, contentType)).findFirst().orElseThrow(() -> new UnsupportedMediaTypeStatusException("Could not convert request: no suitable HttpMessageReader found for expected type [" + elementType + "] and content type [" + contentType + "]"));
            Map<String, Object> readHints = Collections.emptyMap();
            if (adapter != null && adapter.isMultiValue()) {
                Flux<?> flux = httpMessageReader.read(bodyType, elementType, request, response, readHints);
                return (Mono<T>) Mono.just(adapter.fromPublisher(flux));
            } else {
                Mono<?> mono = httpMessageReader.readMono(bodyType, elementType, request, response, readHints);
                if (adapter != null) {
                    return (Mono<T>) Mono.just(adapter.fromPublisher(mono));
                } else {
                    return (Mono<T>) mono;
                }
            }
        }
    } else {
        return (Mono<T>) Mono.just(exchange.getRequest().getQueryParams());
    }
}
Also used : Arrays(java.util.Arrays) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) WebHandler(org.springframework.web.server.WebHandler) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Map(java.util.Map) Message(org.springframework.messaging.Message) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) ResolvableType(org.springframework.core.ResolvableType) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) LinkedHashSet(java.util.LinkedHashSet) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HandlerMapping(org.springframework.web.reactive.HandlerMapping) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ReactiveAdapter(org.springframework.core.ReactiveAdapter) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) BaseHttpInboundEndpoint(org.springframework.integration.http.inbound.BaseHttpInboundEndpoint) MultiValueMap(org.springframework.util.MultiValueMap) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) MessagingGatewaySupport(org.springframework.integration.gateway.MessagingGatewaySupport) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) CollectionUtils(org.springframework.util.CollectionUtils) Expression(org.springframework.expression.Expression) ResponseEntity(org.springframework.http.ResponseEntity) Comparator(java.util.Comparator) Collections(java.util.Collections) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 39 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxTagsTests method methodTagToleratesNonStandardHttpMethods.

@Test
void methodTagToleratesNonStandardHttpMethods() {
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    given(exchange.getRequest()).willReturn(request);
    given(request.getMethod()).willReturn(HttpMethod.valueOf("CUSTOM"));
    Tag tag = WebFluxTags.method(exchange);
    assertThat(tag.getValue()).isEqualTo("CUSTOM");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Tag(io.micrometer.core.instrument.Tag) Test(org.junit.jupiter.api.Test)

Example 40 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxTagsTests method outcomeTagIsSuccessWhenResponseStatusIsAvailableFromUnderlyingServer.

@Test
void outcomeTagIsSuccessWhenResponseStatusIsAvailableFromUnderlyingServer() {
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    ServerHttpResponse response = mock(ServerHttpResponse.class);
    given(response.getStatusCode()).willReturn(HttpStatus.OK);
    given(response.getRawStatusCode()).willReturn(null);
    given(exchange.getRequest()).willReturn(request);
    given(exchange.getResponse()).willReturn(response);
    Tag tag = WebFluxTags.outcome(exchange, null);
    assertThat(tag.getValue()).isEqualTo("SUCCESS");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Tag(io.micrometer.core.instrument.Tag) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)71 HttpHeaders (org.springframework.http.HttpHeaders)26 Test (org.junit.jupiter.api.Test)25 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)20 ServerWebExchange (org.springframework.web.server.ServerWebExchange)19 URI (java.net.URI)17 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)17 Mono (reactor.core.publisher.Mono)13 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)10 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)9 HttpMethod (org.springframework.http.HttpMethod)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Map (java.util.Map)6 HttpStatus (org.springframework.http.HttpStatus)6 MediaType (org.springframework.http.MediaType)6 Flux (reactor.core.publisher.Flux)6 Principal (java.security.Principal)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5