1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.comparator.spring.boot;
18
19 import java.util.List;
20 import java.util.Optional;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.bremersee.comparator.model.SortOrderTextSeparators;
24 import org.bremersee.comparator.spring.web.SortOrderHandlerMethodArgumentResolver;
25 import org.springframework.boot.autoconfigure.AutoConfiguration;
26 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29 import org.springframework.boot.context.event.ApplicationReadyEvent;
30 import org.springframework.boot.context.properties.EnableConfigurationProperties;
31 import org.springframework.context.event.EventListener;
32 import org.springframework.util.ClassUtils;
33 import org.springframework.util.StringUtils;
34 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
35 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
36
37
38
39
40
41
42 @ConditionalOnWebApplication(type = Type.SERVLET)
43 @ConditionalOnClass(name = {
44 "org.bremersee.comparator.model.SortOrderTextSeparators",
45 "org.bremersee.comparator.spring.web.SortOrderHandlerMethodArgumentResolver"
46 })
47 @AutoConfiguration
48 @EnableConfigurationProperties(SortOrderConverterProperties.class)
49 public class SortOrderWebAutoConfiguration implements WebMvcConfigurer {
50
51 private static final Log log = LogFactory.getLog(SortOrderWebAutoConfiguration.class);
52
53 private final SortOrderTextSeparators separators;
54
55
56
57
58
59
60 public SortOrderWebAutoConfiguration(SortOrderConverterProperties properties) {
61 SortOrderTextSeparators defaults = SortOrderTextSeparators.defaults();
62 this.separators = SortOrderTextSeparators.builder()
63 .argumentSeparator(Optional.ofNullable(properties.getArgumentSeparator())
64 .filter(StringUtils::hasText)
65 .orElse(defaults.getArgumentSeparator()))
66 .chainSeparator(Optional.ofNullable(properties.getChainSeparator())
67 .filter(StringUtils::hasText)
68 .orElse(defaults.getChainSeparator()))
69 .build();
70 }
71
72
73
74
75 @EventListener(ApplicationReadyEvent.class)
76 public void init() {
77 log.info(String.format("""
78
79 *********************************************************************************
80 * %s
81 *********************************************************************************""",
82 ClassUtils.getUserClass(getClass()).getSimpleName()));
83 }
84
85 @Override
86 public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
87 var resolver = new SortOrderHandlerMethodArgumentResolver();
88 resolver.setTextSeparators(separators);
89 resolvers.add(resolver);
90 }
91
92 }