DATAGEODE-243 - Introduce RestTemplateConfigurer to configure the RestTemplate used when sending configuration metadata from client to server.

This commit is contained in:
John Blum
2019-10-28 00:36:28 -07:00
parent 51f8327cc6
commit 77d1d64cdb
9 changed files with 458 additions and 134 deletions

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.geode.cache.client.ClientCache;
@@ -30,6 +31,7 @@ import org.apache.geode.cache.execute.Function;
import org.springframework.data.gemfire.config.admin.GemfireAdminOperations;
import org.springframework.data.gemfire.config.schema.definitions.IndexDefinition;
import org.springframework.data.gemfire.config.schema.definitions.RegionDefinition;
import org.springframework.data.gemfire.config.support.RestTemplateConfigurer;
import org.springframework.data.gemfire.util.ArrayUtils;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.data.gemfire.util.NetworkUtils;
@@ -100,7 +102,7 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
/**
* Constructs a new instance of {@link RestHttpGemfireAdminTemplate} initialized with the given {@link ClientCache}
* and configured with the default host and port when accessing the Apache Geode or Pivotal GemFire
* and configured with the default HTTP schema, host and port when accessing the Apache Geode or Pivotal GemFire
* Management REST API interface.
*
* @param clientCache reference to the {@link ClientCache}.
@@ -117,8 +119,8 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
/**
* Constructs a new instance of {@link RestHttpGemfireAdminTemplate} initialized with the given {@link ClientCache}
* and configured with the specified HTTP scheme, host, port, redirects and
* {@link ClientHttpRequestInterceptor ClientHttpRequestInterceptors} when
* accessing the Apache Geode or Pivotal GemFire Management REST API interface.
* {@link ClientHttpRequestInterceptor ClientHttpRequestInterceptors}
* when accessing the Apache Geode or Pivotal GemFire Management REST API interface.
*
* @param clientCache reference to the {@link ClientCache}
* @param scheme {@link String} specifying the HTTP scheme to use (e.g. HTTP or HTTPS).
@@ -131,19 +133,47 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
* @throws IllegalArgumentException if the {@link ClientCache} reference is {@literal null}.
* @see org.springframework.http.client.ClientHttpRequestInterceptor
* @see org.apache.geode.cache.client.ClientCache
* @see #newClientHttpRequestFactory(boolean)
* @see #newRestOperations(ClientHttpRequestFactory, List)
* @see #resolveManagementRestApiUrl(String, String, int)
*/
public RestHttpGemfireAdminTemplate(ClientCache clientCache, String scheme, String host, int port,
boolean followRedirects, List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors) {
this(clientCache, scheme, host, port, followRedirects, clientHttpRequestInterceptors, Collections.emptyList());
}
/**
* Constructs a new instance of {@link RestHttpGemfireAdminTemplate} initialized with the given {@link ClientCache}
* and configured with the specified HTTP scheme, host, port, redirects and
* {@link ClientHttpRequestInterceptor ClientHttpRequestInterceptors}
* when accessing the Apache Geode or Pivotal GemFire Management REST API interface.
*
* @param clientCache reference to the {@link ClientCache}
* @param scheme {@link String} specifying the HTTP scheme to use (e.g. HTTP or HTTPS).
* @param host {@link String} containing the hostname of the GemFire/Geode Manager.
* @param port integer value specifying the port on which the GemFire/Geode Manager HTTP Service is listening
* for HTTP clients.
* @param followRedirects boolean indicating whether HTTP Redirects (with HTTP Status Code 3xx) should be followed.
* @param clientHttpRequestInterceptors {@link List} of {@link ClientHttpRequestInterceptor} used to intercept
* and decorate the HTTP request and HTTP response.
* @throws IllegalArgumentException if the {@link ClientCache} reference is {@literal null}.
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.data.gemfire.config.support.RestTemplateConfigurer
* @see org.springframework.http.client.ClientHttpRequestInterceptor
* @see #newClientHttpRequestFactory(boolean)
* @see #newRestOperations(ClientHttpRequestFactory, List, List)
* @see #resolveManagementRestApiUrl(String, String, int)
*/
public RestHttpGemfireAdminTemplate(ClientCache clientCache, String scheme, String host, int port,
boolean followRedirects, List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors,
List<RestTemplateConfigurer> restTemplateConfigurers) {
super(clientCache);
ClientHttpRequestFactory clientHttpRequestFactory = newClientHttpRequestFactory(followRedirects);
this.managementRestApiUrl = resolveManagementRestApiUrl(scheme, host, port);
this.restTemplate = newRestOperations(clientHttpRequestFactory, clientHttpRequestInterceptors);
this.restTemplate =
newRestOperations(clientHttpRequestFactory, clientHttpRequestInterceptors, restTemplateConfigurers);
}
/**
@@ -173,13 +203,18 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
*/
@SuppressWarnings("unchecked")
protected <T extends RestOperations> T newRestOperations(ClientHttpRequestFactory clientHttpRequestFactory,
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors) {
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors,
List<RestTemplateConfigurer> restTemplateConfigurers) {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
Optional.ofNullable(clientHttpRequestInterceptors)
.ifPresent(restTemplate.getInterceptors()::addAll);
CollectionUtils.nullSafeList(restTemplateConfigurers).stream()
.filter(Objects::nonNull)
.forEach(configurer -> configurer.configure(restTemplate));
return (T) restTemplate;
}
@@ -283,6 +318,7 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
private ClientCache clientCache;
private final List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors = new ArrayList<>();
private final List<RestTemplateConfigurer> restTemplateConfigurers = new ArrayList<>();
private String hostname = DEFAULT_HOST;
private String scheme = DEFAULT_SCHEME;
@@ -324,15 +360,47 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
return this;
}
/**
* @deprecated use {@link #withInterceptors(ClientHttpRequestInterceptor...)}.
*/
@Deprecated
public Builder with(ClientHttpRequestInterceptor... clientHttpRequestInterceptors) {
clientHttpRequestInterceptors =
ArrayUtils.nullSafeArray(clientHttpRequestInterceptors, ClientHttpRequestInterceptor.class);
return with(Arrays.asList(clientHttpRequestInterceptors));
return withInterceptors(clientHttpRequestInterceptors);
}
/**
* @deprecated use {@link #withInterceptors(List)}.
*/
@Deprecated
public Builder with(List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors) {
return withInterceptors(clientHttpRequestInterceptors);
}
public Builder withConfigurers(RestTemplateConfigurer... restTemplateConfigurers) {
List<RestTemplateConfigurer> restTemplateConfigurerList =
Arrays.asList(ArrayUtils.nullSafeArray(restTemplateConfigurers, RestTemplateConfigurer.class));
return withConfigurers(restTemplateConfigurerList);
}
public Builder withConfigurers(List<RestTemplateConfigurer> restTemplateConfigurers) {
this.restTemplateConfigurers.addAll(CollectionUtils.nullSafeList(restTemplateConfigurers));
return this;
}
public Builder withInterceptors(ClientHttpRequestInterceptor... clientHttpRequestInterceptors) {
List<ClientHttpRequestInterceptor> clientHttpRequestInterceptorList =
Arrays.asList(ArrayUtils.nullSafeArray(clientHttpRequestInterceptors,
ClientHttpRequestInterceptor.class));
return withInterceptors(clientHttpRequestInterceptorList);
}
public Builder withInterceptors(List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors) {
this.clientHttpRequestInterceptors.addAll(CollectionUtils.nullSafeList(clientHttpRequestInterceptors));
@@ -342,7 +410,7 @@ public class RestHttpGemfireAdminTemplate extends FunctionGemfireAdminTemplate {
public RestHttpGemfireAdminTemplate build() {
return new RestHttpGemfireAdminTemplate(this.clientCache, this.scheme, this.hostname, this.port,
this.followRedirects, this.clientHttpRequestInterceptors);
this.followRedirects, this.clientHttpRequestInterceptors, this.restTemplateConfigurers);
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.data.gemfire.config.annotation.support.AutoConfiguredAuthenticationInitializer;
import org.springframework.data.gemfire.config.support.RestTemplateConfigurer;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.ClientHttpRequestInterceptor;
@@ -106,9 +107,7 @@ public class AutoConfiguredAuthenticationConfiguration {
return authenticator;
}
@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
public ClientHttpRequestInterceptor loggingAwareClientHttpRequestInterceptor() {
ClientHttpRequestInterceptor loggingAwareClientHttpRequestInterceptor() {
return (request, body, execution) -> {
@@ -142,8 +141,12 @@ public class AutoConfiguredAuthenticationConfiguration {
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ClientHttpRequestInterceptor securityAwareClientHttpRequestInterceptor(Authenticator authenticator) {
@Order(Ordered.LOWEST_PRECEDENCE)
public RestTemplateConfigurer loggingAwareRestTemplateConfigurer() {
return restTemplate -> restTemplate.getInterceptors().add(loggingAwareClientHttpRequestInterceptor());
}
ClientHttpRequestInterceptor securityAwareClientHttpRequestInterceptor() {
return (request, body, execution) -> {
@@ -168,6 +171,12 @@ public class AutoConfiguredAuthenticationConfiguration {
};
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public RestTemplateConfigurer securityAwareRestTemplateConfigurer(Authenticator authenticator) {
return restTemplate -> restTemplate.getInterceptors().add(securityAwareClientHttpRequestInterceptor());
}
private boolean isAuthenticationEnabled(String username, char[] password) {
return StringUtils.hasText(username) && password != null && password.length > 0;
}

View File

@@ -57,6 +57,7 @@ import org.springframework.data.gemfire.config.schema.support.IndexCollector;
import org.springframework.data.gemfire.config.schema.support.IndexDefiner;
import org.springframework.data.gemfire.config.schema.support.RegionDefiner;
import org.springframework.data.gemfire.config.support.AbstractSmartLifecycle;
import org.springframework.data.gemfire.config.support.RestTemplateConfigurer;
import org.springframework.data.gemfire.util.CacheUtils;
import org.springframework.data.gemfire.util.NetworkUtils;
import org.springframework.http.client.ClientHttpRequestInterceptor;
@@ -93,6 +94,7 @@ import org.springframework.util.StringUtils;
public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
protected static final boolean DEFAULT_HTTP_FOLLOW_REDIRECTS = false;
protected static final boolean DEFAULT_HTTP_REQUEST_INTERCEPTORS_ENABLED = false;
protected static final boolean DEFAULT_MANAGEMENT_USE_HTTP = false;
protected static final boolean DEFAULT_MANAGEMENT_REQUIRE_HTTPS = true;
@@ -105,6 +107,8 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
private static final RegionShortcut DEFAULT_SERVER_REGION_SHORTCUT = RegionDefinition.DEFAULT_REGION_SHORTCUT;
private Boolean enableInterceptors = DEFAULT_HTTP_REQUEST_INTERCEPTORS_ENABLED;
private Boolean followRedirects = DEFAULT_HTTP_FOLLOW_REDIRECTS;
private Boolean requireHttps = DEFAULT_MANAGEMENT_REQUIRE_HTTPS;
private Boolean useHttp = DEFAULT_MANAGEMENT_USE_HTTP;
@@ -116,6 +120,9 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
@Autowired(required = false)
private List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors;
@Autowired(required = false)
private List<RestTemplateConfigurer> restTemplateConfigurers;
private RegionShortcut serverRegionShortcut;
private String managementHttpHost = DEFAULT_MANAGEMENT_HTTP_HOST;
@@ -149,6 +156,30 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
return getManagementHttpPort().orElse(DEFAULT_MANAGEMENT_HTTP_PORT);
}
protected void setManagementHttpEnableInterceptors(Boolean enableInterceptors) {
this.enableInterceptors = enableInterceptors;
}
protected Optional<Boolean> getManagementHttpEnableInterceptors() {
return Optional.ofNullable(this.enableInterceptors);
}
protected boolean resolveManagementHttpEnableInterceptors() {
return getManagementHttpEnableInterceptors().orElse(DEFAULT_HTTP_REQUEST_INTERCEPTORS_ENABLED);
}
protected void setManagementHttpFollowRedirects(Boolean followRedirects) {
this.followRedirects = followRedirects;
}
protected Optional<Boolean> getManagementHttpFollowRedirects() {
return Optional.ofNullable(this.followRedirects);
}
protected boolean resolveManagementHttpFollowRedirects() {
return getManagementHttpFollowRedirects().orElse(DEFAULT_HTTP_FOLLOW_REDIRECTS);
}
protected void setManagementRequireHttps(Boolean requireHttps) {
this.requireHttps = requireHttps;
}
@@ -198,6 +229,12 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
setManagementHttpPort(resolveProperty(managementProperty("http.port"),
enableClusterConfigurationAttributes.<Integer>getNumber("port")));
setManagementHttpEnableInterceptors(resolveProperty(managementProperty("http.enable-interceptors"),
enableClusterConfigurationAttributes.getBoolean("enableInterceptors")));
setManagementHttpFollowRedirects(resolveProperty(managementProperty("http.follow-redirects"),
enableClusterConfigurationAttributes.getBoolean("followRedirects")));
setManagementRequireHttps(resolveProperty(managementProperty("require-https"),
enableClusterConfigurationAttributes.getBoolean("requireHttps")));
@@ -228,32 +265,47 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
.orElse(null);
}
private <T> List<T> resolveBeansOfType(List<T> objects, Class<T> type) {
return Optional.ofNullable(objects).orElseGet(() ->
Optional.of(getBeanFactory())
.filter(ListableBeanFactory.class::isInstance)
.map(ListableBeanFactory.class::cast)
.map(beanFactory -> {
Map<String, T> beansOfType = beanFactory.getBeansOfType(type, true, false);
return nullSafeMap(beansOfType).values().stream().collect(Collectors.toList());
})
.orElseGet(Collections::emptyList));
}
/**
* Attempts to resolve a {@link List} of {@link ClientHttpRequestInterceptor} beans in the Spring
* {@link ApplicationContext}.
*
* @return a {@link List} of declared and registered {@link ClientHttpRequestInterceptor} beans.
* @see org.springframework.http.client.ClientHttpRequestInterceptor
* @see #getBeanFactory()
* @see java.util.List
*/
protected List<ClientHttpRequestInterceptor> resolveClientHttpRequestInterceptors() {
protected List<ClientHttpRequestInterceptor> resolveClientHttpRequestInterceptors(boolean enableInterceptors) {
return Optional.ofNullable(this.clientHttpRequestInterceptors)
.orElseGet(() ->
return enableInterceptors
? resolveBeansOfType(this.clientHttpRequestInterceptors, ClientHttpRequestInterceptor.class)
: Collections.emptyList();
}
Optional.of(getBeanFactory())
.filter(ListableBeanFactory.class::isInstance)
.map(ListableBeanFactory.class::cast)
.map(beanFactory -> {
Map<String, ClientHttpRequestInterceptor> beansOfType = beanFactory
.getBeansOfType(ClientHttpRequestInterceptor.class, true, false);
return nullSafeMap(beansOfType).values().stream().collect(Collectors.toList());
})
.orElseGet(Collections::emptyList));
/**
* Attempts to resolve a {@link List} of {@link RestTemplateConfigurer} beans in the Spring
* {@link ApplicationContext}.
*
* @return a {@link List} of declared and registered {@link RestTemplateConfigurer} beans.
* @see org.springframework.data.gemfire.config.support.RestTemplateConfigurer
* @see java.util.List
*/
protected List<RestTemplateConfigurer> resolveRestTemplateConfigurers() {
return resolveBeansOfType(this.restTemplateConfigurers, RestTemplateConfigurer.class);
}
/**
@@ -286,7 +338,7 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
* on a GemFire system.
* @see org.springframework.data.gemfire.config.admin.GemfireAdminOperations
* @see org.apache.geode.cache.client.ClientCache
* @see #resolveClientHttpRequestInterceptors()
* @see #resolveClientHttpRequestInterceptors(boolean)
* @see #resolveManagementHttpHost()
* @see #resolveManagementHttpPort()
* @see #resolveManagementRequireHttps()
@@ -296,11 +348,10 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
if (resolveManagementUseHttp()) {
boolean setFollowRedirects =
environment.getProperty(HTTP_FOLLOW_REDIRECTS_PROPERTY, Boolean.class, DEFAULT_HTTP_FOLLOW_REDIRECTS);
boolean enableInterceptors = resolveManagementHttpEnableInterceptors();
boolean followRedirects = resolveManagementHttpFollowRedirects();
boolean requireHttps = resolveManagementRequireHttps();
boolean followRedirects = !requireHttps || setFollowRedirects;
boolean resolvedFollowRedirects = !requireHttps || followRedirects;
int port = resolveManagementHttpPort();
@@ -308,11 +359,12 @@ public class ClusterConfigurationConfiguration extends AbstractAnnotationConfigS
String scheme = requireHttps ? HTTPS_SCHEME : HTTP_SCHEME;
return configurePort(new RestHttpGemfireAdminTemplate.Builder()
.with(resolveClientHttpRequestInterceptors())
.withConfigurers(resolveRestTemplateConfigurers())
.withInterceptors(resolveClientHttpRequestInterceptors(enableInterceptors))
.with(clientCache)
.using(scheme)
.on(host)
.followRedirects(followRedirects), port)
.followRedirects(resolvedFollowRedirects), port)
.build();
}
else {

View File

@@ -28,6 +28,8 @@ import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.client.ClientCache;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
/**
* The {@link EnableClusterConfiguration} annotation enables Apache Geode / Pivotal GemFire schema object definitions
@@ -78,6 +80,29 @@ public @interface EnableClusterConfiguration {
*/
int port() default ClusterConfigurationConfiguration.DEFAULT_MANAGEMENT_HTTP_PORT;
/**
* Configures whether to enable {@link ClientHttpRequestInterceptor} bean lookup.
*
* If {@link ClientHttpRequestInterceptor} beans are found in the Spring context, then they will be added to
* the Interceptors on the {@link RestTemplate} when using HTTP.
*
* Alternatively, you can configure this setting using the
* {@literal spring.data.gemfire.management.http.enable-interceptors} property in {@literal application.properties}.
*
* Defaults to {@literal false}.
*/
boolean enableInterceptors() default ClusterConfigurationConfiguration.DEFAULT_HTTP_REQUEST_INTERCEPTORS_ENABLED;
/**
* Configures whether to follow HTTP redirects when using HTTP.
*
* Alternatively, you can configure this setting using the
* {@literal spring.data.gemfire.management.http.follow-redirects} property in {@literal application.properties}.
*
* Defaults to {@literal false}.
*/
boolean followRedirects() default ClusterConfigurationConfiguration.DEFAULT_HTTP_FOLLOW_REDIRECTS;
/**
* Configures whether the HTTP connection between Spring and Apache Geode or Pivotal GemFire should be secure.
* That is, whether the HTTP connections uses TLS and results in a secure HTTPS connection rather a plain text

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2019 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.data.gemfire.config.support;
import org.springframework.web.client.RestTemplate;
/**
* Configurer for a {@link RestTemplate}.
*
* @author John Blum
* @see org.springframework.web.client.RestTemplate
* @since 2.3.0
*/
@FunctionalInterface
public interface RestTemplateConfigurer {
/**
* User-defined method and contract for applying custom configuration to the given {@link RestTemplate}.
*
* @param restTemplate {@link RestTemplate} to customize the configuration for.
* @see org.springframework.web.client.RestTemplate
*/
void configure(RestTemplate restTemplate);
}