@Test public void toStringFormat() { Config config = new Config(); config.setPrefix("myprefix"); GatewayFilter filter = new PrefixPathGatewayFilterFactory().apply(config); assertThat(filter.toString()).contains("myprefix"); }
@Override public GatewayFilter apply(Config config) { return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false); if (alreadyPrefixed) { return chain.filter(exchange); } exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate().path(newPath).build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); if (log.isTraceEnabled()) { log.trace("Prefixed URI with: " + config.prefix + " -> " + request.getURI()); } return chain.filter(exchange.mutate().request(request).build()); } @Override public String toString() { return filterToStringCreator(PrefixPathGatewayFilterFactory.this).append("prefix", config.getPrefix()) .toString(); } }; }
PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory<PrefixPathGatewayFilterFactory.Config> { @Override public GatewayFilter apply(Config config) { return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false); if (alreadyPrefixed) { return chain.filter(exchange); } exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate().path(newPath).build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); if (log.isTraceEnabled()) { log.trace("Prefixed URI with: " + config.prefix + " -> " + request.getURI()); } return chain.filter(exchange.mutate().request(request).build()); } @Override public String toString() { return filterToStringCreator(PrefixPathGatewayFilterFactory.this).append("prefix", config.getPrefix()) .toString(); } }; } }
PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory<PrefixPathGatewayFilterFactory.Config> { @Override public GatewayFilter apply(Config config) { return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false); if (alreadyPrefixed) { return chain.filter(exchange); } exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate().path(newPath).build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); if (log.isTraceEnabled()) { log.trace("Prefixed URI with: " + config.prefix + " -> " + request.getURI()); } return chain.filter(exchange.mutate().request(request).build()); } @Override public String toString() { return filterToStringCreator(PrefixPathGatewayFilterFactory.this).append("prefix", config.getPrefix()) .toString(); } }; } PrefixPathGatewayFilterFactory(); }
PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory<PrefixPathGatewayFilterFactory.Config> { @Override public GatewayFilter apply(Config config) { return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false); if (alreadyPrefixed) { return chain.filter(exchange); } exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate().path(newPath).build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); if (log.isTraceEnabled()) { log.trace("Prefixed URI with: " + config.prefix + " -> " + request.getURI()); } return chain.filter(exchange.mutate().request(request).build()); } @Override public String toString() { return filterToStringCreator(PrefixPathGatewayFilterFactory.this).append("prefix", config.getPrefix()) .toString(); } }; } PrefixPathGatewayFilterFactory(); @Override List<String> shortcutFieldOrder(); @Override GatewayFilter apply(Config config); }
PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory<PrefixPathGatewayFilterFactory.Config> { @Override public GatewayFilter apply(Config config) { return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false); if (alreadyPrefixed) { return chain.filter(exchange); } exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true); ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String newPath = config.prefix + req.getURI().getRawPath(); ServerHttpRequest request = req.mutate().path(newPath).build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); if (log.isTraceEnabled()) { log.trace("Prefixed URI with: " + config.prefix + " -> " + request.getURI()); } return chain.filter(exchange.mutate().request(request).build()); } @Override public String toString() { return filterToStringCreator(PrefixPathGatewayFilterFactory.this).append("prefix", config.getPrefix()) .toString(); } }; } PrefixPathGatewayFilterFactory(); @Override List<String> shortcutFieldOrder(); @Override GatewayFilter apply(Config config); static final String PREFIX_KEY; }