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.Optional;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.bremersee.comparator.model.SortOrderTextSeparators;
23  import org.bremersee.comparator.spring.web.ReactiveSortOrderHandlerMethodArgumentResolver;
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.reactive.config.WebFluxConfigurer;
34  import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
35  
36  /**
37   * The sort order autoconfiguration for webflux.
38   *
39   * @author Christian Bremer
40   */
41  @ConditionalOnWebApplication(type = Type.REACTIVE)
42  @ConditionalOnClass(name = {
43      "org.bremersee.comparator.model.SortOrderTextSeparators",
44      "org.bremersee.comparator.spring.web.ReactiveSortOrderHandlerMethodArgumentResolver"
45  })
46  @AutoConfiguration
47  @EnableConfigurationProperties(SortOrderConverterProperties.class)
48  public class SortOrderWebFluxAutoConfiguration implements WebFluxConfigurer {
49  
50    private static final Log log = LogFactory.getLog(SortOrderWebFluxAutoConfiguration.class);
51  
52    private final SortOrderTextSeparators separators;
53  
54    /**
55     * Instantiates a new sort order autoconfiguration for webflux.
56     *
57     * @param properties the properties
58     */
59    public SortOrderWebFluxAutoConfiguration(SortOrderConverterProperties properties) {
60      SortOrderTextSeparators defaults = SortOrderTextSeparators.defaults();
61      this.separators = SortOrderTextSeparators.builder()
62          .argumentSeparator(Optional.ofNullable(properties.getArgumentSeparator())
63              .filter(StringUtils::hasText)
64              .orElse(defaults.getArgumentSeparator()))
65          .chainSeparator(Optional.ofNullable(properties.getChainSeparator())
66              .filter(StringUtils::hasText)
67              .orElse(defaults.getChainSeparator()))
68          .build();
69    }
70  
71    /**
72     * Init.
73     */
74    @EventListener(ApplicationReadyEvent.class)
75    public void init() {
76      log.info(String.format("""
77              
78              *********************************************************************************
79              * %s
80              *********************************************************************************""",
81          ClassUtils.getUserClass(getClass()).getSimpleName()));
82    }
83  
84    @Override
85    public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
86      var resolver = new ReactiveSortOrderHandlerMethodArgumentResolver();
87      resolver.setTextSeparators(separators);
88      configurer.addCustomResolver(resolver);
89    }
90  
91  }