Search in sources :

Example 1 with GATEWAY_REQUEST_URL_ATTR

use of org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR in project spring-cloud-gateway by spring-cloud.

the class NettyRoutingFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    String scheme = requestUrl.getScheme();
    if (isAlreadyRouted(exchange) || (!"http".equals(scheme) && !"https".equals(scheme))) {
        return chain.filter(exchange);
    }
    setAlreadyRouted(exchange);
    ServerHttpRequest request = exchange.getRequest();
    final HttpMethod method = HttpMethod.valueOf(request.getMethod().toString());
    final String url = requestUrl.toString();
    HttpHeaders filtered = filterRequest(this.headersFilters.getIfAvailable(), exchange);
    final DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
    filtered.forEach(httpHeaders::set);
    String transferEncoding = request.getHeaders().getFirst(HttpHeaders.TRANSFER_ENCODING);
    boolean chunkedTransfer = "chunked".equalsIgnoreCase(transferEncoding);
    boolean preserveHost = exchange.getAttributeOrDefault(PRESERVE_HOST_HEADER_ATTRIBUTE, false);
    return this.httpClient.request(method, url, req -> {
        final HttpClientRequest proxyRequest = req.options(NettyPipeline.SendOptions::flushOnEach).headers(httpHeaders).chunkedTransfer(chunkedTransfer).failOnServerError(false).failOnClientError(false);
        if (preserveHost) {
            String host = request.getHeaders().getFirst(HttpHeaders.HOST);
            proxyRequest.header(HttpHeaders.HOST, host);
        }
        return // I shouldn't need this
        proxyRequest.sendHeaders().send(request.getBody().map(dataBuffer -> ((NettyDataBuffer) dataBuffer).getNativeBuffer()));
    }).doOnNext(res -> {
        ServerHttpResponse response = exchange.getResponse();
        // put headers and status so filters can modify the response
        HttpHeaders headers = new HttpHeaders();
        res.responseHeaders().forEach(entry -> headers.add(entry.getKey(), entry.getValue()));
        HttpHeaders filteredResponseHeaders = HttpHeadersFilter.filter(this.headersFilters.getIfAvailable(), headers, exchange, Type.RESPONSE);
        response.getHeaders().putAll(filteredResponseHeaders);
        response.setStatusCode(HttpStatus.valueOf(res.status().code()));
        // Defer committing the response until all route filters have run
        // Put client response as ServerWebExchange attribute and write response later NettyWriteResponseFilter
        exchange.getAttributes().put(CLIENT_RESPONSE_ATTR, res);
    }).then(chain.filter(exchange));
}
Also used : HttpHeadersFilter.filterRequest(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.filterRequest) CLIENT_RESPONSE_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR) Ordered(org.springframework.core.Ordered) PRESERVE_HOST_HEADER_ATTRIBUTE(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.PRESERVE_HOST_HEADER_ATTRIBUTE) ServerWebExchangeUtils.setAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Type(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.Type) HttpHeadersFilter(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter) HttpMethod(io.netty.handler.codec.http.HttpMethod) Mono(reactor.core.publisher.Mono) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) ObjectProvider(org.springframework.beans.factory.ObjectProvider) NettyPipeline(reactor.ipc.netty.NettyPipeline) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpClientRequest(reactor.ipc.netty.http.client.HttpClientRequest) URI(java.net.URI) ServerWebExchangeUtils.isAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) URI(java.net.URI) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpClientRequest(reactor.ipc.netty.http.client.HttpClientRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 2 with GATEWAY_REQUEST_URL_ATTR

use of org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR in project spring-cloud-gateway by spring-cloud.

the class WebsocketRoutingFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    changeSchemeIfIsWebSocketUpgrade(exchange);
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    String scheme = requestUrl.getScheme();
    if (isAlreadyRouted(exchange) || (!"ws".equals(scheme) && !"wss".equals(scheme))) {
        return chain.filter(exchange);
    }
    setAlreadyRouted(exchange);
    HttpHeaders headers = exchange.getRequest().getHeaders();
    HttpHeaders filtered = filterRequest(getHeadersFilters(), exchange);
    List<String> protocols = headers.get(SEC_WEBSOCKET_PROTOCOL);
    if (protocols != null) {
        protocols = headers.get(SEC_WEBSOCKET_PROTOCOL).stream().flatMap(header -> Arrays.stream(commaDelimitedListToStringArray(header))).map(String::trim).collect(Collectors.toList());
    }
    return this.webSocketService.handleRequest(exchange, new ProxyWebSocketHandler(requestUrl, this.webSocketClient, filtered, protocols));
}
Also used : HttpHeadersFilter.filterRequest(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.filterRequest) Ordered(org.springframework.core.Ordered) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServerWebExchangeUtils.setAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted) Arrays(java.util.Arrays) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) HttpHeadersFilter(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) StringUtils.commaDelimitedListToStringArray(org.springframework.util.StringUtils.commaDelimitedListToStringArray) ObjectProvider(org.springframework.beans.factory.ObjectProvider) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) WebSocketService(org.springframework.web.reactive.socket.server.WebSocketService) Collectors(java.util.stream.Collectors) List(java.util.List) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ServerWebExchangeUtils.isAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted) Collections(java.util.Collections) HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI)

Aggregations

URI (java.net.URI)2 List (java.util.List)2 ObjectProvider (org.springframework.beans.factory.ObjectProvider)2 HttpHeadersFilter (org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter)2 HttpHeadersFilter.filterRequest (org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.filterRequest)2 GATEWAY_REQUEST_URL_ATTR (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR)2 ServerWebExchangeUtils.isAlreadyRouted (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted)2 ServerWebExchangeUtils.setAlreadyRouted (org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted)2 Ordered (org.springframework.core.Ordered)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 Mono (reactor.core.publisher.Mono)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collectors (java.util.stream.Collectors)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1