@Override public Map<String, Object> getAttributes() { return this.delegate.getAttributes(); }
@Override public Map<String, Object> attributes() { return this.exchange.getAttributes(); }
/** * Return the request attribute value, or a default, fallback value. * @param name the attribute name * @param defaultValue a default value to return instead * @param <T> the attribute type * @return the attribute value */ @SuppressWarnings("unchecked") default <T> T getAttributeOrDefault(String name, T defaultValue) { return (T) getAttributes().getOrDefault(name, defaultValue); }
private static void addAttributes(ServerWebExchange exchange, ServerRequest request) { Map<String, Object> attributes = exchange.getAttributes(); attributes.put(REQUEST_ATTRIBUTE, request); }
/** * Return the request attribute value if present. * @param name the attribute name * @param <T> the attribute type * @return the attribute value */ @SuppressWarnings("unchecked") @Nullable default <T> T getAttribute(String name) { return (T) getAttributes().get(name); }
private Object handleMatch(Object handler, PathPattern bestMatch, PathContainer pathWithinMapping, ServerWebExchange exchange) { // Bean name or resolved handler? if (handler instanceof String) { String handlerName = (String) handler; handler = obtainApplicationContext().getBean(handlerName); } validateHandler(handler, exchange); exchange.getAttributes().put(BEST_MATCHING_HANDLER_ATTRIBUTE, handler); exchange.getAttributes().put(BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatch); exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping); return handler; }
@Override public Map<String, Object> getAttributes() { return getDelegate().getAttributes(); }
@SuppressWarnings("unchecked") private Map<String, String> getUriTemplateVariables(ServerWebExchange exchange) { return (Map<String, String>) exchange.getAttributes() .get(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); }
@SuppressWarnings("unchecked") private MultiValueMap<String, String> getMatrixVariables(ServerWebExchange exchange, String uriVarName) { return ((Map<String, MultiValueMap<String, String>>) exchange.getAttributes() .get(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE)).get(uriVarName); }
@Override protected Mono<?> getHandlerInternal(ServerWebExchange exchange) { if (this.routerFunction != null) { ServerRequest request = ServerRequest.create(exchange, this.messageReaders); return this.routerFunction.route(request) .doOnNext(handler -> setAttributes(exchange.getAttributes(), request, handler)); } else { return Mono.empty(); } }
private void setPathWithinHandlerMapping(ServerWebExchange exchange, String path) { exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PathContainer.parsePath(path)); }
@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"); } }) ); }
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); }; }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { String name = "test-attribute"; String value = exchange.getAttributeOrDefault(name, ""); exchange.getAttributes().put(name, value + ":" + this.name); return chain.filter(exchange); } }
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { return WebHttpHandlerBuilder.webHandler(exchange -> { DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); String text = exchange.getAttributes().toString(); DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8)); return exchange.getResponse().writeWith(Mono.just(buffer)); }); } }
@Test public void getHandlerProducibleMediaTypesAttribute() { ServerWebExchange exchange = MockServerWebExchange.from(get("/content").accept(MediaType.APPLICATION_XML)); this.handlerMapping.getHandler(exchange).block(); String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE; assertEquals(Collections.singleton(MediaType.APPLICATION_XML), exchange.getAttributes().get(name)); exchange = MockServerWebExchange.from(get("/content").accept(MediaType.APPLICATION_JSON)); this.handlerMapping.getHandler(exchange).block(); assertNull("Negated expression shouldn't be listed as producible type", exchange.getAttributes().get(name)); }
@Test public void handleMatchBestMatchingPatternAttribute() { RequestMappingInfo key = paths("/{path1}/2", "/**").build(); ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2")); this.handlerMapping.handleMatch(key, handlerMethod, exchange); PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE); assertEquals("/{path1}/2", bestMatch.getPatternString()); HandlerMethod mapped = (HandlerMethod) exchange.getAttributes().get(BEST_MATCHING_HANDLER_ATTRIBUTE); assertSame(handlerMethod, mapped); }
@Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() { RequestMappingInfo key = paths("/{group}/{identifier}").build(); URI url = URI.create("/group/a%2Fb"); ServerWebExchange exchange = MockServerWebExchange.from(method(HttpMethod.GET, url)); this.handlerMapping.handleMatch(key, handlerMethod, exchange); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; @SuppressWarnings("unchecked") Map<String, String> uriVariables = (Map<String, String>) exchange.getAttributes().get(name); assertNotNull(uriVariables); assertEquals("group", uriVariables.get("group")); assertEquals("a/b", uriVariables.get("identifier")); }
@Test @SuppressWarnings("unchecked") public void handleMatchUriTemplateVariables() { ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2")); RequestMappingInfo key = paths("/{path1}/{path2}").build(); this.handlerMapping.handleMatch(key, handlerMethod, exchange); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) exchange.getAttributes().get(name); assertNotNull(uriVariables); assertEquals("1", uriVariables.get("path1")); assertEquals("2", uriVariables.get("path2")); }
@Test public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() { RequestMappingInfo key = paths().build(); ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2")); this.handlerMapping.handleMatch(key, handlerMethod, exchange); PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE); assertEquals("/1/2", bestMatch.getPatternString()); }