Spencer Gibb
8 years ago
28 changed files with 375 additions and 294 deletions
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* 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 org.springframework.web.reactive.function.server.RequestPredicates; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class PathRequestPredicateFactory implements RequestPredicateFactory { |
||||
|
||||
@Override |
||||
public RequestPredicate apply(String... args) { |
||||
validate(1, args); |
||||
String pattern = args[0]; |
||||
|
||||
//TODO: support custom PathPatternParser
|
||||
return RequestPredicates.path(pattern); |
||||
} |
||||
} |
@ -1,70 +0,0 @@
@@ -1,70 +0,0 @@
|
||||
/* |
||||
* 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 java.util.Map; |
||||
import java.util.function.Predicate; |
||||
|
||||
import org.springframework.util.PathMatcher; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
import org.springframework.web.server.support.HttpRequestPathHelper; |
||||
import org.springframework.web.util.ParsingPathMatcher; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class PathRoutePredicate implements RoutePredicate { |
||||
|
||||
public static final String URL_PREDICATE_VARS_ATTR = "urlPredicateVars"; |
||||
|
||||
private PathMatcher pathMatcher = new ParsingPathMatcher(); |
||||
private HttpRequestPathHelper pathHelper = new HttpRequestPathHelper(); |
||||
|
||||
public PathMatcher getPathMatcher() { |
||||
return pathMatcher; |
||||
} |
||||
|
||||
public void setPathMatcher(PathMatcher pathMatcher) { |
||||
this.pathMatcher = pathMatcher; |
||||
} |
||||
|
||||
public HttpRequestPathHelper getPathHelper() { |
||||
return pathHelper; |
||||
} |
||||
|
||||
public void setPathHelper(HttpRequestPathHelper pathHelper) { |
||||
this.pathHelper = pathHelper; |
||||
} |
||||
|
||||
@Override |
||||
public Predicate<ServerWebExchange> apply(String... args) { |
||||
validate(1, args); |
||||
String pattern = args[0]; |
||||
|
||||
return exchange -> { |
||||
String lookupPath = getPathHelper().getLookupPathForRequest(exchange); |
||||
boolean match = getPathMatcher().match(pattern, lookupPath); |
||||
if (match) { |
||||
Map<String, String> variables = getPathMatcher().extractUriTemplateVariables(pattern, lookupPath); |
||||
exchange.getAttributes().put(URL_PREDICATE_VARS_ATTR, variables); |
||||
} |
||||
return match; |
||||
//TODO: support trailingSlashMatch
|
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* 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.web.reactive.function.server; |
||||
|
||||
import org.springframework.http.HttpCookie; |
||||
import org.springframework.util.MultiValueMap; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class PublicDefaultServerRequest extends DefaultServerRequest { |
||||
private ServerWebExchange exchange; |
||||
|
||||
public PublicDefaultServerRequest(ServerWebExchange exchange) { |
||||
this(exchange, HandlerStrategies.withDefaults()); |
||||
} |
||||
|
||||
public PublicDefaultServerRequest(ServerWebExchange exchange, HandlerStrategies strategies) { |
||||
super(exchange, strategies); |
||||
this.exchange = exchange; |
||||
} |
||||
|
||||
public MultiValueMap<String, HttpCookie> getCookies() { |
||||
return this.exchange.getRequest().getCookies(); |
||||
} |
||||
|
||||
public ServerWebExchange getExchange() { |
||||
return exchange; |
||||
} |
||||
} |
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
/* |
||||
* 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.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.boot.SpringBootConfiguration; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.gateway.handler.RequestPredicateHandlerMapping; |
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.web.reactive.function.client.ClientResponse; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
||||
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus; |
||||
|
||||
import reactor.core.publisher.Mono; |
||||
import reactor.test.StepVerifier; |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(webEnvironment = RANDOM_PORT) |
||||
@DirtiesContext |
||||
public class MethodRequestPredicateFactoryTests extends BaseWebClientTests { |
||||
|
||||
@Test |
||||
public void hostRouteWorks() { |
||||
Mono<ClientResponse> result = webClient.get() |
||||
.uri("/get") |
||||
.header("Host", "www.method.org") |
||||
.exchange(); |
||||
|
||||
StepVerifier.create(result) |
||||
.consumeNextWith( |
||||
response -> { |
||||
assertStatus(response, HttpStatus.OK); |
||||
HttpHeaders httpHeaders = response.headers().asHttpHeaders(); |
||||
assertThat(httpHeaders.getFirst(HANDLER_MAPPER_HEADER)) |
||||
.isEqualTo(RequestPredicateHandlerMapping.class.getSimpleName()); |
||||
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER)) |
||||
.isEqualTo("method_test"); |
||||
}) |
||||
.expectComplete() |
||||
.verify(DURATION); |
||||
} |
||||
|
||||
@EnableAutoConfiguration |
||||
@SpringBootConfiguration |
||||
@Import(DefaultTestConfig.class) |
||||
public static class TestConfig { } |
||||
|
||||
} |
Loading…
Reference in new issue