-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathHateoasConfiguration.java
More file actions
187 lines (153 loc) · 6.44 KB
/
Copy pathHateoasConfiguration.java
File metadata and controls
187 lines (153 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2015-2024 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.config;
import org.springframework.core.io.support.ResourcePatternResolver;
import tools.jackson.databind.json.JsonMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;
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;
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.plugin.core.OrderAwarePluginRegistry;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.config.EnablePluginRegistries;
import org.springframework.plugin.core.support.PluginRegistryFactoryBean;
import org.springframework.util.ClassUtils;
/**
* Common HATEOAS specific configuration.
*
* @author Oliver Gierke
* @author Greg Turnquist
* @author Réda Housni Alaoui
* @soundtrack Elephants Crossing - Wait (Live at Stadtfest Dresden)
* @since 0.19
*/
@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {
static String I18N_BASE_NAME = "rest-messages";
static String I18N_DEFAULTS_BASE_NAME = "rest-default-messages";
private @Autowired ApplicationContext context;
@Bean
public MessageResolver messageResolver() {
return MessageResolver.of(lookupMessageSource());
}
@Bean
WebConverters hypermediaWebMvcConverters(ObjectProvider<JsonMapper> mapper,
List<HypermediaMappingInformation> information, Optional<HypermediaMappingInformationComparator> comparator) {
comparator.ifPresent(information::sort);
return WebConverters.of(mapper.getIfUnique(JsonMapper::new), information);
}
// RelProvider
@Bean
LinkRelationProvider defaultRelProvider() {
return ClassUtils.isPresent("org.atteo.evo.inflector.English", null) //
? new EvoInflectorLinkRelationProvider()
: new DefaultLinkRelationProvider();
}
@Bean
AnnotationLinkRelationProvider annotationRelProvider() {
return new AnnotationLinkRelationProvider();
}
@Primary
@Bean
DelegatingLinkRelationProvider _relProvider(
PluginRegistry<LinkRelationProvider, LookupContext> relProviderPluginRegistry) {
return new DelegatingLinkRelationProvider(relProviderPluginRegistry);
}
@Bean
OrderAwarePluginRegistry<LinkRelationProvider, LookupContext> relProviderPluginRegistry(
ListableBeanFactory beanFactory) {
PluginRegistryFactoryBean<LinkRelationProvider, LookupContext> factory = new PluginRegistryFactoryBean<>();
factory.setBeanFactory(beanFactory);
factory.setType(LinkRelationProvider.class);
factory.setExclusions(new Class[] { DelegatingLinkRelationProvider.class });
return factory.getObject();
}
// LinkDiscoverers
@Bean
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 final AbstractMessageSource lookupMessageSource() {
List<Resource> candidates = loadResourceBundleResources(I18N_DEFAULTS_BASE_NAME, false);
if (candidates.isEmpty() && loadResourceBundleResources(I18N_BASE_NAME, true).isEmpty()) {
return null;
}
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setResourceLoader(context);
messageSource.setBasename("classpath:".concat(I18N_BASE_NAME));
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
if (!candidates.isEmpty()) {
messageSource.setCommonMessages(loadProperties(candidates));
}
return messageSource;
}
@Nullable
private final Properties loadProperties(List<Resource> sources) {
PropertiesFactoryBean factory = new PropertiesFactoryBean();
factory.setLocations(sources.toArray(new Resource[sources.size()]));
try {
factory.afterPropertiesSet();
return factory.getObject();
} catch (IOException o_O) {
throw new IllegalStateException("Could not load default properties from resources!", o_O);
}
}
private final List<Resource> loadResourceBundleResources(String baseName, boolean withWildcard) {
try {
return Arrays //
.stream(context.getResources(String.format("%s%s%s.properties", //
ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX, baseName, withWildcard ? "*" : ""))) //
.filter(Resource::exists) //
.collect(Collectors.toList());
} catch (IOException e) {
return Collections.emptyList();
}
}
}