/** * Return the request attribute value or if not present raise an * {@link IllegalArgumentException}. * @param name the attribute name * @param <T> the attribute type * @return the attribute value */ @SuppressWarnings("unchecked") default <T> T getRequiredAttribute(String name) { T value = getAttribute(name); Assert.notNull(value, () -> "Required attribute '" + name + "' is missing"); return value; }
/* for testing */ static Map<String, String> getWeights(ServerWebExchange exchange) { Map<String, String> weights = exchange.getAttribute(WEIGHT_ATTR); if (weights == null) { weights = new ConcurrentHashMap<>(); exchange.getAttributes().put(WEIGHT_ATTR, weights); } return weights; }
private WebFilter createFilter(String name) { return (exchange, chain) -> { String value = exchange.getAttribute(ATTRIBUTE); value = (value != null ? value + "::" + name : name); exchange.getAttributes().put(ATTRIBUTE, value); return chain.filter(exchange); }; }
@SuppressWarnings("unchecked") private List<MediaType> getProducibleTypes(ServerWebExchange exchange, Supplier<List<MediaType>> producibleTypesSupplier) { Set<MediaType> mediaTypes = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); return (mediaTypes != null ? new ArrayList<>(mediaTypes) : producibleTypesSupplier.get()); }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { exchange.getAttributes().put(REQUEST_TIME_BEGIN, System.currentTimeMillis()); return chain.filter(exchange).then( Mono.fromRunnable(() -> { Long startTime = exchange.getAttribute(REQUEST_TIME_BEGIN); if (startTime != null) { log.info(exchange.getRequest().getURI().getRawPath() + ": " + (System.currentTimeMillis() - startTime) + "ms"); } }) ); }
/** * Retrieve the model object for the given model name, either from the model * or from the request attributes. * @param modelName the name of the model object * @return the model object */ @SuppressWarnings("unchecked") @Nullable protected <T> T getModelObject(String modelName) { T modelObject = (T) this.model.get(modelName); if (modelObject == null) { modelObject = this.exchange.getAttribute(modelName); } return modelObject; }
@Override public GatewayFilter apply(Config config) { return (exchange, chain) -> { exchange.getAttributes().put(REQUEST_TIME_BEGIN, System.currentTimeMillis()); return chain.filter(exchange).then( Mono.fromRunnable(() -> { Long startTime = exchange.getAttribute(REQUEST_TIME_BEGIN); if (startTime != null) { StringBuilder sb = new StringBuilder(exchange.getRequest().getURI().getRawPath()) .append(": ") .append(System.currentTimeMillis() - startTime) .append("ms"); if (config.isWithParams()) { sb.append(" params:").append(exchange.getRequest().getQueryParams()); } log.info(sb.toString()); } }) ); }; }
private boolean spanWithoutParent(ServerWebExchange exchange) { return exchange.getAttribute(TRACE_SPAN_WITHOUT_PARENT) != null; }
@Override public GatewayFilter apply(Config config) { return (exchange, chain) -> { exchange.getAttributes().put(REQUEST_TIME_BEGIN, System.currentTimeMillis()); return chain.filter(exchange).then( Mono.fromRunnable(() -> { Long startTime = exchange.getAttribute(REQUEST_TIME_BEGIN); if (startTime != null) { StringBuilder sb = new StringBuilder(exchange.getRequest().getURI().getRawPath()) .append(": ") .append(System.currentTimeMillis() - startTime) .append("ms"); if (config.isWithParams()) { sb.append(" params:").append(exchange.getRequest().getQueryParams()); } log.info(sb.toString()); } }) ); }; }
/** * Return the request attribute value or if not present raise an * {@link IllegalArgumentException}. * @param name the attribute name * @param <T> the attribute type * @return the attribute value */ @SuppressWarnings("unchecked") default <T> T getRequiredAttribute(String name) { T value = getAttribute(name); Assert.notNull(value, () -> "Required attribute '" + name + "' is missing"); return value; }
protected ServiceInstance choose(ServerWebExchange exchange) { return loadBalancer.choose(((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost()); } @ResponseStatus(value = NOT_FOUND, reason = "The service was not found.")
exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) { Map<String, MultiValueMap<String, String>> pathParameters = exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE); if (CollectionUtils.isEmpty(pathParameters)) { return null;
@Override protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) { Object value = exchange.getAttribute(name); ReactiveAdapter toAdapter = getAdapterRegistry().getAdapter(parameter.getParameterType()); if (toAdapter != null) { if (value == null) { Assert.isTrue(toAdapter.supportsEmpty(), () -> "No request attribute '" + name + "' and target type " + parameter.getGenericParameterType() + " doesn't support empty values."); return toAdapter.fromPublisher(Mono.empty()); } if (parameter.getParameterType().isAssignableFrom(value.getClass())) { return value; } ReactiveAdapter fromAdapter = getAdapterRegistry().getAdapter(value.getClass()); Assert.isTrue(fromAdapter != null, () -> getClass().getSimpleName() + " doesn't support " + "reactive type wrapper: " + parameter.getGenericParameterType()); return toAdapter.fromPublisher(fromAdapter.toPublisher(value)); } return value; }
public boolean exceedsMaxIterations(ServerWebExchange exchange, RetryConfig retryConfig) { Integer iteration = exchange.getAttribute(RETRY_ITERATION_KEY); //TODO: deal with null iteration boolean exceeds = iteration != null && iteration >= retryConfig.getRetries(); trace("exceedsMaxIterations %b, iteration %d, configured retries %d", exceeds, iteration, retryConfig.getRetries()); return exceeds; }
protected ServiceInstance choose(ServerWebExchange exchange) { URI attribute = (URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); return loadBalancer.choose(attribute.getHost() + "<<>>" + attribute.getPort()); } };
MonoWebFilterTrace(Mono<? extends Void> source, ServerWebExchange exchange, TraceWebFilter parent) { super(source); this.tracer = parent.tracer(); this.extractor = parent.extractor(); this.handler = parent.handler(); this.exchange = exchange; this.attrSpan = exchange.getAttribute(TRACE_REQUEST_ATTR); }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added // until the WebHandler is run return chain.filter(exchange).then(Mono.defer(() -> { ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR); if (clientResponse == null) { return Mono.empty(); } log.trace("WebClientWriteResponseFilter start"); ServerHttpResponse response = exchange.getResponse(); return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())).log("webClient response"); })); }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); URI routeUri = route.getUri(); String scheme = routeUri.getScheme(); if (isAlreadyRouted(exchange) || !"forward".equals(scheme)) { return chain.filter(exchange); } exchange = exchange.mutate().request( exchange.getRequest().mutate().path(routeUri.getPath()).build()) .build(); return chain.filter(exchange); }
private void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) { MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, URI.create(url)).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); Object actual = handlerMapping.getHandler(exchange).block(); if (bean != null) { assertNotNull(actual); assertSame(bean, actual); //noinspection OptionalGetWithoutIsPresent PathContainer path = exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); assertNotNull(path); assertEquals(pathWithinMapping, path.value()); } else { assertNull(actual); } }