All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.gateway.routes.GatewayExchange Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.gateway.routes;

import com.spring.boxes.dollar.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;

public class GatewayExchange {

    /**
     * 更新路由请求地址
     * @param exchange exchange对象
     * @param targetUri 目标地址
     * @param targetPath 目标路径
     * @return 新的exchange对象
     */
    public static ServerWebExchange updateWebExchange(ServerWebExchange exchange, String targetUri, String targetPath) {
        if (StringUtils.isBlank(targetUri)) {
            return exchange;
        }
        URI oldUri = exchange.getRequest().getURI();
        URI newUri = UriComponentsBuilder.fromUriString(targetUri).build().toUri();
        boolean encoded = containsEncodedParts(oldUri);
        URI targetURI = UriComponentsBuilder.fromUri(oldUri)
                .scheme(newUri.getScheme()).port(newUri.getPort()).host(newUri.getHost())
                .replacePath(targetPath).build(encoded).toUri();
        ServerHttpRequest newRequest = exchange.getRequest().mutate().uri(targetURI).build();
        return exchange.mutate().request(newRequest).build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy