View Javadoc
1   /*
2    * Copyright 2022-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.geojson.spring.boot.autoconfigure.web;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.bremersee.geojson.GeoJsonGeometryFactory;
22  import org.bremersee.geojson.converter.GeometryConverters;
23  import org.bremersee.geojson.spring.boot.autoconfigure.GeoJsonGeometryFactoryAutoConfiguration;
24  import org.jspecify.annotations.NonNull;
25  import org.springframework.beans.factory.ObjectProvider;
26  import org.springframework.boot.autoconfigure.AutoConfiguration;
27  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
28  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
29  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
30  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
31  import org.springframework.boot.context.event.ApplicationReadyEvent;
32  import org.springframework.context.event.EventListener;
33  import org.springframework.format.FormatterRegistry;
34  import org.springframework.util.ClassUtils;
35  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
36  
37  /**
38   * The GeoJSON web mvc configurer.
39   *
40   * @author Christian Bremer
41   */
42  @ConditionalOnClass(name = {"org.bremersee.geojson.converter.GeometryConverters"})
43  @ConditionalOnWebApplication(type = Type.SERVLET)
44  @AutoConfiguration
45  @AutoConfigureAfter(GeoJsonGeometryFactoryAutoConfiguration.class)
46  public class GeoJsonWebMvcConfigurer implements WebMvcConfigurer {
47  
48    private static final Log log = LogFactory.getLog(GeoJsonWebMvcConfigurer.class);
49  
50    private final GeoJsonGeometryFactory geometryFactory;
51  
52    /**
53     * Instantiates a new GeoJSON web mvc configurer.
54     *
55     * @param geometryFactory the geometry factory
56     */
57    public GeoJsonWebMvcConfigurer(ObjectProvider<GeoJsonGeometryFactory> geometryFactory) {
58      this.geometryFactory = geometryFactory.getIfAvailable(GeoJsonGeometryFactory::new);
59    }
60  
61    /**
62     * Init.
63     */
64    @EventListener(ApplicationReadyEvent.class)
65    public void init() {
66      log.info(String.format("""
67              
68              *********************************************************************************
69              * %s
70              *********************************************************************************""",
71          ClassUtils.getUserClass(getClass()).getSimpleName()));
72    }
73  
74    @Override
75    public void addFormatters(@NonNull FormatterRegistry registry) {
76      GeometryConverters.getConvertersToRegister(geometryFactory)
77          .forEach(registry::addConverter);
78    }
79  
80  }