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 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   * The sort order autoconfiguration for webmvc.
39   *
40   * @author Christian Bremer
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     * Instantiates a new sort order autoconfiguration for webmvc.
57     *
58     * @param properties the properties
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     * Init.
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  }