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.xml.spring.boot.http.converter;
18  
19  import java.util.AbstractCollection;
20  import java.util.HashSet;
21  import java.util.Set;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.bremersee.xml.JaxbContextBuilder;
25  import org.bremersee.xml.http.converter.Jaxb2HttpMessageConverter;
26  import org.bremersee.xml.spring.boot.JaxbContextBuilderAutoConfiguration;
27  import org.bremersee.xml.spring.boot.http.JaxbReadWriteConfigurer;
28  import org.springframework.beans.factory.ObjectProvider;
29  import org.springframework.boot.autoconfigure.AutoConfiguration;
30  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
31  import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
32  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
33  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
35  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
36  import org.springframework.boot.context.event.ApplicationReadyEvent;
37  import org.springframework.context.annotation.Bean;
38  import org.springframework.context.event.EventListener;
39  import org.springframework.util.ClassUtils;
40  
41  /**
42   * The Jaxb 2 http message converter autoconfiguration.
43   *
44   * @author Christian Bremer
45   */
46  @ConditionalOnWebApplication(type = Type.SERVLET)
47  @ConditionalOnClass(JaxbContextBuilder.class)
48  @AutoConfigureAfter(JaxbContextBuilderAutoConfiguration.class)
49  @AutoConfiguration
50  public class Jaxb2HttpMessageConverterAutoConfiguration {
51  
52    private static final Log log = LogFactory
53        .getLog(Jaxb2HttpMessageConverterAutoConfiguration.class);
54  
55    /**
56     * Instantiates a new jaxb 2 http message converter auto configuration.
57     */
58    public Jaxb2HttpMessageConverterAutoConfiguration() {
59      super();
60    }
61  
62    /**
63     * Init.
64     */
65    @EventListener(ApplicationReadyEvent.class)
66    public void init() {
67      log.info(String.format("""
68  
69              *********************************************************************************
70              * %s
71              *********************************************************************************""",
72          ClassUtils.getUserClass(getClass()).getSimpleName()));
73    }
74  
75    /**
76     * Creates jaxb http message converter bean.
77     *
78     * @param jaxbContextBuilder the jaxb context builder
79     * @param readWriteConfigurers the read write configurers
80     * @return the jaxb http message converter
81     */
82    @ConditionalOnBean(JaxbContextBuilder.class)
83    @ConditionalOnMissingBean(Jaxb2HttpMessageConverter.class)
84    @Bean
85    public Jaxb2HttpMessageConverter jaxb2HttpMessageConverter(
86        JaxbContextBuilder jaxbContextBuilder,
87        ObjectProvider<JaxbReadWriteConfigurer> readWriteConfigurers) {
88  
89      Set<Class<?>> ignoreReadingClasses = readWriteConfigurers
90          .stream()
91          .collect(
92              HashSet::new,
93              (a, b) -> a.addAll(b.getIgnoreReadingClasses()),
94              AbstractCollection::addAll);
95      Set<Class<?>> ignoreWritingClasses = readWriteConfigurers
96          .stream()
97          .collect(
98              HashSet::new,
99              (a, b) -> a.addAll(b.getIgnoreWritingClasses()),
100             AbstractCollection::addAll);
101     log.info(String.format(
102         "Creating bean %s with ignoreReadingClasses (%s) and ignoreWritingClasses (%s).",
103         Jaxb2HttpMessageConverter.class.getSimpleName(),
104         ignoreReadingClasses,
105         ignoreWritingClasses));
106     return new Jaxb2HttpMessageConverter(
107         jaxbContextBuilder,
108         ignoreReadingClasses,
109         ignoreWritingClasses);
110   }
111 
112 }