Browse Source

Creates HandlerDiscoverer to dynamically load HandlerFunctions

mvc-server
sgibb 2 years ago
parent
commit
4624823490
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 22
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java
  2. 29
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerDiscoverer.java
  3. 30
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctionsHandlerSupplier.java
  4. 25
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerSupplier.java
  5. 3
      spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories

22
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrar.java

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.server.mvc.config;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@ -32,6 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -32,6 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
import org.springframework.cloud.gateway.server.mvc.filter.FilterDiscoverer;
import org.springframework.cloud.gateway.server.mvc.handler.HandlerDiscoverer;
import org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions;
import org.springframework.cloud.gateway.server.mvc.invoke.InvocationContext;
import org.springframework.cloud.gateway.server.mvc.invoke.OperationArgumentResolver;
@ -65,10 +67,12 @@ public class GatewayMvcPropertiesBeanDefinitionRegistrar implements ImportBeanDe @@ -65,10 +67,12 @@ public class GatewayMvcPropertiesBeanDefinitionRegistrar implements ImportBeanDe
private final Environment env;
private final PredicateDiscoverer predicateDiscoverer = new PredicateDiscoverer();
private final FilterDiscoverer filterDiscoverer = new FilterDiscoverer();
private final HandlerDiscoverer handlerDiscoverer = new HandlerDiscoverer();
private final PredicateDiscoverer predicateDiscoverer = new PredicateDiscoverer();
private final ParameterValueMapper parameterValueMapper = new ConversionServiceParameterValueMapper();
private final Binder binder;
@ -106,15 +110,19 @@ public class GatewayMvcPropertiesBeanDefinitionRegistrar implements ImportBeanDe @@ -106,15 +110,19 @@ public class GatewayMvcPropertiesBeanDefinitionRegistrar implements ImportBeanDe
RouterFunctions.Builder builder = route();
// TODO: cache, externalize?
MultiValueMap<String, OperationMethod> handlerOperations = handlerDiscoverer.getOperations();
// TODO: cache?
// translate handlerFunction
String scheme = routeProperties.getUri().getScheme();
Method handlerFunctionMethod = ReflectionUtils.findMethod(HandlerFunctions.class, scheme);
if (handlerFunctionMethod == null) {
Optional<OperationMethod> handlerOperationMethod = handlerOperations.get(scheme.toLowerCase()).stream()
.filter(operationMethod -> matchOperation(operationMethod, Collections.emptyMap())).findFirst();
if (handlerOperationMethod.isEmpty()) {
throw new IllegalStateException("Unable to find HandlerFunction for scheme: " + scheme);
}
HandlerFunction<ServerResponse> handlerFunction = (HandlerFunction) ReflectionUtils
.invokeMethod(handlerFunctionMethod, null);
ReflectiveOperationInvoker operationInvoker = new ReflectiveOperationInvoker(handlerOperationMethod.get(),
this.parameterValueMapper);
InvocationContext context = new InvocationContext(Collections.emptyMap(), trueNullOperationArgumentResolver);
HandlerFunction<ServerResponse> handlerFunction = operationInvoker.invoke(context);
// translate predicates
MultiValueMap<String, OperationMethod> predicateOperations = predicateDiscoverer.getOperations();

29
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerDiscoverer.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
/*
* Copyright 2013-2023 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
*
* https://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.server.mvc.handler;
import org.springframework.cloud.gateway.server.mvc.common.AbstractGatewayDiscoverer;
import org.springframework.web.servlet.function.HandlerFunction;
public class HandlerDiscoverer extends AbstractGatewayDiscoverer {
@Override
public void discover() {
doDiscover(HandlerSupplier.class, HandlerFunction.class);
}
}

30
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerFunctionsHandlerSupplier.java

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright 2013-2023 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
*
* https://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.server.mvc.handler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
public class HandlerFunctionsHandlerSupplier implements HandlerSupplier {
@Override
public Collection<Method> get() {
return Arrays.asList(HandlerFunctions.class.getMethods());
}
}

25
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/HandlerSupplier.java

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
/*
* Copyright 2013-2023 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
*
* https://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.server.mvc.handler;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.function.Supplier;
public interface HandlerSupplier extends Supplier<Collection<Method>> {
}

3
spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring.factories

@ -1,6 +1,9 @@ @@ -1,6 +1,9 @@
org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier=\
org.springframework.cloud.gateway.server.mvc.filter.FilterFunctionsFilterSupplier
org.springframework.cloud.gateway.server.mvc.handler.HandlerSupplier=\
org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctionsHandlerSupplier
org.springframework.cloud.gateway.server.mvc.predicate.PredicateSupplier=\
org.springframework.cloud.gateway.server.mvc.predicate.MvcPredicateSupplier,\
org.springframework.cloud.gateway.server.mvc.predicate.GatewayPredicateSupplier

Loading…
Cancel
Save