From 244c57f712c0223229070a7aaf82e7776c463b46 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 31 Jul 2019 14:49:54 +0200 Subject: [PATCH] DATAREST-1414 - Use I18N facilities of Spring HATEOAS more intensively. We now rely on the MessageResolver capabilities of Spring HATEOAS to benefit of the optimizations in resource bundle resolution. Tweaked a couple of tests to make sure they properly use the resolver resorting to the default message the MessageResourceResolvable exposes. --- .../rest/tests/RepositoryTestsConfig.java | 3 +- .../PersistentEntitySerializationTests.java | 7 +--- .../webmvc/json/RepositoryTestsConfig.java | 3 +- .../PersistentEntitySerializationTests.java | 8 ++-- ...tEntityToJsonSchemaConverterUnitTests.java | 7 ++-- ...eInformationToAlpsDescriptorConverter.java | 11 +++--- .../RepositoryRestMvcConfiguration.java | 39 +++++++------------ .../data/rest/webmvc/json/EnumTranslator.java | 13 ++++--- ...PersistentEntityToJsonSchemaConverter.java | 26 ++++++------- ...ryRestMvConfigurationIntegrationTests.java | 12 ------ .../webmvc/json/EnumTranslatorUnitTests.java | 4 +- .../ProjectionJacksonIntegrationTests.java | 3 +- 12 files changed, 55 insertions(+), 81 deletions(-) diff --git a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/RepositoryTestsConfig.java b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/RepositoryTestsConfig.java index c45df7d10..d9a8213b8 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/RepositoryTestsConfig.java +++ b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/RepositoryTestsConfig.java @@ -49,6 +49,7 @@ import org.springframework.data.rest.webmvc.support.PagingAndSortingTemplateVari import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.FormattingConversionService; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider; import org.springframework.hateoas.mediatype.hal.Jackson2HalModule; import org.springframework.hateoas.server.EntityLinks; @@ -135,7 +136,7 @@ public class RepositoryTestsConfig { mapper.registerModule(new Jackson2HalModule()); mapper.registerModule(persistentEntityModule()); mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider, - new DefaultCurieProvider(Collections.emptyMap()), null)); + new DefaultCurieProvider(Collections.emptyMap()), MessageResolver.DEFAULTS_ONLY)); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setSerializationInclusion(Include.NON_EMPTY); diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java index 3b1eb260e..7c78d46df 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java @@ -25,12 +25,9 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.support.MessageSourceAccessor; -import org.springframework.context.support.StaticMessageSource; import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.projection.ProjectionFactory; @@ -45,6 +42,7 @@ import org.springframework.hateoas.LinkRelation; import org.springframework.hateoas.PagedModel; import org.springframework.hateoas.PagedModel.PageMetadata; import org.springframework.hateoas.client.LinkDiscoverer; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.HalLinkDiscoverer; import org.springframework.hateoas.server.core.EmbeddedWrapper; import org.springframework.hateoas.server.core.EmbeddedWrappers; @@ -88,8 +86,7 @@ public class PersistentEntitySerializationTests { public ObjectMapper objectMapper() { ObjectMapper objectMapper = super.objectMapper(); - objectMapper.registerModule( - new JacksonSerializers(new EnumTranslator(new MessageSourceAccessor(new StaticMessageSource())))); + objectMapper.registerModule(new JacksonSerializers(new EnumTranslator(MessageResolver.DEFAULTS_ONLY))); return objectMapper; } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java index 0207a69e6..41edcf8ab 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java @@ -50,6 +50,7 @@ import org.springframework.data.rest.webmvc.support.PagingAndSortingTemplateVari import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.FormattingConversionService; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider; import org.springframework.hateoas.mediatype.hal.Jackson2HalModule; import org.springframework.hateoas.server.EntityLinks; @@ -143,7 +144,7 @@ public class RepositoryTestsConfig { mapper.registerModule(new Jackson2HalModule()); mapper.registerModule(persistentEntityModule()); mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider, - new DefaultCurieProvider(Collections.emptyMap()), null)); + new DefaultCurieProvider(Collections.emptyMap()), MessageResolver.DEFAULTS_ONLY)); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setSerializationInclusion(Include.NON_EMPTY); diff --git a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java index a21867ab7..07c647b81 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java @@ -23,12 +23,9 @@ import java.util.HashMap; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.support.MessageSourceAccessor; -import org.springframework.context.support.StaticMessageSource; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.repository.support.Repositories; @@ -44,6 +41,7 @@ import org.springframework.hateoas.Link; import org.springframework.hateoas.PagedModel; import org.springframework.hateoas.PagedModel.PageMetadata; import org.springframework.hateoas.client.LinkDiscoverer; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.HalLinkDiscoverer; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ContextConfiguration; @@ -78,8 +76,8 @@ public class PersistentEntitySerializationTests { public ObjectMapper objectMapper() { ObjectMapper objectMapper = super.objectMapper(); - objectMapper.registerModule( - new JacksonSerializers(new EnumTranslator(new MessageSourceAccessor(new StaticMessageSource())))); + objectMapper.registerModule(new JacksonSerializers(new EnumTranslator(MessageResolver.DEFAULTS_ONLY))); + return objectMapper; } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java index f7dc281cd..91ed1cd6c 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java @@ -28,10 +28,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.context.support.MessageSourceAccessor; import org.springframework.data.mapping.context.PersistentEntities; import org.springframework.data.rest.core.config.JsonSchemaFormat; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; @@ -47,6 +45,7 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.ValueTypeSchemaPropertyCustomizerFactory; import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverterUnitTests.TestConfiguration; import org.springframework.data.rest.webmvc.mapping.Associations; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -63,7 +62,7 @@ import com.jayway.jsonpath.PathNotFoundException; @ContextConfiguration(classes = { MongoDbRepositoryConfig.class, TestConfiguration.class }) public class PersistentEntityToJsonSchemaConverterUnitTests { - @Autowired @Qualifier("resourceDescriptionMessageSourceAccessor") MessageSourceAccessor accessor; + @Autowired MessageResolver resolver; @Autowired RepositoryRestConfiguration configuration; @Autowired PersistentEntities entities; @Autowired Associations associations; @@ -93,7 +92,7 @@ public class PersistentEntityToJsonSchemaConverterUnitTests { ValueTypeSchemaPropertyCustomizerFactory customizerFactory = mock(ValueTypeSchemaPropertyCustomizerFactory.class); - converter = new PersistentEntityToJsonSchemaConverter(entities, associations, accessor, objectMapper, configuration, + converter = new PersistentEntityToJsonSchemaConverter(entities, associations, resolver, objectMapper, configuration, customizerFactory); } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java index 6cb7b160e..a8e665ed5 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java @@ -29,7 +29,6 @@ import java.util.Map; import java.util.Map.Entry; import org.springframework.context.NoSuchMessageException; -import org.springframework.context.support.MessageSourceAccessor; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; @@ -55,16 +54,17 @@ import org.springframework.data.rest.webmvc.RootResourceInformation; import org.springframework.data.rest.webmvc.json.EnumTranslator; import org.springframework.data.rest.webmvc.json.JacksonMetadata; import org.springframework.data.rest.webmvc.mapping.Associations; -import org.springframework.hateoas.server.EntityLinks; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkRelation; import org.springframework.hateoas.TemplateVariable; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.alps.Alps; import org.springframework.hateoas.mediatype.alps.Descriptor; import org.springframework.hateoas.mediatype.alps.Descriptor.DescriptorBuilder; import org.springframework.hateoas.mediatype.alps.Doc; import org.springframework.hateoas.mediatype.alps.Format; import org.springframework.hateoas.mediatype.alps.Type; +import org.springframework.hateoas.server.EntityLinks; import org.springframework.http.HttpMethod; import org.springframework.util.StringUtils; @@ -87,7 +87,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { private final @NonNull Repositories repositories; private final @NonNull PersistentEntities persistentEntities; private final @NonNull EntityLinks entityLinks; - private final @NonNull MessageSourceAccessor messageSource; + private final @NonNull MessageResolver resolver; private final @NonNull RepositoryRestConfiguration configuration; private final @NonNull ObjectMapper mapper; private final @NonNull EnumTranslator translator; @@ -415,9 +415,10 @@ public class RootResourceInformationToAlpsDescriptorConverter { } try { - return messageSource.getMessage(description); + return resolver.resolve(description); } catch (NoSuchMessageException o_O) { - return configuration.getMetadataConfiguration().omitUnresolvableDescriptionKeys() ? null + return configuration.getMetadataConfiguration().omitUnresolvableDescriptionKeys() // + ? null // : description.getMessage(); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index c682b4224..344cf8284 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -15,6 +15,7 @@ */ package org.springframework.data.rest.webmvc.config; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -22,10 +23,10 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import java.util.Set; import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.ObjectFactory; @@ -40,8 +41,6 @@ import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; -import org.springframework.context.support.MessageSourceAccessor; -import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.Ordered; import org.springframework.core.convert.ConversionService; import org.springframework.core.io.ClassPathResource; @@ -68,7 +67,6 @@ import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.event.AnnotatedEventHandlerInvoker; import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; import org.springframework.data.rest.core.mapping.RepositoryResourceMappings; -import org.springframework.data.rest.core.mapping.ResourceDescription; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.core.support.DefaultSelfLinkProvider; import org.springframework.data.rest.core.support.EntityLookup; @@ -100,6 +98,7 @@ import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.RepresentationModel; import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.CurieProvider; import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider; import org.springframework.hateoas.mediatype.hal.HalConfiguration; @@ -165,6 +164,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Autowired Optional halConfiguration; @Autowired ObjectProvider objectMapper; @Autowired ObjectProvider invoker; + @Autowired MessageResolver resolver; private final Lazy mapper; @@ -405,18 +405,12 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Bean public PersistentEntityToJsonSchemaConverter jsonSchemaConverter() { - return new PersistentEntityToJsonSchemaConverter(persistentEntities(), associationLinks(), - resourceDescriptionMessageSourceAccessor(), objectMapper(), repositoryRestConfiguration(), + return new PersistentEntityToJsonSchemaConverter(persistentEntities(), associationLinks(), resolver, objectMapper(), + repositoryRestConfiguration(), new ValueTypeSchemaPropertyCustomizerFactory(repositoryInvokerFactory(defaultConversionService()))); } - /** - * The {@link MessageSourceAccessor} to provide messages for {@link ResourceDescription}s being rendered. - * - * @return - */ - @Bean - public MessageSourceAccessor resourceDescriptionMessageSourceAccessor() { + private final Properties lookupDefaultMessages() { try { @@ -424,15 +418,10 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon propertiesFactoryBean.setLocation(new ClassPathResource("rest-default-messages.properties")); propertiesFactoryBean.afterPropertiesSet(); - ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); - messageSource.setBasename("classpath:rest-messages"); - messageSource.setCommonMessages(propertiesFactoryBean.getObject()); - messageSource.setDefaultEncoding("UTF-8"); + return propertiesFactoryBean.getObject(); - return new MessageSourceAccessor(messageSource); - - } catch (Exception o_O) { - throw new BeanCreationException("resourceDescriptionMessageSourceAccessor", "", o_O); + } catch (IOException o_O) { + throw new IllegalStateException("Unable to resolve default rest-default-messages.properties!", o_O); } } @@ -506,8 +495,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon HalConfiguration halConfiguration = this.halConfiguration.orElseGet(HalConfiguration::new); HalHandlerInstantiator instantiator = new HalHandlerInstantiator(defaultedRelProvider, - curieProvider.orElse(new DefaultCurieProvider(Collections.emptyMap())), - resourceDescriptionMessageSourceAccessor(), halConfiguration); + curieProvider.orElse(new DefaultCurieProvider(Collections.emptyMap())), resolver, halConfiguration); ObjectMapper mapper = basicObjectMapper(); mapper.registerModule(persistentEntityJackson2Module()); @@ -817,7 +805,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Bean public EnumTranslator enumTranslator() { - return new EnumTranslator(resourceDescriptionMessageSourceAccessor()); + return new EnumTranslator(resolver); } private Set> getProjections(Repositories repositories) { @@ -845,11 +833,10 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon Repositories repositories = repositories(); PersistentEntities persistentEntities = persistentEntities(); RepositoryEntityLinks entityLinks = entityLinks(); - MessageSourceAccessor messageSourceAccessor = resourceDescriptionMessageSourceAccessor(); RepositoryRestConfiguration config = repositoryRestConfiguration(); return new RootResourceInformationToAlpsDescriptorConverter(associationLinks(), repositories, persistentEntities, - entityLinks, messageSourceAccessor, config, objectMapper(), enumTranslator()); + entityLinks, resolver, config, objectMapper(), enumTranslator()); } @Bean diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/EnumTranslator.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/EnumTranslator.java index 254125c21..e9d681788 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/EnumTranslator.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/EnumTranslator.java @@ -25,6 +25,7 @@ import org.springframework.context.MessageSourceResolvable; import org.springframework.context.support.MessageSourceAccessor; import org.springframework.data.rest.core.config.EnumTranslationConfiguration; import org.springframework.data.util.StreamUtils; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -35,7 +36,7 @@ import org.springframework.util.StringUtils; */ public class EnumTranslator implements EnumTranslationConfiguration { - private final MessageSourceAccessor messageSourceAccessor; + private final MessageResolver resolver; private boolean enableDefaultTranslation; private boolean parseEnumNameAsFallback; @@ -43,13 +44,13 @@ public class EnumTranslator implements EnumTranslationConfiguration { /** * Creates a new {@link EnumTranslator} using the given {@link MessageSourceAccessor}. * - * @param messageSourceAccessor must not be {@literal null}. + * @param resolver must not be {@literal null}. */ - public EnumTranslator(MessageSourceAccessor messageSourceAccessor) { + public EnumTranslator(MessageResolver resolver) { - Assert.notNull(messageSourceAccessor, "MessageSourceAccessor must not be null!"); + Assert.notNull(resolver, "MessageResolver must not be null!"); - this.messageSourceAccessor = messageSourceAccessor; + this.resolver = resolver; this.enableDefaultTranslation = true; this.parseEnumNameAsFallback = true; } @@ -84,7 +85,7 @@ public class EnumTranslator implements EnumTranslationConfiguration { Assert.notNull(value, "Enum value must not be null!"); - return messageSourceAccessor.getMessage(TranslatedEnum.of(value, enableDefaultTranslation)); + return resolver.resolve(TranslatedEnum.of(value, enableDefaultTranslation)); } /** diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java index 9ef660961..85e483f6a 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java @@ -33,7 +33,6 @@ import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceResolvable; import org.springframework.context.NoSuchMessageException; import org.springframework.context.support.DefaultMessageSourceResolvable; -import org.springframework.context.support.MessageSourceAccessor; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; import org.springframework.data.domain.Sort; @@ -56,6 +55,7 @@ import org.springframework.data.rest.webmvc.mapping.Associations; import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.Optionals; import org.springframework.data.util.TypeInformation; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -83,7 +83,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric private final ObjectMapper objectMapper; private final RepositoryRestConfiguration configuration; private final ValueTypeSchemaPropertyCustomizerFactory customizerFactory; - private final MessageResolver resolver; + private final InternalMessageResolver resolver; /** * Creates a new {@link PersistentEntityToJsonSchemaConverter} for the given {@link PersistentEntities} and @@ -96,12 +96,12 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric * @param configuration must not be {@literal null}. */ public PersistentEntityToJsonSchemaConverter(PersistentEntities entities, Associations associations, - MessageSourceAccessor accessor, ObjectMapper objectMapper, RepositoryRestConfiguration configuration, + MessageResolver resolver, ObjectMapper objectMapper, RepositoryRestConfiguration configuration, ValueTypeSchemaPropertyCustomizerFactory customizerFactory) { Assert.notNull(entities, "PersistentEntities must not be null!"); Assert.notNull(associations, "AssociationLinks must not be null!"); - Assert.notNull(accessor, "MessageSourceAccessor must not be null!"); + Assert.notNull(resolver, "MessageResolver must not be null!"); Assert.notNull(objectMapper, "ObjectMapper must not be null!"); Assert.notNull(configuration, "RepositoryRestConfiguration must not be null!"); @@ -110,7 +110,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric this.objectMapper = objectMapper; this.configuration = configuration; this.customizerFactory = customizerFactory; - this.resolver = new DefaultMessageResolver(accessor, configuration); + this.resolver = new DefaultMessageResolver(resolver, configuration); for (TypeInformation domainType : entities.getManagedTypes()) { convertiblePairs.add(new ConvertiblePair(domainType.getType(), JsonSchema.class)); @@ -143,7 +143,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric * @return */ public JsonSchema convert(Class domainType) { - return (JsonSchema) convert(domainType, STRING_TYPE, SCHEMA_TYPE); + return convert(domainType, STRING_TYPE, SCHEMA_TYPE); } /* @@ -334,7 +334,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric JsonSerializer serializer = metadata.getTypeSerializer(type.getType()); - if ((serializer instanceof JsonSchemaPropertyCustomizer)) { + if (serializer instanceof JsonSchemaPropertyCustomizer) { properties.add(((JsonSchemaPropertyCustomizer) serializer).customize(property, type)); return; } @@ -455,7 +455,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric .orElseGet(() -> ClassTypeInformation.from(definition.getPrimaryMember().getRawType())); } - public JsonSchemaProperty getSchemaProperty(ResourceDescription description, MessageResolver resolver) { + public JsonSchemaProperty getSchemaProperty(ResourceDescription description, InternalMessageResolver resolver) { JsonSchemaProperty result = getSchemaProperty(definition, getPropertyType(), description, resolver); @@ -471,7 +471,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } private JsonSchemaProperty getSchemaProperty(BeanPropertyDefinition definition, TypeInformation type, - ResourceDescription description, MessageResolver resolver) { + ResourceDescription description, InternalMessageResolver resolver) { String name = definition.getName(); String title = resolver.resolveWithDefault(new ResolvableProperty(definition)); @@ -490,7 +490,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } } - private interface MessageResolver { + private interface InternalMessageResolver { String resolve(MessageSourceResolvable resolvable); @@ -500,9 +500,9 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } @RequiredArgsConstructor - private static class DefaultMessageResolver implements MessageResolver { + private static class DefaultMessageResolver implements InternalMessageResolver { - private final MessageSourceAccessor accessor; + private final MessageResolver accessor; private final RepositoryRestConfiguration configuration; public String resolve(MessageSourceResolvable resolvable) { @@ -512,7 +512,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } try { - return accessor.getMessage(resolvable); + return accessor.resolve(resolvable); } catch (NoSuchMessageException o_O) { if (configuration.getMetadataConfiguration().omitUnresolvableDescriptionKeys()) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java index 9efd0e5a3..01e3d013f 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java @@ -36,7 +36,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.context.support.MessageSourceAccessor; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort.Direction; @@ -56,7 +55,6 @@ import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2Http import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.MultiValueMap; import org.springframework.web.util.UriComponentsBuilder; @@ -196,16 +194,6 @@ public class RepositoryRestMvConfigurationIntegrationTests { assertThat(service.canConvert(String.class, LdapName.class)).isTrue(); } - @Test // DATAREST-686 - public void defaultsEncodingForMessageSourceToUtfEight() { - - MessageSourceAccessor accessor = context.getBean("resourceDescriptionMessageSourceAccessor", - MessageSourceAccessor.class); - Object messageSource = ReflectionTestUtils.getField(accessor, "messageSource"); - - assertThat((String) ReflectionTestUtils.getField(messageSource, "defaultEncoding")).isEqualTo("UTF-8"); - } - private static ObjectMapper getObjectMapper() { AbstractJackson2HttpMessageConverter converter = context.getBean("halJacksonHttpMessageConverter", diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/EnumTranslatorUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/EnumTranslatorUnitTests.java index 763c72265..6d75e95fe 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/EnumTranslatorUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/EnumTranslatorUnitTests.java @@ -22,8 +22,8 @@ import java.util.Locale; import org.junit.Before; import org.junit.Test; import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.context.support.MessageSourceAccessor; import org.springframework.context.support.StaticMessageSource; +import org.springframework.hateoas.mediatype.MessageResolver; /** * Unit tests for {@link EnumTranslator}. @@ -43,7 +43,7 @@ public class EnumTranslatorUnitTests { this.messageSource = new StaticMessageSource(); this.messageSource.addMessage(MyEnum.class.getName().concat(".").concat(MyEnum.FIRST_VALUE.name()), Locale.US, "Translated"); - this.configuration = new EnumTranslator(new MessageSourceAccessor(messageSource)); + this.configuration = new EnumTranslator(MessageResolver.of(messageSource)); } @Test(expected = IllegalArgumentException.class) // DATAREST-654 diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/ProjectionJacksonIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/ProjectionJacksonIntegrationTests.java index cfbbd057a..28162c50a 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/ProjectionJacksonIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/ProjectionJacksonIntegrationTests.java @@ -25,6 +25,7 @@ import org.junit.Test; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.mediatype.MessageResolver; import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider; import org.springframework.hateoas.mediatype.hal.Jackson2HalModule; import org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalHandlerInstantiator; @@ -50,7 +51,7 @@ public class ProjectionJacksonIntegrationTests { this.mapper = new ObjectMapper(); this.mapper.registerModule(new Jackson2HalModule()); this.mapper.setHandlerInstantiator(new HalHandlerInstantiator(new EvoInflectorLinkRelationProvider(), - new DefaultCurieProvider(Collections.emptyMap()), null)); + new DefaultCurieProvider(Collections.emptyMap()), MessageResolver.DEFAULTS_ONLY)); } @Test // DATAREST-221