#1019 - Further optimizations in case not I18N is enabled.
Introduced MessageResolver interface and moved all components previously referring to MessageSourceResolvable to it. This allows us to directly shortcut message resolution by providing a no-op instance in case our default resource bundle rest-messages does not exist and thus avoid the overhead of attempted message resolutions in that case.
This commit is contained in:
@@ -15,15 +15,17 @@
|
||||
*/
|
||||
package org.springframework.hateoas.config;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.context.support.AbstractResourceBasedMessageSource;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.hateoas.client.LinkDiscoverer;
|
||||
import org.springframework.hateoas.client.LinkDiscoverers;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider.LookupContext;
|
||||
import org.springframework.hateoas.server.core.AnnotationLinkRelationProvider;
|
||||
@@ -31,6 +33,7 @@ import org.springframework.hateoas.server.core.DefaultLinkRelationProvider;
|
||||
import org.springframework.hateoas.server.core.DelegatingLinkRelationProvider;
|
||||
import org.springframework.hateoas.server.core.EvoInflectorLinkRelationProvider;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.plugin.core.PluginRegistry;
|
||||
import org.springframework.plugin.core.config.EnablePluginRegistries;
|
||||
import org.springframework.plugin.core.support.PluginRegistryFactoryBean;
|
||||
@@ -49,24 +52,9 @@ import org.springframework.util.ClassUtils;
|
||||
@EnablePluginRegistries({ LinkDiscoverer.class })
|
||||
class HateoasConfiguration {
|
||||
|
||||
/**
|
||||
* The {@link MessageSourceAccessor} to provide messages for {@link ResourceDescription}s being rendered.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public MessageSourceAccessor linkRelationMessageSource() {
|
||||
|
||||
try {
|
||||
|
||||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
messageSource.setBasename("classpath:rest-messages");
|
||||
|
||||
return new MessageSourceAccessor(messageSource);
|
||||
|
||||
} catch (Exception o_O) {
|
||||
throw new BeanCreationException("resourceDescriptionMessageSourceAccessor", "", o_O);
|
||||
}
|
||||
public MessageResolver messageResolver() {
|
||||
return MessageResolver.of(lookupMessageSource());
|
||||
}
|
||||
|
||||
// RelProvider
|
||||
@@ -108,4 +96,25 @@ class HateoasConfiguration {
|
||||
LinkDiscoverers linkDiscoverers(PluginRegistry<LinkDiscoverer, MediaType> discoverers) {
|
||||
return new LinkDiscoverers(discoverers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a message source for the {@code rest-messages} resource bundle if the file exists or a
|
||||
* {@link NoOpMessageSource} otherwise.
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
private static final MessageSource lookupMessageSource() {
|
||||
|
||||
ClassPathResource resource = new ClassPathResource("rest-messages");
|
||||
|
||||
if (!resource.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
AbstractResourceBasedMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
messageSource.setBasename("classpath:rest-messages");
|
||||
|
||||
return messageSource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.hateoas.mediatype;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* A simplified variant of {@link MessageSourceAccessor} to allow more direct replacement with a no-op implementation in
|
||||
* case the target {@link MessageSource} is unavailable to avoid resolution overhead.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface MessageResolver {
|
||||
|
||||
public static final MessageResolver NONE = NoMessageResolver.INSTANCE;
|
||||
|
||||
/**
|
||||
* Resolve the given {@link MessageSourceResolvable}. Return {@literal null} if no message was found.
|
||||
*
|
||||
* @param resolvable must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
String resolve(MessageSourceResolvable resolvable);
|
||||
|
||||
/**
|
||||
* Obtains a {@link MessageResolver} for the given {@link MessageSource}.
|
||||
*
|
||||
* @param messageSource can be {@literal null}.
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
public static MessageResolver of(@Nullable MessageSource messageSource) {
|
||||
return messageSource == null ? NoMessageResolver.INSTANCE : new MessageSourceResolver(messageSource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.hateoas.mediatype;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link MessageResolver} based on a {@link MessageSource}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
class MessageSourceResolver implements MessageResolver {
|
||||
|
||||
private final MessageSourceAccessor accessor;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MessageSourceResolver} for the given {@link MessageSource}.
|
||||
*
|
||||
* @param messageSource must not be {@literal null}.
|
||||
*/
|
||||
MessageSourceResolver(MessageSource messageSource) {
|
||||
|
||||
Assert.notNull(messageSource, "MessageSource must not be null!");
|
||||
|
||||
this.accessor = new MessageSourceAccessor(messageSource);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.mediatype.MessageResolver#resolve(org.springframework.context.MessageSourceResolvable)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public String resolve(MessageSourceResolvable resolvable) {
|
||||
|
||||
String resolved = accessor.getMessage(resolvable);
|
||||
|
||||
return StringUtils.hasText(resolved) ? resolved : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.hateoas.mediatype;
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
enum NoMessageResolver implements MessageResolver {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.mediatype.MessageResolver#resolve(org.springframework.context.MessageSourceResolvable)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public String resolve(MessageSourceResolvable resolvable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -18,13 +18,12 @@ package org.springframework.hateoas.mediatype.hal;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.hateoas.client.LinkDiscoverer;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
|
||||
import org.springframework.hateoas.config.HypermediaMappingInformation;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
@@ -43,22 +42,21 @@ public class HalMediaTypeConfiguration implements HypermediaMappingInformation {
|
||||
private final LinkRelationProvider relProvider;
|
||||
private final ObjectProvider<CurieProvider> curieProvider;
|
||||
private final ObjectProvider<HalConfiguration> halConfiguration;
|
||||
private final MessageSourceAccessor messageSourceAccessor;
|
||||
private final MessageResolver resolver;
|
||||
|
||||
/**
|
||||
* @param relProvider
|
||||
* @param curieProvider
|
||||
* @param halConfiguration
|
||||
* @param messageSourceAccessor
|
||||
* @param resolver
|
||||
*/
|
||||
public HalMediaTypeConfiguration(LinkRelationProvider relProvider, ObjectProvider<CurieProvider> curieProvider,
|
||||
ObjectProvider<HalConfiguration> halConfiguration,
|
||||
@Qualifier("linkRelationMessageSource") MessageSourceAccessor messageSourceAccessor) {
|
||||
ObjectProvider<HalConfiguration> halConfiguration, MessageResolver resolver) {
|
||||
|
||||
this.relProvider = relProvider;
|
||||
this.curieProvider = curieProvider;
|
||||
this.halConfiguration = halConfiguration;
|
||||
this.messageSourceAccessor = messageSourceAccessor;
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -85,7 +83,7 @@ public class HalMediaTypeConfiguration implements HypermediaMappingInformation {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.registerModule(new Jackson2HalModule());
|
||||
mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider,
|
||||
curieProvider.getIfAvailable(() -> CurieProvider.NONE), messageSourceAccessor,
|
||||
curieProvider.getIfAvailable(() -> CurieProvider.NONE), resolver,
|
||||
halConfiguration.getIfAvailable(HalConfiguration::new)));
|
||||
|
||||
return mapper;
|
||||
|
||||
@@ -27,14 +27,13 @@ import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
import org.springframework.hateoas.Links;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.hateoas.mediatype.hal.HalConfiguration.RenderSingleLinks;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider;
|
||||
import org.springframework.lang.NonNull;
|
||||
@@ -113,23 +112,23 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
private final @Nullable BeanProperty property;
|
||||
private final CurieProvider curieProvider;
|
||||
private final EmbeddedMapper mapper;
|
||||
private final MessageSourceAccessor accessor;
|
||||
private final MessageResolver resolver;
|
||||
private final HalConfiguration halConfiguration;
|
||||
|
||||
public HalLinkListSerializer(CurieProvider curieProvider, EmbeddedMapper mapper, MessageSourceAccessor accessor,
|
||||
public HalLinkListSerializer(CurieProvider curieProvider, EmbeddedMapper mapper, MessageResolver resolver,
|
||||
HalConfiguration halConfiguration) {
|
||||
this(null, curieProvider, mapper, accessor, halConfiguration);
|
||||
this(null, curieProvider, mapper, resolver, halConfiguration);
|
||||
}
|
||||
|
||||
public HalLinkListSerializer(@Nullable BeanProperty property, CurieProvider curieProvider, EmbeddedMapper mapper,
|
||||
MessageSourceAccessor accessor, HalConfiguration halConfiguration) {
|
||||
MessageResolver accessor, HalConfiguration halConfiguration) {
|
||||
|
||||
super(TypeFactory.defaultInstance().constructType(Links.class));
|
||||
|
||||
this.property = property;
|
||||
this.curieProvider = curieProvider;
|
||||
this.mapper = mapper;
|
||||
this.accessor = accessor;
|
||||
this.resolver = accessor;
|
||||
this.halConfiguration = halConfiguration;
|
||||
}
|
||||
|
||||
@@ -206,26 +205,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
|
||||
HalLinkRelation rel = HalLinkRelation.of(link.getRel());
|
||||
|
||||
return new HalLink(link, getTitle(rel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the title for the given local link relation resolved through the configured {@link MessageSourceAccessor}
|
||||
* .
|
||||
*
|
||||
* @param relation must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
private String getTitle(HalLinkRelation relation) {
|
||||
|
||||
Assert.notNull(relation, "Local relation must not be null or empty!");
|
||||
|
||||
try {
|
||||
return accessor == null ? null : accessor.getMessage(relation);
|
||||
} catch (NoSuchMessageException o_O) {
|
||||
return null;
|
||||
}
|
||||
return new HalLink(link, resolver.resolve(rel));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -236,7 +216,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
@SuppressWarnings("null")
|
||||
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
|
||||
throws JsonMappingException {
|
||||
return new HalLinkListSerializer(property, curieProvider, mapper, accessor, halConfiguration);
|
||||
return new HalLinkListSerializer(property, curieProvider, mapper, resolver, halConfiguration);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -719,43 +699,43 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
private final @Nullable AutowireCapableBeanFactory delegate;
|
||||
|
||||
public HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider,
|
||||
MessageSourceAccessor messageSourceAccessor) {
|
||||
this(provider, curieProvider, messageSourceAccessor, new HalConfiguration());
|
||||
MessageResolver resolver) {
|
||||
this(provider, curieProvider, resolver, new HalConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link HalHandlerInstantiator} using the given {@link LinkRelationProvider}, {@link CurieProvider}
|
||||
* and {@link MessageSourceAccessor}. Registers a prepared {@link HalResourcesSerializer} and
|
||||
* and {@link MessageResolver}. Registers a prepared {@link HalResourcesSerializer} and
|
||||
* {@link HalLinkListSerializer} falling back to instantiation expecting a default constructor.
|
||||
*
|
||||
* @param provider must not be {@literal null}.
|
||||
* @param curieProvider can be {@literal null}.
|
||||
* @param messageSourceAccessor can be {@literal null}.
|
||||
* @param resolver must not be {@literal null}.
|
||||
*/
|
||||
public HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider,
|
||||
MessageSourceAccessor messageSourceAccessor, HalConfiguration halConfiguration) {
|
||||
this(provider, curieProvider, messageSourceAccessor, true, halConfiguration);
|
||||
public HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider, MessageResolver resolver,
|
||||
HalConfiguration halConfiguration) {
|
||||
this(provider, curieProvider, resolver, true, halConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link HalHandlerInstantiator} using the given {@link LinkRelationProvider}, {@link CurieProvider}
|
||||
* and {@link MessageSourceAccessor} and whether to enforce embedded collections. Registers a prepared
|
||||
* and {@link MessageResolver} and whether to enforce embedded collections. Registers a prepared
|
||||
* {@link HalResourcesSerializer} and {@link HalLinkListSerializer} falling back to instantiation expecting a
|
||||
* default constructor.
|
||||
*
|
||||
* @param provider must not be {@literal null}.
|
||||
* @param curieProvider can be {@literal null}
|
||||
* @param accessor can be {@literal null}.
|
||||
* @param resolver must not be {@literal null}..
|
||||
* @param enforceEmbeddedCollections
|
||||
*/
|
||||
public HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider,
|
||||
MessageSourceAccessor accessor, boolean enforceEmbeddedCollections, HalConfiguration halConfiguration) {
|
||||
this(provider, curieProvider, accessor, enforceEmbeddedCollections, null, halConfiguration);
|
||||
public HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider, MessageResolver resolver,
|
||||
boolean enforceEmbeddedCollections, HalConfiguration halConfiguration) {
|
||||
this(provider, curieProvider, resolver, enforceEmbeddedCollections, null, halConfiguration);
|
||||
}
|
||||
|
||||
private HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider,
|
||||
MessageSourceAccessor accessor, boolean enforceEmbeddedCollections,
|
||||
@Nullable AutowireCapableBeanFactory delegate, HalConfiguration halConfiguration) {
|
||||
private HalHandlerInstantiator(LinkRelationProvider provider, CurieProvider curieProvider, MessageResolver resolver,
|
||||
boolean enforceEmbeddedCollections, @Nullable AutowireCapableBeanFactory delegate,
|
||||
HalConfiguration halConfiguration) {
|
||||
|
||||
Assert.notNull(provider, "RelProvider must not be null!");
|
||||
Assert.notNull(curieProvider, "CurieProvider must not be null!");
|
||||
@@ -766,7 +746,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
|
||||
this.serializers.put(HalResourcesSerializer.class, new HalResourcesSerializer(mapper));
|
||||
this.serializers.put(HalLinkListSerializer.class,
|
||||
new HalLinkListSerializer(curieProvider, mapper, accessor, halConfiguration));
|
||||
new HalLinkListSerializer(curieProvider, mapper, resolver, halConfiguration));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -22,10 +22,10 @@ import java.util.List;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.hateoas.client.LinkDiscoverer;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
|
||||
import org.springframework.hateoas.config.HypermediaMappingInformation;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.hateoas.mediatype.hal.CurieProvider;
|
||||
import org.springframework.hateoas.mediatype.hal.HalConfiguration;
|
||||
import org.springframework.hateoas.server.core.DelegatingLinkRelationProvider;
|
||||
@@ -48,7 +48,7 @@ class HalFormsMediaTypeConfiguration implements HypermediaMappingInformation {
|
||||
private final ObjectProvider<CurieProvider> curieProvider;
|
||||
private final ObjectProvider<HalFormsConfiguration> halFormsConfiguration;
|
||||
private final ObjectProvider<HalConfiguration> halConfiguration;
|
||||
private final MessageSourceAccessor messageSourceAccessor;
|
||||
private final MessageResolver resolver;
|
||||
|
||||
@Bean
|
||||
LinkDiscoverer halFormsLinkDiscoverer() {
|
||||
@@ -77,7 +77,7 @@ class HalFormsMediaTypeConfiguration implements HypermediaMappingInformation {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.registerModule(new Jackson2HalFormsModule());
|
||||
mapper.setHandlerInstantiator(new Jackson2HalFormsModule.HalFormsHandlerInstantiator(relProvider,
|
||||
curieProvider.getIfAvailable(() -> CurieProvider.NONE), messageSourceAccessor, true, configuration));
|
||||
curieProvider.getIfAvailable(() -> CurieProvider.NONE), resolver, true, configuration));
|
||||
|
||||
return mapper;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.hateoas.Affordance;
|
||||
import org.springframework.hateoas.AffordanceModel.InputPayloadMetadata;
|
||||
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
|
||||
@@ -35,17 +34,17 @@ import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
class HalFormsTemplateBuilder {
|
||||
|
||||
private final HalFormsConfiguration configuration;
|
||||
private final MessageSourceAccessor accessor;
|
||||
private final MessageResolver resolver;
|
||||
|
||||
/**
|
||||
* Extract template details from a {@link RepresentationModel}'s {@link Affordance}s.
|
||||
@@ -96,8 +95,7 @@ class HalFormsTemplateBuilder {
|
||||
|
||||
public HalFormsTemplate applyTo(HalFormsTemplate template, HalFormsTemplateBuilder.TemplateTitle templateTitle) {
|
||||
|
||||
return Optional.ofNullable(accessor.getMessage(templateTitle)) //
|
||||
.filter(StringUtils::hasText) //
|
||||
return Optional.ofNullable(resolver.resolve(templateTitle)) //
|
||||
.map(template::withTitle) //
|
||||
.orElse(template);
|
||||
}
|
||||
@@ -109,8 +107,12 @@ class HalFormsTemplateBuilder {
|
||||
|
||||
private HalFormsProperty apply(HalFormsProperty property) {
|
||||
|
||||
String message = accessor.getMessage(PropertyPrompt.of(metadata, property));
|
||||
HalFormsProperty withPrompt = property.withPrompt(message);
|
||||
String message = resolver.resolve(PropertyPrompt.of(metadata, property));
|
||||
|
||||
HalFormsProperty withPrompt = Optional.ofNullable(message) //
|
||||
.map(it -> property.withPrompt(it)) //
|
||||
.orElse(property);
|
||||
|
||||
HalFormsProperty withConfig = metadata.getPropertyMetadata(withPrompt.getName()) //
|
||||
.flatMap(it -> applyConfig(it, withPrompt)) //
|
||||
.orElse(withPrompt);
|
||||
|
||||
@@ -21,13 +21,13 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Links;
|
||||
import org.springframework.hateoas.PagedModel;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||
import org.springframework.hateoas.mediatype.hal.CurieProvider;
|
||||
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule.EmbeddedMapper;
|
||||
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalHandlerInstantiator;
|
||||
@@ -163,7 +163,7 @@ class Jackson2HalFormsModule extends SimpleModule {
|
||||
private final Map<Class<?>, Object> serializers = new HashMap<>();
|
||||
|
||||
public HalFormsHandlerInstantiator(LinkRelationProvider resolver, CurieProvider curieProvider,
|
||||
MessageSourceAccessor accessor, boolean enforceEmbeddedCollections, HalFormsConfiguration configuration) {
|
||||
MessageResolver accessor, boolean enforceEmbeddedCollections, HalFormsConfiguration configuration) {
|
||||
|
||||
super(resolver, curieProvider, accessor, enforceEmbeddedCollections, configuration.getHalConfiguration());
|
||||
|
||||
@@ -180,10 +180,9 @@ class Jackson2HalFormsModule extends SimpleModule {
|
||||
}
|
||||
|
||||
public HalFormsHandlerInstantiator(LinkRelationProvider relProvider, CurieProvider curieProvider,
|
||||
MessageSourceAccessor messageSource, boolean enforceEmbeddedCollections,
|
||||
AutowireCapableBeanFactory beanFactory) {
|
||||
MessageResolver resolver, boolean enforceEmbeddedCollections, AutowireCapableBeanFactory beanFactory) {
|
||||
|
||||
this(relProvider, curieProvider, messageSource, enforceEmbeddedCollections,
|
||||
this(relProvider, curieProvider, resolver, enforceEmbeddedCollections,
|
||||
beanFactory.getBean(HalFormsConfiguration.class));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user