Adds Routes, WebFilterFactories and GatewayRequestPredicates

This commit is contained in:
Spencer Gibb
2017-03-17 23:47:30 -06:00
parent d0f97ca8ed
commit 3aff230f6a
6 changed files with 243 additions and 17 deletions

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.filter.factory;
import org.springframework.tuple.Tuple;
import org.springframework.web.server.WebFilter;
import static org.springframework.cloud.gateway.filter.factory.WebFilterFactory.NAME_KEY;
import static org.springframework.cloud.gateway.filter.factory.WebFilterFactory.VALUE_KEY;
import static org.springframework.tuple.TupleBuilder.tuple;
/**
* @author Spencer Gibb
*/
public class WebFilterFactories {
//TODO: add support for AddRequestHeaderWebFilterFactory
//TODO: add support for AddRequestParameterWebFilterFactory
public static WebFilter addResponseHeader(String headerName, String headerValue) {
Tuple args = tuple().of(NAME_KEY, headerName, VALUE_KEY, headerValue);
return new AddResponseHeaderWebFilterFactory().apply(args);
}
//TODO: add support for HystrixWebFilterFactory
//TODO: add support for PrefixPathWebFilterFactory
//TODO: add support for RedirectToWebFilterFactory
//TODO: add support for RemoveNonProxyHeadersWebFilterFactory
//TODO: add support for RemoveRequestHeaderWebFilterFactory
//TODO: add support for RemoveResponseHeaderWebFilterFactory
//TODO: add support for RewritePathWebFilterFactory
//TODO: add support for SecureHeadersProperties
//TODO: add support for SecureHeadersWebFilterFactory
//TODO: add support for SetPathWebFilterFactory
//TODO: add support for SetResponseHeaderWebFilterFactory
//TODO: add support for SetStatusWebFilterFactory
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.web.reactive.function.server.RequestPredicate;
import static org.springframework.cloud.gateway.handler.predicate.PathRequestPredicateFactory.PATTERN_KEY;
import static org.springframework.tuple.TupleBuilder.tuple;
/**
* @author Spencer Gibb
*/
public class GatewayRequestPredicates {
//TODO: add support for AfterRequestPredicateFactory
//TODO: add support for BeforeRequestPredicateFactory
//TODO: add support for BetweenRequestPredicateFactory
//TODO: add support for CookieRequestPredicateFactory
//TODO: add support for GatewayRequestPredicates
//TODO: add support for HeaderRequestPredicateFactory
//TODO: add support for HostRequestPredicateFactory
//TODO: add support for MethodRequestPredicateFactory
public static RequestPredicate path(String pattern) {
return new PathRequestPredicateFactory().apply(tuple().of(PATTERN_KEY, pattern));
}
//TODO: add support for PredicateDefinition
//TODO: add support for QueryRequestPredicateFactory
//TODO: add support for RemoteAddrRequestPredicateFactory
}

View File

@@ -99,8 +99,8 @@ public class Route {
return this;
}
public Builder addAll(Collection<WebFilter> webFilter) {
this.webFilters.addAll(webFilter);
public Builder addAll(Collection<WebFilter> webFilters) {
this.webFilters.addAll(webFilters);
return this;
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.route;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Flux;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author Spencer Gibb
*/
public class Routes {
public static LocatorBuilder locator() {
return new LocatorBuilder();
}
public static class LocatorBuilder {
private List<Route> routes = new ArrayList<>();
public RouteBuilder route(String id) {
return new RouteBuilder(this).id(id);
}
private void add(Route route) {
this.routes.add(route);
}
public RouteLocator build() {
return () -> Flux.fromIterable(this.routes);
}
}
public static class RouteBuilder {
private final Route.Builder builder = Route.builder();
private final LocatorBuilder locatorBuilder;
private RouteBuilder(LocatorBuilder locatorBuilder) {
this.locatorBuilder = locatorBuilder;
}
public RouteBuilder id(String id) {
this.builder.id(id);
return this;
}
public RouteBuilder uri(String uri) {
this.builder.uri(uri);
return this;
}
public RouteBuilder uri(URI uri) {
this.builder.uri(uri);
return this;
}
public RouteBuilder predicate(RequestPredicate predicate) {
this.builder.requestPredicate(predicate);
return this;
}
//TODO: maybe add WebFilterBuilder so no need to import WebFilterFactories?
public RouteBuilder webFilters(List<WebFilter> webFilters) {
this.builder.webFilters(webFilters);
return this;
}
public RouteBuilder add(WebFilter webFilter) {
this.builder.add(webFilter);
return this;
}
public RouteBuilder addAll(Collection<WebFilter> webFilters) {
this.builder.addAll(webFilters);
return this;
}
public LocatorBuilder and() {
Route route = this.builder.build();
this.locatorBuilder.add(route);
return this.locatorBuilder;
}
}
}

View File

@@ -21,14 +21,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.gateway.EnableGateway;
import org.springframework.cloud.gateway.filter.factory.AddResponseHeaderWebFilterFactory;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.Routes;
import org.springframework.context.annotation.Bean;
import org.springframework.tuple.TupleBuilder;
import org.springframework.web.reactive.function.server.RequestPredicates;
import reactor.core.publisher.Flux;
import static org.springframework.cloud.gateway.filter.factory.WebFilterFactories.addResponseHeader;
import static org.springframework.cloud.gateway.handler.predicate.GatewayRequestPredicates.path;
// import static org.springframework.web.reactive.function.server.RequestPredicates.path;
/**
* @author Spencer Gibb
@@ -40,16 +39,18 @@ public class GatewaySampleApplication {
@Bean
public RouteLocator customRouteLocator() {
return () -> {
Route route = Route.builder()
.id("test")
return Routes.locator()
.route("test")
.uri("http://httpbin.org:80")
.requestPredicate(RequestPredicates.path("/image/png"))
.add(new AddResponseHeaderWebFilterFactory()
.apply(TupleBuilder.tuple().of("name", "X-TestHeader", "value", "foobar")))
.build();
return Flux.just(route);
};
.predicate(path("/image/png"))
.add(addResponseHeader("X-TestHeader", "foobar"))
.and()
.route("test2")
.uri("http://httpbin.org:80")
.predicate(path("/image/webp"))
.add(addResponseHeader("X-AnotherHeader", "baz"))
.and()
.build();
}
public static void main(String[] args) {

View File

@@ -50,7 +50,6 @@ public class GatewaySampleApplicationTests {
@Before
public void setup() {
//TODO: how to set new ReactorClientHttpConnector()
baseUri = "http://localhost:" + port;
this.webClient = WebClient.create(baseUri);
}