Search in sources :

Example 61 with ServerHttpRequest

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

the class RedirectView method createTargetUrl.

/**
 * Create the target URL and, if necessary, pre-pend the contextPath, expand
 * URI template variables, append the current request query, and apply the
 * configured {@link #getRequestDataValueProcessor()
 * RequestDataValueProcessor}.
 */
protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) {
    String url = getUrl();
    Assert.state(url != null, "'url' not set");
    ServerHttpRequest request = exchange.getRequest();
    StringBuilder targetUrl = new StringBuilder();
    if (isContextRelative() && url.startsWith("/")) {
        targetUrl.append(request.getPath().contextPath().value());
    }
    targetUrl.append(url);
    if (StringUtils.hasText(targetUrl)) {
        Map<String, String> uriVars = getCurrentUriVariables(exchange);
        targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars);
    }
    if (isPropagateQuery()) {
        targetUrl = appendCurrentRequestQuery(targetUrl.toString(), request);
    }
    String result = targetUrl.toString();
    RequestDataValueProcessor processor = getRequestDataValueProcessor();
    return (processor != null ? processor.processUrl(exchange, result) : result);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 62 with ServerHttpRequest

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

the class ConsumesRequestCondition method getMatchingCondition.

/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#includes(MediaType)}.
 * @param exchange the current exchange
 * @return the same instance if the condition contains no expressions;
 * or a new condition with matching expressions only;
 * or {@code null} if no expressions match.
 */
@Override
public ConsumesRequestCondition getMatchingCondition(ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    if (CorsUtils.isPreFlightRequest(request)) {
        return EMPTY_CONDITION;
    }
    if (isEmpty()) {
        return this;
    }
    if (!hasBody(request) && !this.bodyRequired) {
        return EMPTY_CONDITION;
    }
    List<ConsumeMediaTypeExpression> result = getMatchingExpressions(exchange);
    return !CollectionUtils.isEmpty(result) ? new ConsumesRequestCondition(result) : null;
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 63 with ServerHttpRequest

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

the class AbstractHandlerMapping method getHandler.

@Override
public Mono<Object> getHandler(ServerWebExchange exchange) {
    return getHandlerInternal(exchange).map(handler -> {
        if (logger.isDebugEnabled()) {
            logger.debug(exchange.getLogPrefix() + "Mapped to " + handler);
        }
        ServerHttpRequest request = exchange.getRequest();
        if (hasCorsConfigurationSource(handler) || CorsUtils.isPreFlightRequest(request)) {
            CorsConfiguration config = (this.corsConfigurationSource != null ? this.corsConfigurationSource.getCorsConfiguration(exchange) : null);
            CorsConfiguration handlerConfig = getCorsConfiguration(handler, exchange);
            config = (config != null ? config.combine(handlerConfig) : handlerConfig);
            if (config != null) {
                config.validateAllowCredentials();
            }
            if (!this.corsProcessor.process(config, exchange) || CorsUtils.isPreFlightRequest(request)) {
                return NO_OP_HANDLER;
            }
        }
        return handler;
    });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 64 with ServerHttpRequest

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

the class DefaultServerRequestBuilder method build.

@Override
public ServerRequest build() {
    ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(), this.method, this.uri, this.headers, this.cookies, this.body);
    ServerWebExchange exchange = new DelegatingServerWebExchange(serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
    return new DefaultServerRequest(exchange, this.messageReaders);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 65 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class ForwardedHeadersFilter method filter.

@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    HttpHeaders original = input;
    HttpHeaders updated = new HttpHeaders();
    // copy all headers except Forwarded
    original.entrySet().stream().filter(entry -> !entry.getKey().toLowerCase().equalsIgnoreCase(FORWARDED_HEADER)).forEach(entry -> updated.addAll(entry.getKey(), entry.getValue()));
    List<Forwarded> forwardeds = parse(original.get(FORWARDED_HEADER));
    for (Forwarded f : forwardeds) {
        updated.add(FORWARDED_HEADER, f.toString());
    }
    // TODO: add new forwarded
    URI uri = request.getURI();
    String host = original.getFirst(HttpHeaders.HOST);
    Forwarded forwarded = new Forwarded().put("host", host).put("proto", uri.getScheme());
    InetSocketAddress remoteAddress = request.getRemoteAddress();
    if (remoteAddress != null) {
        String forValue = remoteAddress.getAddress().getHostAddress();
        int port = remoteAddress.getPort();
        if (port >= 0) {
            forValue = forValue + ":" + port;
        }
        forwarded.put("for", forValue);
    }
    // TODO: support by?
    updated.add(FORWARDED_HEADER, forwarded.toHeaderValue());
    return updated;
}
Also used : Ordered(org.springframework.core.Ordered) HttpHeaders(org.springframework.http.HttpHeaders) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) ObjectUtils(org.springframework.util.ObjectUtils) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) Map(java.util.Map) URI(java.net.URI) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) StringUtils(org.springframework.util.StringUtils) HttpHeaders(org.springframework.http.HttpHeaders) InetSocketAddress(java.net.InetSocketAddress) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) URI(java.net.URI)

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