Skip to content

Instantly share code, notes, and snippets.

@spencergibb
Created December 11, 2018 02:06
Show Gist options
  • Save spencergibb/56283e9b371a7f0a65df0ebfd153e946 to your computer and use it in GitHub Desktop.
Save spencergibb/56283e9b371a7f0a65df0ebfd153e946 to your computer and use it in GitHub Desktop.
package com.example.demogatewaypathtoparam;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.pattern.PathPattern;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
@SpringBootApplication
public class GatewayApplication {
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_to_query", r -> r.path("/{ticker}")
.filters(f -> f.setPath("/get")
.filter(pathToParamGatewayFilterFactory().apply("")))
.uri("http://httpbin.org"))
.build();
}
@Bean
public PathToParamGatewayFilterFactory pathToParamGatewayFilterFactory() {
return new PathToParamGatewayFilterFactory();
}
static class PathToParamGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
PathPattern.PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
Map<String, String> uriVariables;
if (variables != null) {
uriVariables = variables.getUriVariables();
} else {
uriVariables = Collections.emptyMap();
}
if (uriVariables.containsKey("ticker")) {
String ticker = uriVariables.get("ticker");
URI uri = exchange.getRequest().getURI();
URI newUri = UriComponentsBuilder.fromUri(uri)
.replaceQuery("ticker="+ticker)
.build(true)
.toUri();
ServerHttpRequest request = exchange.getRequest().mutate().uri(newUri).build();
return chain.filter(exchange.mutate().request(request).build());
}
return chain.filter(exchange);
};
}
}
public static void main(String[] args) {
SpringApplication.run(DemogatewaypathtoparamApplication.class, args);
}
}
$ http :8080/PVTL
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Length: 427
Content-Type: application/json
Date: Tue, 11 Dec 2018 02:05:15 GMT
Server: gunicorn/19.9.0
Via: 1.1 vegur
{
"args": {
"ticker": "PVTL"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Forwarded": "proto=http;host=\"localhost:8080\";for=\"127.0.0.1:33410\"",
"Host": "httpbin.org",
"User-Agent": "HTTPie/0.9.8",
"X-Forwarded-Host": "localhost:8080"
},
"origin": "127.0.0.1, 107.5.46.99",
"url": "http://localhost:8080/get?ticker=PVTL"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment