List of usage examples for org.springframework.web.server WebFilterChain filter
Mono<Void> filter(ServerWebExchange exchange);
From source file:org.springframework.cloud.gateway.filter.LoadBalancerClientFilter.java
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { URI url = getAttribute(exchange, GATEWAY_REQUEST_URL_ATTR, URI.class); if (url == null || !url.getScheme().equals("lb")) { return chain.filter(exchange); }// w w w. j ava 2 s.c om log.trace("LoadBalancerClientFilter url before: " + url); final ServiceInstance instance = loadBalancer.choose(url.getHost()); if (instance == null) { throw new NotFoundException(""); } URI requestUrl = UriComponentsBuilder.fromUri(url).scheme(instance.isSecure() ? "https" : "http") //TODO: support websockets .host(instance.getHost()).port(instance.getPort()).build(true).toUri(); log.trace("LoadBalancerClientFilter url chosen: " + requestUrl); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl); return chain.filter(exchange); }
From source file:org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.java
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { Route route = getAttribute(exchange, GATEWAY_ROUTE_ATTR, Route.class); if (route == null) { return chain.filter(exchange); }/*from www . ja v a 2s.c om*/ log.info("RouteToRequestUrlFilter start"); URI requestUrl = UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).uri(route.getUri()).build(true) .toUri(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl); return chain.filter(exchange); }
From source file:org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.java
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { Map<String, String> weights = getWeights(exchange); for (String group : groupWeights.keySet()) { GroupWeightConfig config = groupWeights.get(group); if (config == null) { if (log.isDebugEnabled()) { log.debug("No GroupWeightConfig found for group: " + group); }/* www . j ava2 s . com*/ continue; // nothing we can do, but this is odd } double r = this.random.nextDouble(); List<Double> ranges = config.ranges; if (log.isTraceEnabled()) { log.trace("Weight for group: " + group + ", ranges: " + ranges + ", r: " + r); } for (int i = 0; i < ranges.size() - 1; i++) { if (r >= ranges.get(i) && r < ranges.get(i + 1)) { String routeId = config.rangeIndexes.get(i); weights.put(group, routeId); break; } } } if (log.isTraceEnabled()) { log.trace("Weights attr: " + weights); } return chain.filter(exchange); }
From source file:org.springframework.cloud.gateway.filter.WriteResponseFilter.java
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added // until the WebHandler is run return chain.filter(exchange).then(() -> { HttpClientResponse clientResponse = getAttribute(exchange, CLIENT_RESPONSE_ATTR, HttpClientResponse.class); if (clientResponse == null) { return Mono.empty(); }/* w w w . ja va2 s . c o m*/ log.trace("WriteResponseFilter start"); ServerHttpResponse response = exchange.getResponse(); NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory(); //TODO: what if it's not netty final Flux<NettyDataBuffer> body = clientResponse.receive().retain() //TODO: needed? .map(factory::wrap); return response.writeWith(body); }); }
From source file:org.springframework.cloud.sleuth.instrument.web.TraceWebFilter.java
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { if (tracer().currentSpan() != null) { // clear any previous trace tracer().withSpanInScope(null);/*from www .j av a 2 s. c o m*/ } String uri = exchange.getRequest().getPath().pathWithinApplication().value(); if (log.isDebugEnabled()) { log.debug("Received a request to uri [" + uri + "]"); } Span spanFromAttribute = getSpanFromAttribute(exchange); final String CONTEXT_ERROR = "sleuth.webfilter.context.error"; return chain.filter(exchange).compose(f -> f.then(Mono.subscriberContext()) .onErrorResume(t -> Mono.subscriberContext().map(c -> c.put(CONTEXT_ERROR, t))).flatMap(c -> { // reactivate span from context Span span = spanFromContext(c); Mono<Void> continuation; Throwable t = null; if (c.hasKey(CONTEXT_ERROR)) { t = c.get(CONTEXT_ERROR); continuation = Mono.error(t); } else { continuation = Mono.empty(); } String httpRoute = null; Object attribute = exchange.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE); if (attribute instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) attribute; addClassMethodTag(handlerMethod, span); addClassNameTag(handlerMethod, span); Object pattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); httpRoute = pattern != null ? pattern.toString() : ""; } addResponseTagsForSpanWithoutParent(exchange, exchange.getResponse(), span); DecoratedServerHttpResponse delegate = new DecoratedServerHttpResponse(exchange.getResponse(), exchange.getRequest().getMethodValue(), httpRoute); handler().handleSend(delegate, t, span); if (log.isDebugEnabled()) { log.debug("Handled send of " + span); } return continuation; }).subscriberContext(c -> { Span span; if (c.hasKey(Span.class)) { Span parent = c.get(Span.class); span = tracer().nextSpan(TraceContextOrSamplingFlags.create(parent.context())).start(); if (log.isDebugEnabled()) { log.debug("Found span in reactor context" + span); } } else { if (spanFromAttribute != null) { span = spanFromAttribute; if (log.isDebugEnabled()) { log.debug("Found span in attribute " + span); } } else { span = handler().handleReceive(extractor(), exchange.getRequest().getHeaders(), exchange.getRequest()); if (log.isDebugEnabled()) { log.debug("Handled receive of span " + span); } } exchange.getAttributes().put(TRACE_REQUEST_ATTR, span); } return c.put(Span.class, span); })); }