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);
}
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);
}
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());
}
}
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");
}
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");
}
Aggregations