DATAREST-1543 - Deprecate RepositoryRestConfiguration.getCorsRegistry().

Slightly tweaked the configuration model to rather handle the CorsRegistry via RepositoryRestConfigurer.configureRepositoryRestConfiguration(…) rather than RepositoryRestConfiguration itself. That allows moving of Spring WebMVC as a dependency in the core module.

The original methods exposing access to the CorsRegistry are now still available in deprecated form to not break existing clients.

Follow-up ticket: DATAREST-1542.
This commit is contained in:
Oliver Drotbohm
2020-07-03 14:53:59 +02:00
parent 239a86cc2d
commit 9f62237dd3
9 changed files with 187 additions and 29 deletions

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2020 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.rest.webmvc.config;
import java.util.Map;
import org.springframework.web.cors.CorsConfiguration;
/**
* Components that are aware of CORS configuration.
*
* @author Oliver Drotbohm
* @since 3.4
* @soundtrack Elen - Blind über Rot (Blind über Rot)
*/
public interface CorsConfigurationAware {
/**
* Return the registered {@link CorsConfiguration} objects, keyed by path pattern.
*
* @return will never be {@literal null}.
*/
Map<String, CorsConfiguration> getCorsConfigurations();
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.rest.webmvc.config;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.springframework.core.convert.support.ConfigurableConversionService;
@@ -23,6 +24,7 @@ import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -39,7 +41,7 @@ public interface RepositoryRestConfigurer {
/**
* Convenience method to easily create simple {@link RepositoryRestConfigurer} instances that solely want to tweak the
* {@link RepositoryRestConfiguration}.
*
*
* @param consumer must not be {@literal null}.
* @return
* @since 3.1
@@ -50,7 +52,7 @@ public interface RepositoryRestConfigurer {
return new RepositoryRestConfigurer() {
/*
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)
*/
@@ -61,13 +63,50 @@ public interface RepositoryRestConfigurer {
};
}
/**
* Convenience method to easily create simple {@link RepositoryRestConfigurer} instances that solely want to tweak the
* {@link RepositoryRestConfiguration}.
*
* @param consumer must not be {@literal null}.
* @return
* @since 3.4
*/
static RepositoryRestConfigurer withConfig(BiConsumer<RepositoryRestConfiguration, CorsRegistry> consumer) {
Assert.notNull(consumer, "Consumer must not be null!");
return new RepositoryRestConfigurer() {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration, org.springframework.web.servlet.config.annotation.CorsRegistry)
*/
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
consumer.accept(config, cors);
}
};
}
/**
* Override this method to add additional configuration.
*
* @param config Main configuration bean.
* @deprecated since 3.4, implement
* {@link #configureRepositoryRestConfiguration(RepositoryRestConfiguration, CorsRegistry)} instead.
*/
@Deprecated
default void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {}
/**
* Override this method to add additional configuration.
*
* @param config Main configuration bean.
* @param cors CORS configuration.
* @since 3.4
*/
default void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {}
/**
* Override this method to add your own converters.
*

View File

@@ -22,6 +22,7 @@ import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -61,6 +62,20 @@ class RepositoryRestConfigurerDelegate implements RepositoryRestConfigurer {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration, org.springframework.web.servlet.config.annotation.CorsRegistry)
*/
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
configureRepositoryRestConfiguration(config);
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureRepositoryRestConfiguration(config, cors);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureConversionService(org.springframework.core.convert.support.ConfigurableConversionService)

View File

@@ -59,6 +59,7 @@ import org.springframework.data.rest.core.UriToEntityConverter;
import org.springframework.data.rest.core.config.MetadataConfiguration;
import org.springframework.data.rest.core.config.Projection;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration;
import org.springframework.data.rest.core.config.RepositoryCorsRegistry;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.AnnotatedEventHandlerInvoker;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
@@ -278,7 +279,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
* Main configuration for the REST exporter.
*/
@Bean
public RepositoryRestConfiguration repositoryRestConfiguration() {
public <T extends RepositoryRestConfiguration & CorsConfigurationAware> T repositoryRestConfiguration() {
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
@@ -287,11 +288,13 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
configuration.addProjection(projection);
}
RepositoryRestConfiguration config = new RepositoryRestConfiguration(configuration, metadataConfiguration(),
enumTranslator());
configurerDelegate.configureRepositoryRestConfiguration(config);
RepositoryCorsRegistry registry = new RepositoryCorsRegistry();
return config;
WebMvcRepositoryRestConfiguration config = new WebMvcRepositoryRestConfiguration(configuration,
metadataConfiguration(), enumTranslator(), registry);
configurerDelegate.configureRepositoryRestConfiguration(config, registry);
return (T) config;
}
@Bean
@@ -542,8 +545,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Bean
public AbstractHandlerMapping restHandlerMapping() {
Map<String, CorsConfiguration> corsConfigurations = repositoryRestConfiguration().getCorsRegistry()
.getCorsConfigurations();
Map<String, CorsConfiguration> corsConfigurations = repositoryRestConfiguration().getCorsConfigurations();
RepositoryRestHandlerMapping repositoryMapping = new RepositoryRestHandlerMapping(resourceMappings(),
repositoryRestConfiguration(), repositories());

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2020 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.rest.webmvc.config;
import java.util.Map;
import org.springframework.data.rest.core.config.EnumTranslationConfiguration;
import org.springframework.data.rest.core.config.MetadataConfiguration;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration;
import org.springframework.data.rest.core.config.RepositoryCorsRegistry;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.util.Assert;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
/**
* Internal variant of {@link RepositoryRestConfiguration} to also expose the {@link CorsRegistry} via
* {@link CorsConfigurationAware}.
*
* @author Oliver Drotbohm
* @since 3.4
* @soundtrack Elen - Andre Arcaden (Blind über Rot)
*/
class WebMvcRepositoryRestConfiguration extends RepositoryRestConfiguration implements CorsConfigurationAware {
/**
* Creates a new {@link WebMvcRepositoryRestConfiguration}.
*
* @param projectionConfiguration must not be {@literal null}.
* @param metadataConfiguration must not be {@literal null}.
* @param enumTranslationConfiguration must not be {@literal null}.
* @param registry must not be {@literal null}.
*/
public WebMvcRepositoryRestConfiguration(ProjectionDefinitionConfiguration projectionConfiguration, //
MetadataConfiguration metadataConfiguration, //
EnumTranslationConfiguration enumTranslationConfiguration, //
RepositoryCorsRegistry registry) {
super(projectionConfiguration, metadataConfiguration, enumTranslationConfiguration);
Assert.notNull(registry, "CorsRegistry must not be null!");
this.registry = registry;
setCorsRegistry(registry);
}
private final RepositoryCorsRegistry registry;
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.CorsConfigurationAware#getCorsConfigurations()
*/
@Override
public Map<String, CorsConfiguration> getCorsConfigurations() {
return registry.getCorsConfigurations();
}
}