Browse Source

use URI rather than string

pull/41/head
Spencer Gibb 8 years ago
parent
commit
bffabd6c6b
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 29
      src/main/java/org/springframework/cloud/gateway/GatewayProperties.java
  2. 3
      src/main/java/org/springframework/cloud/gateway/GatewayWebHandler.java
  3. 9
      src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java
  4. 3
      src/main/resources/application.yml

29
src/main/java/org/springframework/cloud/gateway/GatewayProperties.java

@ -28,7 +28,8 @@ public class GatewayProperties { @@ -28,7 +28,8 @@ public class GatewayProperties {
public static class Route {
private String id;
private String path;
private String url;
private String host;
private String port;
public String getPath() {
return this.path;
@ -38,12 +39,30 @@ public class GatewayProperties { @@ -38,12 +39,30 @@ public class GatewayProperties {
this.path = path;
}
public String getUrl() {
return this.url;
public String getHost() {
return this.host;
}
public void setUrl(String url) {
this.url = url;
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
@Override
public String toString() {
return "Route{" +
"id='" + id + '\'' +
", path='" + path + '\'' +
", host='" + host + '\'' +
", port='" + port + '\'' +
'}';
}
}
}

3
src/main/java/org/springframework/cloud/gateway/GatewayWebHandler.java

@ -10,6 +10,7 @@ import org.springframework.web.server.WebHandler; @@ -10,6 +10,7 @@ import org.springframework.web.server.WebHandler;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.Optional;
/**
@ -27,7 +28,7 @@ public class GatewayWebHandler implements WebHandler { @@ -27,7 +28,7 @@ public class GatewayWebHandler implements WebHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange) {
Optional<String> requestUrl = exchange.getAttribute("requestUrl");
Optional<URI> requestUrl = exchange.getAttribute("requestUri");
ServerHttpRequest request = exchange.getRequest();
ClientRequest<Void> clientRequest = ClientRequest
.method(request.getMethod(), requestUrl.get())

9
src/main/java/org/springframework/cloud/gateway/filters/FindRouteFilter.java

@ -10,6 +10,7 @@ import org.springframework.util.AntPathMatcher; @@ -10,6 +10,7 @@ import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import java.net.URI;
@ -38,8 +39,12 @@ public class FindRouteFilter implements WebFilter { @@ -38,8 +39,12 @@ public class FindRouteFilter implements WebFilter {
String path = uri.getPath();
for (Route route : this.properties.getRoutes().values()) {
if (this.matcher.match(route.getPath(), path)) {
String url = route.getUrl() + path;
exchange.getAttributes().put("requestUrl", url);
URI requestUri = UriComponentsBuilder.fromHttpRequest(request)
.host(route.getHost())
.port(route.getPort())
.build(true)
.toUri();
exchange.getAttributes().put("requestUri", requestUri);
return chain.filter(exchange);
}
}

3
src/main/resources/application.yml

@ -7,4 +7,5 @@ spring: @@ -7,4 +7,5 @@ spring:
routes:
test1:
path: /**
url: http://httpbin.org
host: httpbin.org
port: 80

Loading…
Cancel
Save