Example usage for org.springframework.web.server ServerWebExchange getRequiredAttribute

List of usage examples for org.springframework.web.server ServerWebExchange getRequiredAttribute

Introduction

In this page you can find the example usage for org.springframework.web.server ServerWebExchange getRequiredAttribute.

Prototype

@SuppressWarnings("unchecked")
default <T> T getRequiredAttribute(String name) 

Source Link

Document

Return the request attribute value or if not present raise an IllegalArgumentException .

Usage

From source file:org.springframework.cloud.gateway.filter.ForwardRoutingFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);

    String scheme = requestUrl.getScheme();
    if (isAlreadyRouted(exchange) || !"forward".equals(scheme)) {
        return chain.filter(exchange);
    }/*from   ww w  . j  a v a  2 s.  co  m*/
    setAlreadyRouted(exchange);

    // TODO: translate url?

    if (log.isTraceEnabled()) {
        log.trace("Forwarding to URI: " + requestUrl);
    }

    return this.getDispatcherHandler().handle(exchange);
}

From source file:org.springframework.cloud.gateway.filter.WebsocketRoutingFilter.java

@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);
    }/*from w w w.  j  a  v a2 s .  c  o  m*/
    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));
}

From source file:org.springframework.cloud.gateway.filter.WebsocketRoutingFilter.java

private void changeSchemeIfIsWebSocketUpgrade(ServerWebExchange exchange) {
    // Check the Upgrade
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    String scheme = requestUrl.getScheme().toLowerCase();
    String upgrade = exchange.getRequest().getHeaders().getUpgrade();
    // change the scheme if the socket client send a "http" or "https"
    if ("WebSocket".equalsIgnoreCase(upgrade) && ("http".equals(scheme) || "https".equals(scheme))) {
        String wsScheme = convertHttpToWs(scheme);
        URI wsRequestUrl = UriComponentsBuilder.fromUri(requestUrl).scheme(wsScheme).build().toUri();
        exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, wsRequestUrl);
        if (log.isTraceEnabled()) {
            log.trace("changeSchemeTo:[" + wsRequestUrl + "]");
        }//from   w w w .  j av  a 2s . c  o  m
    }
}

From source file:org.springframework.web.reactive.resource.ResourceWebHandler.java

protected Mono<Resource> getResource(ServerWebExchange exchange) {

    String name = HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE;
    PathContainer pathWithinHandler = exchange.getRequiredAttribute(name);
    String path = processPath(pathWithinHandler.value());
    if (!StringUtils.hasText(path) || isInvalidPath(path)) {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring invalid resource path [" + path + "]");
        }//from   ww w  .  jav a  2s.c  o  m
        return Mono.empty();
    }

    if (path.contains("%")) {
        try {
            // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars
            if (isInvalidPath(URLDecoder.decode(path, "UTF-8"))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Ignoring invalid resource path with escape sequences [" + path + "].");
                }
                return Mono.empty();
            }
        } catch (IllegalArgumentException ex) {
            // ignore
        } catch (UnsupportedEncodingException ex) {
            return Mono.error(Exceptions.propagate(ex));
        }
    }

    ResourceResolverChain resolveChain = createResolverChain();
    return resolveChain.resolveResource(exchange, path, getLocations()).flatMap(resource -> {
        ResourceTransformerChain transformerChain = createTransformerChain(resolveChain);
        return transformerChain.transform(exchange, resource);
    });
}