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 lombok.extern.slf4j.Slf4j;
22 import org.bremersee.comparator.model.SortOrderTextSeparators;
23 import org.bremersee.comparator.spring.web.SortOrderHandlerMethodArgumentResolver;
24 import org.springframework.boot.autoconfigure.AutoConfiguration;
25 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
27 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
28 import org.springframework.boot.context.event.ApplicationReadyEvent;
29 import org.springframework.boot.context.properties.EnableConfigurationProperties;
30 import org.springframework.context.event.EventListener;
31 import org.springframework.util.ClassUtils;
32 import org.springframework.util.StringUtils;
33 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
34 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
35
36
37
38
39
40
41 @ConditionalOnWebApplication(type = Type.SERVLET)
42 @ConditionalOnClass(name = {
43 "org.bremersee.comparator.model.SortOrderTextSeparators",
44 "org.bremersee.comparator.spring.web.SortOrderHandlerMethodArgumentResolver"
45 })
46 @AutoConfiguration
47 @EnableConfigurationProperties(SortOrderConverterProperties.class)
48 @Slf4j
49 public class SortOrderWebAutoConfiguration implements WebMvcConfigurer {
50
51 private final SortOrderTextSeparators separators;
52
53
54
55
56
57
58 public SortOrderWebAutoConfiguration(SortOrderConverterProperties properties) {
59 SortOrderTextSeparators defaults = SortOrderTextSeparators.defaults();
60 this.separators = SortOrderTextSeparators.builder()
61 .argumentSeparator(Optional.ofNullable(properties.getArgumentSeparator())
62 .filter(StringUtils::hasText)
63 .orElse(defaults.getArgumentSeparator()))
64 .chainSeparator(Optional.ofNullable(properties.getChainSeparator())
65 .filter(StringUtils::hasText)
66 .orElse(defaults.getChainSeparator()))
67 .build();
68 }
69
70
71
72
73 @EventListener(ApplicationReadyEvent.class)
74 public void init() {
75 log.info("""
76
77 *********************************************************************************
78 * {}
79 *********************************************************************************""",
80 ClassUtils.getUserClass(getClass()).getSimpleName());
81 }
82
83 @Override
84 public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
85 var resolver = new SortOrderHandlerMethodArgumentResolver();
86 resolver.setTextSeparators(separators);
87 resolvers.add(resolver);
88 }
89
90 }