Skip to content

Commit

Permalink
Tighten Java DSL.
Browse files Browse the repository at this point in the history
Removes M6 deprecations.
  • Loading branch information
spencergibb committed Feb 12, 2018
1 parent 9d04c4e commit ee55ef5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
Expand All @@ -16,27 +16,18 @@

package org.springframework.cloud.gateway.route.builder;

import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.http.HttpStatus;
import org.springframework.tuple.Tuple;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;

import com.netflix.hystrix.HystrixObservableCommand;

import static org.springframework.cloud.gateway.route.builder.BooleanSpec.Operator.AND;
import static org.springframework.cloud.gateway.route.builder.BooleanSpec.Operator.NEGATE;
import static org.springframework.cloud.gateway.route.builder.BooleanSpec.Operator.OR;

public class BooleanSpec extends GatewayFilterSpec { //TODO after next release, extend UriSpec
public class BooleanSpec extends UriSpec {

enum Operator { AND, OR, NEGATE }

Expand Down Expand Up @@ -86,206 +77,8 @@ public BooleanSpec predicate(Predicate<ServerWebExchange> predicate) {
case NEGATE:
this.routeBuilder.negate();
}
return createBooleanSpec();
return new BooleanSpec(this.routeBuilder, this.builder);
}
}

//TODO: after next release remove all deprecated methods
@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec gatewayFilters(List<GatewayFilter> gatewayFilters) {
return super.gatewayFilters(gatewayFilters);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec add(GatewayFilter gatewayFilter) {
return super.add(gatewayFilter);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec filter(GatewayFilter gatewayFilter) {
return super.filter(gatewayFilter);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec filter(GatewayFilter gatewayFilter, int order) {
return super.filter(gatewayFilter, order);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec addAll(Collection<GatewayFilter> gatewayFilters) {
return super.addAll(gatewayFilters);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec addRequestHeader(String headerName, String headerValue) {
return super.addRequestHeader(headerName, headerValue);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec addRequestParameter(String param, String value) {
return super.addRequestParameter(param, value);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec addResponseHeader(String headerName, String headerValue) {
return super.addResponseHeader(headerName, headerValue);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec hystrix(String commandName) {
return super.hystrix(commandName);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec hystrix(HystrixObservableCommand.Setter setter) {
return super.hystrix(setter);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec hystrix(String commandName, URI fallbackUri) {
return super.hystrix(commandName, fallbackUri);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec hystrix(HystrixObservableCommand.Setter setter, URI fallbackUri) {
return super.hystrix(setter, fallbackUri);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec prefixPath(String prefix) {
return super.prefixPath(prefix);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec preserveHostHeader() {
return super.preserveHostHeader();
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec redirect(int status, URI url) {
return super.redirect(status, url);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec redirect(int status, String url) {
return super.redirect(status, url);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec redirect(String status, URI url) {
return super.redirect(status, url);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec redirect(String status, String url) {
return super.redirect(status, url);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec redirect(HttpStatus status, URL url) {
return super.redirect(status, url);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec removeNonProxyHeaders() {
return super.removeNonProxyHeaders();
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec removeNonProxyHeaders(String... headersToRemove) {
return super.removeNonProxyHeaders(headersToRemove);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec removeRequestHeader(String headerName) {
return super.removeRequestHeader(headerName);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec removeResponseHeader(String headerName) {
return super.removeResponseHeader(headerName);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec requestRateLimiter(Tuple args) {
return super.requestRateLimiter(args);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec rewritePath(String regex, String replacement) {
return super.rewritePath(regex, replacement);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec secureHeaders() {
return super.secureHeaders();
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setPath(String template) {
return super.setPath(template);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setRequestHeader(String headerName, String headerValue) {
return super.setRequestHeader(headerName, headerValue);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setResponseHeader(String headerName, String headerValue) {
return super.setResponseHeader(headerName, headerValue);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setStatus(int status) {
return super.setStatus(status);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setStatus(String status) {
return super.setStatus(status);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec setStatus(HttpStatus status) {
return super.setStatus(status);
}

@Override
@Deprecated /** @deprecated use {@link #filters(Function)} */
public GatewayFilterSpec saveSession() {
return super.saveSession();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
Expand Down Expand Up @@ -60,11 +60,15 @@ public GatewayFilterSpec(Route.Builder routeBuilder, RouteLocatorBuilder.Builder
super(routeBuilder, builder);
}

/** @deprecated use {@link #filters(Collection)} */
@Deprecated
public GatewayFilterSpec gatewayFilters(List<GatewayFilter> gatewayFilters) {
this.addAll(gatewayFilters);
return this;
}

/** @deprecated use {@link #filter(GatewayFilter)} */
@Deprecated
public GatewayFilterSpec add(GatewayFilter gatewayFilter) {
return this.filter(gatewayFilter);
}
Expand All @@ -78,11 +82,22 @@ public GatewayFilterSpec filter(GatewayFilter gatewayFilter, int order) {
return this;
}

/** @deprecated use {@link #filters(Collection)} */
@Deprecated
public GatewayFilterSpec addAll(Collection<GatewayFilter> gatewayFilters) {
this.routeBuilder.addAll(gatewayFilters);
return this;
}

public GatewayFilterSpec filters(GatewayFilter... gatewayFilters) {
this.routeBuilder.addAll(Arrays.asList(gatewayFilters));
return this;
}

public GatewayFilterSpec filters(Collection<GatewayFilter> gatewayFilters) {
this.routeBuilder.addAll(gatewayFilters);
return this;
}

public GatewayFilterSpec addRequestHeader(String headerName, String headerValue) {
return filter(getBean(AddRequestHeaderGatewayFilterFactory.class).apply(headerName, headerValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
Expand Down Expand Up @@ -46,11 +46,11 @@ public PredicateSpec order(int order) {

public BooleanSpec predicate(Predicate<ServerWebExchange> predicate) {
this.routeBuilder.predicate(predicate);
return createBooleanSpec();
return new BooleanSpec(this.routeBuilder, this.builder);
}

protected BooleanSpec createBooleanSpec() {
return new BooleanSpec(this.routeBuilder, this.builder);
protected GatewayFilterSpec createGatewayFilterSpec() {
return new GatewayFilterSpec(this.routeBuilder, this.builder);
}

public BooleanSpec after(ZonedDateTime datetime) {
Expand All @@ -69,6 +69,10 @@ public BooleanSpec cookie(String name, String regex) {
return predicate(getBean(CookieRoutePredicateFactory.class).apply(name, regex));
}

public BooleanSpec header(String header) {
return predicate(getBean(HeaderRoutePredicateFactory.class).apply(header));
}

public BooleanSpec header(String header, String regex) {
return predicate(getBean(HeaderRoutePredicateFactory.class).apply(header, regex));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
Expand Down Expand Up @@ -94,8 +94,8 @@ class RouteLocatorDsl(val builder: RouteLocatorBuilder) {
* Extension method to add filters {} block to dsl
*/
fun PredicateSpec.filters(init: GatewayFilterSpec.() -> Unit) {
val booleanSpec = createBooleanSpec()
booleanSpec.apply(init)
val spec = createGatewayFilterSpec()
spec.apply(init)
}


Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 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.ratelimit;

import java.security.Principal;
Expand Down Expand Up @@ -94,9 +110,9 @@ public Map<String, String> myapi(@PathVariable String id, Principal principal) {
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/myapi/**")
.requestRateLimiter(tuple().build())
.prefixPath("/downstream")
.uri("http://localhost:"+port))
.filters(f -> f.requestRateLimiter(tuple().build())
.prefixPath("/downstream"))
.uri("http://localhost:"+port))
.build();
}

Expand Down

0 comments on commit ee55ef5

Please sign in to comment.