View Javadoc
1   /*
2    * Copyright 2019-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The sort order autoconfiguration for webmvc.
38   *
39   * @author Christian Bremer
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     * Instantiates a new sort order autoconfiguration for webmvc.
55     *
56     * @param properties the properties
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     * Init.
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  }